Advertisement
Shailrshah

Circular Queue Using Linked List

Nov 7th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct queue{
  4.     int data;
  5.     struct queue *next;
  6. };
  7. void inqueue(struct queue **front,struct queue **rear,int x)
  8.  {
  9.     struct queue *newNode;
  10.     newNode=(struct queue*) malloc(sizeof(struct queue));
  11.     newNode->data = x; newNode->next = *front;
  12.     if((*front)==NULL) (*front)=newNode;
  13.     else (*rear)->next=newNode;
  14.     (*rear)=newNode;
  15.  }
  16. void dequeue(struct queue **front, struct queue **rear){
  17.     struct queue *save = *front;
  18.     if((*front) == (*rear))(*front) = (*rear)  = NULL;
  19.     else{
  20.         (*front) = (*front)->next;
  21.         (*rear)->next = (*front);
  22.     }
  23.     free(save);
  24. }
  25. void display(struct queue *front, struct queue *rear){
  26.     printf("\nThe Queue is ");
  27.     if(!front) printf("Empty");
  28.     else if(front == rear) printf("%d", front->data);
  29.     else{
  30.         while(front != rear){
  31.             printf(" ->%d", front->data);
  32.             front = front->next;
  33.         }
  34.         printf(" ->%d", rear->data);
  35.     }
  36. }
  37. int main(){
  38.     int n, choice;
  39.     struct queue *front = NULL, *rear = NULL;
  40.     while(1){
  41.         display(front, rear);
  42.         printf("\n1. Insert 2.Delete 3.Exit: ");
  43.         scanf("%d", &choice);
  44.         switch(choice){
  45.             case 1: printf("Enter the value: ");
  46.                     scanf("%d", &n);
  47.                     inqueue(&front, &rear, n);
  48.                     break;
  49.             case 2: if(front) dequeue(&front, &rear);
  50.                     break;
  51.             case 3: return 0;
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement