-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlists.c
More file actions
65 lines (54 loc) · 1.99 KB
/
linkedlists.c
File metadata and controls
65 lines (54 loc) · 1.99 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
#include <stdio.h>
#include <stdlib.h>
// arrays store values in continuos form so any modification is very hard
// linked lists are not continuous and each node only knows where the next node is (different memory addresses)
// NULL is the address for the last one (tail)
// no shifting is required, we insert or delete(make the node point to node after the one being deleted)
struct node {
int value;
struct node *next; // struct pointer pointing to next element
};
void printall(struct node* list){
struct node* p = list;
while(p!=NULL){
printf("%d\n", p->value);
p = p->next;
}
}
void clear(struct node* list) // since we are allocating memory we need to free it
{
struct node* p = list;
while(p != NULL){
struct node* temp = p; // temporary node holding list
p = p->next; // traversing through list
free(temp); // freeing the list
}
}
struct node* add_to_end(struct node* list, int value)
// we need to find node before tail (pointing to null), make that node point to new node (being added) and new node to tail (NULL)
{
struct node* new = malloc(sizeof(struct node));
new->next = NULL; // new node pointing to NULL (tail)
new->value = value;
if(list == NULL){
return new; // if list is empty new becomes our list
}
struct node* p = list;
// p->next is pointer p pointing to another struct pointer next
while(p->next != NULL){
p = p->next; // finding node before tail (second last)
}
// p->next now points to null here so we make it point to new node baing added at end
p->next = new; // p->next(last) points to new and new->next points to null (tail - becomes last)
// order becomes p new new->next (last- NULL)
return list;
}
int main(){
struct node* list = NULL;
list = add_to_end(list, 1);
list = add_to_end(list, 3);
list = add_to_end(list, 5);
printall(list);
clear(list);
return 0;
}