0% found this document useful (0 votes)
21 views

Experiment 6

Uploaded by

pranav
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)
21 views

Experiment 6

Uploaded by

pranav
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/ 29

Delhi Technological University

IT DATA STRUCTURE G2 LAB

Practical File
Experiment – 6

Name: Prabhav Kumar


Roll no.: 2k22/ec/167
Teachers Name: Mrs. Bindu Verma
Q1:
WAP to construct simple linear linked list using dynamic memory allocation for the
given elements with the following functions, (a) Inserting a new node, (b) Accessing
a node (finding the position wrt header), (c) Removing a node with particular key
value, (d) Complete deletion of a linked list, and (e) Displaying the current list. (f)
Copy the linked list and return the pointer of the new list.

Code:
#include<iostream>
using namespace std;

class Node {
public:
int data;
Node* next;

};

class LinkedList {
Node* head;

public:
LinkedList() {
head = nullptr;
}

// (a) Insert a new node


void Insert(int newData) {
Node* newNode = new Node();

newNode->data = newData;
newNode->next = head;
head = newNode;
cout << "Inserted: " << newData << endl;
}

// (b) Access a node (finding the position wrt header)


Node* Access(int position) {
Node* temp = head;
int count = 0;
while (temp != nullptr) {
if (count == position) {
return temp;

}
count++;
temp = temp->next;
}
return nullptr; // If position is invalid
}

// (c) Remove a node with a particular key value


void Remove(int key) {
Node* temp = head;
Node* prev = nullptr;

if (temp != nullptr && temp->data == key) {


head = temp->next;
delete temp;
cout << "Removed: " << key << endl;
return;
}
while (temp != nullptr && temp->data != key) {
prev = temp;
temp = temp->next;
}

if (temp == nullptr) return; // Key not found

prev->next = temp->next;
delete temp;
cout << "Removed: " << key << endl;

// (d) Complete deletion of the linked list


void DeleteList() {
Node* current = head;
Node* next;

while (current != nullptr) {


next = current->next;
delete current;
current = next;
}
head = nullptr;
cout << "List deleted." << endl;
}

// (e) Display the current list


void Display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

// (f) Copy the linked list and return the pointer of the new list
LinkedList* Copy() {
LinkedList* newList = new LinkedList();

Node* current = head;


Node** lastPtrRef = &(newList->head);

while (current != nullptr) {


Node* newNode = new Node();
newNode->data = current->data;
newNode->next = nullptr;
*lastPtrRef = newNode;
lastPtrRef = &(newNode->next);
current = current->next;
}
return newList;
}
};

int main() {
LinkedList list;
list.Insert(10);
list.Insert(20);
list.Insert(30);

list.Display();
Node* node = list.Access(1);
if (node) cout << "Node at position 1: " << node->data << endl;

list.Remove(20);
list.Display();

LinkedList* copiedList = list.Copy();

cout << "Copied list: ";


copiedList->Display();

list.DeleteList();
list.Display();

return 0;
}

Output:

Q2: (a) WAP to reverse a singly linked list using one auxillary pointer. And try without
using any auxillary pointer. (b) WAP to implement Stack ADT using Linked list with
the basic operations as Create(), IsEmpty(), Push(), Pop(), IsFull() with appropriate
prototype to a functions.

Code a:
#include<iostream>
using namespace std;

class Node {
public:
int data;

Node* next;
};

class LinkedList {

Node* head;

public:
LinkedList() {
head = nullptr;
}

void Insert(int newData) {


Node* newNode = new Node();
newNode->data = newData;
newNode->next = head;
head = newNode;
}

void Display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

// (ii)(a) Reverse using one auxiliary pointer


void Reverse() {
Node* prev = nullptr;

Node* current = head;


Node* next = nullptr;

while (current != nullptr) {


next = current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
}
};

int main() {
LinkedList list;
list.Insert(10);
list.Insert(20);
list.Insert(30);
cout << "Original list: ";
list.Display();

list.Reverse();

cout << "Reversed list: ";


list.Display();

return 0;
}

Output A:

Code b:
#include<iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
};
class Stack {
Node* top;

public:
Stack() {
top = nullptr;
}

bool IsEmpty() {
return top == nullptr;

void Push(int newData) {


Node* newNode = new Node();
newNode->data = newData;
newNode->next = top;
top = newNode;
cout << "Pushed: " << newData << endl;
}

void Pop() {
if (IsEmpty()) {
cout << "Stack is Empty" << endl;
return;
}
Node* temp = top;
cout << "Popped: " << top->data << endl;
top = top->next;
delete temp;
}

void Display() {
Node* temp = top;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

};

int main() {
Stack stack;
stack.Push(10);
stack.Push(20);
stack.Push(30);

stack.Display();
stack.Pop();
stack.Display();

return 0;
}

Output B;
Q3: WAP to implement Queue ADT using Linked list with the basic functions of
Create(), IsEmpty(), Insert(), Delete() and IsFull() with suitable prototype to a
functions.

Code:
#include<iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
};

class Queue {
Node *front, *rear;

public:
Queue() {
front = rear = nullptr;
}

bool IsEmpty() {
return front == nullptr;
}

void Insert(int newData) {


Node* newNode = new Node();
newNode->data = newData;

newNode->next = nullptr;

if (rear == nullptr) {
front = rear = newNode;
cout << "Inserted: " << newData << endl;
return;
}

rear->next = newNode;
rear = newNode;
cout << "Inserted: " << newData << endl;
}

void Delete() {
if (IsEmpty()) {
cout << "Queue is Empty" << endl;
return;
}
Node* temp = front;
front = front->next;

if (front == nullptr) {
rear = nullptr;
}

cout << "Deleted: " << temp->data << endl;


delete temp;
}

void Display() {
Node* temp = front;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};

int main() {
Queue q;
q.Insert(10);
q.Insert(20);
q.Insert(30);

q.Display();
q.Delete();
q.Display();

return 0;
}
Output:

Q4: WAP for polynomial addition. Represent polynomial in linked list form with
suitable data structure.

Code:
#include<iostream>
using namespace std;

class Node {
public:
int coeff;
int power;
Node* next;
};

class Polynomial {
Node* head;

public:
Polynomial() {
head = nullptr;
}

// Add term to the polynomial

void AddTerm(int coeff, int power) {


Node* newNode = new Node();
newNode->coeff = coeff;
newNode->power = power;
newNode->next = nullptr;

if (!head || head->power < power) {


newNode->next = head;
head = newNode;
} else {
Node* temp = head;
while (temp->next && temp->next->power >= power) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
// Display the polynomial
void Display() {
if (!head) {
cout << "0" << endl;
return;
}

Node* temp = head;


while (temp != nullptr) {
cout << temp->coeff << "x^" << temp->power;

if (temp->next != nullptr && temp->next->coeff > 0)


cout << " + ";
temp = temp->next;
}
cout << endl;
}

// Polynomial addition
Polynomial* Add(Polynomial* poly2) {
Polynomial* result = new Polynomial();
Node* p1 = head;
Node* p2 = poly2->head;

while (p1 != nullptr && p2 != nullptr) {


if (p1->power == p2->power) {
result->AddTerm(p1->coeff + p2->coeff, p1->power);
p1 = p1->next;
p2 = p2->next;
} else if (p1->power > p2->power) {
result->AddTerm(p1->coeff, p1->power);
p1 = p1->next;
} else {
result->AddTerm(p2->coeff, p2->power);
p2 = p2->next;
}
}

while (p1 != nullptr) {


result->AddTerm(p1->coeff, p1->power);

p1 = p1->next;
}

while (p2 != nullptr) {


result->AddTerm(p2->coeff, p2->power);
p2 = p2->next;
}

return result;
}
};

int main() {
Polynomial poly1, poly2;

poly1.AddTerm(3, 2);
poly1.AddTerm(5, 1);
poly1.AddTerm(6, 0);
poly2.AddTerm(2, 2);
poly2.AddTerm(3, 1);
poly2.AddTerm(4, 0);

cout << "Polynomial 1: ";


poly1.Display();
cout << "Polynomial 2: ";
poly2.Display();

Polynomial* result = poly1.Add(&poly2);

cout << "Result of addition: ";


result->Display();

delete result;
return 0;
}

Output:
Q5: WAP to swap elements in pairs in the given linked list. (e.g.: 1->2->3->4->5-
>null, then result should be 2->1->4->3->5->null.

Code:
#include<iostream>
using namespace std;

class Node {

public:
int data;
Node* next;
};

class LinkedList {
Node* head;

public:
LinkedList() {
head = nullptr;
}

void Insert(int newData) {


Node* newNode = new Node();
newNode->data = newData;
newNode->next = head;

head = newNode;
}
// Swap elements in pairs
void SwapInPairs() {
Node* temp = head;
while (temp != nullptr && temp->next != nullptr) {
// Swap the data of the two nodes
int swap = temp->data;
temp->data = temp->next->data;
temp->next->data = swap;
// Move to the next pair
temp = temp->next->next;

}
}

void Display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};

int main() {
LinkedList list;
list.Insert(5);
list.Insert(4);
list.Insert(3);
list.Insert(2);
list.Insert(1);

cout << "Original list: ";


list.Display();

list.SwapInPairs();

cout << "List after swapping in pairs: ";


list.Display();

return 0;
}

Output:

Q6: (a) WAP to find second last node of the given linked list. (b) WAP to
concatenate two singly linked list in sorted order either ascending or descending.

Code A:
#include<iostream>
using namespace std;

class Node {
public:
int data;
Node* next;
};

class LinkedList {
Node* head;

public:
LinkedList() {
head = nullptr;
}

void Insert(int newData) {


Node* newNode = new Node();
newNode->data = newData;
newNode->next = head;
head = newNode;
}

// Find the second last node


Node* FindSecondLast() {
if (head == nullptr || head->next == nullptr) {
return nullptr; // No second last node exists
}

Node* temp = head;


while (temp->next->next != nullptr) {
temp = temp->next;
}
return temp;
}

void Display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
};

int main() {
LinkedList list;
list.Insert(10);
list.Insert(20);
list.Insert(30);

list.Display();

Node* secondLast = list.FindSecondLast();


if (secondLast) {
cout << "Second last node: " << secondLast->data << endl;
} else {
cout << "Second last node doesn't exist." << endl;
}

return 0;
}
Output A:

Code B:
#include<iostream>
using namespace std;

class Node {
public:
int data;

Node* next;
};

class LinkedList {
Node* head;

public:
LinkedList() {
head = nullptr;
}

void Insert(int newData) {


Node* newNode = new Node();
newNode->data = newData;
newNode->next = nullptr;

if (!head || head->data >= newData) {


newNode->next = head;
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr && temp->next->data < newData) {
temp = temp->next;
}

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

void Display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}

// Concatenate two sorted lists in sorted order


LinkedList* Concatenate(LinkedList* list2) {
Node* head1 = head;
Node* head2 = list2->head;
LinkedList* resultList = new LinkedList();

while (head1 != nullptr && head2 != nullptr) {


if (head1->data <= head2->data) {
resultList->Insert(head1->data);
head1 = head1->next;
} else {
resultList->Insert(head2->data);
head2 = head2->next;
}

while (head1 != nullptr) {


resultList->Insert(head1->data);
head1 = head1->next;
}

while (head2 != nullptr) {


resultList->Insert(head2->data);
head2 = head2->next;
}

return resultList;
}
};

int main() {
LinkedList list1, list2;
list1.Insert(10);
list1.Insert(20);
list1.Insert(30);

list2.Insert(15);
list2.Insert(25);
list2.Insert(35);

cout << "List 1: ";


list1.Display();

cout << "List 2: ";


list2.Display();

LinkedList* resultList = list1.Concatenate(&list2);


cout << "Concatenated sorted list: ";
resultList->Display();

return 0;
}
Output B

You might also like