C Tutorial
A queue is a data structure in which addition of new element takes place at end ( also called rear of the queue) and deletion of existing element are always take place at the front of the queue.
First In First Out (FIFO).
Linked list are not stored in adjacent locations and array occupy continuous locations.
#include<stdio.h> //structure contains data and link struct node { int data; struct node *link; }; // add queue element in the rear end void addq(struct node **front, struct node **rear, int data) { struct node *queue; queue = malloc( sizeof (struct node)); queue->data = data; queue->link = NULL; if(*front == NULL) *front = queue; else (*rear)->link = queue; *rear = queue; } // displays the queue elements from front to rear end. void display(struct node *head) { printf("\n"); // traversing entire linked list printf("\nfront==>"); while(head != NULL) { printf("%2d-->", head->data); head = head->link; } printf("rear\n"); } // displays the number of elements in the queue int count(struct node *head) { int c = 0; while(head != NULL) { head = head->link; c++; } return c; } // deletes the front element in the queue. int delq(struct node **front, struct node **rear) { int data; struct node *queue; if(*front == NULL) { printf("\nqueue is empty!"); } else { queue = *front; data = queue->data; *front = queue->link; free(queue); // if deletion leads to queue empty if(*front == NULL) *rear = NULL; return data; } return 0; } void main() { int op, data; struct node *front, *rear; // Empty queue front = rear = NULL; printf("\nNumber of elements in the queue: %d", count(front)); addq(&front, &rear, 1); addq(&front, &rear, 2); addq(&front, &rear, 3); display(front); printf("\nNumber of elements in the queue: %d", count(front)); do { printf("\nQueue\n---------------\n1.add queue \n2.delete queue\n3.display\n4.count\n0.exit"); printf("\nEnter choice: "); scanf("%d", &op); switch(op) { case 1: printf("\nEnter element: "); scanf("%d", &data); addq(&front, &rear, data); break; case 2: printf("Deleted: %d", delq(&front, &rear)); break; case 3: display(front); break; case 4: printf("\nNumber of elements in the queue: %d", count(front)); break; } }while(op>=1 && op<=4); }Compilation:
$ cc queue.c queue.c: In function ‘addq’: queue.c:14:13: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration] queue = malloc( sizeof (struct node)); ^~~~~~ queue.c:14:13: warning: incompatible implicit declaration of built-in function ‘malloc’ queue.c:14:13: note: include ‘Output:’ or provide a declaration of ‘malloc’ queue.c: In function ‘delq’: queue.c:61:9: warning: implicit declaration of function ‘free’ [-Wimplicit-function-declaration] free(queue); ^~~~ queue.c:61:9: warning: incompatible implicit declaration of built-in function ‘free’ queue.c:61:9: note: include ‘ ’ or provide a declaration of ‘free’
$ ./a.out Number of elements in the queue: 0 front==> 1--> 2--> 3-->rear Number of elements in the queue: 3 Queue --------------- 1.add queue 2.delete queue 3.display 4.count 0.exit Enter choice: 3 front==> 1--> 2--> 3-->rear Queue --------------- 1.add queue 2.delete queue 3.display 4.count 0.exit Enter choice: 1 Enter element: 4 Queue --------------- 1.add queue 2.delete queue 3.display 4.count 0.exit Enter choice: 3 front==> 1--> 2--> 3--> 4-->rear Queue --------------- 1.add queue 2.delete queue 3.display 4.count 0.exit Enter choice: 2 Deleted: 1 Queue --------------- 1.add queue 2.delete queue 3.display 4.count 0.exit Enter choice: 3 front==> 2--> 3--> 4-->rear Queue --------------- 1.add queue 2.delete queue 3.display 4.count 0.exit Enter choice: 4 Number of elements in the queue: 3 Queue --------------- 1.add queue 2.delete queue 3.display 4.count 0.exit Enter choice: 0
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page