Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- typedef struct data
- {
- int a;
- struct data *next;
- } data;
- data *head=NULL;
- void push(int x)
- {
- data *first=(data*)malloc(sizeof(data));
- first->a=x;
- first->next=NULL;
- if(head==NULL)
- {
- head=first;
- return;
- }
- first->next=head;
- head=first;
- return;
- }
- int pop()
- {
- if(head==NULL)
- {
- printf("Empty\n");
- return -1;
- }
- data *x=head->a;
- data *del=head;
- data *temp=head->next;
- head=temp;
- free(del);
- return printf("%d ",x);
- }
- int top()
- {
- if(head==NULL)
- {
- printf("Empty\n");
- return -1;
- }
- return printf("%d ",head->a);
- }
- int main()
- {
- /* push(6);
- push(5);
- push(4);
- pop();
- push(3);
- push(2);
- top();
- */
- while(1)
- {
- printf("Enter 1 for PUSH.\n");
- printf("Enter 2 for POP.\n");
- printf("Enter 3 for TOP.\n");
- printf("Enter 0 for Quit.\n");
- int m,n;
- scanf("%d",&m);
- if(m==1)
- {
- printf("Plz Enter:");
- scanf("%d",&n);
- push(n);
- printf("\n");
- }
- if(m==2)
- {
- pop();
- printf("\n");
- }
- if(m==3)
- {
- top();
- printf("\n");
- }
- if(m==0)
- {
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement