-
Notifications
You must be signed in to change notification settings - Fork 17
/
linked.c
95 lines (85 loc) · 2.38 KB
/
linked.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
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
// just some form of data; could be a char* or whatever
int num;
// pointer to next node; have to include `struct` since this is a recursive definition
struct node *next;
}
node;
// protoypes
node* insert_to_list(node *head, int value);
void print_list(node *head);
void free_list(node *head);
int main(void) {
// initialize a new linked list
node *head = malloc(sizeof(node));
if (head == NULL) {
return 1;
}
head->num = 1;
// add values to the list
head = insert_to_list(head, 2);
head = insert_to_list(head, 4);
head = insert_to_list(head, 5);
head = insert_to_list(head, 2);
head = insert_to_list(head, 3);
// print the list
print_list(head);
// free the list
free_list(head);
// that's all folks!
return 0;
}
node* insert_to_list(node* head, int value) {
node *original_head = head;
// iterate through the linked list
while (head) {
// check if the current place is the right place to insert the node
if (head->num <= value && (head->next == NULL || head->next->num > value)) {
// make a new node with given value
node *new_node = malloc(sizeof(node));
if (new_node == NULL) {
return original_head;
}
new_node->num = value;
// insert the new node in to the list
new_node->next = head->next;
head->next = new_node;
return original_head;
}
if (head->num > value) {
// make a new node with given value
node *new_node = malloc(sizeof(node));
if (new_node == NULL) {
return original_head;
}
new_node->num = value;
// insert the new node to the head of the list.
new_node->next = head;
return new_node;
}
head = head->next;
}
// code should never reach here
return original_head;
}
void print_list(node *head) {
int counter = 0;
printf("Your list:\n");
while(head) {
printf("The value at position %d is %d\n", counter, head->num);
counter++;
head = head->next;
}
}
void free_list(node *head) {
node* current;
while(head) {
current = head;
head = head->next;
free(current);
}
return;
}