Python Program to Implement a Stack using Linked List














































Python Program to Implement a Stack using Linked List



Description:
We have to implement Stack using Linked List, In order to do that first we need to implement Linked List after that in the linked list we have to define two method push() and pop(). To implement a linked list you may prefer Python Program to Create a Linked List & Display the Elements in the List.

In the push() method we add a new node with push data at the beginning of the linked list.

In the pop() method we remove the node from the beginning of the linked list and return the data of the removed node. If there is no node then return 'None' and print 'Stack is empty'. Otherwise, just return the data of the removed node.

Finally, we have implemented the stack using a linked list with pop and push functionality.

For better understanding prefer Python3 Program:
class Node:
def __init__(self,data):
self.data = data
self.next = None


class Stack:
def __init__(self):
self.head = None

def display(self):
temp = self.head
print('Element of Stack is:[', end='')
while temp:
print(temp.data, end=", ")
temp = temp.next
print(']')

def push(self, data):

if self.head is None:
self.head = Node(data)
else:
temp = Node(data)
temp.next=self.head
self.head=temp
self.display()

def pop(self):

if self.head is None:
print('Stack is Empty')
return None

else:
popped=self.head.data
self.head=self.head.next
return popped



def main():
l = Stack()
print('1.push\n2.pop\n3.exit')
while 1:
choice=input('Enter your option:')
if choice is '1':
d=int(input('Enter data:'))
l.push(d)
elif choice is '2':
print('poped element is:',l.pop())
l.display()
else:
break


if __name__ == '__main__':
main()



Output:
1.push
2.pop
3.exit
Enter your option:1
Enter data:5
Element of Stack is:[5, ]
Enter your option:1
Enter data:6
Element of Stack is:[6, 5, ]
Enter your option:1
Enter data:7
Element of Stack is:[7, 6, 5, ]
Enter your option:1
Enter data:8
Element of Stack is:[8, 7, 6, 5, ]
Enter your option:1
Enter data:9
Element of Stack is:[9, 8, 7, 6, 5, ]
Enter your option:2
poped element is: 9
Element of Stack is:[8, 7, 6, 5, ]
Enter your option:2
poped element is: 8
Element of Stack is:[7, 6, 5, ]
Enter your option:2
poped element is: 7
Element of Stack is:[6, 5, ]
Enter your option:2
poped element is: 6
Element of Stack is:[5, ]
Enter your option:2
poped element is: 5
Element of Stack is:[]
Enter your option:2
Stack is Empty
poped element is: None
Element of Stack is:[]
Enter your option:3


Time complexity to perform push and pop operation is O(1).

More Articles of Dilkhush Kumar:

Name Views Likes
Python Program to Read the Contents of a File 1069 22
Python PostgreSQL insert record into table and get inserted ID 2030 6
Python PostgreSQL prevent SQL injection in DELETE 1149 4
Python PostgreSQL sort the result in ascending order 1464 7
Python Program to Reverse a Stack using Recursion 956 32
Python PostgreSQL where example and usgae 888 6
Python Program to Count the Number of Occurrences of an Element in the Linked List using Recursion 944 26
Python PostgreSQL select particlular column from table 868 7
Python PostgreSQL create table 991 7
Python PostgreSQL 831 4
Python PostgreSQL Order By 830 7
Python Program to Implement Queue 925 15
Python Program to Implement Queue Data Structure using Linked List 1303 29
Python PostgreSQL check database is exist or not 8990 16
Python Program to Create a Linked List & Display the Elements in the List 911 21
Python PostgreSQL limit the no of records in a table 975 7
Python Program to Count the Number of Lines in a Text File 1015 16
Python Program to Implement Bubble Sort 873 17
Python Program to Implement a Stack 974 32
Python Program to Reverse a Stack without using Recursion 1210 35
Python PostgreSQL update existing record 999 7
Python PostgreSQL select from table 1063 6
Python Program to Implement Queues using Stacks 994 23
Python Program to Implement Stack Using Two Queues 1679 27
Python PostgreSQL select with a Filter 2414 6
Python Program to Search for an Element in the Linked List using Recursion 1024 27
Python PostgreSQL insert record into table 961 7
Python PostgreSQL prevent SQL injection in SELECT 1146 4
Python PostgreSQL multiple insert record into table 1029 7
Python PostgreSQL select using fetchone method 962 7
Python Program to Search for an Element in the Linked List without using Recursion 756 22
Python Program to Implement Quicksort 807 21
Python Program to Copy the Contents of One File into Another 2565 14
Python PostgreSQL wildcard select 1344 6
Python PostgreSQL create database 1031 19
Python PostgreSQL sort the result in descending order 1566 7
Python Program to Implement Stack using One Queue 1020 23
Python Program to Count the Number of Occurrences of an Element in the Linked List without using Recursion 1089 18
Python PostgreSQL prevent SQL injection in UPDATE 801 3
Python Program to Append the Contents of One File to Another File 1300 29
Python PostgreSQL join two table 1256 3
Python DB2 connector get started 866 1
Python Program to Find Whether a Number is a Power of Two 753 24
Python PostgreSQL sort the result 837 6
Python Program to Search for a Particular Value in a Binary Tree 909 17
test 876 9
test1 654 4
Python Program to Find the Area of a Triangle Given All Three Sides 826 27
Python Program to Count the Frequency of Words Appearing in a String Using a Dictionary 1000 25
Python Program to Count the Occurrences of a Word in a Text File 1662 24
Python Program to Implement a Stack using Linked List 3256 31
Python PostgreSQL drop table 923 7
Python Program to Find the Length of the Linked List using Recursion 1026 20
Python Program to Display all the Nodes in a Linked List using Recursion 1025 32
Python Program to Find the Length of the Linked List without using Recursion 724 25
Python PostgreSQL delete record from the table 1709 6
Python PostgreSQL drop table only if exists 2263 9
Python PostgreSQL connector get started 935 17
Python Program to Count the Number of Words in a Text File 1494 24
Python Program to Print all the Prime Numbers within a Given Range 785 18
Python Program to Read a File and Capitalize the First Letter of Every Word in the File 1406 22
Python Program to Read a String from the User and Append it into a File 1205 14
Python Program to Implement Merge Sort 912 25

Comments