0% found this document useful (0 votes)
15 views4 pages

LAB 3 Graded Task

The document contains code for implementing a Doubly Linked List (DLL) and a Singly Linked List (SLL) in C++. It includes tasks for appending nodes, reversing the DLL, swapping nodes, and converting an SLL to a DLL. Additionally, it provides dry runs explaining the functionality of the code and how it operates on linked lists.

Uploaded by

yetikaustaad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

LAB 3 Graded Task

The document contains code for implementing a Doubly Linked List (DLL) and a Singly Linked List (SLL) in C++. It includes tasks for appending nodes, reversing the DLL, swapping nodes, and converting an SLL to a DLL. Additionally, it provides dry runs explaining the functionality of the code and how it operates on linked lists.

Uploaded by

yetikaustaad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB no: 03

Subject: DSA
Course instructor: Mam Marwa
Reg no: FA23-BCS-167
Name: Hamza
Code:
Task 1: Task 2:
#include <iostream> #include <iostream>
using namespace std; using namespace std;
class Node { class Node {
public: public:
int data; int data;
Node* next; Node* next;
Node* prev; Node* prev;

Node(int val) : data(val), next(nullptr), prev(nullptr) {} Node(int val) : data(val), next(nullptr), prev(nullptr) {}
}; };

class DoublyLinkedList { class DoublyLinkedList {


private: private:
Node* head; Node* head;

public: public:
DoublyLinkedList() : head(nullptr) {} DoublyLinkedList() : head(nullptr) {}

void append(int data) { void append(int data) {


Node* newNode = new Node(data); Node* newNode = new Node(data);
if (!head) { if (!head) {
head = newNode; head = newNode;
return; return;
} }

Node* temp = head; Node* temp = head;


while (temp->next) temp = temp->next; while (temp->next) temp = temp->next;

temp->next = newNode; temp->next = newNode;


newNode->prev = temp; newNode->prev = temp;
} }
void reverse() {
if (!head) return; void swapNodes(int val1, int val2) {
if (val1 == val2) return;
Node* current = head;
Node* temp = nullptr; Node* node1 = nullptr;
Node* node2 = nullptr;
while (current) { Node* temp = head;
temp = current->prev;
current->prev = current->next; while (temp) {
current->next = temp; if (temp->data == val1) node1 = temp;
current = current->prev; if (temp->data == val2) node2 = temp;
} temp = temp->next;
}
if (temp) head = temp->prev;
} if (!node1 || !node2) return;
void print() {
Node* temp = head; if (node1->prev) node1->prev->next = node2;
while (temp) { if (node2->prev) node2->prev->next = node1;
cout << temp->data << " "; if (node1->next) node1->next->prev = node2;
temp = temp->next; if (node2->next) node2->next->prev = node1;
}
cout << endl; swap(node1->prev, node2->prev);
} swap(node1->next, node2->next);
};
if (node1 == head) head = node2;
int main() { else if (node2 == head) head = node1;
DoublyLinkedList dll; }
dll.append(1);
dll.append(2); void print() {
dll.append(3); Node* temp = head;
dll.append(4); while (temp) {
dll.append(5); cout << temp->data << " ";
temp = temp->next;
cout << "Original list: "; }
dll.print(); cout << endl;
}
dll.reverse(); };

cout << "Reversed list: "; int main() {


dll.print(); DoublyLinkedList dll;
dll.append(1);
return 0; dll.append(2);
} dll.append(3);
dll.append(4);
dll.append(5);

cout << "Original list: ";


dll.print();

int val1, val2;


cout << "Enter two values to swap: ";
cin >> val1 >> val2;

dll.swapNodes(val1, val2);

cout << "List after swapping: ";


dll.print();

return 0;
}

Dry Run: Dry Run:


In this code we declare two pointers temp and current to get The code ask user to which values they need to swap in my case I
reverse order the loop runs until current node points to a value have used 1 and 5 the code reads both lists first and after finding
and storing the reverse values in the list which can be seen by the position of swap nodes and checks if the head node is
the output. shifted or not if head node is shifted then the code will declare
the head node.

Task 3:
#include <iostream>
using namespace std;
Dry Run:
class Node { This code converts sll to dll the function has given a single link
public: list as argument and first the head is copied into double link list
int data; and loop starts from next to head of single link list
Node* next; and copies the data into dll finally the the list is displayed
Node* prev; through print list function;
Node(int val) : data(val), next(nullptr), prev(nullptr) {}
};

class SinglyLinkedList {
public:
Node* head;
SinglyLinkedList() : head(nullptr) {}
void insert(int val) {
Node* newNode = new Node(val);
if (!head) {
head = newNode;
return;
}
Node* temp = head;
while (temp->next) {
temp = temp->next;
}
temp->next = newNode;
}
};

class DoublyLinkedList {
public:
Node* head;
DoublyLinkedList() : head(nullptr) {}
void convertFromSingly(SinglyLinkedList& sll) {
if (!sll.head) return;
head = new Node(sll.head->data);
Node* sllTemp = sll.head->next;
Node* dllTemp = head;
while (sllTemp) {
Node* newNode = new Node(sllTemp->data);
dllTemp->next = newNode;
newNode->prev = dllTemp;
dllTemp = newNode;
sllTemp = sllTemp->next;
}
}
void printList(Node* head)
{
Node* temp = head;
cout << "Forward List: ";
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};

int main() {
SinglyLinkedList sll;
sll.insert(1);
sll.insert(2);
sll.insert(3);
DoublyLinkedList dll;
dll.convertFromSingly(sll);
dll.printList(dll.head);
return 0;
}

You might also like