Implement a stack using singly linked listTo implement a stack using singly linked list concept , all the singly linked list operations are performed based on Stack operations LIFO(last in first out) and with the help of that knowledge we are going to implement a stack using single linked list. Using singly linked lists , we implement stack by storing the information in the form of nodes and we need to follow the stack rules and implement using singly linked list nodes . So we need to follow a simple rule in the implementation of a stack which is last in first out and all the operations can be performed with the help of a top variable .Let us learn how to perform Pop , Push , Peek ,Display operations in the following article . Show
A stack can be easily implemented using the linked list. In stack Implementation, a stack contains a top pointer. which is “head” of the stack where pushing and popping items happens at the head of the list. First node have null in link field and second node link have first node address in link field and so on and last node address in “top” pointer.
Below is the implementation of the above approach: C++
Java
Python3
C#
Javascript
Output: 44->33->22->11-> Top element is 44 22->11-> Top element is 22 Time Complexity: The time complexity for all push(), pop(), and peek() operations is O(1) as we are not performing any kind of traversal over the list. We perform all the operations through the current pointer only.
Article Tags :
Linked List Stack Technical Scripter
Technical Scripter 2018 Practice Tags :
Linked List Stack Advantages and Disadvantages of Linked ListAdvantages of Linked ListDynamic Data Structure Linked list is a dynamic data structure so it can grow and shrink at runtime by allocating and deallocating memeory. So there is no need to give initial size of linked list. Insertion and Deletion Insertion and deletion of nodes are really easier. Unlike array here we don’t have to shift elements after insertion or deletion of an element. In linked list we just have to update the address present in next pointer of a node. No Memory Wastage As size of linked list can increase or decrease at run time so there is no memory wastage. In case of array there is lot of memory wastage, like if we declare an array of size 10 and store only 6 elements in it then space of 4 elements are wasted. There is no such problem in linked list as memory is allocated only when required. Implementation Data structures such as stack and queues can be easily implemented using linked list. Disadvantages of Linked ListMemory Usage More memory is required to store elements in linked list as compared to array. Because in linked list each node contains a pointer and it requires extra memory for itself. Traversal Elements or nodes traversal is difficult in linked list. We can not randomly access any element as we do in array by index. For example if we want to access a node at position n then we have to traverse all the nodes before it. So, time required to access a node is large. Reverse Traversing In linked list reverse traversing is really difficult. In case of doubly linked list its easier but extra memory is required for back pointer hence wastage of memory. Video Tutorial
You May Also Like:
Advantages and Disadvantages of Linked ListAdvantages of Linked List
Disadvantages of Linked List
Application of Linked ListThe linked list is a primitive data structure, which is used in various types of applications.
Difference between linked list and arrayBoth array and linked list are used to store the same type of linear data, but array is allocated contiguous memory location in the compile-time while the linked list is allocated memory in run-time.
? Previous Next ? A quick brush up on the advantages and disadvantages of Linked Lists!In Python, a list is an ordered sequence of values that can be of any type-string, integer, float, etc. It is a non-scalar type Data Structure. All the elements of a list are enclosed in square brackets, separated by commas. Elements in a list are arranged in a sequence beginning with index 0, just like characters in strings. Now those who are well-versed with Data Structures in Python know that stacks allow insertion and deletion of objects at one end while, in queues, new objects are inserted at one end and deletion takes from the other end. Well, there exists a more flexible data structure called a linked list that allows the insertion and deletion of an object at an arbitrary position. Lets' discuss the advantages and disadvantages of linked lists: |