Stack using linked listStack using an array - drawbackIf we implement the stack using an array, we need to specify the array size at the beginning(at compile time). We can't change the size of an array at runtime. So, it will only work for a fixed number of elements. SolutionWe can implement the stack using the linked list. In the linked list, we can change its size at runtime. Linked list implementation of stackInstead of using array, we can also use linked list to implement stack. Linked list allocates the memory dynamically. However, time complexity in both the scenario is same for all the operations i.e. push, pop and peek. In linked list implementation of stack, the nodes are maintained non-contiguously in the memory. Each node contains a pointer to its immediate successor node in the stack. Stack is said to be overflown if the space left in the memory heap is not enough to create a node. The top most node in the stack always contains null in its address field. Lets discuss the way in which, each operation is performed in linked list implementation of stack. Stack using Linked ListStack as we know is a Last In First Out(LIFO) data structure. It has the following operations :
C
DownloadRun Code |