-
Notifications
You must be signed in to change notification settings - Fork 0
/
steque.c
100 lines (76 loc) · 1.78 KB
/
steque.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
#include <stdlib.h>
#include <stdio.h>
#include "steque.h"
void steque_init(steque_t *this){
this->front = NULL;
this->back = NULL;
this->N = 0;
}
void steque_enqueue(steque_t* this, steque_item item){
steque_node_t* node;
node = (steque_node_t*) malloc(sizeof(steque_node_t));
node->item = item;
if(item==NULL){
printf("Item is NULL\n");
}
node->next = NULL;
if(this->back == NULL)
this->front = node;
else
this->back->next = node;
this->back = node;
this->N++;
}
void steque_push(steque_t* this, steque_item item){
steque_node_t* node;
node = (steque_node_t*) malloc(sizeof(steque_node_t));
node->item = item;
node->next = this->front;
if(this->back == NULL)
this->back = node;
this->front = node;
this->N++;
}
int steque_size(steque_t* this){
return this->N;
}
int steque_isempty(steque_t *this){
return this->N == 0;
}
steque_item steque_pop(steque_t* this){
steque_item ans;
steque_node_t* node;
if(this->front == NULL){
fprintf(stderr, "Error: underflow in steque_pop.\n");
fflush(stderr);
exit(EXIT_FAILURE);
}
node = this->front;
ans = node->item;
this->front = this->front->next;
if (this->front == NULL) this->back = NULL;
free(node);
this->N--;
return ans;
}
void steque_cycle(steque_t* this){
if(this->back == NULL)
return;
this->back->next = this->front;
this->back = this->front;
this->front = this->front->next;
this->back->next = NULL;
}
steque_item steque_front(steque_t* this){
if(this->front == NULL){
// printf("Error\n");
fprintf(stderr, "Error: underflow in steque_front.\n");
fflush(stderr);
exit(EXIT_FAILURE);
}
return this->front->item;
}
void steque_destroy(steque_t* this){
while(!steque_isempty(this))
steque_pop(this);
}