# 栈、队列和链表

## 栈

class Stack:
def __init__(self,sz=65536):
self.stack = []
self.top = -1
self.size = sz

def isEmpty(self):
return True if self.top is -1 else False

def isFull(self):
return True if self.top is self.size - 1 else False

def push(self,obj): #压入栈操作
if self.isFull():
raise Exception("Stack over flow!")
else:
self.stack.append(obj)
self.top += 1

def pop(self): #弹出栈顶元素
if self.isEmpty():
raise Exception("Stack is empty!")
else:
self.top -= 1
return self.stack.pop()

def count(self): #获取栈中元素个数
return self.top + 1

def top(self): #获取栈顶元素值
if self.isEmpty():
raise Exception("Stack is empty!")
return self.stack[top]

def show(self):
print(self.stack)

## 队列

class Queue:
def __init__(self,sz=65535):
self.queue = []
self.size = sz
self.len = 0

def isEmpty(self):
return True if self.len is 0 else False

def isFull(self):
return True if self.len is self.size else False

def length(self):
return self.len

def enqueue(self,obj): #入队
if self.isFull():
raise Exception("Queue is Full!")
else:
self.queue.append(obj)
self.len += 1

def dequeue(self): #出队
if self.isEmpty():
raise Exception("Queue is Empty!")
else:
self.len -= 1
return self.queue.pop(0)

def show(self):
print(self.queue)

class Deque:
def __init__(self,sz=65535):
self.queue = []
self.size = sz
self.len = 0

def isEmpty(self):
return True if self.len is 0 else False

def isFull(self):
return True if self.len is self.size else False

def length(self):
return self.len

def enqueue(self,obj): #从右边入队
if self.isFull():
raise Exception("DeQueue is Full!")
else:
self.len += 1
self.queue.append(obj)

def enqueueLeft(self,obj): #从左边入队
if self.isFull():
raise Exception("DeQueue is Full!")
else:
self.len += 1
self.queue.insert(0,obj)

def dequeue(self): #从左边出队
if self.isEmpty():
raise Exception("DeQueue is Empty!")
else:
self.len -= 1
return self.queue.pop(0)

def dequeueRight(self): #从右边出队
if self.isEmpty():
raise Exception("DeQueue is Empty!")
else:
self.len -= 1
return self.queue.pop()

def show(self):
print(self.queue)

## 链表

#构成双向链表的结点
class Node:
def __init__(self,key=None):
self.key = key
self.prev = None #指向前驱
self.next = None #指向后继

def getKey(self):
return self.key

def getPrev(self):
return self.prev

def getNext(self):
return self.next

def setKey(self, key):
self.key = key

def setPrev(self, prev):
self.prev = prev

def setNext(self, next):
self.next = next

def __init__(self,data=None):
self.len = 0
if data:
self.len += 1
for key in data[1:]:
node = Node(key)
p.setNext(node)
node.setPrev(p)
self.len += 1
p = node
#重载[]运算符，使其具体下标访问能力，比如像obj[1]这样访问
def __getitem__(self, index):
if index >= self.len or index < 0:
raise Exception("Index out of range!")
else:
for i in range(index):
p = p.getNext()
return p.getKey()

def length(self):
return self.len

def search(self,key):
while p and p.getKey() is not key:
p = p.getNext()
return p

def insert(self,key):
p = Node(key)
self.len += 1
else:
self.len += 1

def delete(self,key):
p = self.search(key)
if p:
if p.getPrev():
p.getPrev().setNext(p.getNext())
else:
if p.getNext():
p.getNext().setPrev(p.getPrev())
p.setPrev(None)
p.setNext(None)
self.len -= 1

def show(self):
while p:
print(p.getKey(),end=' ')
p = p.getNext()
print()

#### java 链表、栈、队列、树的实现

2016-12-16 15:59:15

#### 线性表、链表、栈、队列的关系

2017-05-16 09:14:31

#### 关于链表，队列 ，栈，散列表，树，图

2015-06-26 18:11:03

#### 数组、链表、堆栈和队列、线性表和顺序表

2016-12-26 13:51:06

#### 数据结构（栈，队列，链表，二叉树）

2016-05-04 17:03:53

#### 常见的线性列表结构---【数组、链表、栈、队列、堆】

2015-01-22 10:44:11

#### 【万字总结】图解堆算法、链表、栈与队列（多图预警）

2015-12-18 08:40:12

#### 基础数据结构 链表、栈、队列

2014-08-24 19:15:44

#### 链表，队列和栈的区别

2014-03-15 19:32:06

#### 【数据结构】数组、链表、栈、队列、二叉树

2017-04-23 10:50:47