Experiment 6
Experiment 6
Practical File
Experiment – 6
Code:
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
};
class LinkedList {
Node* head;
public:
LinkedList() {
head = nullptr;
}
newNode->data = newData;
newNode->next = head;
head = newNode;
cout << "Inserted: " << newData << endl;
}
}
count++;
temp = temp->next;
}
return nullptr; // If position is invalid
}
prev->next = temp->next;
delete temp;
cout << "Removed: " << key << endl;
// (f) Copy the linked list and return the pointer of the new list
LinkedList* Copy() {
LinkedList* newList = new LinkedList();
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();
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 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);
cout << "Original list: ";
list.Display();
list.Reverse();
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 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;
}
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;
}
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;
}
// Polynomial addition
Polynomial* Add(Polynomial* poly2) {
Polynomial* result = new Polynomial();
Node* p1 = head;
Node* p2 = poly2->head;
p1 = p1->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);
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;
}
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);
list.SwapInPairs();
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 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();
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;
}
newNode->next = temp->next;
temp->next = newNode;
}
}
void Display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " -> ";
temp = temp->next;
}
cout << "NULL" << endl;
}
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);
return 0;
}
Output B