赞
踩
队列和栈都是一种特殊的线性表,所以也各有顺序表和链表两种表示方法。
队列的Python版本数据结构如下:
# -*- coding=utf-8 -*-
class Node(object):
def __init__(self, value, next=0):
self.value = value
self.next = next # 指针
class Queue(object):
# 由于 Python 操作地址困难,所以这里给出了链队列的数据结构
# 队列中存在两个指针,一个头指针,一个尾指针,但是在这里就省去了,只留了一个尾指针
def __init__(self):
self.head = 0
def init_queue(self, data):
self.head = Node(data[0])
p = self.head
for i in data[1:]:
p.next = Node(i)
p = p.next
def clear_queue(self):
self.head = 0
def is_empty(self):
if self.head == 0:
return True
else:
return False
def get_length(self):
p, length = self.head, 0
while p.next != 0:
length += 1
p = p.next
return length
def en_queue(self, value): # 向队列的尾部中添加一个结点
if self.is_empty():
self.head = Node(value)
else:
p = self.head
for _ in xrange(self.get_length()):
p = p.next
p.next = Node(value)
def get_head(self): # 返回队列的队首元素
if self.is_empty():
print 'This is an empty queue.'
return
else:
return self.head.value
def de_queue(self): # 删除队列首部的元素并返回该值
if self.is_empty():
print 'This is an empty queue.'
return
else:
p = self.head
self.head = p.next
return p.value
def show_queue(self): # 打印队列中的所有元素
if self.is_empty():
print 'This is an empty queue.'
else:
p, container = self.head, []
for _ in xrange(self.get_length()):
container.append(p.value)
p = p.next
container.append(p.value)
print container
q = Queue()
q.init_queue([1, 7, 22, 50, 111, 225])
q.show_queue()
print q.get_head()
q.en_queue(999)
q.de_queue()
print q.get_head()
q.show_queue()
q.de_queue()
q.de_queue()
q.show_queue()
栈的Python版本数据结构类如下:
# -*- coding=utf-8 -*-
class Node(object):
# 栈结点
def __init__(self, value, next=0):
self.value = value
self.next = next # 指针
class Stack(object):
# 由于 Python 难以对内存地址进行操作,所以这里给出了链栈的数据结构
# 由于栈的操作比线性表少很多,所以顺序栈的表示法要比链栈方便快捷
def __init__(self):
self.head = 0
def init_stack(self, data):
self.head = Node(data[0])
p = self.head
for i in data[1:]:
p.next = Node(i)
p = p.next
def clear_stack(self):
self.head = 0
def is_empty(self):
if self.head == 0:
return True
else:
return False
def get_length(self):
p, length = self.head, 0
while p != 0:
length += 1
p = p.next
return length
def push(self, value): # 向栈中添加一个结点
if self.is_empty():
self.head = Node(value)
else:
p = self.head
for _ in xrange(self.get_length()-1):
p = p.next
p.next = Node(value)
def get_top(self): # 获得栈顶元素
if self.is_empty():
print 'This is an empty stack.'
return
else:
p = self.head
for _ in xrange(self.get_length()):
p = p.next
return p.value
def pop(self): # 弹出栈顶元素
length = self.get_length()
if self.is_empty():
print 'This is an empty stack.'
return
elif length == 1:
p = self.head
self.head = 0
return p.value
elif length == 2:
p = self.head
value = p.next.value
self.head.next = 0
return value
else:
p = self.head
for _ in xrange(1, length-1):
p = p.next
pop = p.next
p.next = 0
return pop.value
def show_stack(self): # 打印栈中的所有元素
if self.is_empty():
print 'This is an empty stack.'
else:
p, container = self.head, []
for _ in xrange(self.get_length()-1):
container.append(p.value)
p = p.next
container.append(p.value)
print container
s = Stack()
s.init_stack([1, 7, 22, 50, 111, 225])
s.show_stack()
print s.get_top()
s.push(999)
print s.pop()
s.show_stack()
s.pop()
s.pop()
s.show_stack()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。