Lab 9 Dsa
Lab 9 Dsa
DATA STRUCTURES
Submitted By:
[MUHAMMAD QASIM SHAHAD]
[22-CS-155]
[ZOHAIB ASHRAF]
[22-CS-175]
Submitted to:
SIR ABDULLAH SHAHROSE
Dated:
Week [9]
Solution:
Brief description (3-5 lines)
make the following main menu to perform different tasks.
-main menu double linklist----- Enter your choice
1.Insert at the start 2.
Print the List 3.
Reverse Print the List
4.Quit
The code
#include <iostream>
struct Node {
int data;
Node* prev;
Node* next;
};
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() : head(nullptr) {}
if (!head) {
head = newNode;
} else {
newNode->next = head;
Page 1 of 13
head->prev = newNode;
head = newNode;
cout << "Inserted " << value << " at the start."<<endl;
void printList() {
while (current) {
current = current->next;
cout <<endl;
void reversePrintList() {
while (current->next) {
current = current->next;
while (current) {
current = current->prev;
Page 2 of 13
int searchElement(int value) {
int index = 1;
while (current) {
if (current->data == value) {
return index;
current = current->next;
index++;
return -1;
void reverseList() {
while (current) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
if (temp) {
head = temp->prev;
Page 3 of 13
};
int main() {
DoublyLinkedList myList;
int choice;
int value;
do {
cout << "5. Create and Display a Reversed Doubly Linked List"<<endl;
switch (choice) {
case 1:
myList.insertAtStart(value);
break;
case 2:
myList.printList();
Page 4 of 13
break;
case 3:
myList.reversePrintList();
break;
case 4:
if (index != -1) {
cout << "Element found at index: " << index << endl;
} else {
break;
case 5:
List.reverseList();
List.printList();
break;
case 6:
break;
default:
return 0;
Page 5 of 13
Page 6 of 13
Lab Example No 2:
Solution:
Brief description (3-5 lines)
Write a C++ program to create a function to search an element in a double linked list. If
element exists in the double linked list then, it should return its index (or True)
otherwise -1(or False).
The code
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() : head(NULL) {}
Page 7 of 13
}
current = current->next;
index++;
}
return -1; // Element not found
}
void printList() {
Node* current = head;
while (current) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
};
int main() {
DoublyLinkedList myList;
int numElements;
cout << "Enter the number of elements to insert: ";
cin >> numElements;
int searchValue;
cout << "Enter the value to search: ";
cin >> searchValue;
if (result != -1) {
cout << "Element found at index: " << result << endl;
} else {
cout << "Element not found." << endl;
Page 8 of 13
}
return 0;
}
The results (Screenshot)
Lab Example No 3:
Solution:
Brief description (3-5 lines)
Write a C++ program to create and display a reversed doubly linked list. (Use swap algorithm
to reverse a doubly linked list.
The code
#include <iostream>
using namespace std;
struct Node {
int data;
Node* prev;
Node* next;
};
Page 9 of 13
class DoublyLinkedList {
private:
Node* head;
public:
DoublyLinkedList() : head(NULL) {}
void reverseList() {
Node *current = head, *temp = NULL;
while (current) {
temp = current->prev;
current->prev = current->next;
current->next = temp;
current = current->prev;
}
if (temp) {
head = temp->prev;
}
cout << "Reversed doubly linked list using swap algorithm." << endl;
}
void printList() {
Node* current = head;
while (current) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
};
Page 10 of 13
int main() {
DoublyLinkedList myList;
int numElements;
cout << "Enter the number of elements to insert: ";
cin >> numElements;
myList.reverseList();
return 0;
}
Page 11 of 13
Page 12 of 13