-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdequeue_reverse.c
More file actions
172 lines (146 loc) · 2.94 KB
/
Copy pathdequeue_reverse.c
File metadata and controls
172 lines (146 loc) · 2.94 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
171
172
// за основу взят queue из https://github.com/lemito/MagicOfTheC/blob/main/src/queue/queue_dyn_lib.c
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct Item
{
char data[25];
struct Item *next;
};
typedef struct dequeue
{
struct Item *first;
struct Item *last;
int size;
} dequeue;
void Create(dequeue *q)
{
q->first = q->last = malloc(sizeof(struct Item));
q->size = 0;
}
bool Empty(dequeue *q)
{
return q->first == q->last;
}
int Size(dequeue *q)
{
return q->size;
}
bool PushFront(dequeue *q, const char* t)
{
struct Item *newItem = malloc(sizeof(struct Item));
if (!newItem)
return false;
strcpy(newItem->data, t);
newItem->next = q->first;
q->first = newItem;
q->size++;
return true;
}
bool PushBack(dequeue *q, const char* t) {
struct Item *newItem = malloc(sizeof(struct Item));
if (!newItem)
return false;
strcpy(newItem->data, t);
newItem->next = NULL;
if (q->last!= NULL) {
q->last->next = newItem;
} else {
q->first = newItem;
}
q->last = newItem;
q->size++;
return true;
}
bool PopFront(dequeue *q)
{
if (q->first == q->last)
return false;
struct Item *pi = q->first;
q->first = q->first->next;
q->size--;
free(pi);
return true;
}
bool PopBack(dequeue *q)
{
if (q->first == q->last)
return false;
struct Item *current = q->first;
while (current->next!= q->last)
{
current = current->next;
}
free(q->last);
q->last = current;
q->last->next = NULL;
q->size--;
return true;
}
char* Front(const dequeue *q)
{
if (q->first!= q->last)
return q->first->data;
return NULL;
}
char* Back(const dequeue *q)
{
if (q->first!= q->last)
return q->last->data;
return NULL;
}
void Destroy(dequeue *q)
{
while (!Empty(q))
{
struct Item *pi = q->first;
q->first = q->first->next;
if (pi->data!= NULL) {
free(pi->data);
}
free(pi);
}
q->size = 0;
q->first = NULL;
q->last = NULL;
}
// Время: O(n)
// память: O(1)
void Reverse(dequeue *q) {
if (!Empty(q)) {
char *t = strdup(Front(q));
PopFront(q);
Reverse(q);
PushBack(q, t);
free(t);
}
}
void PrintQueue(const dequeue *q) {
if (Empty(q)) {
return;
}
struct Item *current = q->first;
printf("[ ");
while (current!= NULL) {
printf("%s ", current->data);
current = current->next;
}
printf("]\n");
}
int main(void) {
dequeue q;
Create(&q);
PushFront(&q, "First");
PushBack(&q, "Second");
PushFront(&q, "Third");
PushBack(&q, "Fourth");
printf("%d", q.size);
printf("До реверса:\n");
PrintQueue(&q);
Reverse(&q);
printf("После реверса:\n");
PrintQueue(&q);
printf("\n");
Destroy(&q);
return 0;
}