This repository has been archived by the owner on Jan 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
157 lines (121 loc) · 2.45 KB
/
list.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
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct node_t node;
struct node_t {
unsigned int somedata;
struct {
char *fio;
unsigned long long age;
char sex;
unsigned long height;
char *address;
} user;
node* prev;
node* next;
};
typedef unsigned char bool;
#define true (1)
#define false (0)
bool create_list(node** list);
bool add_node(node** list, unsigned int data);
int get_data(node* list, size_t index);
bool set_data(node* list, size_t index, int data);
void print_list(node *list);
bool del_node(node** list, size_t index);
int main() {
srand(time(NULL));
node* list = NULL;
for (int i = 0; i < 10; i++) {
add_node(&list, rand() % 100);
}
print_list(list);
set_data(list, 1, 50);
del_node(&list, 0);
print_list(list);
// printf("%lu, %lu, %lu\n", sizeof(list->user), sizeof(list->next), sizeof(*list));
// printf("%d\n", list->somedata);
// printf("%d\n", list->next->somedata);
// printf("%d\n", list->next->next->somedata);
// printf("%d\n", get_data(list, 4));
//printf("%d\n", list->next->next->next->somedata);
return 0;
}
bool create_list(node** list) {
*list = (node *)malloc(sizeof(node));
return true;
}
bool add_node(node** list, unsigned int data) {
if (*list == NULL) {
create_list(list);
(*list)->somedata = data;
(*list)->next = NULL;
}
else {
node* n = *list;
for (; n->next != NULL ;)
n = n->next;
n->next = (node*) malloc(sizeof(node));
n->next->somedata = data;
n->next->next = NULL;
}
return true;
}
int get_data(node *list, size_t index) {
node *n = list;
int i = 0;
for (; n->next != NULL && i < index; i++) {
// printf("%d %d 0x%X\n", i, n->somedata, n->next);
n = n->next;
}
if (i == index) {
return n->somedata;
}
else {
return 0;
}
}
bool set_data(node *list, size_t index, int data) {
node *n = list;
int i = 0;
for (; n->next != NULL && i < index; i++) {
n = n->next;
}
if (i == index) {
n->somedata = data;
return true;
}
else {
return false;
}
}
void print_list (node* list) {
node *n = list;
while (n != NULL) {
printf("%d\t", n->somedata);
n = n->next;
};
printf("\n");
}
bool del_node(node** list, size_t index) {
node *n = *list;
node *prev = *list;
if (index == 0) {
*list = (*list)->next;
free(n);
return true;
}
int i = 0;
for (; n->next != NULL && i < index; i++) {
prev = n;
n = n->next;
}
if (i == index) {
prev->next = n->next;
free(n);
return true;
}
else {
return false;
}
}