forked from garyexplains/examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
doublelinkedlist.c
105 lines (92 loc) · 2.07 KB
/
doublelinkedlist.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
// Copyright 2019-2020 Gary Sims
//
// To get a copy use git or:
// curl https://raw.githubusercontent.com/garyexplains/examples/master/doublelinkedlist.c --output doublelinkedlist.c
//
// To compile use:
// gcc -O3 -o doublelinkedlist doublelinkedlist.c
//
#include <stdio.h>
#include <stdlib.h>
typedef struct element {
int value;
struct element *next;
struct element *prev;
} element;
element *insert_ordered(element **head, element **tail, int v) {
element *h = *head;
if((h == NULL) || (h->value > v)){
// Empty list or need to insert at start
element *ne = malloc(sizeof(element));
ne->value = v;
ne->next = h;
ne->prev = NULL;
if(ne->next!=NULL)
ne->next->prev = ne;
if(h == NULL)
*tail=ne;
*head=ne;
return NULL;
}
element *p = *head;
while( (p->next != NULL) && (p->next->value < v) ) {
p = p->next;
}
// Now p point to place to insert new element
element *ne = malloc(sizeof(element));
ne->value = v;
ne->next = p->next;
p->next = ne;
ne->prev = p;
if(ne->next==NULL)
*tail = ne;
else
ne->next->prev = ne;
return NULL;
}
void print_list(element *head) {
printf("List: ");
element *p = head;
while(p != NULL) {
printf("%d ", p->value);
p = p->next;
}
printf("\n");
}
void print_rev_list(element *tail) {
printf("Reversed list: ");
element *p = tail;
while(p != NULL) {
printf("%d ", p->value);
p = p->prev;
}
printf("\n");
}
int main() {
element *head = NULL;
element *tail = NULL;
insert_ordered(&head, &tail, 7);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 10);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 20);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 15);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 4);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 30);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 29);
print_list(head);
print_rev_list(tail);
insert_ordered(&head, &tail, 5);
print_list(head);
print_rev_list(tail);
}