-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueue.c
More file actions
84 lines (69 loc) · 1.56 KB
/
Queue.c
File metadata and controls
84 lines (69 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include<stdio.h>
#include<stdlib.h>
struct Node* front = NULL; //Global front
struct Node* rear = NULL; //Global rear
//Linkedlist node representation
struct Node
{
int data;
struct Node* next;
};
struct Node* enqueue(struct Node* front, int x)
{
//Dynamic node creation
struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = x;
temp->next = NULL;
//if the queue is empty front and rear points to the same node
if(rear==NULL)
{
front=rear=temp;
}
//else insert elements in the rear end of the queue
else
{
rear->next = temp;
rear = temp;
}
return front;
}
void dequeue()
{
struct Node* temp;
//if the queue is empty no elements to display
if(front==NULL)
{
printf("Queue is Empty\n");
return;
}
//else delete from the front end of the queue
else
{
temp = front;
front = front->next;
free(temp);
}
}
void print(struct Node* front)
{
while(front!=NULL)
{
printf("%d\t",front->data);
front = front->next;
}
}
int main()
{
//Inserting elements in the queue
front = enqueue(front,1);
front = enqueue(front,2);
front = enqueue(front,3);
front = enqueue(front,4);
//printing the queue elements
print(front);
printf("\n");
//Deleting elements from the queue
dequeue();
//printing the queue elements
print(front);
}