-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
103 lines (90 loc) · 1.53 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
#include "list.h"
void init(list *l) {
*l = NULL;
return;
}
int isempty(list l){
if(l == NULL)
return 1;
else
return 0;
}
void destroy_list(list *l){
list_node *p = *l;
list_node * q;
q = p;
while(p){
p = p->next;
free(q);
}
*l = NULL;
return ;
}
void traverse(list l){
printf("[");
list_node *p;
p = l;
while(p){
printf("%d ", p->data);
p = p->next;
}
printf("]\n");
return;
}
int get_next(list *l){ /*removes and returns 1st element*/
list p = *l;
if(!p)
return INT_MAX;
int k = p->data;
*l = p->next;
free(p);
return k;
}
int touch(list l) {
if(!l)
return INT_MAX;
return l->data;
}
void insert_beg(list *l, int d){
list newnode;
newnode = (list_node*)malloc(sizeof(list_node));
if(newnode){
newnode->data = d;
newnode->next = NULL;
}
else return;
newnode->next = *l;
*l = newnode;
return;
}
void append(list *l, int d){
list p, newnode;
newnode = (list_node*)malloc(sizeof(list_node));
if(newnode){
newnode->data = d;
newnode->next = NULL;
}
else return;
if(*l == NULL){
*l = newnode;
return;
}
p = *l;
while(p -> next) {
p = p->next;
}
p->next = newnode;
return;
}
/*
int main() {
list L1, L2;
init(&L1);
int ch;
while ((ch = getchar()) != '\n') {
append(&L1, ch);
}
traverse(L1);
printf("%d\n", peek(L1));
return 0;
}*/