Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct Stack stack;
- void push(int n);
- void pop();
- int top_v();
- int empty();
- void display();
- int s_s=0;
- struct Stack{
- int x;
- stack *next;
- }*top=NULL;
- int top_v()
- {
- return top->x;
- }
- int empty()
- {
- return !top;
- }
- void push(int n)
- {
- s_s++;
- stack *c;
- c=(stack*)malloc(sizeof(stack));
- if(top==NULL)
- {
- c->x=n;
- c->next=NULL;
- top=c;
- }
- else{
- c->x=n;
- c->next=top;
- top=c;
- }
- }
- void display()
- {
- printf("\n");
- stack *c=top;
- while(c!=NULL)
- {
- printf("%d\n",c->x);
- c=c->next;
- }
- printf("\n");
- }
- void pop()
- {
- if(top==NULL)
- {
- printf("The stack is underflow!!\n");
- }
- else{
- top=top->next;
- }
- }
- int main()
- {
- int n;
- do{
- printf("1. push.\n");
- printf("2. pop.\n");
- printf("3. display.\n");
- printf("4. stack is empty or not.\n");
- printf("5. top value.\n");
- printf("10. exit.\n");
- printf("Enter your choice: ");
- scanf("%d",&n);
- if(n==1)
- {
- int x;
- scanf("%d",&x);
- push(x);
- display();
- }
- else if(n==2)
- {
- pop();
- display();
- }
- else if(n==3)
- {
- display();
- }
- else if(n==4)
- {
- if(empty())printf("The stack is empty\n\n");
- else printf("The stack is not empty\n\n");
- }
- else if(n==5)
- {
- printf("Current top value %d\n\n",top_v());
- }
- else{
- printf("Wrong input\n");
- }
- }while(n!=10);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement