Practical DS File
Practical DS File
#include <iostream>
#include <string>
using namespace std;
public:
LinkedList() : head(NULL), tail(NULL) {}
void deleteHead() {
if (!head) {
return;
}
Node<T> *temp = head;
head = head->next;
delete temp;
}
void display() {
Node<T> *current = head;
while (current) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
}
};
int main() {
LinkedList<int> intList;
LinkedList<int> strList;
int choice;
do {
cout << "Menu:\n";
cout << "1. Insert Integer at Head\n";
cout << "2. Insert Integer at Tail\n";
cout << "3. Delete Integer from Head\n";
cout << "4. Display Integer List\n";
cout << "5. Insert String at Head\n";
cout << "6. Insert String at Tail\n";
cout << "7. Delete String from Head\n";
cout << "8. Display String List\n";
cout << "9. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
int data;
cout << "Enter an integer to insert: ";
cin >> data;
intList.insertHead(data);
break;
case 2:
cout << "Enter an integer to insert: ";
cin >> data;
intList.insertTail(data);
break;
case 3:
intList.deleteHead();
break;
case 4:
cout << "Integer List: ";
intList.display();
break;
case 5:
int strData;
cout << "Enter a string to insert: ";
cin >> strData;
strList.insertHead(strData);
break;
case 6:
cout << "Enter a string to insert: ";
cin >> strData;
strList.insertTail(strData);
break;
case 7:
strList.deleteHead();
break;
case 8:
cout << "String List: ";
strList.display();
break;
case 9:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 9);
return 0;
}
Practical 2
#include <iostream>
using namespace std;
template <class T> class DoubleLinkList;
template <class T>
class Node {
T elem;
Node<T> *next;
Node<T> *prev;
public:
Node(T e) {
elem = e;
next = 0;
prev = 0;
}
public:
DoubleLinkList() {
head = 0;
tail = 0;
}
DoubleLinkList(Node<T> *&n1) {
head = n1;
head->prev = 0;
head->next = 0;
}
// print function
void printAll() {
Node<T> *currentNode = head;
while (currentNode != 0) {
cout << currentNode->elem << endl;
currentNode = currentNode->next;
}
delete currentNode;
}
// 1. Insert at Head
void headInsert(int elem) {
Node<int> *n1 = new Node<int>(elem);
if (head == 0) {
head = n1;
tail = n1;
head->prev = 0;
head->next = 0;
} // if block
else {
n1->next = head;
head->prev = n1;
n1->prev = 0;
head = n1;
} // else block
} // insert at head
// 2. Insert at tail
void tailInsert(int elem) {
Node<int> *n1 = new Node<int>(elem);
if (head == 0) {
head = n1;
head->prev = 0;
head->next = 0;
} // if block
else {
tail->next = n1;
n1->prev = tail;
n1->next = 0;
tail = n1;
} // else block
} // insert at tail
// 3. Delete at Head
void delete_at_head() {
if (head == 0) {
cout << "List is empty ! ";
return;
} // if block
else {
Node<T> *temp = head;
head = head->next;
head->prev = 0;
delete temp;
} // else
}
// 5. Delete at Tail
void delete_at_tail() {
if (head == 0) {
cout << "List is empty ! ";
return;
} // if block
else {
Node<T> *temp = tail;
tail = tail->prev;
tail->next = 0;
delete temp;
} // else
}
};
int main() {
DoubleLinkList<int> l1;
l1.headInsert(5);
l1.headInsert(4);
l1.headInsert(3);
l1.headInsert(2);
l1.headInsert(1);
l1.tailInsert(7);
l1.tailInsert(8);
l1.tailInsert(9);
l1.printAll();
cout << endl;
l1.delete_at_head();
l1.delete_at_tail();
l1.printAll();
return 0;
return 0;
}
Practical 3
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node(int data) {
this->data = data;
this->next = nullptr;
};
class CircularLinkedList {
private:
Node *head;
public:
if (index < 0) {
return;
int currentIndex = 0;
current = current->next;
currentIndex++;
}
newNode->next = current->next;
current->next = newNode;
return;
if (index == 0) {
if (head->next == head) {
delete head;
head = nullptr;
} else {
last = last->next;
head = head->next;
last->next = head;
delete temp;
}
} else {
int currentIndex = 0;
current = current->next;
currentIndex++;
if (current->next == head) {
} else {
delete temp;
void display() {
if (head == nullptr) {
return;
current = current->next;
~CircularLinkedList() {
if (head == nullptr) {
return;
do {
next = current->next;
delete current;
current = next;
};
int main() {
CircularLinkedList list;
switch (choice) {
case 1:
list.insertAtIndex(data, index);
break;
case 2:
list.deleteAtIndex(index);
break;
case 3:
break;
case 4:
break;
default:
break;
}
Practical 4
Using Array
#include <iostream>
using namespace std;
bool overFlow(){
if(top==capacity-1){
return true;
}
return false;
}//overflow function
bool empty(){
if(top==-1){
return true;
}
return false;
}//empty function
top+=1;
stack[top]=elem;
}//push function
T pop(){
if(this->empty()){
cout<<"\nStack is empty already\n";
return -1;
}//if block
top-=1;
return stack[top+1];
}//pop function
void display(){
cout<<"\n Here is the Value of stack \n";
int index=top;
while(index!=-1){
cout<<stack[index]<<endl;
index--;
}
}//display function
T Top(){
return stack[top];
}//top function
};
int main() {
int choice;
int capacity;
cout << "Enter the capacity of the stack: ";
cin >> capacity;
MyStack<int> stack(capacity);
do {
cout << "\nStack Menu:\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Display\n";
cout << "4. Top\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
int elem;
cout << "Enter element to push: ";
cin >> elem;
stack.push(elem);
break;
case 2:
if (!stack.empty()) {
cout << "Popped element: " << stack.pop() << endl;
} else {
cout << "Stack is empty." << endl;
}
break;
case 3:
stack.display();
break;
case 4:
if (!stack.empty()) {
cout << "Top element: " << stack.Top() << endl;
} else {
cout << "Stack is empty." << endl;
}
break;
case 5:
cout << "Exiting program.\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 5);
return 0;
}
Using Linked list:-
#include <iostream>
public:
// Constructor to initialize the stack
Stack()
{
top = nullptr;
}
int main()
{
Stack stack;
int choice, value;
do
{
cout << "Menu:" << endl;
cout << "1. Push" << endl;
cout << "2. Pop" << endl;
cout << "3. Peek" << endl;
cout << "4. Display" << endl;
cout << "5. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter a value to push: ";
cin >> value;
stack.push(value);
break;
case 2:
stack.pop();
break;
case 3:
value = stack.peek();
if (value != -1)
{
cout << "Top element: " << value << endl;
}
break;
case 4:
stack.display();
break;
case 5:
cout << "Quitting the program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 5);
return 0;
}
#include <bits/stdc++.h>
using namespace std;
stack<char> st;
string result;
// If an operator is scanned
else {
while (!st.empty()
&& prec(s[i]) <= prec(st.top())) {
result += st.top();
st.pop();
}
st.push(c);
}
}
// Pop all the remaining elements from the stack
while (!st.empty()) {
result += st.top();
st.pop();
}
// Driver code
int main()
{
string exp = "((A+B)/C+E/F*G-H)+I";
// Function call
infixToPostfix(exp);
return 0;
}
Practical 5
#include <iostream>
using namespace std;
class Queue {
private:
int capacity;
int size;
int *Q;
int front;
int rear;
public:
Queue() {
capacity = 0;
front = 0;
rear = 0;
size = 0;
Q=new int[capacity];
} // default constructor
Queue(int capacity) {
this->capacity = capacity;
front = 0;
rear = 0;
size = 0;
Q=new int[capacity];
} // parameterized constructor
// Insertion operations
void Insert(int elem) {
if (IsFull()) {
cout << "Queue is full" << endl;
return;
} else {
Q[rear] = elem;
rear = ((rear + 1) % capacity);
size++;
}
} // Insert
// Deletion operations
int Delete() {
if (IsEmpty()) {
cout << "Queue is empty" << endl;
return -1;
} else {
int elem = Q[front];
front = ((front + 1) % capacity);
size--;
return elem;
}
} // Delete
// Utility functions
int Size() { return size; } // Size
} else {
int i = front;
while (i != rear) {
cout << Q[i] << " ";
i = ((i + 1) % capacity);
}
}
}
int Front() {
if (IsEmpty()) {
cout << "Queue is empty" << endl;
return -1;
} else {
return Q[front];
}
}
int Rear() {
if (IsEmpty()) {
cout << "Queue is empty" << endl;
return -1;
} else {
return Q[rear];
}
}
void Clear() {
front = 0;
rear = 0;
size = 0;
}
};
int main() {
int capacity, choice, elem;
cout << "Enter the capacity of the queue: ";
cin >> capacity;
Queue q(capacity);
while (true) {
cout << "\nMenu:\n";
cout << "1. Insert an element\n";
cout << "2. Delete an element\n";
cout << "3. Get the front element\n";
cout << "4. Get the rear element\n";
cout << "5. Check if the queue is empty\n";
cout << "6. Check if the queue is full\n";
cout << "7. Print the queue\n";
cout << "8. Clear the queue\n";
cout << "9. Quit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter an element to insert: ";
cin >> elem;
q.Insert(elem);
cout << "Element " << elem << " inserted into the queue.\n";
break;
case 2:
elem = q.Delete();
if (elem != -1)
cout << "Element " << elem << " deleted from the queue.\n";
break;
case 3:
elem = q.Front();
if (elem != -1)
cout << "Front element: " << elem << endl;
break;
case 4:
elem = q.Rear();
if (elem != -1)
cout << "Rear element: " << elem << endl;
break;
case 5:
if (q.IsEmpty())
cout << "Queue is empty.\n";
else
cout << "Queue is not empty.\n";
break;
case 6:
if (q.IsFull())
cout << "Queue is full.\n";
else
cout << "Queue is not full.\n";
break;
case 7:
cout << "Queue: ";
q.Print();
cout << endl;
break;
case 8:
q.Clear();
cout << "Queue cleared.\n";
break;
case 9:
cout << "Exiting the program. Goodbye!\n";
return 0;
default:
cout << "Invalid choice. Please try again.\n";
}
}
return 0;
}
Practical 6
#include <iostream>
#include <algorithm> // For max function
return root;
}
// Utility function to delete a node with the given value from the binary search
tree
Node* deleteNode(Node* root, int value) {
if (root == nullptr) {
return root;
}
return root;
}
#include <algorithm>
#include <iostream>
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = std::max(getHeight(y->left), getHeight(y->right)) + 1;
x->height = std::max(getHeight(x->left), getHeight(x->right)) + 1;
return x;
}
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = std::max(getHeight(x->left), getHeight(x->right)) + 1;
y->height = std::max(getHeight(y->left), getHeight(y->right)) + 1;
return y;
}
// Right-Right Case
if (balance < -1 && value > node->right->data) {
return rotateLeft(node);
}
// Left-Right Case
if (balance > 1 && value > node->left->data) {
node->left = rotateLeft(node->left);
return rotateRight(node);
}
// Right-Left Case
if (balance < -1 && value < node->right->data) {
node->right = rotateRight(node->right);
return rotateLeft(node);
}
return node;
}
if (value == node->data) {
return true;
} else if (value < node->data) {
return search(node->left, value);
} else {
return search(node->right, value);
}
}
public:
AVLTree() : root(NULL) {}
void displayInorder() {
inorderTraversal(root);
std::cout << std::endl;
}
int main() {
AVLTree<int> myAVL;
myAVL.insert(10);
myAVL.insert(20);
myAVL.insert(30);
myAVL.insert(40);
myAVL.insert(50);
myAVL.insert(25);
searchValue = 15;
if (myAVL.search(searchValue)) {
std::cout << "Value " << searchValue << " found in AVL Tree." << std::endl;
} else {
std::cout << "Value " << searchValue << " not found in AVL Tree."
<< std::endl;
}
return 0;
}