Skip to content

Commit 102cf2f

Browse files
committed
add single linked list
1 parent 5c8c9f5 commit 102cf2f

File tree

4 files changed

+154
-1
lines changed

4 files changed

+154
-1
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"files.associations": {
33
"ostream": "cpp",
44
"string": "cpp",
5-
"iostream": "cpp"
5+
"iostream": "cpp",
6+
"cstdlib": "cpp"
67
}
78
}

dataStructures/dataStructures.h

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
// #define NULL nullptr
5+
6+
typedef int item_type;
7+
8+
typedef struct LinkedList
9+
{
10+
item_type item;
11+
struct LinkedList *next;
12+
} list;
13+
14+
LinkedList *search_recursive(LinkedList *list, item_type quarry)
15+
{
16+
if (list == NULL)
17+
return NULL;
18+
19+
if (list->item == quarry)
20+
{
21+
return list;
22+
}
23+
else
24+
{
25+
return search_recursive(list->next, quarry);
26+
}
27+
}
28+
29+
LinkedList *search(LinkedList *list, item_type quarry)
30+
{
31+
LinkedList *current = list;
32+
33+
while ((current->item != quarry) && (current->next != NULL))
34+
{
35+
current = current->next;
36+
}
37+
if (current->item == quarry)
38+
{
39+
return current;
40+
}
41+
42+
return NULL;
43+
}
44+
45+
void insert(LinkedList **list, item_type new_element)
46+
{
47+
LinkedList *new_list_node = (LinkedList *)malloc(sizeof(LinkedList));
48+
49+
new_list_node->item = new_element;
50+
new_list_node->next = *list;
51+
52+
*list = new_list_node;
53+
}
54+
55+
LinkedList *find_predecessor(LinkedList *list, item_type quarry)
56+
{
57+
LinkedList *current = list;
58+
59+
while (current->next)
60+
{
61+
if ((current->next)->item == quarry)
62+
{
63+
return current;
64+
}
65+
else
66+
{
67+
current = current->next;
68+
}
69+
}
70+
}
71+
72+
int delete_element(LinkedList *list, item_type target)
73+
{
74+
75+
LinkedList *target_elem = search_recursive(list, target);
76+
77+
LinkedList *predecessor = find_predecessor(list, target);
78+
79+
if (target_elem != NULL)
80+
{
81+
if (predecessor == NULL)
82+
{
83+
list = list->next;
84+
}
85+
else
86+
{
87+
predecessor->next = predecessor->next->next;
88+
}
89+
free(target_elem);
90+
return 1;
91+
}
92+
93+
return 0;
94+
}
95+
96+
void display_list(LinkedList **list)
97+
{
98+
LinkedList *current = *list;
99+
100+
101+
printf("\n");
102+
printf("Printing linked list \n");
103+
printf("\n");
104+
while (current != NULL)
105+
{
106+
printf("%d ", current->item);
107+
current = current->next;
108+
}
109+
}

dataStructures/main

75 KB
Binary file not shown.

dataStructures/main.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "dataStructures.h"
2+
3+
int main(int argc, char const *argv[])
4+
{
5+
int input_array[] = {1, 4, 3, 2, 6, 7};
6+
7+
LinkedList *list = (LinkedList *)malloc(sizeof(LinkedList));
8+
// LinkedList *list;
9+
list->item = input_array[0];
10+
printf("%d ", input_array[0]);
11+
12+
for (int i = 1; i < 6; i++)
13+
{
14+
insert(&list, input_array[i]);
15+
}
16+
17+
display_list(&list);
18+
printf("\n");
19+
20+
printf("20 exists ?");
21+
22+
printf("\n");
23+
if (search(list, 20)) {
24+
printf("YES");
25+
} else {
26+
printf("NO");
27+
}
28+
printf("\n");
29+
30+
LinkedList *pred = find_predecessor(list, 2);
31+
if (pred != NULL) {
32+
printf("FOUND %d", pred->item);
33+
} else {
34+
printf("NOT FOUND");
35+
}
36+
printf("\n");
37+
38+
delete_element(list, 2);
39+
display_list(&list);
40+
printf("\n");
41+
42+
return 0;
43+
}

0 commit comments

Comments
 (0)