4

Click here to load reader

Stack Using LL

Embed Size (px)

Citation preview

Page 1: Stack Using LL

1 | P a g e

C program to implement a stack using linked listWe will make a linked list that will be treated as a stack, i.e. we will only implement LIFO in the linked list instead of haphazard operations on it. We can insert data in stack or delete data from stack in only one way, i.e. the top element. So we will operate the insert and delete func on only the top element. when attack is implemented as linked list, the pointer part of each node points to the next node in stack, i.e. top will point the top element of the stack (by default),but the pointer of top node in the stack will point to the node beneath it, and so on.

#include<stdio.h>#include<malloc.h>

void push();void pop();void display();

int main() { int n; /*Global variable*/ printf("MENU\n 1.PUSH\n 2.POP\n 3.DISPLAY\n 4.EXIT\n"); do /*Do-While loop*/ { printf("\nEnter your choice\n"); scanf("%d",&n); switch(n) { case 1: push(); break; case 2: pop(); break; case 3: display(); break; case 4: break; default: printf("Invalid choice\n"); break; }

Page 2: Stack Using LL

2 | P a g e

} while(n!=4); /*loop ends*/ } typedef struct node /*type define ‘node’ as of type struct where node is a node in the linked list*/ { int data; struct node *link; /*link is the pointer part of type node(struct) for a node in the linked list*/ }n; /*n is a node of type struct*/

n *top=NULL; /*top is a pointer of type n(node) since it will always point the top node or top element*//*top is now initialized to point NULL or nothing*/

void push() { /*declare a data part(here item) and a pointer(here temp)*/ int item; n *temp; /*temp is a pointer of type n (node) since it will point to some node*/ printf("Enter the item\n"); scanf("%d",&item); /*put the data inserted in item(data part of a node)*/ temp=(n*)malloc(sizeof(n));

/*malloc() takes the size of n, i.e the node, given by the compiler as input or

argument and return the address of the respective inserted node as a pointer to the pointer variable temp that merely points the node inserted currently*/

temp->data=item; /*since temp is a pointer of type node, it inserts item in the data part of this new node*/

temp->link=top; /*temp declares the top to point this new node*/ top=temp; /*top will be now the same as temp since it points to the last added node*/ }

void pop() { n *temp; /*temp is a pointer of type n (node) since it will point to some node*/ if(top==NULL) /*if top pointer points nothing that means stack is empty*/ printf("Stack is empty\n"); else {

Page 3: Stack Using LL

3 | P a g e

temp=top; /*assign top as temp*/ printf("The element deleted = %d\n",temp->data); /*show that the data is being deleted*/ free(temp); /*free() function clears the memory area assigned to the top node*/ top=top->link; /*now the link part of the top pointer or simply the top pointer will point

the new top node in the stack*/ } }

void display() { n *save; /*declare save as a pointer of type n (node)*/

if (top==NULL) /*if top pointer points nothing that means stack is empty*/ printf("Stack is empty\n");

else { save=top;

/*assign top pointer to save pointer, since save pointer will point the node that is to be displayed on the screen*/

printf("The elements of the stack are :"); while(save!=NULL) /*start a while loop to test whether save becomes null or the

stack becomes empty */ { printf("%d",save->data); /*print the data part of the node save is pointing to*/ save=save->link; /*increment save pointer to point the next node in the stack */ } printf("\n Topmost element = %d\n",top->data); /*prints the top element of the stack at last*/ } }