数据结构--栈 初学

栈是一种“先进后出”的一种数据结构,有压栈出栈两种操作方式。如下图:
在这里插入图片描述
栈的操作:

class Stack(object):
def init(self):
self.__list = []

def push(self,item):
    self.__list .append(item)

def pop(self):
    return self.__list .pop()

def peek(self):
    if self.__list :
        return self.__list[-1]
    else:
        return None

def is_empty(self):
    return self.__list ==[]

def size(self):
    return (self.__list )

if name ==“main”:
s = Stack()
s.push(1)
s.push(2)
s.push(3)
s.push(4)
print(s.pop())
print(s.pop())
print(s.pop())
print(s.pop())

输出:

G:\anaconda\python.exe G:/pycharm/python_jichu/02_stack.py
4
3
2
1

Process finished with exit code 0

发布了34 篇原创文章 · 获赞 4 · 访问量 1564

猜你喜欢

转载自blog.csdn.net/qq_36495569/article/details/103154206
今日推荐