1/*
2 * Program : Queue using linked list
3 * Language : C
4 */
5
6#include<stdio.h>
7#include<stdlib.h>
8
9struct node
10{
11 int data;
12 struct node *next;
13};
14
15struct node *front = NULL, *rear = NULL;
16
17void enqueue(int val)
18{
19 struct node *newNode = malloc(sizeof(struct node));
20 newNode->data = val;
21 newNode->next = NULL;
22
23 //if it is the first node
24 if(front == NULL && rear == NULL)
25 //make both front and rear points to the new node
26 front = rear = newNode;
27 else
28 {
29 //add newnode in rear->next
30 rear->next = newNode;
31
32 //make the new node as the rear node
33 rear = newNode;
34 }
35}
36
37void dequeue()
38{
39 //used to free the first node after dequeue
40 struct node *temp;
41
42 if(front == NULL)
43 printf("Queue is Empty. Unable to perform dequeue\n");
44 else
45 {
46 //take backup
47 temp = front;
48
49 //make the front node points to the next node
50 //logically removing the front element
51 front = front->next;
52
53 //if front == NULL, set rear = NULL
54 if(front == NULL)
55 rear = NULL;
56
57 //free the first node
58 free(temp);
59 }
60
61}
62
63void printList()
64{
65 struct node *temp = front;
66
67 while(temp)
68 {
69 printf("%d->",temp->data);
70 temp = temp->next;
71 }
72 printf("NULL\n");
73}
74
75int main()
76{
77 enqueue(10);
78 enqueue(20);
79 enqueue(30);
80 printf("Queue :");
81 printList();
82 dequeue();
83 printf("After dequeue the new Queue :");
84 printList();
85 dequeue();
86 printf("After dequeue the new Queue :");
87 printList();
88
89 return 0;
90}
91
1/*
2 * Program : Queue using linked list
3 * Language : C
4 */
5
6#include<stdio.h>
7#include<stdlib.h>
8
9struct node
10{
11 int data;
12 struct node *next;
13};
14
15struct node *front = NULL, *rear = NULL;
16
17void enqueue(int val)
18{
19 struct node *newNode = malloc(sizeof(struct node));
20 newNode->data = val;
21 newNode->next = NULL;
22
23 //if it is the first node
24 if(front == NULL && rear == NULL)
25 //make both front and rear points to the new node
26 front = rear = newNode;
27 else
28 {
29 //add newnode in rear->next
30 rear->next = newNode;
31
32 //make the new node as the rear node
33 rear = newNode;
34 }
35}
36
37void dequeue()
38{
39 //used to free the first node after dequeue
40 struct node *temp;
41
42 if(front == NULL)
43 printf("Queue is Empty. Unable to perform dequeue\n");
44 else
45 {
46 //take backup
47 temp = front;
48
49 //make the front node points to the next node
50 //logically removing the front element
51 front = front->next;
52
53 //if front == NULL, set rear = NULL
54 if(front == NULL)
55 rear = NULL;
56
57 //free the first node
58 free(temp);
59 }
60
61}
62
63void printList()
64{
65 struct node *temp = front;
66
67 while(temp)
68 {
69 printf("%d->",temp->data);
70 temp = temp->next;
71 }
72 printf("NULL\n");
73}
74
75int main()
76{
77 enqueue(10);
78 enqueue(20);
79 enqueue(30);
80 printf("Queue :");
81 printList();
82 dequeue();
83 printf("After dequeue the new Queue :");
84 printList();
85 dequeue();
86 printf("After dequeue the new Queue :");
87 printList();
88
89 return 0;
90}