-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedListTestDrive.c
120 lines (105 loc) · 3.66 KB
/
linkedListTestDrive.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
#include <stdio.h>
#include <stdlib.h>
typedef struct n{
int x;
struct n* next;
}node;
void addElement(node* r,int x){
while(r->next != NULL){
r = r->next;
}
r->next = (node*)malloc(sizeof(node));
r->next->x=x;
r->next->next=NULL;
}
void printList(node* r){
while( r != NULL){
printf("%d\n",r->x);
r = r->next;
}
}
void delete(node** r,int x){
node* toBeDeleted;
node* newRoot = *r;
int endOfList = 0;
while(endOfList !=1){
if( (*r)->x == x){ //If the element you want to delete is the root of the list...
toBeDeleted = (*r); //...we are going to delete it and change the pointer to root with the new one.
*r = (*r)->next; //Then you have to check the list again with "continue" to see
newRoot = *(r); //...whether the data you want to delete is again in the root of your list or not.
free(toBeDeleted); //We also have to update the newRoot to be able to delete other elements if asked.
continue;
}
if(newRoot->next->x == x){ //If the element you want to delete is amongst the other elements...
toBeDeleted = newRoot->next; //...this will run to check whether your next element is the one...
newRoot->next = toBeDeleted->next; //...you want to delete. If so, the next element of the element...
free(toBeDeleted); //...you want to delete will be assigned to the current element's next not to...
if(newRoot->next == NULL){ //...lose the rest of the list. If the newRoot's next element is NULL
endOfList = 1; //...that means we are at the end of the list so atTheEnd must be assigned 1.
continue;
}
else
continue;
}
newRoot = newRoot->next;
if(newRoot->next == NULL){
endOfList =1;
}
}
}
void addInOrder(node** r,int x){
node* root = *r;
if(root == NULL){ //If our root is NULL then we will allocate memory for it...
root = (node*) malloc(sizeof(node)); //...and then assign x to root->x and Null to root->next.
root ->next = NULL; //Then we will have to change the pointer of the root...
root ->x = x;
*r = root; //...like so.
//return;
}
if( root->x > x){ //If the root's x is greater than the x we want to add...
node* currentRoot = root; //... then we will have to prepend it to the root.
node* toBePrepended =(node*) malloc(sizeof(node)); //Allocating memory for our element to be prepended.
toBePrepended-> x = x;
toBePrepended->next = root;
(*r) = toBePrepended; //Changing our pointer's first element to be "toBePrepended"
return;
}
node* iter = *r;
while( iter->next != NULL && iter->next->x < x){ //while iter->next is not null and iter->next's x is less than our X...
iter = iter-> next;
}
node* temp = (node*) malloc(sizeof(node)); //Allocate memory for our new element.
temp->next = iter->next; //Then assign the iter's null to our new element's next item.
temp->x = x; //Assign the x value.
iter->next = temp; //Now our current element's next should point to our new element...
} //...which is greater than the current element.
int main(){
node* root;
root = (node*)malloc(sizeof(node));
root->next = NULL;
root->x = 13;
addInOrder(&root,5);
addInOrder(&root,7);
addInOrder(&root,6);
addInOrder(&root,1);
addInOrder(&root,3);
addInOrder(&root,21);
addInOrder(&root,19);
//addElement(root,13);
//addElement(root,500);
//addElement(root,13);
//addElement(root,500);
//addElement(root,82);
//addElement(root,18);
//addElement(root,13);
//addElement(root,500);
for(int i = 0; i < 5;i++){
addInOrder(&root,i*5);
}
//addElement(root,13);
printList(root);
delete(&root,3);
printf("\n");
printList(root);
return 0;
}