-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path22Queue.c
More file actions
167 lines (140 loc) · 3.18 KB
/
Copy path22Queue.c
File metadata and controls
167 lines (140 loc) · 3.18 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
//gcc 22Queue.c
//In Queue,The order in which the data arrives is important
//Queue is a line of items waiting to be served in sequential order
//A Queue is an ordered list in which insertions are done at one end(rear) and deletions are done at other end(front)//Working principal is First In First Out(FIFO)
//Define Structure
struct ArrayQueue{
int front,rear; //rear means last index
int capacity;
int *array;
};
//Create Queue Function
struct ArrayQueue* CreateQueue(int cap){
struct ArrayQueue *Q;
Q=malloc(sizeof(struct ArrayQueue));
if(Q==NULL){
return (NULL);
}
Q->capacity=cap;
Q->front=-1;
Q->rear=-1;
Q->array=malloc(sizeof(int)*Q->capacity);
if(Q->array==NULL){
return NULL;
}
return (Q); //return dynamic block address
}
//Check Queue Empty or Not Function
int IsEmptyQueue(struct ArrayQueue *Q){
//agr queue ke front me -1 hua toh sidda sa matlab hai queue empty hoga
return(Q->front==-1);
}
//Check Queue Is Full Or Not Function
int IsFullQueue(struct ArrayQueue *Q){
//agr full hua toh 1 return hoga warna 0 return hoga
return((Q->rear+1)%Q->capacity==Q->front);
}
//Check Queue Size Function
int QueueSize(struct ArrayQueue *Q){
return((Q->capacity-Q->front+Q->rear+1)%Q->capacity);
}
//Enter Insert Data in Queue
void InsertData(struct ArrayQueue *Q){
int n;
if(IsFullQueue(Q)){
printf("Overflow! Queue Is full");
}
else{
Q->rear=(Q->rear+1)%Q->capacity; //agr phli bar value insert kr rahe hain toh rear me 0 index jayega
printf("\nEnter a number");
scanf("%d",&n);
Q->array[Q->rear]=n;
if(Q->front==-1){
Q->front=Q->rear; //yaha par bhi same agr phli bar value insert rahe toh front me first index yani ki 0 jayega
}
}
}
//Delete Queue Array Value
int DeleteValue(struct ArrayQueue *Q){
int data=-1;
if(IsEmptyQueue(Q)){
printf("Can't Deleted Queue Is Empty");
return (-1);
}
else{
data=Q->array[Q->front];
if(Q->front==Q->rear){
//agr delete krne wali value hi phli 0 index par hui toh ye uske liye front aur rear me dowara -1 set hoga
Q->front=Q->rear=-1;
}
else{
Q->front=((Q->front+1)%Q->capacity);
}
}
return data;
}
//Delete Parmanant Whole Queue
void DeleteQueue(struct ArrayQueue *Q){
if(Q){
if(Q->array)
{
free(Q->array);
}
free(Q);
printf("Queue Successfully deleted");
}
}
int menu(){
int choice;
printf("\n1. Create Queue");
printf("\n2. Check Queue Size");
printf("\n3. Insert Data In Queue");
printf("\n4. Delete Queue Array Value");
printf("\n4. Delete Whole Queue");
printf("\n5. Exit");
printf("\nENTER YOUR CHOICE");
scanf("%d",&choice);
return (choice);
}
void main(){
struct ArrayQueue *Q; //main pointer
int size,qsize,dvalue;
while (1)
{
system("cls");
switch (menu())
{
case 1:
printf("Enter a Queue Size");
scanf("%d",&size);
Q=CreateQueue(size);
break;
case 2:
qsize=QueueSize(Q);
printf("Queue Size is: %d",qsize);
break;
case 3:
InsertData(Q);
break;
case 4:
dvalue=DeleteValue(Q);
printf("deleted value is: %d",dvalue);
break;
case 5:
DeleteQueue(Q);
break;
case 6:
exit(0);
break;
default:
printf("please enter a invaild choice");
break;
}
getch();
}
}
//By Navjot Singh Prince