1
1
/*
2
2
3
+ This program will do simple linkedlist operations like Insertion, Deletion, Searching and Modifying.
4
+
3
5
*/
4
6
5
7
#include < iostream>
@@ -39,12 +41,6 @@ Node* insert_to_list(Node* head){
39
41
40
42
Node* delete_from_list (Node* head){
41
43
42
- // cout << "enter the number to be removed from list : ";
43
-
44
- // int number;
45
-
46
- // cin >> number;
47
-
48
44
Node* temp = new Node ();
49
45
50
46
temp = head;
@@ -57,14 +53,77 @@ Node* delete_from_list(Node* head){
57
53
58
54
}
59
55
56
+ // search node in the list, returns true if the node is found in the list, if not false
57
+
58
+ void search_list (Node* head){
59
+
60
+ cout << " enter the number to search : " ;
61
+
62
+ int number;
63
+
64
+ cin >> number;
65
+
66
+ string result = " false" ;
67
+
68
+ while (head != NULL )
69
+ {
70
+
71
+ if (head -> data == number){
72
+
73
+ // cout << head -> data << endl;
74
+
75
+ if (head -> data){
76
+
77
+ result = " true" ;
78
+ }
79
+
80
+ }
81
+
82
+ head = head -> next;
83
+
84
+ }
85
+
86
+ cout << result << endl;
87
+
88
+ }
89
+
90
+ // modifies the node in the list
91
+
92
+ void modify_list (Node* head){
93
+
94
+ int modify_number, replace_number;
95
+
96
+ cout << " enter the number to modify : " ;
97
+
98
+ cin >> modify_number;
99
+
100
+ cout << " enter the number that replaces the number : " ;
101
+
102
+ cin >> replace_number;
103
+
104
+ while (head != NULL )
105
+ {
106
+
107
+ if (head -> data == modify_number){
108
+
109
+ head -> data = replace_number;
110
+ }
111
+
112
+ head = head -> next;
113
+
114
+ }
115
+
116
+ }
117
+
118
+
60
119
// prints list
61
120
62
- void print_list (Node* n ){
121
+ void print_list (Node* head ){
63
122
64
- while (n != NULL )
123
+ while (head != NULL )
65
124
{
66
- cout << n -> data << " " ;
67
- n = n -> next;
125
+ cout << head -> data << " " ;
126
+ head = head -> next;
68
127
}
69
128
cout << endl;
70
129
@@ -79,7 +138,7 @@ int main()
79
138
Node* third = new Node ();
80
139
81
140
head -> data = 1 ;
82
- head -> next = second;
141
+ head -> next = second;
83
142
84
143
second -> data = 2 ;
85
144
second -> next = third;
@@ -92,10 +151,12 @@ int main()
92
151
93
152
while (!quit){
94
153
95
- cout << " 1. add to list begninnig" << endl
96
- << " 2. delete first node from list" << endl
97
- << " 3. print list" << endl
98
- << " 4. quit" << endl;
154
+ cout << " 1. add number to the list beginning" << endl
155
+ << " 2. delete first number from the list" << endl
156
+ << " 3. search for number in the list" << endl
157
+ << " 4. modify number in the list" << endl
158
+ << " 5. print the list" << endl
159
+ << " 6. quit" << endl;
99
160
100
161
cin >> choice;
101
162
@@ -118,12 +179,27 @@ int main()
118
179
}
119
180
120
181
case 3 :
182
+ {
183
+
184
+ search_list (head);
185
+ break ;
186
+ }
187
+
188
+ case 4 :
189
+ {
190
+ modify_list (head);
191
+ print_list (head);
192
+ break ;
193
+
194
+ }
195
+
196
+ case 5 :
121
197
{
122
198
print_list (head);
123
199
break ;
124
200
}
125
201
126
- case 4 :
202
+ case 6 :
127
203
{
128
204
quit = true ;
129
205
break ;
@@ -137,14 +213,5 @@ int main()
137
213
}
138
214
}
139
215
140
- // Node* temp = insert_node(head);
141
-
142
- // print_list(temp);
143
-
144
-
145
- // Node* temp = delete_node(head);
146
-
147
- // print_list(temp);
148
-
149
216
return 0 ;
150
217
}
0 commit comments