TOPICS (Click to Navigate)

Pages

Wednesday, January 17, 2018

C program to implement stack ADT using singly linked list

C program for implementing Stack ADT using singly linked list explained, How to implement stack ADT using singly linked list? How to implement stack data structure using linear linked list in C?


Implementation of Stack using singly linked list


// Program to implement stack using singly linked list
# include <stdio.h>
# include <stdlib.h>

struct stack //structure stack has one data element and one pointer to the next stack node
{
int data;
struct stack *next;
};
/*structure is like our own data type. We can declare variables using the syntax struct structure_name. typedef in the following line helps in giving a simple name so that we can use that name wherever we need to use struct structure_name. For example, in the following sentence, we define the word node in place of struct stack.*/
typedef struct stack node;
void pop();
void display();

node *start=NULL; //initialize the start node with NULL value
/*whenever we perform push operation, we are creating a new node and set that node as the top node. The following function node* pushNode() creates a new node and pushes it in the stack */
node* pushNode()
{
node *temp;
temp=(node *) malloc( sizeof(node)) ; //dynamically allocates memory for new node
printf("\n Enter data ");
scanf("%d", &temp -> data); // reads the value for data element of new node

/* if start node points to NULL, the element pushed into the stack is the first element (new element). Hence, we set the temp->next to NULL to mark that the current new node is the last node also */
if (start==NULL)
{
                temp -> next = NULL;
}
Else // if already elements are in the stack
{
    temp -> next = start; // set the new nodes’ next with the address of current top node in the stack
}
start = temp; // set the new node as the first or top element. The address stored in temp will be copied to start.
}

void pop() // to remove the top element from stack
{
node *temp;
if(start==NULL) //if start == NULL, no elements are there in the stack. Hence stack underflow
{
        printf("Stack underflow...\n");
        return;
}
temp = start; // assigns the address stored in start to temp

/* Following statement resets the start with the address in temp->next. It means the top element is removed. Here we reset the pointer of start to the second element in the stack from top */

start = temp->next;
printf("The popped element is %d\n", temp->data);
free(temp); //the memory for the deleted node is freed so that the memory can be used by others
}

void display()
{
node *temp;
if(start == NULL)
{
printf("\n\n\t\t Stack is empty ");
}
else
{
temp = start;
printf("\n\n\n\t\t Elements in the stack: \n");
while(temp!=NULL) //until we reach NULL pointer, we display temp->data and move temp to temp->next in each iteration
{
printf("%5d ", temp -> data); //displays the data in the stack
temp = temp -> next; //resets the pointer to point to the next node
}
}
}

int main()
{
int ch;

while(1)
{
printf("\n \tStack operations using pointers.. ");
printf("\n -----------**********-------------\n");
printf("\n 1. Push ");
printf("\n 2. Pop ");
printf("\n 3. Display");
printf("\n 4. Quit ");
printf("\n Enter your choice: ");
scanf("%d", &ch);

switch(ch)
{
case 1 : pushNode();
       break;
case 2 : pop();
       break;
case 3 : display();
       break;
case 4: exit(0);
}
}
}


***************