Python Program to Implement a Stack Using Linked List29 Aug 2024 | 4 min read Stacks are linear data structures that follow the Last-In-First-Out (LIFO) principle, which states that the item that was most recently added is the one that gets deleted first. A stack's fundamental commands are "push", "pop", "peek" (or top), and "isEmpty". Each stack element in a linked list implementation is represented as a node in the linked list. The head of the linked list serves as a representation of the top of the stack. The following is a description of an algorithm to implement a stack using a linked list:
Complete implementation of a stack using a linked list in Python: Node class: It is a simple class that defines the structure of each node in the linked list. It has two instance variables: next and data, which represents the item being added to the stack, and a pointer to the next node in the stack. If a Node object is created without any arguments, the default value of data is None. Stack class: This class represents the stack itself. It has only one instance variable: top, which is a pointer to the top node of the stack. If the stack is empty, top is None. The push method adds a new node to the top of the stack. It creates a new Node object with the given data and sets its next pointer to the current top of the stack. The top pointer is then updated to include the new node. The pop method removes and returns the top item from the stack. It first checks if the stack is empty (i.e. if the top is None). If it's not empty, it saves the data of the current top node, updates the top to point to the next node in the stack, and returns the saved data. Without deleting it, the peek method returns the data of the top item in the stack. It begins by making sure the stack is empty. If it's not empty, it returns the data attribute of the top node. The is_empty method returns True if the stack is empty (i.e., if top is None) and False otherwise. Some other additional points:
It's important to note that a linked list implementation of a stack in Python is not thread-safe, meaning that it's not safe to use in a multi-threaded environment. If multiple threads are accessing the same stack object and modifying it concurrently, it can result in unexpected behavior and data corruption. In a multi-threaded environment, it's recommended to use a thread-safe implementation of a stack. Time Complexity: The time complexity of the basic stack operations using a linked list implementation are: Push: O(1) Pop: O(1) Peek: O(1) Is Empty: O(1) All these operations take constant time, regardless of the size of the stack. It is because we only need to modify the top node of the linked list to perform any of these operations. Space Complexity: The space complexity of a stack using a linked list in Python depends on the number of elements in the stack. Since each element in the stack is represented by a node in the linked list, the space required is proportional to the number of elements in the stack. We can conclude that the space complexity of a stack using a linked list in Python is O(n), where n is the number of elements in the stack. It is to be noted that the space complexity of a linked list implementation of a stack is not affected by the maximum size of the stack, unlike an array implementation which would require a fixed amount of memory for the maximum size of the stack. Next TopicScraping a JSON Response with Scrapy |
? Using Python's tolist() function, you may turn an array into a list. Here is an example: import array arr = array.array("i", [1, 2, 3, 4, 5]) lst = arr.tolist() print(type(lst)) # <class 'list'> print(lst) Output: [1, 2, 3, 4, 5] In this example, the array.array() constructor is used to create an array arr...
2 min read
Bokeh is an Interactive Data visualization library of Python. It creates its plots by using HTML and JavaScript languages. Its basic targets are modern website browsers for presenting provided elegance, concise construction of novel graphics with high-performance interactivity. In this tutorial, we will learn how to create...
3 min read
Python has a built-in function called classmethod() that gives a class method of the specified function.; Syntax: classmethod( function ) Parameter: This method accepts the name of the function as its parameter. Return Type: This method returns the function converted to a class method. We can also use the decorator form...
3 min read
SFTP, abbreviated for SSH File Transfer Protocol and known as Secure File Transfer Protocol, is a network protocol that allows us to access files, transfer them and manage them over any dependable data stream. The program works on a secure channel, like SSH, that the...
7 min read
The internet is quite large, and approximately 4.10 billion individuals use it to interact online. According to reports, there are more than 100 billion websites, though the figure changes daily. All praise goes to the digital revolution and our quick progress toward moving our operations online. The...
9 min read
This post will demonstrate how to use PyQt5 to develop a flames calculator. Based on an algorithm of two provided names, this flames calculator evaluates relationships and forecasts how they may turn out. The most well-liked and effective programming language is Python. Python has a strong developer...
10 min read
Python automated testing can be done with the Selenium Python Module. The Selenium Python bindings provide a straightforward API for writing functional and acceptability tests with Selenium WebDriver. Visit Navigating links using the get method - Selenium Python to learn how to open a website using...
2 min read
In the following tutorial, we will discuss some of the best Python modules or tools that are used for automation and testing. There can be various issues while producing software, and Automation and Testing are one of the best ways to resolve these problems without spending...
5 min read
? Introduction: This post will teach us how to employ Python to clean the Recycle Bin. The Recycle Bin is a temporary storage location for deleted files and folders on Windows systems. A deleted document or folder is relocated to the Recycle Bin in which it can if...
4 min read
The most adaptable language is Python, which is used in nearly every industry, including game development, web development, machine learning, artificial intelligence, and GUI applications. The game is developed using the pygame package, which is a built-in feature of Python. With a rudimentary understanding of Python programming,...
12 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India