C
x
48
1
/*this is a simple LL implementation*/
2
3
4
5
struct List
6
{
7
int data;
8
struct List *next;
9
};
10
11
struct List sentinel_node_instance;
12
/* a pointer to permanently point to the Sentinel Node */
13
struct List* const SENTINEL_NODE = &sentinel_node_instance;
14
15
/* the sentinel node should be the first thing on the list when it's created */
16
struct List* head = SENTINEL_NODE;
17
18
void ListInsert(int new_data)
19
{
20
struct List *p;
21
p = (struct List *)malloc(sizeof(struct List));
22
p->data = new_data;
23
p->next = (head);
24
head = p;
25
}
26
27
void printList(void)
28
{
29
struct List* q = head;
30
while (q != SENTINEL_NODE)
31
{
32
printf("%d ", q->data);
33
q = q->next;
34
}
35
printf("\n");
36
}
37
38
int main()
39
{
40
ListInsert(5);
41
ListInsert(7);
42
ListInsert(6);
43
ListInsert(4);
44
ListInsert(2);
45
printList();
46
return 0;
47
}
48
$ gcc prog.c -Wall -Wextra -std=c89 -pedantic-errors
Start
2 4 6 7 5
0
Finish