diff --git a/CPP/SortingTechniques/BubbleSort.c b/C/SortingTechniques/BubbleSort.c similarity index 100% rename from CPP/SortingTechniques/BubbleSort.c rename to C/SortingTechniques/BubbleSort.c diff --git a/CPP/SortingTechniques/BucketSort.c b/C/SortingTechniques/BucketSort.c similarity index 100% rename from CPP/SortingTechniques/BucketSort.c rename to C/SortingTechniques/BucketSort.c diff --git a/CPP/SortingTechniques/CountSort b/C/SortingTechniques/CountSort similarity index 100% rename from CPP/SortingTechniques/CountSort rename to C/SortingTechniques/CountSort diff --git a/CPP/SortingTechniques/CountSort.c b/C/SortingTechniques/CountSort.c similarity index 100% rename from CPP/SortingTechniques/CountSort.c rename to C/SortingTechniques/CountSort.c diff --git a/CPP/SortingTechniques/InsertionSort b/C/SortingTechniques/InsertionSort similarity index 100% rename from CPP/SortingTechniques/InsertionSort rename to C/SortingTechniques/InsertionSort diff --git a/CPP/SortingTechniques/InsertionSort.c b/C/SortingTechniques/InsertionSort.c similarity index 100% rename from CPP/SortingTechniques/InsertionSort.c rename to C/SortingTechniques/InsertionSort.c diff --git a/CPP/SortingTechniques/MergeSort b/C/SortingTechniques/MergeSort similarity index 100% rename from CPP/SortingTechniques/MergeSort rename to C/SortingTechniques/MergeSort diff --git a/CPP/SortingTechniques/MergeSort.c b/C/SortingTechniques/MergeSort.c similarity index 100% rename from CPP/SortingTechniques/MergeSort.c rename to C/SortingTechniques/MergeSort.c diff --git a/CPP/SortingTechniques/QuickSort b/C/SortingTechniques/QuickSort similarity index 100% rename from CPP/SortingTechniques/QuickSort rename to C/SortingTechniques/QuickSort diff --git a/CPP/SortingTechniques/QuickSort.c b/C/SortingTechniques/QuickSort.c similarity index 100% rename from CPP/SortingTechniques/QuickSort.c rename to C/SortingTechniques/QuickSort.c diff --git a/CPP/SortingTechniques/SelectionSort b/C/SortingTechniques/SelectionSort similarity index 100% rename from CPP/SortingTechniques/SelectionSort rename to C/SortingTechniques/SelectionSort diff --git a/CPP/SortingTechniques/SelectionSort.c b/C/SortingTechniques/SelectionSort.c similarity index 100% rename from CPP/SortingTechniques/SelectionSort.c rename to C/SortingTechniques/SelectionSort.c diff --git a/CPP/SortingTechniques/ShellSort b/C/SortingTechniques/ShellSort similarity index 100% rename from CPP/SortingTechniques/ShellSort rename to C/SortingTechniques/ShellSort diff --git a/CPP/SortingTechniques/ShellSort.c b/C/SortingTechniques/ShellSort.c similarity index 100% rename from CPP/SortingTechniques/ShellSort.c rename to C/SortingTechniques/ShellSort.c diff --git a/CPP/Data Structures/Stack Using Arrays/stackClass.cpp b/CPP/Data Structures/Stack Using Arrays/stackClass.cpp index 5030e7f55..b7325be88 100644 --- a/CPP/Data Structures/Stack Using Arrays/stackClass.cpp +++ b/CPP/Data Structures/Stack Using Arrays/stackClass.cpp @@ -14,6 +14,10 @@ class stack { this -> size = size; p = new t[size]; } + + ~stack(){ + delete [] p; + } int getSize(){ return size; diff --git a/CPP/Data Structures/queue/queue.cpp b/CPP/Data Structures/queue/queue.cpp new file mode 100644 index 000000000..030c9429e --- /dev/null +++ b/CPP/Data Structures/queue/queue.cpp @@ -0,0 +1,33 @@ +#include +#include "queueClass.cpp" + +using namespace std; + +// dynamic queue, changes its size according to input + +int main() { + Queue q1(5); + for (int i = 0; i < 5; i++) { + q1.enqueue(i + 1); + } + cout << "Dequeued: " << q1.dequeue() << endl; + cout << "Dequeued: " << q1.dequeue() << endl; + q1.enqueue(6); + q1.enqueue(7); + while (!q1.empty()) { + cout << "Dequeued: " << q1.dequeue() << endl; + } + cout << "Current size of queue: " << q1.size() << endl; + + Queue q2(5); + q2.enqueue('h'); + q2.enqueue('e'); + q2.enqueue('l'); + q2.enqueue('l'); + q2.enqueue('o'); + while (!q2.empty()) { + cout << "Dequeued: " << q2.dequeue() << endl; + } + + return 0; +} \ No newline at end of file diff --git a/CPP/Data Structures/queue/queueClass.cpp b/CPP/Data Structures/queue/queueClass.cpp new file mode 100644 index 000000000..8d9e69b2d --- /dev/null +++ b/CPP/Data Structures/queue/queueClass.cpp @@ -0,0 +1,82 @@ +#include +#include + +using namespace std; + +template +class Queue { + T *arr; + int head, tail; + int sz; + int capacity; + + public: + Queue(int capacity = 10) { + this->capacity = capacity; + sz = 0; + head = 0; + tail = 0; + arr = new T[capacity]; + } + + int size() { + return sz; + } + + bool empty() { + return sz == 0; + } + + bool full() { + return sz == capacity; + } + + void resize(int cap) { + if(cap < 1) cap = 1; + T* narr = new T[cap]; + + for(int i=0; i + +using namespace std; + +class Node{ + public: + int data; + Node *next; +}; + +class LinkedList{ + private: + Node *first, *last; + + public: + LinkedList(){first = last = NULL;} + ~LinkedList(); + void Insert(int x); + int Delete(); + bool isEmpty(); + void Sort(); +}; + +LinkedList :: ~LinkedList() +{ + Node *p; + while(first){ + first = first->next; + delete p; + } + first = last = NULL; +} + +void LinkedList :: Insert(int x) +{ + Node *t = new Node; + t->data = x; + t->next = NULL; + if(first == NULL) + first = last = t; + else{ + last->next = t; + last = last->next; + } +} + +int LinkedList :: Delete() +{ + if(first == NULL) return -1; + Node * tr = first; + first = first->next; + int x = tr->data; + delete tr; + return x; +} + +bool LinkedList :: isEmpty() +{ + return first == NULL; +} + +void LinkedList :: Sort() +{ + if(!first || !first->next) return; // Array is empty or has one element + Node* sorted = nullptr; + Node* current = first; + while (current != nullptr) { + Node* next = current->next; + if(sorted == nullptr || sorted->data >= current->data) { + current->next = sorted; + sorted = current; + } else { + Node* temp = sorted; + while(temp->next != nullptr && temp->next->data < current->data) { + temp = temp->next; + } + current->next = temp->next; + temp->next = current; + } + current = next; + } + first = sorted; + + // reupdate last pointer after sorting + last = first; + while(last && last->next) { + last = last->next; + } +} + +int Max(int A[], int n) +{ + int mx = INT32_MIN; + for(int i=0; i < n; i++) + if(mx < A[i]) mx = A[i]; + return mx; +} + +void BucketSort(int A[], int n) +{ + int max = Max(A, n); + // Create n buckets (linked lists) + LinkedList *bin = new LinkedList[n]; + + // Put array elements in different buckets + for(int i=0; i < n; i++) + { + int bin_idx = (long long)n * A[i] / (max + 1); + bin[bin_idx].Insert(A[i]); + } + + // Sort individual buckets and concatenate + int i=0; + for(int j=0; j < n; j++) + { + bin[j].Sort(); // Sort each bucket + while(!bin[j].isEmpty()) + A[i++] = bin[j].Delete(); + } + delete []bin; +} + +int main() +{ + int A[] = {237, 140, 259, 348, 152, 163, 1244, 48, 36, 62}; + int n = sizeof(A)/sizeof(A[0]); + + BucketSort(A, n); + //Display + cout << "Sorted List : "; + for(int i=0; i < n; i++) + cout << A[i] << " "; + cout << endl; + + return 0; +} \ No newline at end of file