-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinked_list_incomplete.c
62 lines (53 loc) · 1.18 KB
/
linked_list_incomplete.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
#include <stdlib.h>
#include <stdio.h>
typedef struct item {
int value;
struct item* rest;
} Item;
Item* new_item(int value){
Item* newitem = (Item *) malloc(sizeof(Item));
newitem->value = value;
newitem->rest = NULL;
return newitem;
}
Item* insert_front(Item* listptr, int value){
Item* newitem = new_item(value);
newitem->rest = listptr;
return newitem;
}
int get(Item* listptr, int index){
int ctr = 0;
Item* p;
for(p = listptr; p!= NULL; p = p->rest){
if (ctr==index){
return p->value;
}
ctr++;
}
return -1;
}
void free_all(Item* listptr) {
Item *p;
Item *next;
for(p = listptr; p!= NULL; p = next){
next = p->rest;
free(p);
}
}
int main(){
Item* listptr;
int i;
listptr = new_item(0);
for (i=1; i < 6; i++){
listptr=insert_front(listptr, i);
}
for (i=0; i < 6; i++){
printf("i %d Item %d\n", i, get(listptr, i));
}
listptr = remove_item(listptr, 3);
for (i=0; i <= 5; i++){
printf("i %d Item %d\n", i, get(listptr, i));
}
printf("Index for 3 %d\n", get_index(listptr, 3));
free_all(listptr);
}