File tree 1 file changed +31
-20
lines changed
1 file changed +31
-20
lines changed Original file line number Diff line number Diff line change 3
3
4
4
#define MAX 100
5
5
6
- typedef struct {
6
+ typedef struct
7
+ {
7
8
int front ;
8
9
int rear ;
9
10
int q [MAX ];
10
- }queue ;
11
-
11
+ } queue ;
12
12
13
13
void enqueue (queue * Q , int x )
14
14
{
15
- if (Q -> front == -1 ){
15
+ if (Q -> front == -1 )
16
+ {
16
17
Q -> front = 0 ;
17
18
Q -> q [++ Q -> rear ] = x ;
18
19
}
@@ -47,20 +48,23 @@ int dequeue(queue *Q)
47
48
}
48
49
}
49
50
50
- void display (queue * Q ){
51
+ void display (queue * Q )
52
+ {
51
53
if (Q -> front == -1 )
52
54
return ;
53
55
54
- for (int i = Q -> front ; i <= Q -> rear ; i ++ ){
56
+ for (int i = Q -> front ; i <= Q -> rear ; i ++ )
57
+ {
55
58
printf ("-> %d " , Q -> q [i ]);
56
59
}
57
60
printf ("\n" );
58
61
}
59
62
60
- void arrange (queue * Q ){
63
+ void arrange (queue * Q )
64
+ {
61
65
for (int i = Q -> front ; i <= Q -> rear ; i ++ )
62
66
{
63
- Q -> q [i - Q -> front ] = Q -> q [i ];
67
+ Q -> q [i - Q -> front ] = Q -> q [i ];
64
68
}
65
69
Q -> rear = Q -> rear - Q -> front ;
66
70
Q -> front = 0 ;
@@ -69,29 +73,36 @@ void arrange(queue *Q){
69
73
display (Q );
70
74
}
71
75
72
- int main (){
73
-
76
+ int main ()
77
+ {
78
+
74
79
queue q ;
75
80
q .front = -1 ;
76
81
q .rear = -1 ;
77
82
78
83
int ch , x ;
79
84
80
- while (1 ){
85
+ while (1 )
86
+ {
81
87
printf ("Enqueue(1),Dequeue(2),Display(3): " );
82
88
scanf ("%d" , & ch );
83
89
84
90
switch (ch )
85
91
{
86
- case 1 : scanf ("%d" , & x );
87
- enqueue (& q , x );
88
- break ;
89
- case 2 : x = dequeue (& q );
90
- if (x != -1 ) printf ("dequeued %d\n" , x );
91
- break ;
92
- case 3 : display (& q );
93
- break ;
94
- default : exit (0 );
92
+ case 1 :
93
+ scanf ("%d" , & x );
94
+ enqueue (& q , x );
95
+ break ;
96
+ case 2 :
97
+ x = dequeue (& q );
98
+ if (x != -1 )
99
+ printf ("dequeued %d\n" , x );
100
+ break ;
101
+ case 3 :
102
+ display (& q );
103
+ break ;
104
+ default :
105
+ exit (0 );
95
106
}
96
107
}
97
108
return 0 ;
You can’t perform that action at this time.
0 commit comments