Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct Node
- {
- int data;
- struct Node *next;
- }*temp;
- struct Head
- {
- int count;
- struct Node *top;
- }*head;
- void PushNode(int data)
- {
- if(head->top==NULL)
- {
- temp=(struct Node*)malloc(sizeof(struct Node));
- temp->data=data;
- temp->next=NULL;
- head->top=temp;
- }
- else{
- temp=(struct Node*)malloc(sizeof(struct Node));
- temp->data=data;
- temp->next=head->top;
- head->top=temp;
- }
- head->count++;
- }
- void PopNode()
- {
- temp=head->top;
- if(temp==NULL)
- {
- printf("ERROR!!!\n Empty stack");
- return;
- }
- else{
- head->top=temp->next;
- printf("delated value is %d",temp->data);
- free(temp);
- head->count--;
- }
- }
- void createStack()
- {
- head=(struct Head*)malloc(sizeof(struct Head));
- head->count=0;
- head->top=NULL;
- }
- void checkTop()
- {
- temp=head->top;
- if(temp==NULL)
- {
- printf("Empty Stack");
- }
- else
- {
- printf("\ntop of the stack is %d",temp->data);
- }
- }
- void show()
- {
- int i;
- temp=head->top;
- for(i=1;i<=head->count;i++)
- {
- printf("\n%d",temp->data);
- temp=temp->next;
- }
- }
- int main()
- {
- int choice,data;
- /* createStack();
- PushNode(9);
- PushNode(11);
- PushNode(16);
- PushNode(19);
- show();
- PopNode();
- show();
- checkTop();
- */
- createStack();
- while(1)
- {
- printf("\npress 1 for push node\n");
- printf("press 2 for pop node\n");
- printf("press 3 for stack top\n");
- printf("press 4 for check stack\n");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- printf("enter data\n");
- scanf("%d",&data);
- PushNode(data);
- break;
- case 2:
- PopNode();
- break;
- case 3:
- checkTop();
- break;
- case 4:
- show();
- default:
- printf("nothing to show");
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment