目录标题
队列的定义 顺序队列的实现
循环队列的实现
链式队列的实现 链式队列的初始化 入队 出队 获取队首元素 链式队列总代码与调试
队列的定义
定义:队列(Queue)是一种常见的数据结构,它遵循“先进先出”(First In First Out,FIFO)的原则。类似于排队买票的情况,排在前面的先离开队列,后面来的排在队尾。 特点:
队列只允许在一端删除,在另一端插入的线性表 允许删除的一端称为队首,允许插入的一端称为队尾 向队列中插入元素称入队,从队列中删除元素称为出队 队列示意图
顺序队列的实现
初始化
class SeqQueue :
"""
顺序队列的定义
"""
def __init__ ( self, max ) :
"""
顺序队列的初始化
:param max: 队列大小
"""
self. max = max
self. data = [ None for i in range ( self. max ) ]
self. front = 0
self. rear = 0
入队
def enter ( self, data) :
"""
入队
:param data: 入队元素
:return:
"""
if self. rear == self. max :
raise IndexError( '队列已满' )
self. data[ self. rear] = data
self. rear += 1
出队
核心思想
检查队列是否为空 获取队首元素 队首指针front向后移动一格 出队示意图
def delete ( self) :
"""
出队
:return: 出队元素
"""
if self. front == self. rear:
raise IndexError( '队空' )
data = self. data[ self. front]
self. front += 1
return data
顺序队列总代码与调试
class SeqQueue :
"""
顺序队列的定义
"""
def __init__ ( self, max ) :
"""
顺序队列的初始化
:param max: 队列大小
"""
self. max = max
self. data = [ None for i in range ( self. max ) ]
self. front = 0
self. rear = 0
def enter ( self, data) :
"""
入队
:param data: 入队元素
:return:
"""
if self. rear == self. max :
raise IndexError( '队列已满' )
self. data[ self. rear] = data
self. rear += 1
def delete ( self) :
"""
出队
:return: 出队元素
"""
if self. front == self. rear:
raise IndexError( '队空' )
data = self. data[ self. front]
self. front += 1
return data
if __name__ == '__main__' :
print ( 'PyCharm' )
seqQueue = SeqQueue( 8 )
seqQueue. enter( 10 )
seqQueue. enter( 20 )
seqQueue. enter( 30 )
data = seqQueue. delete( )
print ( '当前出队元素:' , data)
data = seqQueue. delete( )
print ( '当前出队元素:' , data)
seqQueue. enter( 40 )
seqQueue. enter( 50 )
seqQueue. enter( 60 )
data = seqQueue. delete( )
print ( '当前出队元素:' , data)
data = seqQueue. delete( )
print ( '当前出队元素:' , data)
data = seqQueue. delete( )
print ( '当前出队元素:' , data)
循环队列的实现
顺序队列易出现队列溢出的情况,针对这种情况的解决思路,设计了循环队列的线性表。 顺序队列中存在的假溢出现象
设定
队首取下一个位置为:(front + 1) % max 队尾取下一个位置为:(rear + 1) % max 队空:front == rear 队满:(rear + 1) % max == front
初始化
class CSeqQueue :
"""
顺序队列的定义
"""
def __init__ ( self, max ) :
"""
顺序队列的初始化
:param max: 队列大小
"""
self. max = max
self. data = [ None for i in range ( self. max ) ]
self. front = 0
self. rear = 0
def isEmpty ( self) :
"""
循环队列是否为空
:return: true or false
"""
return self. front == self. rear
入队
核心思想
检查队列是否已满 在队尾插入元素值 取队尾的下一个位置 代码实现
def enter ( self, data) :
"""
入队
:param data: 入队元素
:return:
"""
if ( self. rear + 1 ) % self. max == self. front:
raise IndexError( '队列已满' )
self. data[ self. rear] = data
self. rear = ( self. rear + 1 ) % self. max
出队
def delete ( self) :
"""
出队
:return: 返回队首元素
"""
if self. isEmpty( ) :
raise IndexError( '队列为空' )
data = self. data[ self. front]
self. front = ( self. front + 1 ) % self. max
return data
获取队首元素
def peak ( self) :
"""
获取队首元素
:return: 队首元素
"""
if self. isEmpty( ) :
raise IndexError( '队列为空' )
return self. data[ self. front]
循环队列总代码与调试
class CSeqQueue :
"""
顺序队列的定义
"""
def __init__ ( self, max ) :
"""
顺序队列的初始化
:param max: 队列大小
"""
self. max = max
self. data = [ None for i in range ( self. max ) ]
self. front = 0
self. rear = 0
def isEmpty ( self) :
"""
循环队列是否为空
:return: true or false
"""
return self. front == self. rear
def enter ( self, data) :
"""
入队
:param data: 入队元素
:return:
"""
if ( self. rear + 1 ) % self. max == self. front:
raise IndexError( '队列已满' )
self. data[ self. rear] = data
self. rear = ( self. rear + 1 ) % self. max
def delete ( self) :
"""
出队
:return: 返回队首元素
"""
if self. isEmpty( ) :
raise IndexError( '队列为空' )
data = self. data[ self. front]
self. front = ( self. front + 1 ) % self. max
return data
def peak ( self) :
"""
获取队首元素
:return: 队首元素
"""
if self. isEmpty( ) :
raise IndexError( '队列为空' )
return self. data[ self. front]
if __name__ == '__main__' :
print ( 'PyCharm' )
cseqQueue = CSeqQueue( 5 )
cseqQueue. enter( 10 )
cseqQueue. enter( 20 )
cseqQueue. enter( 30 )
print ( '输出队首元素:' , cseqQueue. peak( ) )
data = cseqQueue. delete( )
print ( '出队的元素:' , data)
print ( '队首元素:' , cseqQueue. peak( ) )
data = cseqQueue. delete( )
print ( '出队的元素:' , data)
print ( '队首元素:' , cseqQueue. peak( ) )
cseqQueue. enter( 40 )
cseqQueue. enter( 50 )
cseqQueue. enter( 60 )
print ( '队首元素:' , cseqQueue. peak( ) )
链式队列的实现
结点设置:与单链表一致,包括数据域data与指针域next。 链式队列示意图 代码实现
class Node :
"""
定义链式队列结点
"""
def __init__ ( self, data) :
self. data = data
self. next = None
链式队列的初始化
def __init__ ( self) :
"""
链式队列初始化
"""
self. front = Node( None )
self. rear = self. front
def isEmpty ( self) :
"""
判断是否为空
:return: true or false
"""
return self. front == self. rear
入队
核心思想
入队示意图 代码实现
def enter ( self, data) :
"""
入队
:param data: 入队元素值
"""
newNode = Node( data)
self. rear. next = newNode
self. rear = newNode
出队
核心思想
检查队列是否为空 保留队首结点 将队首结点的next指针指向队首结点的后继结点,实现删除 出队示意图 代码实现
def delete ( self) :
"""
出队
:return: 出队值
"""
if self. isEmpty( ) :
raise IndexError( '队列为空' )
else :
node = self. front. next
self. front. next = node. next
return node
获取队首元素
def peak ( self) :
"""
获取队首元素
:return: 队首元素
"""
if self. isEmpty( ) :
raise IndexError( '队列为空' )
else :
return self. front. next . data
链式队列总代码与调试
class Node :
"""
定义链式队列结点
"""
def __init__ ( self, data) :
self. data = data
self. next = None
class LinkedQueue :
"""
链式队列定义
"""
def __init__ ( self) :
"""
链式队列初始化
"""
self. front = Node( None )
self. rear = self. front
def isEmpty ( self) :
"""
判断是否为空
:return: true or false
"""
return self. front == self. rear
def enter ( self, data) :
"""
入队
:param data: 入队元素值
"""
newNode = Node( data)
self. rear. next = newNode
self. rear = newNode
def delete ( self) :
"""
出队
:return: 出队值
"""
if self. isEmpty( ) :
raise IndexError( '队列为空' )
else :
node = self. front. next
self. front. next = node. next
return node
def peak ( self) :
"""
获取队首元素
:return: 队首元素
"""
if self. isEmpty( ) :
raise IndexError( '队列为空' )
else :
return self. front. next . data
if __name__ == '__main__' :
print ( 'PyCharm' )
linkedQueue = LinkedQueue( )
linkedQueue. enter( 'A' )
linkedQueue. enter( 'B' )
linkedQueue. enter( 'C' )
print ( '输出队首元素:' , linkedQueue. peak( ) )
node = linkedQueue. delete( )
print ( '出队元素:' , node. data)
node = linkedQueue. delete( )
print ( '出队元素:' , node. data)
print ( '输出队首元素:' , linkedQueue. peak( ) )