-
Notifications
You must be signed in to change notification settings - Fork 368
/
Copy pathSinglyLinkedList.cpp
135 lines (121 loc) · 3.01 KB
/
SinglyLinkedList.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <stdio.h>
using namespace std;
class Node {
public:
int data;
Node *next;
Node() {
data = 0;
next = NULL;
}
Node(int data) {
this->data = data;
this->next = NULL;
}
};
class LinkedList {
Node *head;
public:
LinkedList() { head = NULL; }
void insertNode(int);
void deleteNode(int);
void searchNode(int);
void printList();
};
// insertion method
void LinkedList::insertNode(int x) {
Node *newNode = new Node(x);
if (!head)
{
head = newNode;
return;
} // if empty list
Node *temp = head;
while (temp->next)
temp = temp->next;
temp->next = newNode;
}
// deletion method
void LinkedList::deleteNode(int pos) {
Node *p1 = head, *p2 = NULL;
int length = 0;
if (!head) { cout << "EMPTY LIST" << endl; return; }
// list length
while (p1) { p1 = p1->next; ++length; }
// check if postion less than list length
if (length < pos) { cout << "Index Out Of Range" << endl; return; }
p1 = head;
// if haed to be deleted
if (pos == 1) {
head = head->next;
delete p1;
cout << "\n\t!!! ELEMENT DELETED !!!";
return;
}
while (pos-- > 1) { p2 = p1; p1 = p1->next; }
p2->next = p1->next;
delete p1;
cout << "\n\t!!! ELEMENT DELETED !!!";
}
// search method
void LinkedList::searchNode(int x) {
Node *p = head;
int flag = 0;
while (p)
{
p = p->next;
if (p->data == x)
{
flag = 1;
break;
}
}
if (flag) cout << "\n!!! ELEMENT FOUND !!!";
else cout << "\n*** ELEMENT NOT FOUND ***";
}
// print method
void LinkedList::printList() {
Node *p = head;
if (head == NULL) { cout << "Empty List" << endl; return; }
while (p) { cout << p->data << " "; p = p->next; }
}
int main() {
LinkedList list;
int n, ele, c;
char ch = 'y';
// Input code
while (ch == 'y') {
cout << "\nChoose an operation :\n1. Insertion\n2. Deletion\n3. Search\n4. Print";
cout << "\n\tMAKE YOU CHOICE : ";
cin >> c;
switch (c) {
case 1: // Insertion
cout << "\nINSERTION\nEnter List Size : ";
cin >> n;
cout << "\nEnter List Elements : ";
for (int i = 0; i < n; i++) { cin >> ele; list.insertNode(ele); }
break;
case 2: // Deletion
cout << "\nDELETION\nEnter the position for deletion : ";
cin >> ele;
list.deleteNode(ele);
break;
case 3: // Searching
cout << "\nSEARCHING\nEnter the element to be searched : ";
cin >> ele;
list.searchNode(ele);
break;
case 4: // Print
cout << "\nList Elements : ";
list.printList();
break;
default:
cout << "\n\t*** INVALID INPUT ***";
break;
}
cout << "\n\tTry Again ? (y/n) : ";
cin >> ch;
}
return 0;
}