-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha4f3.c
More file actions
170 lines (125 loc) · 2.92 KB
/
a4f3.c
File metadata and controls
170 lines (125 loc) · 2.92 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
168
169
170
/* ASKISI 4
* FYLLADIO 3
* STYLIANOS KARAKOSTAS it12146
*/
#include <stdio.h>
#include <stdlib.h>
#define QueueLimit 21
typedef int QueueElementType;
typedef struct {
int Front, Rear;
QueueElementType Element[QueueLimit];;
} QueueType;
typedef enum {
FALSE, TRUE
} boolean;
void CreateQ(QueueType *Queue);
boolean EmptyQ(QueueType Queue);
boolean FullQ(QueueType Queue);
void AddQ(QueueType *Queue, QueueElementType Item);
void TraverseQ(QueueType Queue);
void RemoveQ(QueueType *Queue, QueueElementType *Item);
void GetNthElementA(QueueType *Queue, int N);
void GetNthElementB(QueueType Queue, int N);
main()
{
int i, n;
QueueType Queue5;
QueueElementType Item;
CreateQ(&Queue5);
for (i=5; i<=100; i=i+5)
{
Item=i;
AddQ(&Queue5, Item);
}
TraverseQ(Queue5);
printf("Give an Integer: ");
scanf("%d", &n);
fflush(stdin);
while(n>=QueueLimit || n<=0)
{
printf("Give a correct Integer (Integer smaller from 20!): ");
scanf("%d", &n);
fflush(stdin);
}
GetNthElementA(&Queue5, n-1);
GetNthElementB(Queue5, n-1);
system("Pause");
return 0;
}
void CreateQ(QueueType *Queue)
{
Queue->Front=0;
Queue->Rear=0;
}
boolean EmptyQ(QueueType Queue)
{
return (Queue.Front==Queue.Rear);
}
boolean FullQ(QueueType Queue)
{
return ((Queue.Front)==((Queue.Rear +1) % QueueLimit));
}
void RemoveQ(QueueType *Queue, QueueElementType *Item)
{
if(!EmptyQ(*Queue))
{
*Item=Queue->Element[Queue->Front];
Queue->Front=(Queue->Front +1) % QueueLimit;
}
else
printf("Empty Queue");
}
void AddQ(QueueType *Queue, QueueElementType Item)
{
int NewRear;
if(!FullQ(*Queue))
{
NewRear=(Queue->Rear+1)%QueueLimit;
Queue->Element[Queue ->Rear]=Item;
Queue->Rear=NewRear;
}
else
printf("Full Queue");
}
void TraverseQ(QueueType Queue)
{
int current;
current=Queue.Front;
printf("Our Queue is: ");
while (current!=Queue.Rear)
{
printf("%d ",Queue.Element[current]);
current=(current+1)%QueueLimit;
}
printf("\n");
}
void GetNthElementA(QueueType *Queue, int N)
{
int It,i;
int thesi=0;
QueueType TempQ;
CreateQ(&TempQ);
while(!EmptyQ(*Queue))
{
RemoveQ(Queue,&It);
AddQ(&TempQ,It);
if(thesi==N)
{
printf("The %d item with the GetNthElementA has the value: %d \n",N+1,Queue->Element[thesi]);
}
thesi++;
}
for(i=1;i<=(thesi);i++)
{
RemoveQ(&TempQ,&It);
AddQ(Queue,It);
}
}
void GetNthElementB(QueueType Queue, int N)
{
int p, Item;
p=Queue.Rear-Queue.Front;
Item = Queue.Element[N-1];
printf("The %d item with the GetNthElementB has the value: %d\n",N+1,Item);
}