![]() | ||
|
In Data Structures using Python, stack is a linear data structure that follows LIFO (Last In First Out) order. Here, we will implement stack using Linked List concept with methods like push(), pop(), peek(), size() and is_empty(). This is also part of DSA Assignment 10 for Python students.
Task 1 – Import Linked List Module
- Import module containing singly linked list code in your python file.
Task 2 – Create Stack Class
- Define a class Stack to implement stack data structure. Define \_\_init\_\_() method to create Singly Linked List (SLL) object.
Task 3 – Check If Stack is Empty
- Define a method is\_empty() to check if the stack is empty in Stack class.
Task 4 – Add Element in Stack
- In Stack class, define push() method to add data onto the stack.
Task 5 – Remove Element from Stack
- In Stack class, define pop() method to remove top element from the stack.
Task 6 – Return Top Element
- In Stack class, define peek() method to return top element on the stack.
Task 7 – Return Stack Size
- In Stack class, define size() method to return size of the stack that is number of items present in the stack.
✔ Works on LIFO principle
✔ Constant time complexity O(1) for push & pop
✔ Best for dynamic stack implementation
This was the complete implementation of Stack using Linked List in Python with all major operations. Students can use this code for their DSA Assignment 10 and improve Python skills.
