Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct queue{
- int data;
- struct queue *next;
- };
- void inqueue(struct queue **front,struct queue **rear,int x)
- {
- struct queue *newNode;
- newNode=(struct queue*) malloc(sizeof(struct queue));
- newNode->data = x; newNode->next = *front;
- if((*front)==NULL) (*front)=newNode;
- else (*rear)->next=newNode;
- (*rear)=newNode;
- }
- void dequeue(struct queue **front, struct queue **rear){
- struct queue *save = *front;
- if((*front) == (*rear))(*front) = (*rear) = NULL;
- else{
- (*front) = (*front)->next;
- (*rear)->next = (*front);
- }
- free(save);
- }
- void display(struct queue *front, struct queue *rear){
- printf("\nThe Queue is ");
- if(!front) printf("Empty");
- else if(front == rear) printf("%d", front->data);
- else{
- while(front != rear){
- printf(" ->%d", front->data);
- front = front->next;
- }
- printf(" ->%d", rear->data);
- }
- }
- int main(){
- int n, choice;
- struct queue *front = NULL, *rear = NULL;
- while(1){
- display(front, rear);
- printf("\n1. Insert 2.Delete 3.Exit: ");
- scanf("%d", &choice);
- switch(choice){
- case 1: printf("Enter the value: ");
- scanf("%d", &n);
- inqueue(&front, &rear, n);
- break;
- case 2: if(front) dequeue(&front, &rear);
- break;
- case 3: return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement