DataStructure using C++
DataStructure using C++
// Node structure
struct Node {
int data;
Node* next;
};
LinkedList() {
head = nullptr;
}
if (head == nullptr) {
head = newNode;
} else {
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = newNode;
}
}
// Search for the key and keep track of the previous node
while (temp != nullptr && temp->data != key) {
prev = temp;
temp = temp->next;
}
int main() {
LinkedList list;
int choice, value;
do {
cout << "\nLinked List Operations:\n";
cout << "1. Insert at the end\n";
cout << "2. Insert at the beginning\n";
cout << "3. Delete a node\n";
cout << "4. Search for a node\n";
cout << "5. Display the list\n";
cout << "6. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to insert: ";
cin >> value;
list.insertAtEnd(value);
break;
case 2:
cout << "Enter the value to insert: ";
cin >> value;
list.insertAtBeginning(value);
break;
case 3:
cout << "Enter the value to delete: ";
cin >> value;
list.deleteNode(value);
break;
case 4:
cout << "Enter the value to search: ";
cin >> value;
if (list.search(value)) {
cout << "Element found in the list" << endl;
} else {
cout << "Element not found in the list" << endl;
}
break;
case 5:
list.display();
break;
case 6:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 6);
return 0;
}
Output :-
Program – 2
class Stack {
int top;
public:
int arr[MAX]; // Stack array
int main() {
Stack stack;
int choice, value;
do {
cout << "\nStack Operations:\n";
cout << "1. Push\n";
cout << "2. Pop\n";
cout << "3. Peek\n";
cout << "4. Display\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the 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 is: " << value << endl;
}
break;
case 4:
stack.display();
break;
case 5:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 5);
return 0;
}
Output :-
Program – 3
class Queue {
int front, rear;
int arr[MAX];
public:
Queue() {
front = -1;
rear = -1;
}
int main() {
Queue queue;
int choice, value;
do {
cout << "\nQueue Operations:\n";
cout << "1. Enqueue\n";
cout << "2. Dequeue\n";
cout << "3. Peek\n";
cout << "4. Display\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the value to enqueue: ";
cin >> value;
queue.enqueue(value);
break;
case 2:
queue.dequeue();
break;
case 3:
value = queue.peek();
if (value != -1) {
cout << "Front element is: " << value << endl;
}
break;
case 4:
queue.display();
break;
case 5:
cout << "Exiting..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
}
} while (choice != 5);
return 0;
}
Output :-
Program – 4
Aim :- write a program to implement binary search tree with their operation.
Code :-
#include <iostream>
using namespace std;
public:
BST() : root(nullptr) {}
void inorder() {
inorder(root);
cout << endl;
}
void preorder() {
preorder(root);
cout << endl;
}
void postorder() {
postorder(root);
cout << endl;
}
};
int main() {
BST bst;
// Deleting a value
cout << "Deleting 70...\n";
bst.deleteNode(70);
return 0;
}
Output :-
Program – 5
int main() {
int n;
int arr[n];
return 0;
}
Output :-
Program – 6
int main() {
int n;
int arr[n];
return 0;
}
Output :-
Program – 7
int main() {
int n, key;
int arr[n];
// Get the elements of the array
cout << "Enter the elements of the array: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
return 0;
}
Output :-
Program – 8
int main() {
int n, key;
int arr[n];
return 0;
}
Output :-
Program – 9
public:
// Constructor
Graph(int V) {
vertices = V;
adjList.resize(V);
}
// Main function
int main() {
int V = 5; // Number of vertices
Graph graph(V);
// Adding edges
graph.addEdge(0, 1);
graph.addEdge(0, 4);
graph.addEdge(1, 2);
graph.addEdge(1, 3);
graph.addEdge(1, 4);
graph.addEdge(2, 3);
graph.addEdge(3, 4);
// Print the graph
graph.printGraph();
return 0;
}
Output :-
Program – 10
Aim :- Write a program to implement graph traversing using BFS and DFS.
Code :-
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
// Graph class
class Graph {
int vertices; // Number of vertices
vector<vector<int>> adjList; // Adjacency list
public:
// Constructor
Graph(int v) {
vertices = v;
adjList.resize(v);
}
if (!visited[node]) {
cout << node << " ";
visited[node] = true;
}
// Main function
int main() {
Graph g(6); // Create a graph with 6 vertices
// Adding edges
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 3);
g.addEdge(1, 4);
g.addEdge(2, 4);
g.addEdge(3, 4);
g.addEdge(3, 5);
return 0;
}
Output :-