栈--python篇

本篇文章使用python实现栈的增、删、改、查、插入、删除

首先,创建一个栈,实现最基本的获取栈的长度、判空、压栈,弹栈功能

# 创建栈
class Stack:
    def __init__(self):
        self.items = []

    def length(self):
        return len(self.items)

    def push_(self, elem):
        return self.items.append(elem)

    def pop_(self):
        return self.items.pop()

    def is_empty(self):
        if self.length == 0:
            print("empty!")
            return 0
        else:
            return len(self.items)

其次,实现增删改查功能

    def show_stack(self):#输出栈内元素
        for i in range(self.length()):
            print(self.items[i])

    def insert_index(self, add, elem):#在指定下标处插入指定元素
        if add > self.length():#位置不合理
            print("Index over the length!")
            return -1
        else:
            self.items.append(self.items[-1])#现将最后一个元素进行拷贝,方便后续操作
            #由于range函数的特性,add需要减一,使i可以取得到add后面这个位置的下标
            for i in range(self.length() - 1, add - 1, -1):
                self.items[i] = self.items[i - 1]
            self.items[add - 1] = elem

    def search_index(self, add):  # 以指定位置查找
        if add > self.length():#位置不合理
            print("Index over the length!")
            return -1
        else:
            print(self.items[add])

    def search_elem(self, elem):  # 以指定元素查找
        for i in range(self.length()):
            if self.items[i] == elem:
                print("Find it!")
                return i + 1
            else:
                print("Can not find it!")
                return -1

    def del_(self, elem):
        temp = 0
        i = 0
        while i < self.length():
            if self.items[i] == elem:
                temp = i
                break
            i += 1
        #由于上个循环结束后,i等于栈的长度,由此可知,栈内并没有该元素
        if i == self.length():
            print("Can not find it!")
            return -1
        for i in range(temp, self.length() - 1):
            self.items[i] = self.items[i + 1]

最后,完整的代码以及测试代码如下:

# 创建栈
class Stack:
    def __init__(self):
        self.items = []

    def length(self):
        return len(self.items)

    def push_(self, elem):
        return self.items.append(elem)

    def pop_(self):
        return self.items.pop()

    def is_empty(self):
        if self.length == 0:
            print("empty!")
            return 0
        else:
            return len(self.items)

    def show_stack(self):
        for i in range(self.length()):
            print(self.items[i])

    def insert_index(self, add, elem):
        if add > self.length():
            print("Index over the length!")
            return -1
        else:
            self.items.append(self.items[-1])
            for i in range(self.length() - 1, add - 1, -1):
                self.items[i] = self.items[i - 1]
            self.items[add - 1] = elem

    def search_index(self, add):  # 以指定位置查找
        if add > self.length():
            print("Index over the length!")
            return -1
        else:
            print(self.items[add])

    def search_elem(self, elem):  # 以指定元素查找
        for i in range(self.length()):
            if self.items[i] == elem:
                print("Find it!")
                return i + 1
            else:
                print("Can not find it!")
                return -1

    def del_(self, elem):
        temp = 0
        i = 0
        while i < self.length():
            if self.items[i] == elem:
                temp = i
                break
            i += 1
        if i == self.length():
            print("Can not find it!")
            return -1
        for i in range(temp, self.length() - 1):
            self.items[i] = self.items[i + 1]


if __name__ == "__main__":
    stack = Stack()
    for i in range(10):
        stack.push_(i)
    print("Push over!")
    print("Before:")
    stack.show_stack()
    print("-----------------------------")
    add = int(input("请输入需要插入的位置!"))
    elem = int(input("请输入需要插入的元素!"))
    stack.insert_index(add, elem)
    print("After:")
    stack.show_stack()
    print("-----------------------------")
    num = eval(input("请输入你需要删除的元素!"))
    stack.del_(num)
    print("Afer del:")
    stack.show_stack()

你可能感兴趣的:(数据结构python描述,python,数据结构,蓝桥杯)