Data_StructuresC++_manual
Data_StructuresC++_manual
Course Objectives
• To write and execute programs in C++ to solve problems using data structures such as arrays,
linked lists, stacks, queues, trees, graphs and search trees.
Experiments
#include <iostream>
struct Node {
int data;
Node* next;
};
newNode->data = data;
newNode->next = nullptr;
return newNode;
if (head == nullptr) {
head = newNode;
} else {
current = current->next;
current->next = newNode;
if (head == nullptr) {
return;
if (head->data == target) {
delete temp;
return;
current = current->next;
if (current->next != nullptr) {
current->next = current->next->next;
delete temp;
current = current->next;
// Main function
int main() {
insertAtEnd(head, 1);
insertAtEnd(head, 2);
insertAtEnd(head, 3);
insertAtEnd(head, 4);
insertAtEnd(head, 5);
displayList(head);
int targetToDelete = 3;
deleteNode(head, targetToDelete);
std::cout << "Linked List after Deletion of " << targetToDelete << ": ";
displayList(head);
delete current;
current = nextNode;
return 0;
2. Write a C++ programs to implement i) Bubble sort ii) Selection sort iii) quick sort iv) insertion
sort
#include <iostream>
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
return 0;
}
#include <iostream>
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
return 0;
}
#include <iostream>
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
return 0;
}
#include <iostream>
// Move elements of arr[0..i-1] that are greater than key to one position ahead of their
current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
--j;
}
arr[j + 1] = key;
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
return 0;
}
#include <iostream>
class Stack {
private:
int arr[maxSize];
int top;
public:
Stack() : top(-1) {}
std::cout << "Stack Overflow: Cannot push element, stack is full." << std::endl;
} else {
arr[++top] = data;
std::cout << "Pushed " << data << " to the stack." << std::endl;
}
void pop() {
if (top < 0) {
std::cout << "Stack Underflow: Cannot pop element, stack is empty." << std::endl;
} else {
std::cout << "Popped " << arr[top--] << " from the stack." << std::endl;
void display() {
if (top < 0) {
} else {
};
int main() {
Stack stack;
stack.push(1);
stack.push(2);
stack.push(3);
stack.display();
stack.pop();
stack.display();
stack.pop();
stack.display();
stack.pop();
stack.display();
return 0;
#include <iostream>
class Queue {
private:
int arr[maxSize];
public:
if (rear == maxSize - 1) {
std::cout << "Queue Overflow: Cannot enqueue element, queue is full." << std::endl;
} else {
if (front == -1) {
front = 0;
arr[++rear] = data;
std::cout << "Enqueued " << data << " to the queue." << std::endl;
void dequeue() {
if (front == -1) {
std::cout << "Queue Underflow: Cannot dequeue element, queue is empty." << std::endl;
} else {
std::cout << "Dequeued " << arr[front++] << " from the queue." << std::endl;
void display() {
if (front == -1) {
} else {
std::cout << "Queue elements: ";
};
int main() {
Queue queue;
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.display();
queue.dequeue();
queue.display();
queue.dequeue();
queue.display();
queue.dequeue();
queue.display();
return 0;
4. Write a C++ programs to implement list ADT to perform following operations a) Insert an
element into a list. b) Delete an element from list c) Search for a key element in list d)count
number of nodes in list
#include <iostream>
struct Node {
int data;
Node* next;
};
class List {
private:
Node* head;
public:
List() : head(nullptr) {}
if (head == nullptr) {
head = newNode;
} else {
newNode->next = head;
head = newNode;
std::cout << "Inserted " << data << " into the list." << std::endl;
if (head == nullptr) {
return;
prev = current;
current = current->next;
if (current == nullptr) {
std::cout << "Element " << key << " not found in the list. Cannot delete." << std::endl;
} else {
// Remove the node containing the key element
if (prev == nullptr) {
head = current->next;
} else {
prev->next = current->next;
delete current;
std::cout << "Deleted element with value " << key << " from the list." << std::endl;
if (current->data == key) {
std::cout << "Element " << key << " found in the list." << std::endl;
return;
current = current->next;
std::cout << "Element " << key << " not found in the list." << std::endl;
int count = 0;
++count;
current = current->next;
return count;
};
int main() {
List myList;
myList.insertElement(1);
myList.insertElement(2);
myList.insertElement(3);
std::cout << "Number of nodes in the list: " << myList.countNodes() << std::endl;
myList.searchElement(2);
myList.deleteElement(2);
myList.searchElement(2);
std::cout << "Number of nodes in the list: " << myList.countNodes() << std::endl;
return 0;
#include <iostream>
struct Node {
int data;
Node* next;
};
class Stack {
private:
Node* top;
public:
Stack() : top(nullptr) {}
newNode->next = top;
top = newNode;
std::cout << "Pushed " << data << " onto the stack." << std::endl;
}
// Function to pop an element from the stack
void pop() {
if (top == nullptr) {
std::cout << "Stack Underflow: Cannot pop element, stack is empty." << std::endl;
} else {
top = top->next;
std::cout << "Popped " << temp->data << " from the stack." << std::endl;
delete temp;
void display() {
current = current->next;
};
int main() {
Stack stack;
stack.push(1);
stack.push(2);
stack.push(3);
stack.display();
stack.pop();
stack.display();
stack.pop();
stack.display();
stack.pop();
stack.display();
return 0;
#include <iostream>
struct Node {
int data;
Node* next;
};
class Queue {
private:
Node* front;
Node* rear;
public:
if (rear == nullptr) {
// If the queue is empty, set both front and rear to the new node
} else {
rear->next = newNode;
rear = newNode;
std::cout << "Enqueued " << data << " into the queue." << std::endl;
void dequeue() {
if (front == nullptr) {
std::cout << "Queue Underflow: Cannot dequeue element, queue is empty." << std::endl;
} else {
if (front == nullptr) {
rear = nullptr;
std::cout << "Dequeued " << temp->data << " from the queue." << std::endl;
delete temp;
void display() {
current = current->next;
};
int main() {
Queue queue;
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.display();
queue.dequeue();
queue.display();
queue.dequeue();
queue.display();
queue.dequeue();
queue.display();
return 0;
#include <iostream>
struct Node {
int data;
Node* next;
Node* prev;
};
class Deque {
private:
Node* front;
Node* rear;
public:
if (front == nullptr) {
// If the deque is empty, set both front and rear to the new node
} else {
newNode->next = front;
front->prev = newNode;
front = newNode;
std::cout << "Inserted " << data << " at the front of the deque." << std::endl;
if (rear == nullptr) {
// If the deque is empty, set both front and rear to the new node
front = rear = newNode;
} else {
newNode->prev = rear;
rear->next = newNode;
rear = newNode;
std::cout << "Inserted " << data << " at the rear of the deque." << std::endl;
void deleteFront() {
if (front == nullptr) {
std::cout << "Deque Underflow: Cannot delete element, deque is empty." << std::endl;
} else {
front = front->next;
if (front == nullptr) {
rear = nullptr;
} else {
front->prev = nullptr;
std::cout << "Deleted element at the front of the deque." << std::endl;
delete temp;
}
// Function to delete an element from the rear of the deque
void deleteRear() {
if (rear == nullptr) {
std::cout << "Deque Underflow: Cannot delete element, deque is empty." << std::endl;
} else {
rear = rear->prev;
if (rear == nullptr) {
front = nullptr;
} else {
rear->next = nullptr;
std::cout << "Deleted element at the rear of the deque." << std::endl;
delete temp;
void display() {
current = current->next;
}
std::cout << std::endl;
};
int main() {
Deque deque;
deque.insertFront(1);
deque.insertRear(2);
deque.insertFront(3);
deque.display();
deque.deleteFront();
deque.display();
deque.deleteRear();
deque.display();
deque.deleteFront();
deque.display();
return 0;
#include <iostream>
class Deque {
private:
int arr[maxSize];
int front;
int rear;
public:
if (front == -1) {
front = rear = 0;
arr[front] = data;
std::cout << "Deque Overflow: Cannot insert element at the front, deque is full." <<
std::endl;
} else {
arr[front] = data;
std::cout << "Inserted " << data << " at the front of the deque." << std::endl;
if (front == -1) {
front = rear = 0;
arr[rear] = data;
std::cout << "Deque Overflow: Cannot insert element at the rear, deque is full." <<
std::endl;
} else {
arr[rear] = data;
std::cout << "Inserted " << data << " at the rear of the deque." << std::endl;
void deleteFront() {
if (front == -1) {
std::cout << "Deque Underflow: Cannot delete element from the front, deque is empty."
<< std::endl;
} else {
if (front == rear) {
} else {
std::cout << "Deleted element from the front of the deque." << std::endl;
if (front == -1) {
std::cout << "Deque Underflow: Cannot delete element from the rear, deque is empty."
<< std::endl;
} else {
if (front == rear) {
} else {
std::cout << "Deleted element from the rear of the deque." << std::endl;
void display() {
if (front == -1) {
} else {
int i = front;
while (i != rear) {
i = (i + 1) % maxSize;
};
int main() {
Deque deque;
deque.insertFront(
7. Write a C++ program to perform the following operations: a) Insert an element into a binary
search tree. b) Delete an element from a binary search tree. c) Search for a key element in a
binary search tree.
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
newNode->data = data;
return newNode;
}
// Function to insert an element into a binary search tree
if (root == nullptr) {
return createNode(data);
// Recursively insert into the left or right subtree based on the value
return root;
node = node->left;
return node;
return root;
} else {
if (root->left == nullptr) {
delete root;
return temp;
delete root;
return temp;
// Node with two children, get the inorder successor (smallest in the right subtree)
root->data = temp->data;
// Delete the inorder successor
return root;
if (root == nullptr) {
return false;
if (key == root->data) {
return true;
} else {
if (root != nullptr) {
inorderTraversal(root->left);
inorderTraversal(root->right);
}
int main() {
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorderTraversal(root);
if (search(root, searchKey)) {
std::cout << "Element " << searchKey << " found in the BST." << std::endl;
} else {
std::cout << "Element " << searchKey << " not found in the BST." << std::endl;
}
// Delete an element from the BST
std::cout << "BST after deleting " << deleteKey << ": ";
inorderTraversal(root);
// Free the allocated memory for the BST (not shown in the original question)
// You can add a function to free the memory after you are done with the BST
return 0;
8. Write C++ programs for implementing the following sorting methods:Merge sort b) Heap
sort
#include <iostream>
#include <vector>
std::vector<int> leftArray(n1);
std::vector<int> rightArray(n2);
// Copy data to temporary arrays leftArray[] and rightArray[]
arr[k] = leftArray[i];
++i;
} else {
arr[k] = rightArray[j];
++j;
++k;
arr[k] = leftArray[i];
++i;
++k;
arr[k] = rightArray[j];
++j;
++k;
// Same as (left + right) / 2, but avoids overflow for large left and right
int main() {
return 0;
Heap sort
#include <iostream>
#include <vector>
largest = left;
largest = right;
if (largest != i) {
std::swap(arr[i], arr[largest]);
heapify(arr, n, largest);
int n = arr.size();
heapify(arr, n, i);
std::swap(arr[0], arr[i]);
heapify(arr, i, 0);
int main() {
heapSort(arr);
return 0;
}
9. Write C++ programs that use recursive functions to traverse the given binary tree in a)
Preorder b) inorder and c) postorder
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
newNode->data = data;
return newNode;
if (root != nullptr) {
std::cout << root->data << " "; // Process the current node
}
// Function for inorder traversal of a binary tree
if (root != nullptr) {
std::cout << root->data << " "; // Process the current node
if (root != nullptr) {
std::cout << root->data << " "; // Process the current node
int main() {
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->left = createNode(6);
root->right->right = createNode(7);
preorderTraversal(root);
inorderTraversal(root);
postorderTraversal(root);
return 0;
10. Write a C++ program to perform the following operations a) Insertion into a B-tree b)
Deletion from a B-tree
#include <iostream>
#include <vector>
struct BTreeNode {
std::vector<int> keys;
std::vector<BTreeNode*> children;
bool isLeaf;
};
int i = 0;
++i;
return i;
child->keys.erase(child->keys.begin() + t, child->keys.end());
if (!child->isLeaf) {
newChild->children.assign(child->children.begin() + t, child->children.end());
child->children.erase(child->children.begin() + t, child->children.end());
if (root == nullptr) {
root = createNode(true);
root->keys.push_back(key);
} else {
if (root->keys.size() == 2 * t - 1) {
newRoot->children.push_back(root);
splitChild(newRoot, 0, root);
root = newRoot;
insertNonFull(root, key);
if (node->isLeaf) {
node->keys[i + 1] = node->keys[i];
--i;
node->keys[i + 1] = key;
} else {
--i;
if (node->children[i]->keys.size() == 2 * t - 1) {
splitChild(node, i, node->children[i]);
++i;
insertNonFull(node->children[i], key);
return;
delete root;
root = newRoot;
if (node->isLeaf) {
node->keys.erase(node->keys.begin() + index);
} else {
node->keys[index] = predecessorKey;
} else {
if (child->keys.size() < t) {
borrowFromLeftSibling(node, index);
} else {
borrowFromRightSibling(node, index);
} else {
mergeWithSibling(node, index);
child = node->children[index];
}
return node;
node->keys[index - 1] = leftSibling->keys.back();
if (!child->isLeaf) {
// Move the child pointer from the left sibling to the child
child->children.insert(child->children.begin(), leftSibling->children.back());
leftSibling->children.pop_back();
// Move the last key from the left sibling to the parent
leftSibling->keys.pop_back();
child->keys.push_back(node->keys[index]);
node->keys[index] = right
11. Write a C++ program to perform the following operations a)Insertion into an AVL-tree b)
Deletion from an AVL-tree
#include <iostream>
#include <algorithm>
struct AVLNode {
int key;
AVLNode* left;
AVLNode* right;
int height;
};
newNode->key = key;
return newNode;
// Function to update the height of a node based on the heights of its children
AVLNode* rightRotate(AVLNode* y) {
AVLNode* x = y->left;
AVLNode* T2 = x->right;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
updateHeight(y);
updateHeight(x);
return x;
AVLNode* leftRotate(AVLNode* x) {
AVLNode* y = x->right;
AVLNode* T2 = y->left;
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
updateHeight(x);
updateHeight(y);
return y;
if (root == nullptr) {
return createNode(key);
} else {
return root;
updateHeight(root);
return rightRotate(root);
return leftRotate(root);
root->left = leftRotate(root->left);
return rightRotate(root);
}
root->right = rightRotate(root->right);
return leftRotate(root);
// No rotation needed
return root;
// Function to find the node with the minimum key value in a tree
current = current->left;
return current;
if (root == nullptr) {
return root;
}
if (key < root->key) {
} else {
// No child case
if (temp == nullptr) {
temp = root;
root = nullptr;
} else {
delete temp;
} else {
root->key = temp->key;
if (root == nullptr) {
return root;
updateHeight(root);
return rightRotate(root);
root->left = leftRotate(root->left);
return rightRotate(root);
root->right = rightRotate(root->right);
return leftRotate(root);
// No rotation needed
return root;
if (root != nullptr) {
inorderTraversal(root->left);
inorderTraversal(root->right);
int main() {
// Insertion
inorderTraversal(root);
// Deletion
root =
12. Write a C++ program to implement all the functions of a dictionary (ADT)
#include <iostream>
#include <list>
#include <vector>
struct KeyValuePair {
int key;
std::string value;
};
class Dictionary {
private:
std::vector<std::list<KeyValuePair>> table;
size_t size;
public:
table.resize(tableSize);
if (pair.key == key) {
std::cout << "Key " << key << " already exists. Updating value." << std::endl;
pair.value = value;
return;
}
}
table[index].push_back(newPair);
++size;
std::cout << "Key " << key << " inserted successfully." << std::endl;
// Function to remove a key and its associated value from the dictionary
if (it->key == key) {
bucket.erase(it);
--size;
std::cout << "Key " << key << " removed successfully." << std::endl;
return;
std::cout << "Key " << key << " not found in the dictionary." << std::endl;
if (pair.key == key) {
return pair.value;
std::cout << "(" << pair.key << ", " << pair.value << ") ";
};
int main() {
Dictionary dictionary(10);
dictionary.insert(1, "One");
dictionary.insert(2, "Two");
dictionary.insert(3, "Three");
dictionary.insert(11, "Eleven");
dictionary.insert(22, "Twenty-Two");
dictionary.display();
std::cout << "Value for key 2: " << dictionary.search(2) << std::endl;
std::cout << "Value for key 5: " << dictionary.search(5) << std::endl;
dictionary.remove(3);
dictionary.remove(5);
dictionary.display();
return 0;