Skip to content

Commit 099ac55

Browse files
Merge pull request wangzheng0822#6 from RichardWeiYang/master
Implement array in c
2 parents ae118a9 + 2c37d35 commit 099ac55

File tree

2 files changed

+271
-0
lines changed

2 files changed

+271
-0
lines changed

c-cpp/05_array/array.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
struct array {
6+
int size;
7+
int used;
8+
int *arr;
9+
};
10+
11+
void dump(struct array *array)
12+
{
13+
int idx;
14+
15+
for (idx = 0; idx < array->used; idx++)
16+
printf("[%02d]: %08d\n", idx, array->arr[idx]);
17+
}
18+
19+
void alloc(struct array *array)
20+
{
21+
array->arr = (int *)malloc(array->size * sizeof(int));
22+
}
23+
24+
int insert(struct array *array, int elem)
25+
{
26+
int idx;
27+
if (array->used >= array->size)
28+
return -1;
29+
30+
for (idx = 0; idx < array->used; idx++) {
31+
if (array->arr[idx] > elem)
32+
break;
33+
}
34+
35+
if (idx < array->used)
36+
memmove(&array->arr[array->used], &array->arr[idx],
37+
(array->used - idx) * sizeof(int));
38+
39+
array->arr[idx] = elem;
40+
array->used++;
41+
return idx;
42+
}
43+
44+
int delete(struct array *array, int idx)
45+
{
46+
if (idx < 0 || idx >= array->used)
47+
return -1;
48+
49+
memmove(&array->arr[idx], &array->arr[idx+1],
50+
(array->used - idx) * sizeof(int));
51+
array->used--;
52+
return 0;
53+
}
54+
55+
int search(struct array *array, int elem)
56+
{
57+
int idx;
58+
59+
for (idx = 0; idx < array->used; idx++) {
60+
if (array->arr[idx] == elem)
61+
return idx;
62+
if (array->arr[idx] > elem)
63+
return -1;
64+
}
65+
66+
return -1;
67+
}
68+
69+
int main()
70+
{
71+
int idx;
72+
struct array ten_int = {10, 0, NULL};
73+
74+
alloc(&ten_int);
75+
if (!ten_int.arr)
76+
return -1;
77+
insert(&ten_int, 1);
78+
insert(&ten_int, 3);
79+
insert(&ten_int, 2);
80+
printf("=== insert 1, 3, 2\n");
81+
dump(&ten_int);
82+
83+
idx = search(&ten_int, 2);
84+
printf("2 is at position %d\n", idx);
85+
idx = search(&ten_int, 9);
86+
printf("9 is at position %d\n", idx);
87+
88+
printf("=== delete [6] element \n");
89+
delete(&ten_int, 6);
90+
dump(&ten_int);
91+
printf("=== delete [0] element \n");
92+
delete(&ten_int, 0);
93+
dump(&ten_int);
94+
return 0;
95+
}

