-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinked_list.cpp
More file actions
executable file
·155 lines (124 loc) · 3.28 KB
/
Copy pathlinked_list.cpp
File metadata and controls
executable file
·155 lines (124 loc) · 3.28 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
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
#include <cstdio>
#include <cstdlib>
struct test_struct {
int val;
struct test_struct *next;
};
struct test_struct *head = nullptr;
struct test_struct *curr = nullptr;
struct test_struct *create_list(int val) {
printf("\n Creating list with head node as [%d]\n", val);
auto *ptr =
(struct test_struct *) malloc(sizeof(struct test_struct));
if (nullptr == ptr) {
printf("\n Node creation failed \n");
return nullptr;
}
ptr->val = val;
ptr->next = nullptr;
head = curr = ptr;
return ptr;
}
struct test_struct *add_to_list(int val, bool add_to_end) {
if (nullptr == head) {
return (create_list(val));
}
if (add_to_end)
printf("\n Adding node to end of list with value [%d]\n", val);
else
printf("\n Adding node to beginning of list with value [%d]\n", val);
auto *ptr =
(struct test_struct *) malloc(sizeof(struct test_struct));
if (nullptr == ptr) {
printf("\n Node creation failed \n");
return nullptr;
}
ptr->val = val;
ptr->next = nullptr;
if (add_to_end) {
curr->next = ptr;
curr = ptr;
} else {
ptr->next = head;
head = ptr;
}
return ptr;
}
struct test_struct *search_in_list(int val, struct test_struct **prev) {
struct test_struct *ptr = head;
struct test_struct *tmp = nullptr;
bool found = false;
printf("\n Searching the list for value [%d] \n", val);
while (nullptr != ptr) {
if (ptr->val == val) {
found = true;
break;
} else {
tmp = ptr;
ptr = ptr->next;
}
}
if (found) {
if (prev)
*prev = tmp;
return ptr;
} else {
return nullptr;
}
}
int delete_from_list(int val) {
struct test_struct *prev = nullptr;
struct test_struct *del;
printf("\n Deleting value [%d] from list \n", val);
del = search_in_list(val, &prev);
if (del == nullptr) {
return -1;
} else {
if (nullptr != prev)
prev->next = del->next;
if (del == curr) {
curr = prev;
} else if (del == head) {
head = del->next;
}
}
free(del);
return 0;
}
void print_list() {
struct test_struct *ptr = head;
printf("\n -----Printing List Start-----\n");
while (nullptr != ptr) {
printf("\n [%d] \n", ptr->val);
ptr = ptr->next;
}
printf("\n -----Printing List End-----\n");
}
int main() {
int i, ret;
struct test_struct *ptr;
print_list();
for (i = 5; i < 10; i++)
add_to_list(i, true);
print_list();
for (i = 4; i > 0; i--)
add_to_list(i, false);
print_list();
for (i = 1; i < 10; i += 4) {
ptr = search_in_list(i, nullptr);
if (nullptr == ptr) {
printf("\n Search [val = %d] failed, no such element found\n", i);
} else {
printf("\n Search passed [val = %d]\n", ptr->val);
}
print_list();
ret = delete_from_list(i);
if (ret != 0) {
printf("\n Delete [val = %d] failed, no such element found\n", i);
} else {
printf("\n Delete [val = %d] passed \n", i);
}
print_list();
}
return 0;
}