-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsa2.cpp
More file actions
203 lines (173 loc) · 5.43 KB
/
dsa2.cpp
File metadata and controls
203 lines (173 loc) · 5.43 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <iostream>
// Using namespace std is generally fine for small programs and competitive programming,
// but for larger projects, it's often recommended to qualify names (e.g., std::cout).
using namespace std;
// Represents a single node in the linked list
class Node {
public:
int data; // The data stored in the node
Node* next; // Pointer to the next node in the list
// Constructor to initialize a new node
Node(int val) {
data = val;
next = nullptr; // Initialize next to nullptr by default
}
};
// Represents the linked list itself
class LinkedList {
private: // It's good practice to make member variables private
Node* head; // Pointer to the first node in the list
Node* tail; // Pointer to the last node in the list
public:
// Constructor to initialize an empty linked list
LinkedList() {
head = nullptr; // Both head and tail are null for an empty list
tail = nullptr;
}
// Method to add a new node to the front of the list
void push_front(int val) {
// Create a new node with the given value
Node* n1 = new Node(val);
// Case 1: The list is currently empty
if (head == nullptr) { // Corrected: Used '==' for comparison
head = n1; // The new node is both the head and the tail
tail = n1;
return; // Exit the function
}
// Case 2: The list is not empty
else {
n1->next = head; // The new node points to the current head
head = n1; // The new node becomes the new head
}
}
// Method to print all elements in the linked list
void printl() {
Node* temp = head; // Start from the head of the list
// Loop through the list until temp becomes nullptr (i.e., beyond the last node)
while (temp != nullptr) { // Corrected: Loop until temp is nullptr to print the last element
cout << temp->data << " "; // Print the data of the current node
temp = temp->next; // Move to the next node
}
cout << endl; // Print a newline character at the end for better formatting
}
void push_back(int val){
Node* n1 = new Node(val);
if (head == nullptr){
head = n1;
tail = n1;
return;
}
else {
tail->next = n1;
tail = n1;
}
}
void pop_front(){
Node* temp = head;
if (head == nullptr){
cout << "No element to be removed" << endl;
}
else {
int pop_no = head->data;
head = head->next;
temp->next = nullptr;
}
}
void pop_back(){
Node* temp = head;
if (head == nullptr){
cout << "No element to be removed" << endl;
}
else {
while(temp->next != tail){
temp = temp->next;
}
temp->next = NULL;
delete tail;
tail = temp;
}
}
void insert(int val,int pos){
Node* newNode = new Node(val);
Node* temp = head;
if(pos <0) {
cout << "Invalid index acccesed" << endl;
}
else if (pos ==0) {
push_front(val);
}
else {
for(int i=0;i<pos-1;i++){
if (temp == NULL){
cout << "Invalid pos assigned" << endl;
return;
}
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
int search(int key){
Node* temp = head;
int index = 0;
while(temp->next != NULL){
if (temp->data == key){
cout << "The key has been found" << endl;
return index;
}
index++;
temp = temp->next;
}
if (temp->next == NULL){
if(temp->data == key){
cout << "The key has been found" << endl;
return index++;
}
}
else{
cout << "The key not found" <<endl;
return 0;
}
}
int size(){
Node* temp = head;
int length = 0;
while(temp->next != NULL){
length++;
temp = temp->next;
}
return length+1;
}
// Destructor to free allocated memory when the LinkedList object is destroyed
// This prevents memory leaks.
~LinkedList() {
Node* current = head;
while (current != nullptr) {
Node* next_node = current->next;
delete current;
current = next_node;
}
head = nullptr; // Ensure head and tail are null after deletion
tail = nullptr;
}
};
int main() {
LinkedList l1;
cout << "Pushing elements to the front:" << endl;
l1.push_front(2); // List: 2
l1.push_front(3); // List: 3 -> 2
l1.push_front(4); // List: 4 -> 3 -> 2
l1.push_front(5); // List: 5 -> 4 -> 3 -> 2
l1.push_back(10);
l1.push_front(80);
l1.insert(11,3);
int number;
cout << "Enter Number to be searched" << endl;
cin >> number;
cout << l1.search(number) << endl;
cout << "The size of the LinkedList is: " << l1.size() << endl;
cout << "Printing the linked list:" << endl;
l1.printl(); // Expected output: 5 4 3 2
return 0;
}