動(dòng)態(tài)隊(duì)列, 單純的從兩頭入出入出入出入出.
from linkedlist import *
class Queue(LinkedList):
def __init__(self):
LinkedList.__init__(self)
self.init_list(data = [0])
self.qhead = 0
self.qtail = 0
self.qt_post = self.qhead
def is_empty(self):
if self.qhead == self.qtail:
return True
else:
return False
def get_qt_post(self):
p = self.qhead
while p.next != 0:
p = p.next
self.qt_post = p
def enqueue(self, item):
if self.is_empty():
self.qhead = self.head
self.qhead.data = item
else:
self.get_qt_post()
node = Node(item)
self.qt_post.next = node
node.next = self.qtail
def dequeue(self):
#qwhen qhead.next == qtail, queue empty
if self.is_empty():
print("the queue is empty, no element to dequeue\n")
return
else:
dequeue_val = self.qhead.data
self.qhead = self.qhead.next
return dequeue_val
在一個(gè)洞口進(jìn)進(jìn)出出的動(dòng)態(tài)棧
from linkedlist import *
class Stack(LinkedList):
def __init__(self, data=[0]):
LinkedList.__init__(self)
self.top = self.get_length()
self.init_list(data)
def push(self, item):
self.append(item)
self.top += 1
def pop(self):
if self.top < 0:
print ("no element to pop\n")
return
else:
p = self.head
while p.next != 0:
p = p.next
pop_val = p.data
self.delete(self.get_length()-1)
self.top -= 1
return pop_val
def peek(self):
p = self.head
while p.next != 0:
p = p.next
return p.data