File tree Expand file tree Collapse file tree 1 file changed +97
-0
lines changed Expand file tree Collapse file tree 1 file changed +97
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < cstdio>
2
+
3
+ #define NIL 0
4
+
5
+ using namespace std ;
6
+
7
+ struct node
8
+ {
9
+ int id, value;
10
+ node *next;
11
+ };
12
+
13
+ node *head;
14
+
15
+ void insert (int id, int value)
16
+ {
17
+ node *o = new node;
18
+ o->next = head;
19
+ head = o;
20
+ o->id = id;
21
+ o->value = value;
22
+ }
23
+
24
+ int find (int id)
25
+ {
26
+ node *cur = head;
27
+ while (cur != NIL)
28
+ {
29
+ if (cur->id == id)
30
+ {
31
+ return cur->value ;
32
+ }
33
+ cur = cur->next ;
34
+ }
35
+ return -1 ;
36
+ }
37
+
38
+ bool del (int id)
39
+ {
40
+ if (head == NIL)
41
+ {
42
+ return false ;
43
+ }
44
+ if (head->id == id)
45
+ {
46
+ node *temp = head;
47
+ head = head->next ;
48
+ delete temp;
49
+ return true ;
50
+ }
51
+ node *prev = head, *cur = head->next ;
52
+ while (cur != NIL)
53
+ {
54
+ if (cur->id == id)
55
+ {
56
+ prev->next = cur->next ;
57
+ delete cur;
58
+ return true ;
59
+ }
60
+ prev = cur;
61
+ cur = cur->next ;
62
+ }
63
+ return false ;
64
+ }
65
+
66
+ int main ()
67
+ {
68
+ int in, id, value;
69
+ while (true )
70
+ {
71
+ scanf (" %d" , &in);
72
+ if (in == 1 )
73
+ {
74
+ scanf (" %d%d" , &id, &value);
75
+ insert (id, value);
76
+ }
77
+ else if (in == 2 )
78
+ {
79
+ scanf (" %d" , &id);
80
+ printf (" %d\n " , find (id));
81
+ }
82
+ else if (in == 3 )
83
+ {
84
+ scanf (" %d" , &id);
85
+ printf (" %s\n " , del (id) == true ? " deleted" : " failed" );
86
+ }
87
+ else if (in == 0 )
88
+ {
89
+ return 0 ;
90
+ }
91
+ else
92
+ {
93
+ printf (" No such command!\n " );
94
+ }
95
+ }
96
+ return 0 ;
97
+ }
You can’t perform that action at this time.
0 commit comments