Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Header File-----------------------------------------------
- #include <stdio.h>
- #ifndef Header_h_
- #define Header_h_
- struct list{
- int data;
- struct list *next;
- };
- void push(int value);
- void pop();
- void enqueue(int value);
- void dequeue();
- void show();
- #endif
- //----------------------------------------------------
- // Source 1------------------------------------------(Start)
- #include <stdio.h>
- #include "Header.h"
- #include <stdlib.h>
- typedef struct list node;
- node *temp,*top=NULL,*r,*f=NULL,*tail;
- void push(int value)
- {
- if(top == NULL){
- temp = (node*)malloc(sizeof(node));
- temp->data = value;
- top = temp;
- top->next = NULL;
- r = top;
- }
- else
- {
- temp = (node*)malloc(sizeof(node));
- temp->data = value;
- temp -> next = top;
- top = temp;
- r = top;
- while(r -> next != NULL)
- r = r -> next;
- }
- }
- void pop()
- {
- if(top == NULL)
- printf("\nStack Underflow!!!\n");
- else
- {
- temp = top;
- top = top -> next;
- free(temp);
- r = top;
- }
- }
- void enqueue(int value)
- {
- if(top == NULL)
- {
- temp = (node*)malloc(sizeof(node));
- temp -> data = value;
- temp -> next = NULL;
- r = top;
- }
- else{
- temp = (node*)malloc(sizeof(node));
- temp -> data = value;
- temp -> next = NULL;
- r -> next = temp;
- r = temp;
- }
- }
- void dequeue()
- {
- if(top == NULL)
- printf("\nUnderflow!!!\n");
- else
- {
- temp = top;
- top = top -> next;
- free(temp);
- }
- }
- void show()
- {
- tail = top;
- while(tail != NULL)
- {
- printf("%d ",tail->data);
- tail = tail->next;
- }
- }
- //----------------------------------------------------------(End)
- //Source----------------------------------------------------------(Start)
- #include <stdio.h>
- #include "Header.h"
- #include <stdlib.h>
- int main()
- {
- int value,choice;
- while(1)
- {
- printf("------------Stack and Queue Implementation--------\n");
- printf("1) Push\n");
- printf("2) Pop\n");
- printf("3) Enqueue\n");
- printf("4) Dequeue\n");
- printf("5) Display all\n");
- printf("6) Exit\n");
- printf("Enter your choice: \n");
- scanf("%d",&choice);
- printf("\n");
- switch(choice)
- {
- case 1:
- printf("Enter the value: \n");
- scanf("%d",&value);
- push(value);
- break;
- case 2:
- pop();
- break;
- case 3:
- printf("Enter the value: \n");
- scanf("%d",&value);
- enqueue(value);
- break;
- case 4:
- dequeue();
- break;
- case 5:
- show();
- break;
- case 6:
- exit(0);
- default:
- printf("\n\nWrong input!!!\n\n");
- }
- }
- return 0;
- }
- //----------------------------------------------------------(End)
Add Comment
Please, Sign In to add comment