
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program to Implement a Stack using Linked List
When it is required to implement a stack data structure using a linked list, a method to add (push values) elements to the linked list, and a method to delete (pop values) the elements of the linked list are defined.
Below is a demonstration for the same −
Example
class Node: def __init__(self, data): self.data = data self.next = None class Stack_structure: def __init__(self): self.head = None def push_val(self, data): if self.head is None: self.head = Node(data) else: newNode = Node(data) newNode.next = self.head self.head = newNode def pop_val(self): if self.head is None: return None else: del_Val = self.head.data self.head = self.head.next return del_Val my_instance = Stack_structure() while True: print('push <value>') print('pop') print('quit') my_input = input('What action would you like to perform ? ').split() operation = my_input[0].strip().lower() if operation == 'push': my_instance.push_val(int(my_input[1])) elif operation == 'pop': del_Val = my_instance.pop_val() if del_Val is None: print('The stack is empty.') else: print('The deleted value is : ', int(del_Val)) elif operation == 'quit': break
Output
push <value> pop quit What action would you like to perform ? push 56 push <value> pop quit What action would you like to perform ? push 78 push <value> pop quit What action would you like to perform ? push 90 push <value> pop quit What action would you like to perform ? pop The deleted value is : 90 push <value> pop quit What action would you like to perform ? quit
Explanation
The ‘Node’ class is created.
Another ‘Stack_structure’ class with required attributes is created.
It has an ‘init’ function that is used to initialize the first element, i.e the ‘head’ to ‘None’.
A method named ‘push_val’ is defined, that helps add a value to the stack.
Another method named ‘pop_val’ is defined, that helps delete a value from the top of the stack, and returns the deleted value.
An instance of the ‘Stack_structure’ is created.
Three options are given, such as ‘push’, ‘pop’, and ‘quit’.
The ‘push’ option adds a specific value to the stack.
The ‘pop’ option deletes the topmost element from the stack.
The ‘quit’ option comes out of the loop.
Based on the input/choice by user, the respective operations are performed.
This output is displayed on the console.
- Related Articles
- C++ Program to Implement Stack using linked list
- Python Program to Implement Binary Tree using Linked List
- Python Program to Implement Queue Data Structure using Linked List
- C++ Program to Implement Queue using Linked List
- Python Program to Implement a Stack
- Python Program to Implement Stack using One Queue
- Python Program to Implement Stack Using Two Queues
- Print Reverse a linked list using Stack
- Golang program to implement linked list
- C++ Program to Implement Singly Linked List
- C++ Program to Implement Doubly Linked List
- C++ Program to Implement Stack using array
- C++ Program to Implement Circular Singly Linked List
- C++ Program to Implement Circular Doubly Linked List
- C++ Program to Implement Sorted Doubly Linked List
