From 8fbfd0440ad03d7aaba778eadabd58eb492c7f30 Mon Sep 17 00:00:00 2001 From: jayambe36 Date: Wed, 16 Oct 2024 03:43:44 +0530 Subject: [PATCH] Relevant message --- code/data_structures/test/graph/graph.cpp | 41 ++++++++++++++ code/data_structures/test/heap/heap.cpp | 67 +++++++++++++++++++++++ code/data_structures/test/queue/Queue.cpp | 41 ++++++++++++++ code/data_structures/test/set/set.cpp | 35 ++++++++++++ code/data_structures/test/stack/stack.cpp | 40 ++++++++++++++ 5 files changed, 224 insertions(+) create mode 100644 code/data_structures/test/graph/graph.cpp create mode 100644 code/data_structures/test/heap/heap.cpp create mode 100644 code/data_structures/test/queue/Queue.cpp create mode 100644 code/data_structures/test/set/set.cpp create mode 100644 code/data_structures/test/stack/stack.cpp diff --git a/code/data_structures/test/graph/graph.cpp b/code/data_structures/test/graph/graph.cpp new file mode 100644 index 0000000000..8b0a48ef70 --- /dev/null +++ b/code/data_structures/test/graph/graph.cpp @@ -0,0 +1,41 @@ +#include +#include +using namespace std; + +class Graph { + int V; + list* adj; +public: + Graph(int V) { + this->V = V; + adj = new list[V]; + } + + void addEdge(int v, int w) { + adj[v].push_back(w); + } + + void BFS(int s) { + bool* visited = new bool[V]; + for (int i = 0; i < V; i++) + visited[i] = false; + + list queue; + visited[s] = true; + queue.push_back(s); + + while (!queue.empty()) { + s = queue.front(); + cout << s << " "; + queue.pop_front(); + + for (auto adjVertex : adj[s]) { + if (!visited[adjVertex]) { + visited[adjVertex] = true; + queue.push_back(adjVertex); + } + } + } + cout << endl; + } +}; diff --git a/code/data_structures/test/heap/heap.cpp b/code/data_structures/test/heap/heap.cpp new file mode 100644 index 0000000000..a012722b67 --- /dev/null +++ b/code/data_structures/test/heap/heap.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + +class MaxHeap { + int* heap; + int size; + int capacity; +public: + MaxHeap(int cap) { + size = 0; + capacity = cap; + heap = new int[cap]; + } + + void insert(int value) { + if (size == capacity) { + cout << "Heap is full\n"; + return; + } + int i = size++; + heap[i] = value; + + while (i != 0 && heap[(i - 1) / 2] < heap[i]) { + swap(heap[i], heap[(i - 1) / 2]); + i = (i - 1) / 2; + } + } + + void heapify(int i) { + int largest = i; + int left = 2 * i + 1; + int right = 2 * i + 2; + + if (left < size && heap[left] > heap[largest]) + largest = left; + if (right < size && heap[right] > heap[largest]) + largest = right; + if (largest != i) { + swap(heap[i], heap[largest]); + heapify(largest); + } + } + + void extractMax() { + if (size <= 0) { + cout << "Heap is empty\n"; + return; + } + if (size == 1) { + size--; + return; + } + + int root = heap[0]; + heap[0] = heap[size - 1]; + size--; + heapify(0); + + cout << "Max element: " << root << endl; + } + + void display() { + for (int i = 0; i < size; i++) + cout << heap[i] << " "; + cout << endl; + } +}; diff --git a/code/data_structures/test/queue/Queue.cpp b/code/data_structures/test/queue/Queue.cpp new file mode 100644 index 0000000000..fa5a93d075 --- /dev/null +++ b/code/data_structures/test/queue/Queue.cpp @@ -0,0 +1,41 @@ +#include +using namespace std; + +class Queue { + int front, rear, size; + int* arr; +public: + Queue(int s) { + front = rear = -1; + size = s; + arr = new int[s]; + } + + void enqueue(int value) { + if (rear == size - 1) { + cout << "Queue is full\n"; + return; + } + if (front == -1) + front = 0; + arr[++rear] = value; + } + + void dequeue() { + if (front == -1 || front > rear) { + cout << "Queue is empty\n"; + return; + } + cout << "Dequeued: " << arr[front++] << endl; + } + + void display() { + if (front == -1) { + cout << "Queue is empty\n"; + return; + } + for (int i = front; i <= rear; i++) + cout << arr[i] << " "; + cout << endl; + } +}; diff --git a/code/data_structures/test/set/set.cpp b/code/data_structures/test/set/set.cpp new file mode 100644 index 0000000000..d94b5e8f90 --- /dev/null +++ b/code/data_structures/test/set/set.cpp @@ -0,0 +1,35 @@ +#include +#include +using namespace std; + +int main() { + set mySet; + + // Inserting elements + mySet.insert(10); + mySet.insert(20); + mySet.insert(15); + mySet.insert(10); // Duplicate + + // Displaying elements + cout << "Set elements: "; + for (auto it = mySet.begin(); it != mySet.end(); ++it) + cout << *it << " "; + cout << endl; + + // Finding an element + int key = 15; + if (mySet.find(key) != mySet.end()) + cout << key << " is found in the set\n"; + else + cout << key << " is not found\n"; + + // Removing an element + mySet.erase(10); + cout << "After removing 10: "; + for (auto it = mySet.begin(); it != mySet.end(); ++it) + cout << *it << " "; + cout << endl; + + return 0; +} diff --git a/code/data_structures/test/stack/stack.cpp b/code/data_structures/test/stack/stack.cpp new file mode 100644 index 0000000000..e017dcd441 --- /dev/null +++ b/code/data_structures/test/stack/stack.cpp @@ -0,0 +1,40 @@ +#include +using namespace std; + +class Stack { + int top; + int* arr; + int size; +public: + Stack(int s) { + size = s; + arr = new int[s]; + top = -1; + } + + void push(int value) { + if (top == size - 1) { + cout << "Stack overflow\n"; + return; + } + arr[++top] = value; + } + + void pop() { + if (top == -1) { + cout << "Stack underflow\n"; + return; + } + cout << "Popped: " << arr[top--] << endl; + } + + void display() { + if (top == -1) { + cout << "Stack is empty\n"; + return; + } + for (int i = 0; i <= top; i++) + cout << arr[i] << " "; + cout << endl; + } +};