Skip to content

Commit 517bc9b

Browse files
authored
Merge pull request #444 from hariprasad1003/master
Simple Linked List Operations in C++
2 parents becd8e1 + 99d2abb commit 517bc9b

File tree

1 file changed

+217
-0
lines changed

1 file changed

+217
-0
lines changed
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
/*
2+
3+
This program will do simple linkedlist operations like Insertion, Deletion, Searching and Modifying.
4+
5+
*/
6+
7+
#include<iostream>
8+
#include <bits/stdc++.h>
9+
using namespace std;
10+
11+
struct Node
12+
{
13+
int data;
14+
Node *next;
15+
16+
};
17+
18+
// inserts to the list at the beginning
19+
20+
Node* insert_to_list(Node* head){
21+
22+
cout << "enter the number to be inserted in beginning : ";
23+
24+
int number;
25+
26+
cin >> number;
27+
28+
Node* temp = new Node();
29+
30+
temp -> data = number;
31+
32+
temp -> next = head;
33+
34+
head = temp;
35+
36+
return head;
37+
38+
}
39+
40+
// deletes the first node from list
41+
42+
Node* delete_from_list(Node* head){
43+
44+
Node* temp = new Node();
45+
46+
temp = head;
47+
48+
head = head -> next;
49+
50+
delete temp;
51+
52+
return head;
53+
54+
}
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+
119+
// prints list
120+
121+
void print_list(Node* head){
122+
123+
while (head != NULL)
124+
{
125+
cout << head -> data << " ";
126+
head = head -> next;
127+
}
128+
cout << endl;
129+
130+
}
131+
132+
133+
int main()
134+
{
135+
136+
Node* head = new Node();
137+
Node* second = new Node();
138+
Node* third = new Node();
139+
140+
head -> data = 1;
141+
head -> next = second;
142+
143+
second -> data = 2;
144+
second -> next = third;
145+
146+
third -> data = 3;
147+
third -> next = NULL;
148+
149+
bool quit = false;
150+
int choice;
151+
152+
while (!quit){
153+
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;
160+
161+
cin >> choice;
162+
163+
switch(choice){
164+
165+
case 1:
166+
{
167+
Node* temp = insert_to_list(head);
168+
head = temp;
169+
print_list(temp);
170+
break;
171+
}
172+
173+
case 2:
174+
{
175+
Node* temp = delete_from_list(head);
176+
head = temp;
177+
print_list(temp);
178+
break;
179+
}
180+
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:
197+
{
198+
print_list(head);
199+
break;
200+
}
201+
202+
case 6:
203+
{
204+
quit = true;
205+
break;
206+
}
207+
208+
default:{
209+
cout << "That is not a valid input, quitting program";
210+
quit = true;
211+
212+
}
213+
}
214+
}
215+
216+
return 0;
217+
}

0 commit comments

Comments
 (0)