GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — Python Data Structure - Queue

Python Data Structure Tutorial
  • Python - DS Home
  • Python - DS Introduction
  • Python - DS Environment
  • Python - Arrays
  • Python - Lists
  • Python - Tuples
  • Python - Dictionary
  • Python - 2-D Array
  • Python - Matrix
  • Python - Sets
  • Python - Maps
  • Python - Linked Lists
  • Python - Stack
  • Python - Queue
  • Python - Dequeue
  • Python - Advanced Linked list
  • Python - Hash Table
  • Python - Binary Tree
  • Python - Search Tree
  • Python - Heaps
  • Python - Graphs
  • Python - Algorithm Design
  • Python - Divide and Conquer
  • Python - Recursion
  • Python - Backtracking
  • Python - Sorting Algorithms
  • Python - Searching Algorithms
  • Python - Graph Algorithms
  • Python - Algorithm Analysis
  • Python - Big-O Notation
  • Python - Algorithm Classes
  • Python - Amortized Analysis
  • Python - Algorithm Justifications

Python Data Structures Useful Resources

  • Python - DS Quick Guide
  • Python - DS Useful Resources
  • Python - DS Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

Python Data Structure - Queue



Previous
Quiz
Next

We are familiar with queue in our day to day life as we wait for a service. The queue data structure aslo means the same where the data elements are arranged in a queue. The uniqueness of queue lies in the way items are added and removed. The items are allowed at on end but removed form the other end. So it is a First-in-First out method.

A queue can be implemented using python list where we can use the insert() and pop() methods to add and remove elements. Their is no insertion as data elements are always added at the end of the queue.

Example - Adding Elements to a Queue

In the below example we create a queue class where we implement the First-in-First-Out method. We use the in-built insert method for adding data elements.

main.py

class Queue:
   def __init__(self):
      self.queue = list()

   def addtoq(self,dataval):
      # Insert method to add element
      if dataval not in self.queue:
         self.queue.insert(0,dataval)
         return True
      return False

   def size(self):
      return len(self.queue)

TheQueue = Queue()
TheQueue.addtoq("Mon")
TheQueue.addtoq("Tue")
TheQueue.addtoq("Wed")
print(TheQueue.size())

Output

When the above code is executed, it produces the following result −

3

Example - Removing Element From a Queue

In the below example we create a queue class where we insert the data and then remove the data using the in-built pop method.

main.py

class Queue:
   def __init__(self):
      self.queue = list()

   def addtoq(self,dataval):
      # Insert method to add element
      if dataval not in self.queue:
         self.queue.insert(0,dataval)
         return True
      return False
   # Pop method to remove element
   def removefromq(self):
      if len(self.queue) > 0:
         return self.queue.pop()
      return ("No elements in Queue!")

TheQueue = Queue()
TheQueue.addtoq("Mon")
TheQueue.addtoq("Tue")
TheQueue.addtoq("Wed")
print(TheQueue.removefromq())
print(TheQueue.removefromq())b

Output

When the above code is executed, it produces the following result −

Mon
Tue
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.