What is Stack using Linked List?

 

Stack using Linked List:

In linked list implementation of a stack, every new element is inserted as 'top' element. That means every newly inserted element is pointed by 'top'. Whenever we want to remove an element from the stack, simply remove the node which is pointed by 'top' by moving 'top' to its next node in the list. The next field of the first element must be always NULL.


Operations:

To implement stack using linked list, we need to set the following things before implementing actual operations.

• Step 1:Include all the header files which are used in the program &declare all the user defined functions.

• Step 2: Define a 'Node' structure with two member’s data and next.

• Step 3: Define a Node pointer 'top' and set it to NULL.

• Step 4: Implement the main method by displaying Menu with list of operations and make suitable function calls in the main method.

push(value) -Inserting an element into the Stack

Following are the steps to insert a new node into the stack:

• Step 1: Create a new Node with given value.

• Step 2: Check whether stack is Empty (top == NULL)

• Step 3: If it is Empty, then set new Node → next = NULL.

• Step 4: If it is Not Empty, then set new Node → next = top.

• Step 5: Finally, set top = new Node.

 

pop() -Deleting an Element from a Stack

Following are the steps to delete a node from the stack:

• Step 1: Check whether stack is Empty (top == NULL).

• Step 2: If it is Empty, then display "Stack is Empty!!! Deletion is not possible!!!" and terminate the function

• Step 3: If it is Not Empty, then define a Node pointer 'temp' and set it to 'top'.

• Step 4: Then set 'top =top → next'.

• Step 5: Finally, delete 'temp'. (free(temp)).

 

display() -Displaying stack of elements

Following are the steps to display the elements (nodes) of a stack:

• Step 1: Check whether stack is Empty (top == NULL).

• Step 2: If it is Empty, then display 'Stack is Empty!!!' and terminate the function.

• Step 3: If it is Not Empty, then define a Node pointer 'temp' and initialize with top.

• Step 4: Display 'temp → data --->' and move it to the next node. Repeat the same until temp reaches to the first node in the stack. (temp → next ! = NULL).

• Step 5: Finally! Display 'temp → data ---> NULL'.

  


Post a Comment

Thankyou

Previous Post Next Post