Skip to content

Commit ff8beee

Browse files
authored
Add files via upload
1 parent 5ed72a8 commit ff8beee

File tree

2 files changed

+408
-0
lines changed

2 files changed

+408
-0
lines changed

dublylinkedlist.cpp

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
2+
#include<iostream>
3+
using namespace std;
4+
5+
// Creating structure
6+
struct Node {
7+
int data;
8+
Node* next;
9+
Node* prev; // New pointer for the previous node
10+
11+
Node(int value) {
12+
data = value;
13+
next = nullptr;
14+
prev = nullptr; // Initialize prev to nullptr
15+
}
16+
};
17+
18+
// Creating class
19+
class LinkedList {
20+
Node* head;
21+
Node* tail; // Add a tail pointer for efficient operations at the end
22+
public:
23+
LinkedList() {
24+
head = nullptr;
25+
tail = nullptr;
26+
}
27+
void append(int value);
28+
void deleteBeginning();
29+
void printList();
30+
void deleteEnd();
31+
void showMid();
32+
void insertAtPosition(int value, int position);
33+
void reverseList();
34+
};
35+
36+
// Append function to add value at the end
37+
void LinkedList::append(int value) {
38+
Node* newNode = new Node(value);
39+
if (head == nullptr) {
40+
head = newNode;
41+
tail = newNode; // Update tail when the first node is added
42+
return;
43+
}
44+
45+
tail->next = newNode; // Link the new node after tail
46+
newNode->prev = tail; // Set the new node's prev pointer to the current tail
47+
tail = newNode; // Update tail to the new node
48+
}
49+
50+
// Function to delete from the beginning
51+
void LinkedList::deleteBeginning() {
52+
if (head == nullptr) {
53+
cout << "The List Is Empty" << endl;
54+
return;
55+
}
56+
57+
Node* del = head;
58+
head = head->next;
59+
60+
if (head != nullptr) {
61+
head->prev = nullptr; // Update the prev pointer of the new head
62+
} else {
63+
tail = nullptr; // If the list is empty, reset the tail pointer
64+
}
65+
66+
delete del;
67+
}
68+
69+
// Function to delete from the end
70+
void LinkedList::deleteEnd() {
71+
if (tail == nullptr) {
72+
cout << "The List is empty" << endl;
73+
return;
74+
}
75+
76+
Node* del = tail;
77+
tail = tail->prev;
78+
79+
if (tail != nullptr) {
80+
tail->next = nullptr; // Update the next pointer of the new tail
81+
} else {
82+
head = nullptr; // If the list is empty, reset the head pointer
83+
}
84+
85+
delete del;
86+
}
87+
88+
// Function to calculate the midpoint
89+
void LinkedList::showMid() {
90+
Node *slow = head, *fast = head;
91+
92+
if (head == nullptr) {
93+
cout << "The List Is Empty" << endl;
94+
return;
95+
}
96+
97+
while (fast != nullptr && fast->next != nullptr) {
98+
slow = slow->next;
99+
fast = fast->next->next;
100+
}
101+
cout << "The middle element is: " << slow->data << endl;
102+
}
103+
104+
// Function to print the list
105+
void LinkedList::printList() {
106+
Node* temp = head;
107+
while (temp != nullptr) {
108+
cout << temp->data << " <-> ";
109+
temp = temp->next;
110+
}
111+
cout << "nullptr" << endl;
112+
}
113+
114+
// Function to insert a node at a specific position
115+
void LinkedList::insertAtPosition(int value, int position) {
116+
Node* newNode = new Node(value);
117+
if (position == 1) {
118+
newNode->next = head;
119+
if (head != nullptr) {
120+
head->prev = newNode;
121+
}
122+
head = newNode;
123+
124+
if (tail == nullptr) {
125+
tail = newNode; // If the list was empty, update the tail
126+
}
127+
return;
128+
}
129+
130+
Node* temp = head;
131+
for (int i = 1; i < position - 1 && temp != nullptr; ++i) {
132+
temp = temp->next;
133+
}
134+
135+
if (temp == nullptr) {
136+
cout << "Position out of bounds" << endl;
137+
delete newNode;
138+
} else {
139+
newNode->next = temp->next;
140+
newNode->prev = temp;
141+
142+
if (temp->next != nullptr) {
143+
temp->next->prev = newNode;
144+
} else {
145+
tail = newNode; // If inserting at the end, update the tail
146+
}
147+
148+
temp->next = newNode;
149+
}
150+
}
151+
152+
// Function to reverse the list
153+
void LinkedList::reverseList() {
154+
Node* current = head;
155+
Node* temp = nullptr;
156+
157+
while (current != nullptr) {
158+
temp = current->prev;
159+
current->prev = current->next;
160+
current->next = temp;
161+
current = current->prev; // Move to the next node in the original list
162+
}
163+
164+
if (temp != nullptr) {
165+
head = temp->prev; // Update the head to the new first node
166+
}
167+
}
168+
169+
// Main function to test the linked list operations
170+
int main() {
171+
LinkedList list; // Create an instance of the LinkedList class
172+
173+
// Append some values to the list
174+
list.append(10);
175+
list.append(20);
176+
list.append(30);
177+
list.append(40);
178+
list.append(100);
179+
180+
// Printing the value before deletion
181+
cout << "Before Deletion:" << endl;
182+
list.printList();
183+
184+
// Delete from the beginning and print the list
185+
cout << "After deletion from beginning:" << endl;
186+
list.deleteBeginning();
187+
list.printList();
188+
189+
// Delete from the end and print the list
190+
cout << "After deletion from end:" << endl;
191+
list.deleteEnd();
192+
list.printList();
193+
194+
// Insert at a specific position and print the list
195+
cout << "After inserting 25 at position 2:" << endl;
196+
list.insertAtPosition(25, 2);
197+
list.printList();
198+
199+
// Calculate the midpoint
200+
list.showMid();
201+
202+
// Reverse the current value
203+
cout << "Before reversed:" << endl;
204+
list.printList();
205+
cout << "After reversed:" << endl;
206+
list.reverseList();
207+
list.printList();
208+
209+
return 0;
210+
}

0 commit comments

Comments
 (0)