Learn Python – Python Stack and Queue- Basic and advance

Data structure organizes the storage in computers so that we can without difficulty get admission to and trade data. Stacks and Queues are the earliest information shape described in computer science. A easy Python listing can act as a queue and stack as well. A queue follows FIFO rule (First In First Out) and used in programming for sorting. It is frequent for stacks and queues to be carried out with an array or linked list.

Stack

A Stack is a records structure that follows the LIFO(Last In First Out) principle. To enforce a stack, we want two easy operations:

push – It adds an element to the top of the stack.

pop – It removes an element from the top of the stack.

Operations:

Adding – It adds the items in the stack and increases the stack size. The addition takes place at the top of the stack.

Deletion – It consists of two conditions, first, if no element is present in the stack, then underflow occurs in the stack, and second, if a stack contains some elements, then the topmost element gets removed. It reduces the stack size.

Traversing – It involves visiting each element of the stack.

Characteristics:

Insertion order of the stack is preserved.

Useful for parsing the operations.

Duplicacy is allowed.

Code

# Code to demonstrate Implementation of   
# stack using list   
x = ["Python", "C", "Android"]   
x.push("Java")   
x.push("C++")   
print(x)   
print(x.pop())   
print(x)   
print(x.pop())   
print(x)   

Output:

['Python', 'C', 'Android', 'Java', 'C++']
C++
['Python', 'C', 'Android', 'Java']
Java
['Python', 'C', 'Android']

Queue

A Queue follows the First-in-First-Out (FIFO) principle. It is opened from both the ends hence we can without problems add elements to the lower back and can cast off factors from the front.

To enforce a queue, we need two easy operations:

enqueue – It adds an element to the end of the queue.

dequeue – It removes the element from the beginning of the queue.

Operations on Queue

Addition – It adds the element in a queue and takes place at the rear end, i.e., at the back of the queue.

Deletion – It consists of two conditions – If no element is present in the queue, Underflow occurs in the queue, or if a stack contains some elements then element present at the front gets deleted.

Traversing – It involves to visit each element of the queue.

Characteristics

Insertion order of the queue is preserved.

Duplicacy is allowed.

Useful for parsing CPU task operations.

Note: The implementation of a queue is a little bit different. A queue follows the “First-In-First-Out”. Time performs an necessary issue here. The Stack is quick because we insert and pop the factors from the end of the list, whereas in the queue, the insertion and pops are made from the commencing of the list, so it turns into slow. The purpose of this time difference is due to the properties of the list, which is quickly in the stop operation however gradual at the opening operations due to the fact all different factors have to be shifted one by one.

Code

import queue   
# Queue is created as an object 'L'  
L = queue.Queue(maxsize=10)   
  
# Data is inserted in 'L' at the end using put()   
L.put(9)   
L.put(6)   
L.put(7)   
L.put(4)   
# get() takes data from   
# from the head    
# of the Queue   
print(L.get())   
print(L.get())   
print(L.get())   
print(L.get())   

Output:

9
6
7
4