-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
139 lines (95 loc) · 2.57 KB
/
Copy pathlinkedlist.cpp
File metadata and controls
139 lines (95 loc) · 2.57 KB
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>
struct node{
int value;
struct node *next;
};
//struct node *new_node;
struct node *add_to_list(struct node *list,int new_value){// int value?
struct node *new_node;
new_node = (node*)malloc(sizeof(node)); //«Å§i³o¸Ì¦³¤°»ò¦n³B?
new_node->value = new_value;
new_node->next = list;
// list = new_node;
return new_node;
}
struct node *search_list(struct node *list, int search_value){
/* if (list->value == search_value)
return list;
else if (list->next == NULL)
{printf("cant find");
return list;}
else
return search_list(list->next, search_value);
*/
struct node *p;
for(p=list; p!=NULL; p=p->next)
{
if (p->value == search_value)
return p;
}
//printf("cant find");
return list;
}
struct node *add_value(void){
int n;
struct node *first = NULL;
printf("start. 0 to exit");
for(;;)
{scanf("%d", &n);
if (n!=0)
first =add_to_list(first, n);
else
return first;
}
}
int list_delete(struct node* list, int n){
printf("run delete");
// struct node *first= list;
struct node *cur, *pre;
for (cur= list, pre=NULL; cur->value!=n && cur!=NULL ; pre=cur, cur=cur->next )
;//struct node *p = search_list(list, n);
if (cur== NULL)
printf("no item found");
getchar();
if (pre==NULL)
list = cur->next;
if (cur->value==n)
pre->next = cur->next;
return 0;
}
int main(void){
//struct node *new_node;
//new_node = (node*)malloc(sizeof(node));
//(*new_node).value = 10;
//new_node->value = 10;
/*
struct node *first;
first = NULL;
new_node->next = first;
first = new_node;
new_node = (node*)malloc(sizeof(node));
new_node->value = 20;
new_node->next = first;
first = new_node;
// first = add_to_list(first, 10);
*/
//struct node *first;
// first = add_value();
//printf("hello");
// int n = first-> value;
// struct node *p;
//for(p;p!=NULL;p=p.next)
int pause;
struct node *first = add_value();
struct node *find = search_list(first, 5);
printf("%d\n", find->value );
printf("then delete");
scanf("%d", &pause);
list_delete(first, 2);
// find = search_list(first, 5);
scanf("%d", &pause);
//printf("%d", find->value );
getchar();
return 0;
}