SoFunction
Updated on 2024-12-10

Python common data structures of the stack and queue usage examples

This article example describes the Python common data structure of the stack and queue usage. Shared for your reference, as follows:

Common Python Data Structures - Stacks

First, the stack is a data structure. It has a last-in-first-out characteristic.

#stack implementation
class Stack():
  def __init__(self,size):
    =[]
    =size
    =-1
  def push(self,content):
    if ():
      print "Stack is Full"
    else:
      (content)
      =+1
  def out(self):
    if ():
      print "Stack is Empty"
    else:
      -=1
  def Full(self):
    if ==-1:
      return True
    else:
      return False
  def Empty(self):
    if ==-1:
      print "Stack is Empty"
if __name__=="__main__":
  q=Stack(7)
  ()
  ("hello")
  ()

Run results:

Stack is Empty

Python Common Data Structures - Queues

A queue is a first-in-first-out data structure.

#Queue implementation
class Queue():
  def __init__(self,size):
    =[]
    =size
    =-1
    =-1
  def Empty(self):
    if ==:
      return True
    else:
      return False
  def Full(self):
    if ==-1:
      return True
    else:
      return False
  def enQueue(self,content):
    if ():
      print "Queue is Full"
    else:
      (content)
      +=1
  def outQueue(self):
    if ():
      print "Queue is Empty!"
    else:
      +=1
if __name__=="__main__":
  q=Queue(6)
  print () # True
  ("123")
  print () #False
  ()

Run results:

True
False

Readers interested in more Python related content can check out this site's topic: thePython Data Structures and Algorithms Tutorial》、《Summary of Python encryption and decryption algorithms and techniques》、《Summary of Python coding manipulation techniques》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that the description of this article will be helpful for you Python Programming.