-
Notifications
You must be signed in to change notification settings - Fork 1
/
lab00 linkedlist.cc
99 lines (83 loc) · 2.36 KB
/
lab00 linkedlist.cc
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
#include<stdio.h>
#include<stdlib.h>
struct list_item{
int data;
struct list_item* next;
};
void append(struct list_item *first, int x); /* puts x at the end of the list */
void prepend(struct list_item *first, int x); /* puts x at the beginning of the list */
void print(struct list_item *first); /* prints all elements in the list */
/* input_sorted: find the first element in the list larger than x
and input x right before that element */
void input_sorted(struct list_item *first, int x);
void clear(struct list_item *first); /* free everything dynamically allocated */
void append(struct list_item *first, int x){
struct list_item *curr = first;
while(curr->next != NULL)
curr = curr->next;
curr->next = (struct list_item*)malloc(sizeof(struct list_item));
curr->next->data = x;
curr->next->next = NULL;
}
void prepend(struct list_item *first, int x){
struct list_item *newnode = (struct list_item*)malloc(sizeof(struct list_item));
newnode->data = x;
newnode->next = first->next;
first->next = newnode;
}
void print(struct list_item *first){
struct list_item *curr = first;
printf("begin->");
while(curr->next != NULL){
printf("%d->",curr->next->data);
curr = curr->next;
}
printf("end\n");
}
void input_sorted(struct list_item *first, int x){
struct list_item *curr = first;
while(curr->next != NULL && x > curr->next->data)
curr = curr->next;
struct list_item *newnode = (struct list_item*)malloc(sizeof(struct list_item));
newnode->data = x;
newnode->next = curr->next;
curr->next = newnode;
}
void clear(struct list_item *first){
struct list_item *curr = first->next, *deletenode;
while(curr != NULL){
deletenode = curr;
curr = curr->next;
free(deletenode);
}
first->next = NULL;
}
int main(){
struct list_item *mylist = (struct list_item*)malloc(sizeof(struct list_item));
mylist->data = -1;
mylist->next = NULL;
print(mylist);
append(mylist,1);
append(mylist,2);
append(mylist,3);
append(mylist,4);
append(mylist,5);
print(mylist);
clear(mylist);
prepend(mylist,1);
prepend(mylist,2);
prepend(mylist,3);
prepend(mylist,4);
prepend(mylist,5);
print(mylist);
clear(mylist);
input_sorted(mylist,2);
input_sorted(mylist,1);
input_sorted(mylist,4);
input_sorted(mylist,5);
input_sorted(mylist,3);
print(mylist);
clear(mylist);
print(mylist);
free(mylist);
}