-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack-queue.c
130 lines (118 loc) · 2.69 KB
/
stack-queue.c
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
typedef struct IntStack_T {
int *store;
unsigned int size;
unsigned int count;
unsigned int head_index;
} IntStack_T;
typedef struct IntQueue_T {
int *store;
unsigned int size;
unsigned int count;
unsigned int head_index;
unsigned int tail_index;
} IntQueue_T;
/* For all functions: the return value will be:
* 0 for success, -1 for error .
* The algorithmic return value will be "returned" via pointer arguments */
/* STACK */
int init_stack(IntStack_T *x) {
if (x==NULL) { return -1; }
x->store = (int *)malloc(C_SIZE*sizeof(int));
if (x->store == NULL) { return -1; }
x->size = C_SIZE;
x->count = 0;
x->head_index = -1;
return 0;
}
int push(IntStack_T *x,int value) {
if (x==NULL) { return -1; }
if (x->count == x->size) { return -1; }
x->count = x->count + 1;
x->head_index = x->head_index + 1;
x->store[x->head_index] = value;
return 0;
}
int pop(IntStack_T *x,int *pvalue) {
if (x==NULL) { return -1; }
if (pvalue==NULL) { return -1; }
if (x->count==0) { return -1; }
x->count = x->count - 1;
*pvalue = x->store[x->head_index];
x->head_index = x->head_index - 1;
return 0;
}
int empty_stack(IntStack_T *x,unsigned int *IsEmpty) {
if (x==NULL) { return -1; }
if (IsEmpty==NULL) { return -1; }
if (x->count == 0) {
*IsEmpty = 1;
return 0;
} else {
*IsEmpty = 0;
return 0;
}
}
int full_stack(IntStack_T *x,unsigned int *IsFull) {
if (x==NULL) { return -1; }
if (IsFull==NULL) { return -1; }
if (x->count == x->size) {
*IsFull = 1;
return 0;
} else {
*IsFull = 0;
return 0;
}
}
/* QUEUE */
int init_queue(IntQueue_T *x) {
if (x ==NULL) { return -1; }
x->store=(int *)malloc(C_SIZE *sizeof(int));
if (x->store==NULL) { return -1; }
x->size = C_SIZE;
x->count = 0;
x->head_index = -1;
x->tail_index = -1;
return 0;
}
int enqueue(IntQueue_T *x,int value) {
if (x==NULL) { return -1; }
if (x->count == x->size) { return -1; }
x->count = x->count + 1;
x->head_index = (x->head_index + 1) % (x->size);
x->store[x->head_index] = value;
if (x->count == 1) {
x->tail_index = x->head_index;
}
return 0;
}
int dequeue(IntQueue_T *x,int *pvalue) {
if (x==NULL) { return -1; }
if (pvalue==NULL) { return -1; }
if (x->count==0) { return -1; }
x->count = x->count-1;
*pvalue = x->store[x->tail_index];
x->tail_index = (x->tail_index+1)%(x->size);
return 0;
}
int empty_q(IntQueue_T *x,unsigned int *IsEmpty) {
if (x==NULL) { return -1; }
if (IsEmpty==NULL) { return -1; }
if (x->count == 0) {
*IsEmpty = 1;
return 0;
} else {
*IsEmpty = 0;
return 0;
}
}
int full_q(IntQueue_T *x,unsigned int *IsFull) {
if (x==NULL) { return -1; }
if (IsFull==NULL) { return -1; }
if (x->count == x->size) {
*IsFull=1;
return 0;
} else {
*IsFull=0;
return 0;
}
}