-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_Queue.c
More file actions
103 lines (103 loc) · 2.65 KB
/
Circular_Queue.c
File metadata and controls
103 lines (103 loc) · 2.65 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>
#include<stdlib.h>
struct Cqueue
{
int data;
struct Cqueue *next;
};
struct Cqueue *rear,*traverse;//global struct Cqueue pointers
void Display(struct Cqueue *front)//function to display elements in Circular Queue
{
if(front == NULL)
printf("Circular Queue is EMPTY!!!\n");
else
{
traverse=front;
printf("%d|%d(front)-->",traverse->data,traverse->next);
traverse=traverse->next;
while(traverse != front)
{
if(traverse->next == front)
printf("%d|%d(rear)",traverse->data,traverse->next);
else
printf("%d|%d-->",traverse->data,traverse->next);
traverse=traverse->next;
}
}
}
struct Cqueue *create_node()//function to create memory space for element insertion in Circular Queue
{
struct Cqueue *rear;
rear = (struct Cqueue *)malloc(sizeof(struct Cqueue));//allocate memory to insert elements
printf("Enter the data to insert(Cqueue):");
scanf("%d",&rear->data);
return rear;
}
struct Cqueue *enqueue(struct Cqueue *front)//function to insert elements in Circular Queue and traverse
{
rear = create_node();
if(front == NULL)
{
front = rear;
front->next = front;
}
else
{
traverse = front;
while(traverse->next != front)
traverse=traverse->next;
traverse->next = rear;
rear->next = front;
}
return front;
}
struct Cqueue *dequeue(struct Cqueue *front)//function to delete elements in Circular Queue
{
if(front == NULL)
return front;
else if(front->next == front)
{
rear = front;
front = NULL;
}
else
{
rear=traverse=front;
while(traverse->next != front)
traverse=traverse->next;
traverse->next=front->next;
front=front->next;
}
free(rear);
return front;
}
int main()
{
int a;//switch variable
char ch;//loop variable
struct Cqueue *front = NULL;//head node connecting other nodes
do
{
printf("1.Enqueue\n2.Dequeue\n3.Display\nEnter your choice : ");
scanf("%d",&a);
switch(a)
{
case 1:
front=enqueue(front);
Display(front);
break;
case 2:
front=dequeue(front);
Display(front);
break;
case 3:
Display(front);
break;
default:
printf("INVALID CHOICE!!!\n");
}
printf("\nDo you want to Continue(Y/N): ");
scanf(" %c",&ch);
} while(ch == 'Y' || ch == 'y');
return 0;
}