-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly.cpp
More file actions
163 lines (149 loc) · 3.54 KB
/
singly.cpp
File metadata and controls
163 lines (149 loc) · 3.54 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <iostream>
using namespace std;
class node {
public:
int data;
node* next;
node(int val) {
data = val;
next = NULL;
}
};
class singly {
node* head;
public:
singly() {
head = NULL;
}
// Insert node at end
void insert(int val) {
node* A = new node(val);
if (head == NULL) {
head = A;
return;
}
node* temp = head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = A;
}
// Delete a node
void deletenode(int val) {
if (head == NULL) {
cout << "List is empty\n";
return;
}
if (head->data == val) {
node* temp = head;
head = head->next;
delete temp;
return;
}
node* temp = head;
while (temp->next && temp->next->data != val)
temp = temp->next;
if (temp->next == NULL) {
cout << "Element not found\n";
return;
}
node* delnode = temp->next;
temp->next = temp->next->next;
// delete delnode;
}
// Display the list
void display() {
if (head == NULL) {
cout << "List is empty!\n";
return;
}
node* temp = head;
while (temp != NULL) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL\n";
}
// Search for an element
void search(int val) {
node* temp = head;
int pos = 1;
while (temp) {
if (temp->data == val) {
cout << "Element found at position " << pos << endl;
return;
}
pos++;
temp = temp->next;
}
cout << "Element not found\n";
}
// Reverse the list
void rev() {
node* curr = head;
node* Next = NULL;
node* prev = NULL;
while (curr) {
Next = curr->next;
curr->next = prev;
prev = curr;
curr = Next;
}
head = prev;
cout << "List reversed successfully.\n";
display();
}
// Find middle node
void findMiddle() {
if (head == NULL) {
cout << "List is empty\n";
return;
}
node* slow = head;
node* fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
}
cout << "Middle element: " << slow->data << endl;
}
};
int main() {
singly list;
int choice, val;
while (true) {
cout << "\n--- Singly Linked List Menu ---\n";
cout << "1. Insert\n2. Delete\n3. Display\n4. Search\n5. Reverse\n6. Find Middle\n7. Exit\nEnter choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter value: ";
cin >> val;
list.insert(val);
break;
case 2:
cout << "Enter value to delete: ";
cin >> val;
list.deletenode(val);
break;
case 3:
list.display();
break;
case 4:
cout << "Enter value to search: ";
cin >> val;
list.search(val);
break;
case 5:
list.rev();
break;
case 6:
list.findMiddle();
break;
case 7:
return 0;
default:
cout << "Invalid choice!\n";
}
}
}