Created
October 16, 2017 16:23
-
-
Save VirajKanse/e5da940507a77f23c6986c11f1935bbb to your computer and use it in GitHub Desktop.
Stack Using Linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
void push(); | |
void pop(); | |
void display(); | |
main() | |
{ | |
int n; | |
clrscr(); | |
printf("\n------------\n"); | |
printf("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT\n"); | |
printf("\n------------\n"); | |
do | |
{ | |
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; | |
} | |
} | |
while(n!=4); | |
} | |
typedef struct node | |
{ | |
int data; | |
struct node *link; | |
}n; | |
n *top=NULL; | |
void push() | |
{ | |
int rn,mk; | |
n *temp; | |
printf("\n----------------------\n"); | |
printf("\nEnter The Roll.No.\n"); | |
scanf("%d",&rn); | |
printf("\nEnter The Marks\n"); | |
scanf("%d",&mk); | |
printf("\n----------------------\n"); | |
temp=(n*)malloc(sizeof(n)); | |
temp->data=(rn,mk); | |
temp->link=top; | |
top=temp; | |
} | |
void pop() | |
{ | |
n *temp; | |
if(top==NULL) | |
printf("Stack is empty\n"); | |
else | |
{ | |
temp=top; | |
printf("The element deleted = %d\n",temp->data); | |
free(temp); | |
top=top->link; | |
} | |
} | |
void display() | |
{ | |
n *save; | |
if(top==NULL) | |
printf("Stack is empty\n"); | |
else | |
{ | |
save=top; | |
printf("The elements of the stack are :"); | |
while(save!=NULL) | |
{ | |
printf("%d\t",save->data); | |
save=save->link; | |
} | |
printf("\nTopmost element = %d\n",top->data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment