C Program: An Implementation of Stack Data Structure in C (In linked list)

CWC
4 Min Read

This is a linked list implementation of stack data structure in C language. Linked List is one of the data structures used to implement stack data structure. A linked List is an ordered collection of elements called nodes. Each node contains a data field and a pointer to the next node. The link to the next node is kept in a chain and the last node in the linked list is called the tail pointer. We need to keep track of two things in each node – data and the pointer to the next node. In this code, we use 2 pointers. One is for the head pointer and the other is for the tail pointer. We maintain the stack by inserting the nodes at the end of the linked list.

In this program, I have created a simple program to understand the implementation of Stack Data Structure in C. In the linked list implementation of a stack, every new element is inserted as a ‘top‘ element. That means every newly inserted element is pointed by the ‘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.

  • Step 1: Include all the header files which are used in the program. And 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

We can use the following steps to insert a new node into the stack…

  • Step 1: Create a newNode with given value.
  • Step 2: Check whether stack is Empty (top == NULL)
  • Step 3: If it is Empty, then set newNode → next = NULL .
  • Step 4: If it is Not Empty, then set newNode → next = top .
  • Step 5: Finally, set top = newNode .

pop() – Deleting an Element from a Stack

We can use the following 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 7: Finally, delete ‘temp’ (free(temp)) .

display() – Displaying stack of elements

We can use the following 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 4: Finally! Display ‘temp → data —> NULL’ .

Download source code for C Program: An Implementation of Stack Data Structure in C (In linked list)

[sociallocker]
C Program: An Implementation of Stack Data Structure in C (In linked list)
Password: codewithc.com
[/sociallocker]

 

Share This Article
1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version