Skip to content

Commit 99d2abb

Browse files
search and modify func added
1 parent f2d2cc2 commit 99d2abb

File tree

1 file changed

+92
-25
lines changed

1 file changed

+92
-25
lines changed

Data Structures/Linked Lists/Singly Linked List/simple_linkedlist_operations.cpp

Lines changed: 92 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/*
22
3+
This program will do simple linkedlist operations like Insertion, Deletion, Searching and Modifying.
4+
35
*/
46

57
#include<iostream>
@@ -39,12 +41,6 @@ Node* insert_to_list(Node* head){
3941

4042
Node* delete_from_list(Node* head){
4143

42-
// cout << "enter the number to be removed from list : ";
43-
44-
// int number;
45-
46-
// cin >> number;
47-
4844
Node* temp = new Node();
4945

5046
temp = head;
@@ -57,14 +53,77 @@ Node* delete_from_list(Node* head){
5753

5854
}
5955

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+
60119
// prints list
61120

62-
void print_list(Node* n){
121+
void print_list(Node* head){
63122

64-
while (n != NULL)
123+
while (head != NULL)
65124
{
66-
cout << n -> data << " ";
67-
n = n -> next;
125+
cout << head -> data << " ";
126+
head = head -> next;
68127
}
69128
cout << endl;
70129

@@ -79,7 +138,7 @@ int main()
79138
Node* third = new Node();
80139

81140
head -> data = 1;
82-
head -> next = second;
141+
head -> next = second;
83142

84143
second -> data = 2;
85144
second -> next = third;
@@ -92,10 +151,12 @@ int main()
92151

93152
while (!quit){
94153

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;
99160

100161
cin >> choice;
101162

@@ -118,12 +179,27 @@ int main()
118179
}
119180

120181
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:
121197
{
122198
print_list(head);
123199
break;
124200
}
125201

126-
case 4:
202+
case 6:
127203
{
128204
quit = true;
129205
break;
@@ -137,14 +213,5 @@ int main()
137213
}
138214
}
139215

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-
149216
return 0;
150217
}

0 commit comments

Comments
 (0)