You are on page 1of 13

Lab Report No _9_

DATA STRUCTURES

Submitted By:
[MUHAMMAD QASIM SHAHAD]
[22-CS-155]
[ZOHAIB ASHRAF]
[22-CS-175]

Submitted to:
SIR ABDULLAH SHAHROSE

Dated:
Week [9]

Department of Computer Science,


HITEC University, Taxila
Lab Example No 1:

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>

Using namespace std;

struct Node {

int data;

Node* prev;

Node* next;

};

class DoublyLinkedList {

private:

Node* head;

public:

DoublyLinkedList() : head(nullptr) {}

void insertAtStart(int value) {

Node* newNode = new Node{value, nullptr, 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() {

Node* current = head;

while (current) {

cout << current->data << " ";

current = current->next;

cout <<endl;

void reversePrintList() {

Node* current = head;

while (current->next) {

current = current->next;

while (current) {

cout << current->data << " ";

current = current->prev;

cout << "\n";

Page 2 of 13
int searchElement(int value) {

Node* current = head;

int index = 1;

while (current) {

if (current->data == value) {

return index;

current = current->next;

index++;

return -1;

void reverseList() {

Node *current = head, *temp = nullptr;

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."<<endl;

Page 3 of 13
};

int main() {

DoublyLinkedList myList;

int choice;

int value;

do {

scout << "--- Main Menu Double Linklist ---"<<endl;

cout << "Enter your choice:"<<endl;

cout << "1. Insert at the start"<<endl;

cout << "2. Print the List"<<endl;

cout << "3. Reverse Print the List"<<endl;

cout << "4. Search an element in the List"<<endl;;

cout << "5. Create and Display a Reversed Doubly Linked List"<<endl;

cout << "6. Quit"<<endl;

cin >> choice;

switch (choice) {

case 1:

cout << "Enter value to insert: ";

cin >> value;

myList.insertAtStart(value);

break;

case 2:

myList.printList();

Page 4 of 13
break;

case 3:

myList.reversePrintList();

break;

case 4:

cout << "Enter value to search: ";

cin >> value;

int index = myList.searchElement(value);

if (index != -1) {

cout << "Element found at index: " << index << endl;

} else {

cout << "Element not found."<<endl;

break;

case 5:

List.reverseList();

List.printList();

break;

case 6:

cout << "Exiting program."<<endl;

break;

default:

cout << "Invalid choice. Please try again."<<endl;

} while (choice != 6);

return 0;

The results (Screenshot)

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) {}

void insertAtStart(int value) {


Node* newNode = new Node{value, NULL, NULL};
if (!head) {
head = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
cout << "Inserted " << value << " at the start." << endl;
}

int searchElement(int value) {


Node* current = head;
int index = 1;
while (current) {
if (current->data == value) {
return index;

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;

for (int i = 0; i < numElements; ++i) {


int value;
cout << "Enter element " << i + 1 << ": ";
cin >> value;
myList.insertAtStart(value);
}

cout << "Current List: ";


myList.printList();

int searchValue;
cout << "Enter the value to search: ";
cin >> searchValue;

int result = myList.searchElement(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 insertAtStart(int value) {


Node* newNode = new Node{value, NULL, NULL};
if (!head) {
head = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
cout << "Inserted " << value << " at the start." << endl;
}

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;

for (int i = 0; i < numElements; ++i) {


int value;
cout << "Enter element " << i + 1 << ": ";
cin >> value;
myList.insertAtStart(value);
}

cout << "Original List: ";


myList.printList();

myList.reverseList();

cout << "Reversed List: ";


myList.printList();

return 0;
}

The results (Screenshot)

Page 11 of 13
Page 12 of 13

You might also like