-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinkedList.c
79 lines (70 loc) · 1.78 KB
/
linkedList.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
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
NODE * reverse(NODE * node) {
NODE * temp;
NODE * previous = NULL;
while (node != NULL) {
temp = node->next;
node->next = previous;
previous = node;
node = temp;
}
return previous;
}
void init(NODE** head) {
*head = NULL;
}
NODE* add(NODE* node, DATA data) {
NODE* temp = (NODE*) malloc(sizeof (NODE));
if (temp == NULL) {
exit(0); // no memory available
}
temp->data = data;
temp->next = node;
node = temp;
return node;
}
void print_list(NODE* head) {
head = reverse(head);
NODE * temp;
int indent = 0;
printf("Print chain\n");
printf("=========== \n");
for (temp = head; temp; temp = temp->next, indent = indent+2)
{
printf("%*sPrevious hash\t%d\n", indent,"", temp->data.info.previous_block_hash);
printf("%*sBlock hash\t%d\n", indent,"", temp->data.info.block_hash);
printf("%*sTransaction\t%s\n", indent,"", temp->data.info.transactions);
printf("%*s\n", indent, "");
}
printf("\r\n");
}
void add_at(NODE* node, DATA data) {
NODE* temp = (NODE*) malloc(sizeof (NODE));
if (temp == NULL) {
exit(EXIT_FAILURE); // no memory available
}
temp->data = data;
temp->next = node->next;
node->next = temp;
}
void remove_node(NODE* head) {
NODE* temp = (NODE*) malloc(sizeof (NODE));
if (temp == NULL) {
exit(EXIT_FAILURE); // no memory available
}
temp = head->next;
head->next = head->next->next;
free(temp);
}
NODE *free_list(NODE *head) {
NODE *tmpPtr = head;
NODE *followPtr;
while (tmpPtr != NULL) {
followPtr = tmpPtr;
tmpPtr = tmpPtr->next;
free(followPtr);
}
return NULL;
}