-
Notifications
You must be signed in to change notification settings - Fork 0
/
neoLinkedList.c
executable file
·110 lines (104 loc) · 2.5 KB
/
neoLinkedList.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
typedef struct tLinkedListNode{
void *memAddress;
vc_uint16 file;
vc_uint16 line;
vc_uint16 size;
}tLinkedListNode;
typedef struct tLinkedList {
tLinkedListNode data;
struct tLinkedList* next;
struct tLinkedList* prev;
}tLinkedList;
tLinkedList* vc_heap_head;
tLinkedList* GetNewNode(void *x,unsigned int size, unsigned int file, unsigned int line)
{
tLinkedList* newNode = (tLinkedList*)pvPortMalloc(sizeof(tLinkedList));
newNode->data.memAddress = x;
newNode->data.file = file;
newNode->data.line = line;
newNode->data.size = size;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
//Inserts a Node at head of doubly linked list
void LL_InsertAtHead(void *x,unsigned int size, unsigned int file, unsigned int line) {
tLinkedList* newNode = GetNewNode(x,size,file,line);
if(vc_heap_head == NULL) {
vc_heap_head = newNode;
return;
}
vc_heap_head->prev = newNode;
newNode->next = vc_heap_head;
vc_heap_head = newNode;
}
//Inserts a Node at tail of Doubly linked list
void LL_InsertAtTail(void *x, unsigned int size, unsigned int file, unsigned int line) {
tLinkedList* temp = vc_heap_head;
tLinkedList* newNode = GetNewNode(x,size,file,line);
if(vc_heap_head == NULL) {
vc_heap_head = newNode;
return;
}
while(temp->next != NULL) temp = temp->next;
temp->next = newNode;
newNode->prev = temp;
}
void LL_DeleteNode(void *x)
{
tLinkedList* temp = vc_heap_head, *prev;
if (temp != NULL && temp->data.memAddress == x){
vc_heap_head = temp->next;
vPortFree(temp);
return;
}
while (temp != NULL && temp->data.memAddress != x){
prev = temp;
temp = temp->next;
}
if (temp == NULL) return;
prev->next = temp->next;
vPortFree(temp);
}
int LL_get_len()
{
tLinkedList* temp = vc_heap_head;
vc_uint8 linkedlist_count= 0;
while(temp != NULL) {
temp = temp->next;
linkedlist_count++;
}
return linkedlist_count;
}
/* function to swap data of two nodes a and b*/
void swap(tLinkedList *a, tLinkedList *b)
{
tLinkedListNode t_node;
t_node= a->data;
a->data = b->data;
b->data = t_node;
}
void BubbledSort_linked_list()
{
int swapped, i;
tLinkedList *ptr1;
tLinkedList *lptr = NULL;
/* Checking for empty list */
if (vc_heap_head == NULL){
return;
}
do{
swapped = 0;
ptr1 = vc_heap_head;
while (ptr1->next != lptr)
{
if (ptr1->data.size > ptr1->next->data.size)
{
swap(ptr1, ptr1->next);
swapped = 1;
}
ptr1 = ptr1->next;
}
lptr = ptr1;
}while (swapped);
}