Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add 4 folder with code #6812

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions code/data_structures/test/graph/graph.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
#include <list>
using namespace std;

class Graph {
int V;
list<int>* adj;
public:
Graph(int V) {
this->V = V;
adj = new list<int>[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<int> 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;
}
};
67 changes: 67 additions & 0 deletions code/data_structures/test/heap/heap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <iostream>
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;
}
};
41 changes: 41 additions & 0 deletions code/data_structures/test/queue/Queue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>
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;
}
};
35 changes: 35 additions & 0 deletions code/data_structures/test/set/set.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <iostream>
#include <set>
using namespace std;

int main() {
set<int> 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;
}
40 changes: 40 additions & 0 deletions code/data_structures/test/stack/stack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <iostream>
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;
}
};