2019-08-21 10:10:08 +08:00
|
|
|
#include <iostream>
|
2020-04-22 17:18:56 +08:00
|
|
|
|
|
|
|
struct node {
|
2020-04-22 17:09:27 +08:00
|
|
|
int val;
|
|
|
|
node *next;
|
2017-12-24 01:30:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
node *start;
|
|
|
|
|
2020-04-22 17:18:56 +08:00
|
|
|
void insert(int x) {
|
2020-04-22 17:09:27 +08:00
|
|
|
node *t = start;
|
2020-04-22 22:09:12 +08:00
|
|
|
node *n = new node;
|
|
|
|
n->val = x;
|
|
|
|
n->next = NULL;
|
2020-04-22 17:18:56 +08:00
|
|
|
if (start != NULL) {
|
|
|
|
while (t->next != NULL) {
|
2020-04-22 17:09:27 +08:00
|
|
|
t = t->next;
|
|
|
|
}
|
2020-04-22 22:06:51 +08:00
|
|
|
t->next = n;
|
2020-04-22 17:18:56 +08:00
|
|
|
} else {
|
2020-04-22 17:09:27 +08:00
|
|
|
start = n;
|
|
|
|
}
|
2020-04-22 22:09:12 +08:00
|
|
|
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:18:56 +08:00
|
|
|
void remove(int x) {
|
|
|
|
if (start == NULL) {
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << "\nLinked List is empty\n";
|
2020-04-22 17:09:27 +08:00
|
|
|
return;
|
2020-04-22 17:18:56 +08:00
|
|
|
} else if (start->val == x) {
|
2020-04-22 17:09:27 +08:00
|
|
|
node *temp = start;
|
|
|
|
start = start->next;
|
|
|
|
delete temp;
|
|
|
|
return;
|
|
|
|
}
|
2020-04-22 17:18:56 +08:00
|
|
|
|
2020-04-22 17:09:27 +08:00
|
|
|
node *temp = start, *parent = start;
|
2019-01-09 23:01:55 +08:00
|
|
|
|
2020-04-22 17:18:56 +08:00
|
|
|
while (temp != NULL && temp->val != x) {
|
2020-04-22 17:09:27 +08:00
|
|
|
parent = temp;
|
|
|
|
temp = temp->next;
|
|
|
|
}
|
2019-01-09 23:01:55 +08:00
|
|
|
|
2020-04-22 17:18:56 +08:00
|
|
|
if (temp == NULL) {
|
2020-04-22 17:29:33 +08:00
|
|
|
std::cout << std::endl << x << " not found in list\n";
|
2020-04-22 17:09:27 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-04-22 17:18:56 +08:00
|
|
|
|
|
|
|
parent->next = temp->next;
|
2020-04-22 17:09:27 +08:00
|
|
|
delete temp;
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:18:56 +08:00
|
|
|
void search(int x) {
|
2020-04-22 17:09:27 +08:00
|
|
|
node *t = start;
|
|
|
|
int found = 0;
|
2020-04-22 17:18:56 +08:00
|
|
|
while (t != NULL) {
|
|
|
|
if (t->val == x) {
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << "\nFound";
|
2020-04-22 17:09:27 +08:00
|
|
|
found = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t = t->next;
|
|
|
|
}
|
2020-04-22 17:18:56 +08:00
|
|
|
if (found == 0) {
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << "\nNot Found";
|
2020-04-22 17:09:27 +08:00
|
|
|
}
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:18:56 +08:00
|
|
|
void show() {
|
2020-04-22 17:09:27 +08:00
|
|
|
node *t = start;
|
2020-04-22 17:18:56 +08:00
|
|
|
while (t != NULL) {
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << t->val << "\t";
|
2020-04-22 17:09:27 +08:00
|
|
|
t = t->next;
|
|
|
|
}
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:18:56 +08:00
|
|
|
void reverse() {
|
2020-04-22 17:09:27 +08:00
|
|
|
node *first = start;
|
2020-04-22 17:18:56 +08:00
|
|
|
if (first != NULL) {
|
2020-04-22 16:56:01 +08:00
|
|
|
node *second = first->next;
|
2020-04-22 17:18:56 +08:00
|
|
|
while (second != NULL) {
|
2020-04-22 16:56:01 +08:00
|
|
|
node *tem = second->next;
|
|
|
|
second->next = first;
|
|
|
|
first = second;
|
|
|
|
second = tem;
|
|
|
|
}
|
2020-04-22 17:09:27 +08:00
|
|
|
start->next = NULL;
|
|
|
|
start = first;
|
2020-04-22 17:21:48 +08:00
|
|
|
} else {
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << "\nEmpty list";
|
2020-04-22 17:18:56 +08:00
|
|
|
}
|
2019-08-07 09:34:58 +08:00
|
|
|
}
|
|
|
|
|
2020-04-22 17:18:56 +08:00
|
|
|
int main() {
|
2020-04-22 17:09:27 +08:00
|
|
|
int choice, x;
|
2020-04-22 17:18:56 +08:00
|
|
|
do {
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << "\n1. Insert";
|
|
|
|
std::cout << "\n2. Delete";
|
|
|
|
std::cout << "\n3. Search";
|
|
|
|
std::cout << "\n4. Print";
|
|
|
|
std::cout << "\n5. Reverse";
|
|
|
|
std::cout << "\n0. Exit";
|
|
|
|
std::cout << "\n\nEnter you choice : ";
|
|
|
|
std::cin >> choice;
|
2020-04-22 17:18:56 +08:00
|
|
|
switch (choice) {
|
2020-04-22 17:09:27 +08:00
|
|
|
case 1:
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << "\nEnter the element to be inserted : ";
|
|
|
|
std::cin >> x;
|
2020-04-22 17:09:27 +08:00
|
|
|
insert(x);
|
|
|
|
break;
|
|
|
|
case 2:
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << "\nEnter the element to be removed : ";
|
|
|
|
std::cin >> x;
|
2020-04-22 17:09:27 +08:00
|
|
|
remove(x);
|
|
|
|
break;
|
|
|
|
case 3:
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << "\nEnter the element to be searched : ";
|
|
|
|
std::cin >> x;
|
2020-04-22 17:09:27 +08:00
|
|
|
search(x);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
show();
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << "\n";
|
2020-04-22 17:09:27 +08:00
|
|
|
break;
|
|
|
|
case 5:
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << "The reversed list: \n";
|
2020-04-22 17:09:27 +08:00
|
|
|
reverse();
|
|
|
|
show();
|
2020-04-22 17:26:03 +08:00
|
|
|
std::cout << "\n";
|
2020-04-22 17:09:27 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (choice != 0);
|
2017-12-24 01:30:49 +08:00
|
|
|
|
2020-04-22 17:09:27 +08:00
|
|
|
return 0;
|
2017-12-24 01:30:49 +08:00
|
|
|
}
|