-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollections.c
More file actions
112 lines (88 loc) · 1.82 KB
/
Collections.c
File metadata and controls
112 lines (88 loc) · 1.82 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
#include <stdio.h>
typedef struct Node {
void* data;
struct Node* next;
} Node;
typedef struct {
Node* head;
} List;
typedef struct {
Node* Top;
} Stack;
typedef struct {
Node* header;
Node* tail;
} Queue;
Node* NodeIndex(int Index, Node* start) {
if (!start)
return NULL;
if (Index == 0)
return start;
return NodeIndex(Index - 1, start->next);
}
Node* ListIndex(int Index, List* list) {
return NodeIndex(Index, list->head);
}
int SizeOfNode(Node* start) {
if (!start)
return 0;
return 1 + SizeOfNode(start->next);
}
int SizeOfList(List* list) {
return SizeOfNode(list->head);
}
Node* Last(Node* start) {
if (!start || !start->next)
return start;
return Last(start->next);
}
void AddAt(int Index, List* list, Node* newChain) {
Node* newChainLast = Last(newChain);
if (Index == 0) {
newChainLast->next = list->head;
list->head = newChain;
return;
}
Node* before = NodeIndex(Index - 1, list->head);
if (!before) return;
newChainLast->next = before->next;
before->next = newChain;
}
void StackPush(Stack* s, Node* n) {
n->next = s->Top;
s->Top = n;
}
void StackPop(Stack* s) {
if (!s->Top) return;
s->Top = s->Top->next;
}
Node* StackTop(Stack* s) {
return s->Top;
}
int StackEmpty(Stack* s) {
return s->Top == NULL;
}
void QueueInit(Queue* q) {
q->header = NULL;
q->tail = NULL;
}
void Enqueue(Queue* q, Node* n) {
n->next = NULL;
if (q->tail)
q->tail->next = n;
else
q->header = n;
q->tail = n;
}
void Dequeue(Queue* q) {
if (!q->header) return;
q->header = q->header->next;
if (!q->header)
q->tail = NULL;
}
Node* Front(Queue* q) {
return q->header;
}
int QueueEmpty(Queue* q) {
return q->header == NULL;
}