-
Notifications
You must be signed in to change notification settings - Fork 443
/
all-data-structures-in-c
473 lines (383 loc) · 9.72 KB
/
all-data-structures-in-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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
//ARRAYS
#include <stdio.h>
#define SIZE 5
int main() {
int arr[SIZE] = {2, 4, 6, 8, 10};
int i;
for (i = 0; i < SIZE; i++) {
printf("%d ", arr[i]);
}
return 0;
}
//LINKEDLIST
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
};
void printList(struct Node *node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
}
int main() {
struct Node* head = NULL;
struct Node* second = NULL;
struct Node* third = NULL;
head = (struct Node*)malloc(sizeof(struct Node));
second = (struct Node*)malloc(sizeof(struct Node));
third = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
head->next = second;
second->data = 2;
second->next = third;
third->data = 3;
third->next = NULL;
printList(head);
return 0;
}
//QUEUE
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
// define the structure for the queue
struct queue {
int items[MAX_SIZE];
int front;
int rear;
};
// function to create an empty queue
struct queue* createQueue() {
struct queue* q = malloc(sizeof(struct queue));
q->front = -1;
q->rear = -1;
return q;
}
// function to check if the queue is empty
int isEmpty(struct queue* q) {
if (q->rear == -1)
return 1;
else
return 0;
}
// function to add an element to the queue
void enqueue(struct queue* q, int value) {
if (q->rear == MAX_SIZE - 1)
printf("Queue is full!");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}
// function to remove an element from the queue
int dequeue(struct queue* q) {
int item;
if (isEmpty(q)) {
printf("Queue is empty");
item = -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear) {
q->front = q->rear = -1;
}
}
return item;
}
int main() {
struct queue* q = createQueue();
enqueue(q, 1);
enqueue(q, 2);
enqueue(q, 3);
printf("Dequeued item: %d\n", dequeue(q));
printf("Dequeued item: %d\n", dequeue(q));
printf("Dequeued item: %d\n", dequeue(q));
return 0;
}
//STACK
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
// define the structure for the stack
struct stack {
int items[MAX_SIZE];
int top;
};
// function to create an empty stack
struct stack* createStack() {
struct stack* s = malloc(sizeof(struct stack));
s->top = -1;
return s;
}
// function to check if the stack is empty
int isEmpty(struct stack* s) {
if (s->top == -1)
return 1;
else
return 0;
}
// function to check if the stack is full
int isFull(struct stack* s) {
if (s->top == MAX_SIZE - 1)
return 1;
else
return 0;
}
// function to add an element to the stack
void push(struct stack* s, int value) {
if (isFull(s))
printf("Stack is full!");
else {
s->top++;
s->items[s->top] = value;
}
}
// function to remove an element from the stack
int pop(struct stack* s) {
int item;
if (isEmpty(s)) {
printf("Stack is empty");
item = -1;
} else {
item = s->items[s->top];
s->top--;
}
return item;
}
int main() {
struct stack* s = createStack();
push(s, 1);
push(s, 2);
push(s, 3);
printf("Popped item: %d\n", pop(s));
printf("Popped item: %d\n", pop(s));
printf("Popped item: %d\n", pop(s));
return 0;
}
//TREES
// Tree traversal in C
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
// Preorder traversal
void preorderTraversal(struct node* root) {
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
// Postorder traversal
void postorderTraversal(struct node* root) {
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
// Create a new Node
struct node* createNode(value) {
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Insert on the left of the node
struct node* insertLeft(struct node* root, int value) {
root->left = createNode(value);
return root->left;
}
// Insert on the right of the node
struct node* insertRight(struct node* root, int value) {
root->right = createNode(value);
return root->right;
}
int main() {
struct node* root = createNode(1);
insertLeft(root, 2);
insertRight(root, 3);
insertLeft(root->left, 4);
printf("Inorder traversal \n");
inorderTraversal(root);
printf("\nPreorder traversal \n");
preorderTraversal(root);
printf("\nPostorder traversal \n");
postorderTraversal(root);
}
//GRAPHS
#include <stdio.h>
#include <stdlib.h>
// We are defining the maximum number of vertices in the graph
#define N 6
// It is a data structure to store a graph object
struct Graph
{
// An adjacency list can be represented by an array of pointers to Nodes
struct Node* head[N];
};
// An adjacency list for the graph's nodes is kept in a data structure.
struct Node
{
int dest;
struct Node* next;
};
// An edge-storing data structure for graphs
struct Edge {
int src, dest;
};
// Function to create an adjacency list from specified edges
struct Graph* createGraph(struct Edge edges[], int n)
{
// allocate storage for the graph data structure
struct Graph* graph = (struct Graph*)malloc(sizeof(struct Graph));
// initialize head pointer for all vertices
for (int i = 0; i < N; i++) {
graph->head[i] = NULL;
}
// add edges to the directed graph one by one
for (int i = 0; i < n; i++)
{
// get the source and destination vertex
int src = edges[i].src;
int dest = edges[i].dest;
// allocate a new node of adjacency list from src to dest
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->dest = dest;
// point new node to the current head
newNode->next = graph->head[src];
// point head pointer to the new node
graph->head[src] = newNode;
}
return graph;
}
// Function to print adjacency list representation of a graph
void printGraph(struct Graph* graph)
{
for (int i = 0; i < N; i++)
{
// print current vertex and all its neighbors
struct Node* ptr = graph->head[i];
while (ptr != NULL)
{
printf("(%d ?> %d)\t", i, ptr->dest);
ptr = ptr->next;
}
printf("\n");
}
}
// Directed graph implementation in C
int main(void)
{
// input array containing edges of the graph (as per the above diagram)
// (x, y) pair in the array represents an edge from x to y
struct Edge edges[] =
{
{0, 1}, {1, 2}, {2, 0}, {2, 1}, {3, 2}, {4, 5}, {5, 4}
};
// calculate the total number of edges
int n = sizeof(edges)/sizeof(edges[0]);
// construct a graph from the given edges
struct Graph *graph = createGraph(edges, n);
// Function to print adjacency list representation of a graph
printGraph(graph);
return 0;
}
//HEAPS
// Max-Heap data structure in C
#include <stdio.h>
int size = 0;
void swap(int *a, int *b)
{
int temp = *b;
*b = *a;
*a = temp;
}
void heapify(int array[], int size, int i)
{
if (size == 1)
{
printf("Single element in the heap");
}
else
{
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < size && array[l] > array[largest])
largest = l;
if (r < size && array[r] > array[largest])
largest = r;
if (largest != i)
{
swap(&array[i], &array[largest]);
heapify(array, size, largest);
}
}
}
void insert(int array[], int newNum)
{
if (size == 0)
{
array[0] = newNum;
size += 1;
}
else
{
array[size] = newNum;
size += 1;
for (int i = size / 2 - 1; i >= 0; i--)
{
heapify(array, size, i);
}
}
}
void deleteRoot(int array[], int num)
{
int i;
for (i = 0; i < size; i++)
{
if (num == array[i])
break;
}
swap(&array[i], &array[size - 1]);
size -= 1;
for (int i = size / 2 - 1; i >= 0; i--)
{
heapify(array, size, i);
}
}
void printArray(int array[], int size)
{
for (int i = 0; i < size; ++i)
printf("%d ", array[i]);
printf("\n");
}
int main()
{
int array[10];
insert(array, 3);
insert(array, 4);
insert(array, 9);
insert(array, 5);
insert(array, 2);
printf("Max-Heap array: ");
printArray(array, size);
deleteRoot(array, 4);
printf("After deleting an element: ");
printArray(array, size);
}
//