AIM (1)- Program to insert a node at the beginning, at the end and in the middle of the given linked
list. c++ without comments.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class LinkedList {
public:
Node* head;
LinkedList() {
head = nullptr;
void insertAtBeginning(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = head;
head = newNode;
void insertAtEnd(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
return;
Node* temp = head;
while (temp->next != nullptr)
{
temp = temp->next;
temp->next = newNode;
void insertInMiddle(int value, int position) {
if (position == 1) {
insertAtBeginning(value);
return;
Node* newNode = new Node();
newNode->data = value;
Node* temp = head;
for (int i = 1; i < position - 1 && temp != nullptr; i++) {
temp = temp->next;
if (temp == nullptr || temp->next == nullptr) {
insertAtEnd(value);
return;
newNode->next = temp->next;
temp->next = newNode;
void display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
cout << endl;
}
};
int main() {
LinkedList list;
[Link](10);
[Link](20);
[Link](15, 2);
[Link]();
return 0;
}
AIM (2)- Write a program to reverse a linked list.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class LinkedList {
public:
Node* head;
LinkedList() {
head = nullptr;
void insertAtEnd(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
return;
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
temp->next = newNode;
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;
void display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
cout << endl;
};
int main() {
LinkedList list;
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link]();
[Link]();
[Link]();
return 0;
}
AIM (3)- Write a program to search a value in the given linked list.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class LinkedList {
public:
Node* head;
LinkedList() {
head = nullptr;
void insertAtEnd(int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = nullptr;
if (head == nullptr) {
head = newNode;
return;
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
temp->next = newNode;
bool search(int value) {
Node* temp = head;
while (temp != nullptr) {
if (temp->data == value) {
return true;
temp = temp->next;
return false;
void display() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
cout << endl;
};
int main() {
LinkedList list;
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link]();
int valueToSearch = 30;
if ([Link](valueToSearch)) {
cout << valueToSearch << " is found in the list." << endl;
} else {
cout << valueToSearch << " is not found in the list." << endl;
return 0;
}
AIM (4)- Write a program to evaluate postfix expression.
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int evaluatePostfix(string expression) {
stack<int> s;
for (int i = 0; i < [Link](); i++) {
char ch = expression[i];
if (is digit(ch)) {
s. push (ch - '0');
} else {
int operand2 = [Link] (); [Link] ();
int operand1 = [Link] (); [Link] ();
switch (ch) {
case '+': s. push (operand1 + operand2); break;
case '-': s. push (operand1 - operand2); break;
case '*': s. push (operand1 * operand2); break;
case '/': s. push (operand1 / operand2); break;
return [Link] ();
int main () {
string postfix = "231*+9-";
cout << "Postfix Expression: " << postfix << endl;
cout << "Evaluation Result: " << evaluatePostfix(postfix) << endl;
return 0;
}
AIM (5)- Write a program to sort an array using quick sort.
#include <iostream>
using namespace std;
void swap (int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
int partition (int arr [], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
swap (&arr [i + 1], &arr[high]);
return i + 1;
void quicksort (int arr [], int low, int high) {
if (low < high) {
int pi = partition (arr, low, high);
quicksort (arr, low, pi - 1);
quicksort (arr, pi + 1, high);
void displayArray (int arr [], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
int main () {
int arr [] = {10, 7, 8, 9, 1, 5};
int n = size of(arr) / size of (arr [0]);
cout << "Unsorted array: ";
displayArray (arr, n);
quicksort (arr, 0, n - 1);
cout << "Sorted array: ";
displayArray (arr, n);
return 0;
}
AIM (6)- Write a program to solve towers of Hanoi problem using recursion.
#include <iostream>
using namespace std;
void towersOfHanoi (int n, char from Rod, char to Rod, char auxRod) {
if (n == 1) {
cout << "Move disk 1 from rod " << from Rod << " to rod " << to Rod << endl;
return;
towersOfHanoi (n - 1, from Rod, auxRod, to Rod);
cout << "Move disk " << n << " from rod " << from Rod << " to rod " << to Rod << endl;
towersOfHanoi (n - 1, auxRod, to Rod, from Rod);
int main () {
int n = 3; // Number of disks
towersOfHanoi (n, 'A', 'C', 'B');
return 0;
}
AIM (7)- Write a program to perform insertion and deletion operation in circular.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
class Circular LinkedList {
public:
Node* last;
Circular LinkedList () {
last = nullptr;
void insertAtBeginning (int value) {
Node* newNode = new Node ();
newNode->data = value;
if (last == nullptr) {
last = newNode;
last->next = last;
} else {
newNode->next = last->next;
last->next = newNode;
void insertAtEnd (int value) {
Node* newNode = new Node ();
newNode->data = value;
if (last == nullptr) {
last = newNode;
last->next = last;
} else {
newNode->next = last->next;
last->next = newNode;
last = newNode;
void deleteNode (int value) {
if (last == nullptr) {
cout << "List is empty." << endl;
return;
Node* temp = last->next;
Node* prev = last;
do {
if (temp->data == value) {
if (temp == last->next && temp == last) {
last = nullptr;
} else if (temp == last->next) {
last->next = temp->next;
} else if (temp == last) {
prev->next = last->next;
last = prev;
} else {
prev->next = temp->next;
delete temp;
cout << "Deleted " << value << " from the list." << endl;
return;
prev = temp;
temp = temp->next;
} while (temp! = last->next);
cout << value << " not found in the list." << endl;
void display () {
if (last == nullptr) {
cout << "List is empty." << endl;
return;
Node* temp = last->next;
do {
cout << temp->data << " ";
temp = temp->next;
} while (temp! = last->next);
cout << endl;
};
int main () {
Circular LinkedList cll;
cll. InsertAtEnd (10);
cll. InsertAtEnd (20);
cll. InsertAtEnd (30);
cll. InsertAtBeginning (5);
cout << "Circular Linked List: ";
cll. Display ();
cll. DeleteNode (20);
cout << "After deletion: ";
cll. Display ();
return 0;
}
AIM (8)- Write a program to sort an array using insertion sort.
#include <iostream>
using namespace std;
void insertion Sort (int arr [], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
Arr[j + 1] = key;
void displayArray (int arr [], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
cout << endl;
int main () {
int arr [] = {12, 11, 13, 5, 6};
int n = size of(arr) / size of (arr [0]);
cout << "Unsorted array: ";
displayArray (arr, n);
insertion Sort (arr, n);
cout << "Sorted array: ";
displayArray (arr, n);
return 0;
}
AIM (9)- Write a program to traverse preorder of a binary tree.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node (int value) {
data = value;
left = nullptr;
right = nullptr;
};
void preorder Traversal (Node* root) {
if (root == nullptr)
return;
cout << root->data << " "; // Visit the root
preorderTraversal(root->left); // Traverse the left subtree
preorderTraversal(root->right); // Traverse the right subtree
int main () {
Node* root = new Node (1);
root->left = new Node (2);
root->right = new Node (3);
root->left->left = new Node (4);
root->left->right = new Node (5);
cout << "Preorder traversal of the binary tree: ";
preorderTraversal(root);
cout << endl;
return 0;
}
Output:
/\
23
/\
45
The output of this preorder traversal will be 1 2 4 5
AIM (10)- Write a program to traverse postorder of a binary tree.
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = nullptr;
right = nullptr;
};
void postorderTraversal (Node* root) {
if (root == nullptr)
return;
postorderTraversal(root->left); // Traverse the left subtree
postorderTraversal(root->right); // Traverse the right subtree
cout << root->data << " "; // Visit the root
int main () {
Node* root = new Node (1);
root->left = new Node (2);
root->right = new Node (3);
root->left->left = new Node (4);
root->left->right = new Node (5);
cout << "Postorder traversal of the binary tree: ";
postorderTraversal(root);
cout << endl;
return 0;
}
Output:
/\
23
/\
45
The output of this postorder traversal will be 4 5 2 3 1.