作者:踏山321 | 来源:互联网 | 2023-10-12 17:07
Python数据结构&算法(03)栈和队列文章目录Python数据结构&算法(03)栈和队列3.1栈3.1.1栈的基本操作3.1.2链式栈3.2队列3.2.1队列的基本操作3.2.
Python数据结构&算法(03) 栈和队列
文章目录 Python数据结构&算法(03) 栈和队列 3.1 栈 3.2 队列 3.2.1 队列的基本操作 3.2.2 链式队列 3.3.3 循环队列
3.1 栈 栈 可以理解为只允许在表尾进行插入或删除的线性表,对栈而言,其表尾称为栈顶,相应的其表头端称为栈底,不含元素的空表称为空栈。
假设栈 S = (a1, a2, ..., an)
,则称 a1
为栈底元素,an
为栈顶元素。栈中元素是按照 a1, a2, ..., an
的顺序依次进栈的,退栈的第一个元素应为栈顶元素,因此栈可以被称为 后进先出(LIFO)
结构的线性表。
3.1.1 栈的基本操作 栈的抽象数据类型定义如下所示:
ADT Stack{数据对象:D = {ai | ai ∈ ElemSet, i = 1, 2, ..., n, n >= 0}数据关系:R = { | ai-1, ai ∈ D, i = 2, ..., n}约定 an 为栈顶,a1 为栈底。基本操作:初始化栈插入元素删除元素清空栈判断栈是否为空返回栈中元素个数返回栈顶元素的值遍历栈 }
在python中,列表 很好的实现了栈的功能,使用相关函数即可实现出栈和入栈的操作。
list . append( ) list . pop( [ index= - 1 ] )
3.1.2 链式栈 如果使用链表实现栈则可以使用尾插法模拟入栈操作,指定删除最后一个元素即可实现出栈操作。
class Node : def __init__ ( self, elem= None ) : self. elem = elemself. next = None class Stack : def __init__ ( self) : self. __base = Node( - 1 ) self. __top = self. __basedef push ( self, elem) : newNode = Node( elem) self. __top. next = newNodeself. __top = newNodedef pop ( self) : if self. __base. next == None : print ( "Error!" ) return 0 else : nowNode = self. __basewhile nowNode. next . next != None : nowNode = nowNode. next nowNode. next = None self. __top = nowNodedef clear ( self) : self. __base. next = None self. __top = self. __basedef isEmpty ( self) : return self. __base. elem == self. __top. elemdef num ( self) : num = 0 nowNode = self. __basewhile nowNode. next != None : nowNode = nowNode. next num = num + 1 return numdef getTop ( self) : return self. __top. elemdef printStack ( self) : nowNode = self. __base. next while nowNode != None : print ( nowNode. elem) nowNode = nowNode. next if __name__ == "__main__" : S = Stack( ) for i in range ( 1 , 10 ) : S. push( i) S. printStack( )
3.2 队列 队列 与栈相反,是一种先进先出(FIFO)
的线性表,它只允许在表的一端进行插入,而在另一端删除元素。在队列中,允许插入的一端叫做队尾 ,允许删除的一端叫做队头 。
3.2.1 队列的基本操作 队列的操作与栈的操作类似,不同之处在于删除是在表的头部(队头)进行的。
ADT Queue{数据对象:D = {ai | ai ∈ ElemSet, i = 1, 2, ..., n, n >= 0}数据关系:R = { | ai-1, ai ∈ D, i = 2, ..., n}基本操作:初始化队列插入元素删除元素清空队列判断队列是否为空返回队列中元素个数返回队头元素的值遍历队列 }
3.2.2 链式队列 用链表表示的队列简称为链队列 ,一个链队列需要两个分别指示队头和队尾的指针。
class Node : def __init__ ( self, elem= None ) : self. elem = elemself. next = None class Queue : def __init__ ( self) : self. __front = Node( - 1 ) self. __rear = self. __frontdef enQueue ( self, elem) : newNode = Node( elem) self. __rear. next = newNodeself. __rear = newNodedef deQueue ( self) : if self. __front. next == None : print ( "Error!" ) return 0 else : self. __front. next = self. __front. next . next def clear ( self) : self. __front. next = None self. __rear = self. __frontdef isEmpty ( self) : return self. __front == self. __reardef num ( self) : num = 0 nowNode = self. __frontwhile nowNode. next != None : nowNode = nowNode. next num = num + 1 return numdef getHead ( self) : if self. __front. next == None : print ( "Error!" ) return 0 else : return self. __front. next . elemdef printQueue ( self) : nowNode = self. __front. next while nowNode != None : print ( nowNode. elem) nowNode = nowNode. next if __name__ == "__main__" : Q = Queue( ) for i in range ( 1 , 10 ) : Q. enQueue( i) Q. printQueue( )
3.3.3 循环队列 循环队列需要给队列指定一个最大长度,整体逻辑结构如同一个环,这里实现采用顺序表示(用一组地址连续的存储单元依次存储元素)。
class CircularQueue : def __init__ ( self, maxSize) : self. queue = [ None ] * maxSizeself. maxSize = maxSizeself. __front = 0 self. __rear = 0 def num ( self) : if self. queue[ self. __rear] == None : return ( self. __rear - self. __front + self. maxSize) % self. maxSizeelse : return self. maxSizedef enQueue ( self, elem) : if ( self. __rear + 1 ) % self. maxSize == self. __front: if self. queue[ self. __rear] == None : self. queue[ self. __rear] = elemelse : print ( "FULL QUEUE!" ) else : self. queue[ self. __rear] = elemself. __rear = ( self. __rear + 1 ) % self. maxSizedef deQueue ( self) : if self. __front == self. __rear: print ( "EMPTY QUEUE!" ) return 0 else : self. queue[ self. __front] = None if ( self. __rear + 1 ) % self. maxSize == self. __front: self. __rear = ( self. __rear + 1 ) % self. maxSizeself. __front = ( self. __front + 1 ) % self. maxSizedef printCircularQueue ( self) : for i in range ( self. maxSize) : print ( self. queue[ i] ) if __name__ == "__main__" : CQ = CircularQueue( 9 ) for i in range ( 1 , 10 ) : CQ. enQueue( i) CQ. printCircularQueue( )