Data Structure Stack operation in python

deepalishinkar1 69 views 13 slides Jan 08, 2025
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

stack is last in first out


Slide Content

Stack data structure

stack A stack is a linear data structure that follows the principle of  Last In First Out (LIFO) . This means the last element inserted inside the stack is removed first. Here you can: Put a new plate on top Remove the top plate And, if you want the plate at the bottom, you must first remove all the plates on top. This is exactly how the stack data structure works. LIFO (Last In First Out) principal, putting an item on top of the stack is called  push  and removing an item is called  pop .

Empty Stack PUSH A B A PUSH A B C PUSH A B C POP Stack OPERATIONS push and pop

Basic operations of stack There are some basic operations that allow us to perform different actions on a stack. Push : Add an element to the top of a stack Pop : Remove an element from the top of a stack IsEmpty : Check if the stack is empty IsFull : Check if the stack is full size() -  It returns the length of the stack.  Peek or top : Get the value of the top element without removing it

Implementation of Stack in python Following ways are: List deque LifoQueue

List operations for stack Python list can be used as the stack. It uses the  append()  method to insert elements to the list where stack uses the  push()  method. The list also provides the pop() method to remove the last element. To return size of stack ,python provide function len ().

Implementation using collection.deque The deque is pronounced as the " deck " which means " double-ended queue ". The collection module provides the deque class , which is used to creating Python  stacks. The deque can be preferred over the list because it performs append and pop operation faster than the list.

Implementation Using queue module The queue module has the LIFO queue, which is the same as the stack. Generally, the queue uses the put() method to add the data . Below are a few methods that available in the queue. empty() - If queue empty, returns true; otherwise return false. maxsize () - This method is used to set the maximum number of elements allowed in the queue. qsize () - It returns the size of the queue.

Implementation Using queue module get() - It returns and removes the element from the queue. Sometime. The queue can be empty; it waits until element is available. full() - It returns True if the queue is full. The queue is defined as maxsize = 0 by default. In this case, it will not return True. put(item) - It adds the element to the queue; if the queue is full, wait until space is available. put_nowait (item) - It adds the element into the queue without delaying.

Linked list A linked list is a sequence of data elements, which are connected together via links. Each data element contains a connection to another data element in form of a pointer.   Python does not have linked lists in its standard library. We implement the concept of linked lists using the concept of nodes .

Linked list A linked list is a linear data structure that includes a series of connected nodes. Here, each node stores the  data  and the  address  of the next node. For example,

Single linked list In this type of data structure there is only one link between any two data elements. We create such a list and create additional methods to insert, update and remove elements from the list.

nOTE Every Python module has it’s __name__ defined and if this is ‘__main__’, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions. If you import this script as a module in another script, the __name__ is set to the name of the script/module. Python files can act as either reusable modules, or as standalone programs. if __name__ == “__main__”: is used to execute some code  only  if the file was run directly, and not imported.