forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dedup_linked_list.c
188 lines (147 loc) · 4.1 KB
/
dedup_linked_list.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <stdio.h>
#include <stdlib.h>
// define struct recursively
typedef struct node {
int val;
struct node* next;
} node_t;
// iterating over a list
void print_list(node_t*head) {
// current pointer keeps track of the node currently being printed
node_t* current = head;
// print until the next node is NULL
while(current != NULL) {
printf("%d\n", current->val);
current = current->next; // set current pointer to the next node
}
}
// adding an item to the end of the list
void add(node_t* head, int val) {
// set current to the start
node_t* current = head;
// continuously advance pointer to the next item
while(current->next != NULL) {
current = current->next;
}
// add new variable to list
current->next = malloc(sizeof(node_t));
current->next->val = val;
current->next->next = NULL;
}
// adding an item to the beginning of the list (pushing to the list)
// Pass a pointer to the pointer variable (a double pointer) to modify the
// pointer itself
void push(node_t** head, int val) {
// create variable for new head
node_t* new_node;
new_node = malloc(sizeof(node_t));
// set value for new item
new_node->val = val;
// link the new item to point to the head of the list
new_node->next = *head;
// set the head of the list to be the new item
*head = new_node;
}
// removing the first item from the list(popping from the list)
int pop(node_t** head) {
int retval = -1;
node_t* next_node = NULL;
if(*head == NULL)
return -1;
// save item that head currently points to
next_node = (*head)->next;
retval = (*head)->val;
// free the head item
free(*head);
// set the head item to be previously saved item
*head = next_node;
return retval;
}
// removing the last item from the list
int remove_last(node_t* head) {
int retval = 0;
// remove item for single item list
if(head->next == NULL) {
retval = head->val;
free(head);
return retval;
}
// get penultimate (next to the last) node in list
// look two items ahead to confirm if it is the last
node_t* current = head;
while(current->next->next != NULL) {
current = current->next;
}
// now current points to the penultimate item & current->next can be removed
retval = current->next->val;
free(current->next);
current->next = NULL;
return retval;
}
// removing a specific item
int remove_by_index(node_t** head, int n) {
int retval = -1;
node_t* current = *head;
node_t* temp_node = NULL;
if(n == 0)
return pop(head);
// iterate to the node before the specified item
for(int i = 0; i < n - 1; ++i) {
if(current->next == NULL) {
return -1;
}
current = current->next;
}
// save the node to be deleted in temp pointer
temp_node = current->next;
retval = temp_node->val;
// set previous node's next pointer to point to the node after the node to be
// deleted
current->next = temp_node->next;
// delete the node using the temporary pointer
free(temp_node);
return retval;
}
void remove_duplicate(node_t* head) {
node_t* node;
node_t* other;
node_t* dup;
while(node!= NULL && node->next != NULL) {
other = node;
// compare item with all values
while(other->next != NULL) {
// free node if duplicate
if(node->val == other->next->val) {
dup = other->next;
other->next = other->next->next;
free(dup);
}
else
other = other->next;
}
node = node->next;
}
}
int main(void) {
// head points to the first item of the list
node_t* head = NULL;
head = malloc(sizeof(node_t));
// check weather malloc returned a NULL value or not
if(head == NULL)
return 1;
head->val = 1; // set the value of the first item
head->next = NULL; // set the next item to be empty
// test add function: which adds to the end of the list
add(head, 2);
// test push function
push(&head, 3);
// add duplicate value deliberately
add(head, 3);
// test print function
print_list(head);
// test remove_duplicate
remove_duplicate(head);
printf("\nLinked list after removing duplicate values\n");
print_list(head);
return 0;
}