Stack and Queue

1 stack

A stack is an abstract data type with the following operations:

  • push(item): Adds an item to the top of the stack; = add(item)
  • pop(): remove the item from the top of the stack and return it; = remove()
  • top(): Look at the item at the top of the stack, but don't remove it. This operation is optional as it can be done by popping the top item and then pushing it back onto the stack. = peek()

shortcoming:

  • Only the top element is accessible;
  • Cannot access random items in the stack;
  • No looping over the stack (unless all items are popped);
  • No need to search in the stack.

stack is Last-In-First-Out (LIFO)

python code

node class

class Node:

#由datum和next组成的节点—datum:节点存储的数据—next:下一个节点的引用
 
 def __init__(self, datum, next):
  self.datum = datum
  self.next = next

stack class

class MyStack:

 """
 实现后进先出栈作为连接topNode -> node -> node -> … -> NULL
 """
 def __init__(self):
 

  #构造空堆栈
  self.topNode = None

top method

class MyStack:
 ...
 def top(self):
    #返回存储在堆栈顶部的项,但不删除它。如果堆栈为空,则返回None。 
    if self.topNode == None:
       return None

    return self.topNode.datum

push method

class MyStack:
 ...
 def push(self, item):
 
    #将一个项添加到堆栈的顶部
    #创建一个要放在堆栈顶部的新节点
    newNode = Node(datum=item, next= None)
    if self.topNode == None:
       # 堆栈为空
       self.topNode = newNode
    else:
       newNode.next = self.topNode
       self.topNode = newNode
 #} 

pop method

class MyStack:
 ...
 def pop(self):
    #
删除存储在堆栈顶部的项并返回它。如果堆栈为空,则返回None。

    #如果堆栈为空
    if (self.topNode == None):
       return None
    #获取存储在顶部节点上的项
    item = self.topNode.datum
    #重置堆栈的顶部节点
    self.topNode = self.topNode.next
    return item

2 queue

A queue is an abstract data type with the following operations:

  • enqueue(item): Add an item to the back of the queue;
  • dequeue(): removes an item from the front of the queue and returns it;
  • front(): Look at the front item of the queue, but don't delete it.

shortcoming:

  • only the preceding elements are accessible;
  • cannot access random items in the queue;
  • The queue cannot be traversed (unless all items are dequeued);
  • The queue cannot be searched.

queue is first-in-first-out (FIFO) first-in-first-out:

python code

node class

class Node:

#由datum和next组成的节点—datum:节点存储的数据—next:下一个节点的引用
 
 def __init__(self, datum, next):
  self.datum = datum
  self.next = next

queue class

class MyQueue:

   #FIFO队列作为链表的实现连接frontNode -> node -> node ->…-> endNode -> NULL
 
   def __init__(self):

      #构造空队列
      self.frontNode = None
      self.endNode = None 

front method

class MyQueue:
 ...
   def front(self):
      #返回存储在队列前端的项,但不删除它。如果队列为空,则返回None。
      if self.frontNode == None:
         return None
      return self.frontNode.datum

enqueue method

class MyQueue:
 ...
   def enqueue(self, item):
      #将项添加到队列的末尾
      #创建一个要放在队列末尾的新节点
      newNode = Node(datum=item, next= None)
      if self.frontNode == None:
         #队列为空
         self.frontNode = newNode
         self.endNode = newNode
      else:
         self.endNode.next = newNode
         self.endNode = newNode

dequeue method

class MyQueue:
 ...
   def dequeue(self):

      #删除存储在队列前端的项并返回它。如果队列为空,则返回None。
 
      # 如果队列为空
      if (self.frontNode == None):
         return None
      #获取存储在前节点上的项
      item = self.frontNode.datum
      #重置此队列的前节点
      self.frontNode = self.frontNode.next
      #如果队列变为空
      if (self.frontNode == None):
         self.endNode = None
      return item

Guess you like

Origin blog.csdn.net/helongss/article/details/129923638