This blog is under construction

Friday 22 February 2013

C Program To Implement Circular Queue

Circular queue can be implemented using circular linked list.  In circular queue, insertion is performed at the front(head) of the queue and deletion is performed at the rear(node previous to head) position.  Basically, we have end to end (direct connection between head and tail)connection in case of circular linked list.

See Also:
C Program For Array Implementation Of Queue
C Program For Array Implementation Of Stack
C Program For Linked List Implementation Of Stack
C Program For Linked List Implementation Of Queue
C Program For Double Ended Queue (Dequeue)
C Program To Implement Circular Stack
C Program To Implement Circular Queue




Sample Program For Circular Queue Implementation Using Circular Linked List:



  #include <stdio.h>
  #include <stdlib.h>

  struct cnode {
        int data;
        struct cnode *next;
  };

  struct cnode *head = NULL;

  struct cnode * createNode(int data) {
        struct cnode *newnode;
        newnode = (struct cnode *)malloc(sizeof (struct cnode));
        newnode->data = data;
        newnode->next = NULL;
  }

  /* insertion in circular queue */
  void enqueue(int data) {
        struct cnode *newnode, *temp;
        int tempData;
        if (!head) {
                head = createNode(data);
                head->next = head;
        } else {
                /*
                 * Instead of adding newnode previous to
                 * head(rear position), swap the contents of
                 * newnode and head.  Then insert newnode next
                 * to head and make head point to newnode. This
                 * operation is equivalent to adding newnode
                 * at rear position.(Reason: code simplicity)
                 */
                newnode = createNode(data);
                temp = head->next;
                head->next = newnode;
                newnode->next = temp;
                tempData = newnode->data;
                newnode->data = head->data;
                head->data = tempData;
                head = newnode;
        }
  }

  /* deletion in circular queue */
  void dequeue() {
        struct cnode *temp;
        int tmpdata;
        if (!head) {
                printf("Circular Queue is empty\n");
                return;
        }
        if (head->next == head) {
                free(head);
                head = NULL;
                return;
        }
        /* deletion at front */
        temp = head->next;
        head->data = temp->data;
        head->next = temp->next;
        free(temp);
  }

  /* display operation */
  void display() {
        struct cnode *temp = head;
        if (!head) {
                printf("Circular Queue is empty\n");
                return;
        }
        printf("%-3d", head->data);
        temp = temp->next;
        while (temp != head) {
                printf("%-3d", temp->data);
                temp = temp->next;
        }
        printf("\n");
        return;
  }

  int main() {
        int data, ch;
        while (1) {
                printf("1. Enqueue\t2.Dequeue\n");
                printf("3. Display\t4.Exit\n");
                printf("Please enter your choice:");
                scanf("%d", &ch);
                switch (ch) {
                        case 1:
                                printf("Enter the data to insert:");
                                scanf("%d", &data);
                                enqueue(data);
                                break;
                        case 2:
                                dequeue();
                                break;
                        case 3:
                                display();
                                break;
                        case 4:
                                exit(0);
                        default:
                                printf("Please enter correct option\n");
                                break;
                }
        }
        return 0;
  }



  Output: (C Program To Implement Circular Queue Example)
  jp@jp-VirtualBox:$ ./a.out
  1. Enqueue 2.Dequeue
  3. Display 4.Exit
  Please enter your choice:1
  Enter the data to insert:10
  1. Enqueue 2.Dequeue
  3. Display 4.Exit
  Please enter your choice:1
  Enter the data to insert:20
  1. Enqueue 2.Dequeue
  3. Display 4.Exit
  Please enter your choice:1
  Enter the data to insert:30
  1. Enqueue 2.Dequeue
  3. Display 4.Exit
  Please enter your choice:3
  10 20 30 
  1. Enqueue 2.Dequeue
  3. Display 4.Exit
  Please enter your choice:2
  1. Enqueue 2.Dequeue
  3. Display 4.Exit
  Please enter your choice:3
  20 30 
  1. Enqueue 2.Dequeue
  3. Display 4.Exit
  Please enter your choice:2
  1. Enqueue 2.Dequeue
  3. Display 4.Exit
  Please enter your choice:2
  1. Enqueue 2.Dequeue
  3. Display 4.Exit
  Please enter your choice:3
  Circular Queue is empty
  1. Enqueue 2.Dequeue
  3. Display 4.Exit
  Please enter your choice:4



1 comment:

  1. I’m impressed, I need to say. Actually rarely do I encounter a blog that’s each educative and entertaining, and let me tell you, you've hit the nail on the head. Your thought is outstanding; the difficulty is something that not enough people are talking intelligently about. I'm very happy that I stumbled throughout this in my seek for one thing referring to this. best online casino

    ReplyDelete