c-cpp/06_linkedlist/single_list.c

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#include <stdio.h>
2+
#include <stdbool.h>
3+
4+
struct single_list {
5+
struct single_list *next;
6+
int val;
7+
};
8+
9+
struct single_list_head {
10+
struct single_list *head;
11+
};
12+
13+
bool is_empty(struct single_list_head *head)
14+
{
15+
return head->head == NULL;
16+
}
17+
18+
void dump(struct single_list_head *head)
19+
{
20+
struct single_list *tmp = head->head;
21+
int idx = 0;
22+
23+
while (tmp) {
24+
printf("[%02d]: %08d\n", idx++, tmp->val);
25+
tmp = tmp->next;
26+
}
27+
}
28+
29+
void insert(struct single_list **prev, struct single_list *elem)
30+
{
31+
if (!prev)
32+
return;
33+
34+
if (*prev)
35+
elem->next = *prev;
36+
*prev = elem;
37+
}
38+
39+
void insert_head(struct single_list_head *head, struct single_list *elem)
40+
{
41+
insert(&head->head, elem);
42+
}
43+
44+
struct single_list* delete(struct single_list **prev)
45+
{
46+
struct single_list *tmp;
47+
48+
if (!prev)
49+
return NULL;
50+
51+
tmp = *prev;
52+
*prev = (*prev)->next;
53+
tmp->next = NULL;
54+
55+
return tmp;
56+
}
57+
58+
struct single_list* delete_head(struct single_list_head* head)
59+
{
60+
return delete(&head->head);
61+
}
62+
63+
struct single_list** search(struct single_list_head* head, int target)
64+
{
65+
struct single_list **prev, *tmp;
66+
67+
for (prev = &head->head, tmp = *prev;
68+
tmp && (tmp->val < target);
69+
prev = &tmp->next, tmp = *prev)
70+
;
71+
72+
return prev;
73+
}
74+
75+
void reverse(struct single_list_head* head)
76+
{
77+
struct single_list_head tmp;
78+
struct single_list *elem;
79+
80+
while (!is_empty(head)) {
81+
elem = delete_head(head);
82+
insert_head(&tmp, elem);
83+
}
84+
85+
head->head = tmp.head;
86+
}
87+
88+
bool is_cyclic(struct single_list_head* head)
89+
{
90+
struct single_list *s1, *s2;
91+
92+
s1 = s2 = head->head;
93+
94+
while(s1 && s2) {
95+
s1 = s1->next;
96+
s2 = s2->next ? s2->next->next:s2->next;
97+
98+
if (s1 == s2)
99+
return true;
100+
}
101+
return false;
102+
}
103+
104+
struct single_list* middle(struct single_list_head* head)
105+
{
106+
struct single_list *s1, *s2;
107+
struct single_list pseudo_head;
108+
109+
pseudo_head.next = head->head;
110+
s1 = s2 = &pseudo_head;
111+
112+
while (true) {
113+
if (!s2 || !s2->next)
114+
return s1;
115+
s1 = s1->next;
116+
s2 = s2->next->next;
117+
}
118+
119+
return NULL;
120+
}
121+
122+
int main()
123+
{
124+
struct single_list_head head = {NULL};
125+
struct single_list lists[10];
126+
struct single_list **prev;
127+
int idx;
128+
129+
for (idx = 0; idx < 10; idx++) {
130+
lists[idx].val = idx;
131+
lists[idx].next = NULL;
132+
}
133+
134+
insert_head(&head, &lists[6]);
135+
insert_head(&head, &lists[5]);
136+
insert_head(&head, &lists[4]);
137+
insert_head(&head, &lists[1]);
138+
insert_head(&head, &lists[0]);
139+
140+
printf("=== insert 0, 1, 4, 5, 6\n");
141+
dump(&head);
142+
143+
prev = search(&head, 2);
144+
insert(prev, &lists[2]);
145+
printf("=== insert 2\n");
146+
dump(&head);
147+
148+
printf("middle elem is %d\n", middle(&head)->val);
149+
150+
prev = search(&head, 2);
151+
if ((*prev) && ((*prev)->val == 2))
152+
printf("The list contains 2\n");
153+
else
154+
printf("The list not contains 2\n");
155+
156+
delete(prev);
157+
prev = search(&head, 2);
158+
printf("After remove 2\n");
159+
if ((*prev) && ((*prev)->val == 2))
160+
printf("The list contains 2\n");
161+
else
162+
printf("The list not contains 2\n");
163+
dump(&head);
164+
165+
printf("After reverse \n");
166+
reverse(&head);
167+
dump(&head);
168+
169+
printf("middle elem is %d\n", middle(&head)->val);
170+
171+
lists[0].next = &lists[6];
172+
printf("list is%s cyclic\n", is_cyclic(&head)?"":" not");
173+
174+
return 0;
175+
}
176+

0 commit comments

Comments
 (0)