-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle-direction-list.c
More file actions
146 lines (111 loc) · 2.61 KB
/
single-direction-list.c
File metadata and controls
146 lines (111 loc) · 2.61 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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 50
#define container_of(ptr, type, member) \
(type *)((ptr) - &(((type *)0)->member))
/*
* To use a single direction list need two base modules
*
*/
/* 1.List node to add to a list head */
struct list_node {
int data;
struct list_node *next;
};
/* 2.List head struct having an list head */
struct list_head {
struct list_node node;
int count;
};
void list_init(struct list_head *head)
{
head->count = 0;
head->node.next = NULL;
}
int list_empty(struct list_head *head)
{
return (head->count == 0);
}
struct list_node *list_get_node(struct list_head *head, int index)
{
struct list_node *pos = &head->node;
int i = 0;
if (index >= head->count)
return NULL;
while(pos && i++ <= index)
pos = pos->next;
/*pos could be NULL*/
return pos;
}
void list_add(struct list_head *head, struct list_node *node)
{
struct list_node *pos = &head->node;
node->next = pos->next;
pos->next = node;
head->count++;
}
void list_add_tail(struct list_head *head, struct list_node *node)
{
struct list_node *pos = &head->node;
while(pos->next != NULL) {
pos = pos->next;
}
node->next = NULL;
pos->next = node;
head->count++;
}
int list_insert(struct list_head *head, struct list_node *node, int index)
{
struct list_node *pos;
pos = list_get_node(head, index - 1);
if (!pos)
return -1;
node->next = pos->next;
pos->next = node;
head->count++;
return 0;
}
void list_del(struct list_head *head, struct list_node *node)
{
struct list_node *pos;
for (pos = &head->node; pos->next != NULL; pos = pos->next) {
if (pos->next == node) {
pos->next = node->next;
node->next = NULL;
head->count--;
break;
}
}
}
/***************test sample*****************/
struct list_head head;
int main (int argc, char *argv[])
{
int i = 0;
struct list_node *node, *pos;
list_init(&head);
node = (struct list_node *)malloc(sizeof(struct list_node));
node->data = 1;
list_add(&head, node);
node = (struct list_node *)malloc(sizeof(struct list_node));
node->data = 2;
list_insert(&head, node, 2);
node = (struct list_node *)malloc(sizeof(struct list_node));
node->data = 3;
list_add_tail(&head, node);
pos = node;
node = (struct list_node *)malloc(sizeof(struct list_node));
node->data = 4;
list_add_tail(&head, node);
for(i = 0, node = head.node.next; node; node = node->next) {
printf("index[%d]=%d\n", i++, node->data);
}
list_del(&head, pos);
/* TODO: free list node*/
for(i = 0, node = head.node.next; node; node = node->next) {
printf("index[%d]=%d\n", i++, node->data);
}
/* TODO: free list node*/
return 0;
}