Dynamic Implementation Of Stack
CODE
👇
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node
- {
- int info;
- struct node *next;
- }NODE;
- NODE *top;
- void initstack()
- {
- top=NULL;
- }
- int isempty()
- {
- return(top==NULL);
- }
- void push()
- {
- int num;
- NODE* nn=(NODE*)malloc(sizeof(NODE));
- nn->info=num;
- nn->next=NULL;
- nn->next=top;
- top=nn;
- }
- int pop()
- {
- int num;
- NODE * temp = top;
- num = top -> info;
- top = top -> next;
- free (temp);
- return num;
- }
- void main()
- {
- int choice, n;
- initstack();
- do
- {
- printf("\n1.PUSH \n2.POP \n3.EXIT\n");
- printf("Enter Your Choice:");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:/*PUSH*/
- printf("\n ENTER THE TO BE PUSHED:");
- scanf("%d",&n);
- push(n);
- break;
- case 2:/*POP*/
- if(isempty())
- printf("\n >>STACK IS EMPTY<<");
- else
- printf("\n POPPED ELEMENT:%d",pop());
- break;
- default:
- printf("\n>>ENTER THE RIGHT CHOICE<<\n");
- }
- }while(choice!=3);
- }
👉Execute👈
/*
OUTPUT
1.PUSH
2.POP
3.EXIT
Enter Your Choice:1
ENTER THE TO BE PUSHED:5
1.PUSH
2.POP
3.EXIT
Enter Your Choice:2
POPPED ELEMENT:
1.PUSH
2.POP
3.EXIT
Enter Your Choice:4
>>ENTER THE RIGHT CHOICE<<
1.PUSH
2.POP
3.EXIT
Enter Your Choice:3
*/
//ThE ProFessoR
Comments
Post a Comment