diff --git a/CMakeLists.txt b/CMakeLists.txt index 393bfd8..28aab45 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ project(DataStructures_CPP) set(CMAKE_CXX_STANDARD 17) -add_library(DataStructures_CPP src/List/Node.cpp src/List/Node.h src/List/LinkedList.cpp src/List/LinkedList.h src/List/DoublyNode.cpp src/List/DoublyNode.h src/List/DoublyLinkedList.cpp src/List/DoublyLinkedList.h src/Array/Element.cpp src/Array/Element.h src/Array/Stack.cpp src/Array/Stack.h src/List/Stack.cpp src/List/Stack.h src/Array/Queue.cpp src/Array/Queue.h src/List/Queue.cpp src/List/Queue.h src/Tree/Tree.cpp src/Tree/Tree.h src/Tree/TreeNode.cpp src/Tree/TreeNode.h src/Tree/Element.cpp src/Tree/Element.h src/Tree/Stack.cpp src/Tree/Stack.h src/Tree/Queue.cpp src/Tree/Queue.h src/Tree/AvlTree.cpp src/Tree/AvlTree.h src/Tree/AvlTreeNode.cpp src/Tree/AvlTreeNode.h src/Tree/BTree.cpp src/Tree/BTree.h src/Tree/BTreeNode.cpp src/Tree/BTreeNode.h src/Array/Hash.cpp src/Array/Hash.h src/List/Hash.cpp src/List/Hash.h src/Array/Heap/Heap.cpp src/Array/Heap/Heap.h src/Array/Heap/HeapNode.cpp src/Array/Heap/HeapNode.h src/Array/Heap/DHeap.cpp src/Array/Heap/DHeap.h src/Array/Set.cpp src/Array/Set.h src/Array/DisjointSet.cpp src/Array/DisjointSet.h src/Array/Graph/Graph.cpp src/Array/Graph/Graph.h src/List/Graph/Edge.cpp src/List/Graph/Edge.h src/List/Graph/EdgeList.cpp src/List/Graph/EdgeList.h src/List/Graph/Graph.cpp src/List/Graph/Graph.h src/General/Path.cpp src/General/Path.h src/General/AbstractGraph.cpp src/General/AbstractGraph.h src/Array/Heap/MaxHeap.cpp src/Array/Heap/MaxHeap.h src/Array/Heap/MaxDHeap.cpp src/Array/Heap/MaxDHeap.h src/Array/Heap/MinHeap.cpp src/Array/Heap/MinHeap.h src/Array/Heap/MinDHeap.cpp src/Array/Heap/MinDHeap.h) +add_library(DataStructures_CPP src/List/Node.cpp src/List/Node.h src/List/LinkedList.cpp src/List/LinkedList.h src/List/DoublyNode.cpp src/List/DoublyNode.h src/List/DoublyLinkedList.cpp src/List/DoublyLinkedList.h src/Array/Element.cpp src/Array/Element.h src/Array/Stack.cpp src/Array/Stack.h src/List/Stack.cpp src/List/Stack.h src/Array/Queue.cpp src/Array/Queue.h src/List/Queue.cpp src/List/Queue.h src/Tree/Tree.cpp src/Tree/Tree.h src/Tree/TreeNode.cpp src/Tree/TreeNode.h src/Tree/Element.cpp src/Tree/Element.h src/Tree/Stack.cpp src/Tree/Stack.h src/Tree/Queue.cpp src/Tree/Queue.h src/Tree/AvlTree.cpp src/Tree/AvlTree.h src/Tree/AvlTreeNode.cpp src/Tree/AvlTreeNode.h src/Tree/BTree.cpp src/Tree/BTree.h src/Tree/BTreeNode.cpp src/Tree/BTreeNode.h src/Array/Hash.cpp src/Array/Hash.h src/List/Hash.cpp src/List/Hash.h src/Array/Heap/Heap.cpp src/Array/Heap/Heap.h src/Array/Heap/HeapNode.cpp src/Array/Heap/HeapNode.h src/Array/Heap/DHeap.cpp src/Array/Heap/DHeap.h src/Array/Set.cpp src/Array/Set.h src/Array/DisjointSet.cpp src/Array/DisjointSet.h src/Array/Graph/Graph.cpp src/Array/Graph/Graph.h src/List/Graph/Edge.cpp src/List/Graph/Edge.h src/List/Graph/EdgeList.cpp src/List/Graph/EdgeList.h src/List/Graph/Graph.cpp src/List/Graph/Graph.h src/General/Path.cpp src/General/Path.h src/General/AbstractGraph.cpp src/General/AbstractGraph.h src/Array/Heap/MaxHeap.cpp src/Array/Heap/MaxHeap.h src/Array/Heap/MaxDHeap.cpp src/Array/Heap/MaxDHeap.h src/Array/Heap/MinHeap.cpp src/Array/Heap/MinHeap.h src/Array/Heap/MinDHeap.cpp src/Array/Heap/MinDHeap.h src/Array/Sort/Sort.h src/Array/Sort/BubbleSort.cpp src/Array/Sort/BubbleSort.h src/Array/Sort/BucketSort.cpp src/Array/Sort/BucketSort.h src/Array/Sort/HeapSort.cpp src/Array/Sort/HeapSort.h src/Array/Sort/InsertionSort.cpp src/Array/Sort/InsertionSort.h src/Array/Sort/MergeSort.cpp src/Array/Sort/MergeSort.h src/Array/Sort/QuickSort.cpp src/Array/Sort/QuickSort.h src/Array/Sort/SelectionSort.cpp src/Array/Sort/SelectionSort.h src/Array/Sort/ShellSort.cpp src/Array/Sort/ShellSort.h) add_executable(TestLinkedList src/List/Node.cpp src/List/Node.h src/List/LinkedList.cpp src/List/LinkedList.h Test/LinkedListTest.cpp src/List/DoublyNode.cpp src/List/DoublyNode.h src/List/DoublyLinkedList.cpp src/List/DoublyLinkedList.h src/Array/Element.cpp src/Array/Element.h src/Array/Stack.cpp src/Array/Stack.h src/List/Stack.cpp src/List/Stack.h) add_executable(MathematicalExpression src/Array/MathematicalExpression/TokenType.h src/Array/MathematicalExpression/Token.cpp src/Array/MathematicalExpression/Token.h src/Array/MathematicalExpression/Element.h src/Array/MathematicalExpression/Element.cpp src/Array/MathematicalExpression/Stack.h src/Array/MathematicalExpression/Stack.cpp src/Array/MathematicalExpression/Evaluate.cpp) add_executable(Polynomial src/List/Polynomial/TestPolynomial.cpp src/List/Polynomial/Term.cpp src/List/Polynomial/Term.h src/List/Polynomial/TermNode.cpp src/List/Polynomial/TermNode.h src/List/Polynomial/Polynomial.cpp src/List/Polynomial/Polynomial.h) diff --git a/src/Array/Sort/BubbleSort.cpp b/src/Array/Sort/BubbleSort.cpp new file mode 100644 index 0000000..90530d9 --- /dev/null +++ b/src/Array/Sort/BubbleSort.cpp @@ -0,0 +1,20 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#include "BubbleSort.h" + +void BubbleSort::sort(int *A, int size) { + bool exchanged = true; + while (exchanged){ + exchanged = false; + for (int i = 0; i < size - 1; i++){ + if (A[i] > A[i + 1]){ + exchanged = true; + int tmp = A[i]; + A[i] = A[i + 1]; + A[i + 1] = tmp; + } + } + } +} diff --git a/src/Array/Sort/BubbleSort.h b/src/Array/Sort/BubbleSort.h new file mode 100644 index 0000000..c59809e --- /dev/null +++ b/src/Array/Sort/BubbleSort.h @@ -0,0 +1,17 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#ifndef DATASTRUCTURES_CPP_BUBBLESORT_H +#define DATASTRUCTURES_CPP_BUBBLESORT_H + + +#include "Sort.h" + +class BubbleSort : public Sort{ +public: + void sort(int* A, int size) override; +}; + + +#endif //DATASTRUCTURES_CPP_BUBBLESORT_H diff --git a/src/Array/Sort/BucketSort.cpp b/src/Array/Sort/BucketSort.cpp new file mode 100644 index 0000000..bf0bf7d --- /dev/null +++ b/src/Array/Sort/BucketSort.cpp @@ -0,0 +1,29 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#include "BucketSort.h" + +BucketSort::BucketSort(int maxValue) { + this->maxValue = maxValue; +} + +void BucketSort::sort(int *A, int size) { + int* C = new int[maxValue]; + int* B = new int[size]; + for (int i = 0; i < size; i++){ + C[A[i]]++; + } + for (int i = 1; i < maxValue; i++){ + C[i] += C[i - 1]; + } + for (int i = size - 1; i >= 0; i--){ + B[C[A[i]] - 1] = A[i]; + C[A[i]]--; + } + for (int i = 0; i < size; i++){ + A[i] = B[i]; + } + delete[] C; + delete[] B; +} diff --git a/src/Array/Sort/BucketSort.h b/src/Array/Sort/BucketSort.h new file mode 100644 index 0000000..6336674 --- /dev/null +++ b/src/Array/Sort/BucketSort.h @@ -0,0 +1,20 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#ifndef DATASTRUCTURES_CPP_BUCKETSORT_H +#define DATASTRUCTURES_CPP_BUCKETSORT_H + + +#include "Sort.h" + +class BucketSort : public Sort{ +private: + int maxValue; +public: + explicit BucketSort(int maxValue); + void sort(int* A, int size) override; +}; + + +#endif //DATASTRUCTURES_CPP_BUCKETSORT_H diff --git a/src/Array/Sort/HeapSort.cpp b/src/Array/Sort/HeapSort.cpp new file mode 100644 index 0000000..809bb61 --- /dev/null +++ b/src/Array/Sort/HeapSort.cpp @@ -0,0 +1,18 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#include "HeapSort.h" +#include "../Heap/MinHeap.h" + +void HeapSort::sort(int *A, int size) { + MinHeap heap = MinHeap(size); + for (int i = 0; i < size; i++){ + HeapNode heapNode = HeapNode(A[i], i); + heap.insert(heapNode); + } + for (int i = 0; i < size; i++){ + HeapNode heapNode = heap.deleteTop(); + A[i] = heapNode.getData(); + } +} diff --git a/src/Array/Sort/HeapSort.h b/src/Array/Sort/HeapSort.h new file mode 100644 index 0000000..afc2009 --- /dev/null +++ b/src/Array/Sort/HeapSort.h @@ -0,0 +1,17 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#ifndef DATASTRUCTURES_CPP_HEAPSORT_H +#define DATASTRUCTURES_CPP_HEAPSORT_H + + +#include "Sort.h" + +class HeapSort : public Sort{ +public: + void sort(int* A, int size) override; +}; + + +#endif //DATASTRUCTURES_CPP_HEAPSORT_H diff --git a/src/Array/Sort/InsertionSort.cpp b/src/Array/Sort/InsertionSort.cpp new file mode 100644 index 0000000..ed109ba --- /dev/null +++ b/src/Array/Sort/InsertionSort.cpp @@ -0,0 +1,17 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#include "InsertionSort.h" + +void InsertionSort::sort(int *A, int size) { + for (int j = 1; j < size; j++){ + int t = A[j]; + int i = j - 1; + while (i >= 0 && A[i] > t){ + A[i + 1] = A[i]; + i--; + } + A[i + 1] = t; + } +} diff --git a/src/Array/Sort/InsertionSort.h b/src/Array/Sort/InsertionSort.h new file mode 100644 index 0000000..302c5ce --- /dev/null +++ b/src/Array/Sort/InsertionSort.h @@ -0,0 +1,17 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#ifndef DATASTRUCTURES_CPP_INSERTIONSORT_H +#define DATASTRUCTURES_CPP_INSERTIONSORT_H + + +#include "Sort.h" + +class InsertionSort : public Sort{ +public: + void sort(int* A, int size) override; +}; + + +#endif //DATASTRUCTURES_CPP_INSERTIONSORT_H diff --git a/src/Array/Sort/MergeSort.cpp b/src/Array/Sort/MergeSort.cpp new file mode 100644 index 0000000..f24b444 --- /dev/null +++ b/src/Array/Sort/MergeSort.cpp @@ -0,0 +1,46 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#include +#include "MergeSort.h" + +void MergeSort::merge(int *A, int start, int middle, int end) { + int leftCount = middle - start + 1; + int rightCount = end - middle; + int* leftPart = new int[leftCount + 1]; + int* rightPart = new int[rightCount + 1]; + for (int i = 0; i < leftCount; i++){ + leftPart[i] = A[start + i]; + } + for (int i = 0; i < rightCount; i++){ + rightPart[i] = A[middle + i]; + } + leftPart[leftCount] = INT_MAX; + rightPart[rightCount] = INT_MAX; + int i = 0, j = 0; + for (int k = start; k <= end; k++){ + if (leftPart[i] <= rightPart[j]){ + A[k] = leftPart[i]; + i++; + } else { + A[k] = rightPart[j]; + j++; + } + } + delete[] leftPart; + delete[] rightPart; +} + +void MergeSort::mergeSort(int *A, int first, int last) { + if (first < last){ + int pivot = (first + last) / 2; + mergeSort(A, first, pivot); + mergeSort(A, pivot + 1, last); + merge(A, first, pivot, last); + } +} + +void MergeSort::sort(int *A, int size) { + mergeSort(A, 0, size - 1); +} diff --git a/src/Array/Sort/MergeSort.h b/src/Array/Sort/MergeSort.h new file mode 100644 index 0000000..8bf4d48 --- /dev/null +++ b/src/Array/Sort/MergeSort.h @@ -0,0 +1,20 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#ifndef DATASTRUCTURES_CPP_MERGESORT_H +#define DATASTRUCTURES_CPP_MERGESORT_H + + +#include "Sort.h" + +class MergeSort : public Sort{ +private: + void merge(int* A, int start, int middle, int end); + void mergeSort(int* A, int first, int last); +public: + void sort(int* A, int size) override; +}; + + +#endif //DATASTRUCTURES_CPP_MERGESORT_H diff --git a/src/Array/Sort/QuickSort.cpp b/src/Array/Sort/QuickSort.cpp new file mode 100644 index 0000000..0f9a392 --- /dev/null +++ b/src/Array/Sort/QuickSort.cpp @@ -0,0 +1,34 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#include "QuickSort.h" + +int QuickSort::partition(int* A, int first, int last) { + int x = A[last]; + int i = first - 1; + for (int j = first; j < last; j++){ + if (A[j] <= x){ + i++; + int tmp = A[i]; + A[i] = A[j]; + A[j] = tmp; + } + } + int tmp = A[i + 1]; + A[i + 1] = A[last]; + A[last] = tmp; + return i + 1; +} + +void QuickSort::quickSort(int *A, int first, int last) { + if (first < last){ + int pivot = partition(A, first, last); + quickSort(A, first, pivot - 1); + quickSort(A, pivot + 1, last); + } +} + +void QuickSort::sort(int *A, int size) { + quickSort(A, 0, size - 1); +} diff --git a/src/Array/Sort/QuickSort.h b/src/Array/Sort/QuickSort.h new file mode 100644 index 0000000..bd99be4 --- /dev/null +++ b/src/Array/Sort/QuickSort.h @@ -0,0 +1,20 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#ifndef DATASTRUCTURES_CPP_QUICKSORT_H +#define DATASTRUCTURES_CPP_QUICKSORT_H + + +#include "Sort.h" + +class QuickSort : public Sort{ +private: + int partition(int* A, int first, int last); + void quickSort(int* A, int first, int last); +public: + void sort(int* A, int size) override; +}; + + +#endif //DATASTRUCTURES_CPP_QUICKSORT_H diff --git a/src/Array/Sort/SelectionSort.cpp b/src/Array/Sort/SelectionSort.cpp new file mode 100644 index 0000000..b1135e5 --- /dev/null +++ b/src/Array/Sort/SelectionSort.cpp @@ -0,0 +1,22 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#include "SelectionSort.h" + +void SelectionSort::sort(int *A, int size) { + for (int i = 0; i < size - 1; i++){ + int min = A[i]; + int pos = i; + for (int j = i + 1; j < size; j++){ + if (A[j]< min){ + min = A[j]; + pos = j; + } + } + if (pos != i){ + A[pos] = A[i]; + A[i] = min; + } + } +} diff --git a/src/Array/Sort/SelectionSort.h b/src/Array/Sort/SelectionSort.h new file mode 100644 index 0000000..7783840 --- /dev/null +++ b/src/Array/Sort/SelectionSort.h @@ -0,0 +1,17 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#ifndef DATASTRUCTURES_CPP_SELECTIONSORT_H +#define DATASTRUCTURES_CPP_SELECTIONSORT_H + + +#include "Sort.h" + +class SelectionSort : public Sort{ +public: + void sort(int* A, int size) override; +}; + + +#endif //DATASTRUCTURES_CPP_SELECTIONSORT_H diff --git a/src/Array/Sort/ShellSort.cpp b/src/Array/Sort/ShellSort.cpp new file mode 100644 index 0000000..5199704 --- /dev/null +++ b/src/Array/Sort/ShellSort.cpp @@ -0,0 +1,25 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#include "ShellSort.h" + +ShellSort::ShellSort(int *H, int incrementSize) { + this->H = H; + this->incrementSize = incrementSize; +} + +void ShellSort::sort(int *A, int size) { + for (int k = 0; k < incrementSize; k++){ + int increment = H[k]; + for (int j = increment; j < size; j++){ + int t = A[j]; + int i = j - increment; + while (i >= 0 && A[i] > t){ + A[i + increment] = A[i]; + i -= increment; + } + A[i + increment] = t; + } + } +} diff --git a/src/Array/Sort/ShellSort.h b/src/Array/Sort/ShellSort.h new file mode 100644 index 0000000..c6054e1 --- /dev/null +++ b/src/Array/Sort/ShellSort.h @@ -0,0 +1,21 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#ifndef DATASTRUCTURES_CPP_SHELLSORT_H +#define DATASTRUCTURES_CPP_SHELLSORT_H + + +#include "Sort.h" + +class ShellSort : public Sort{ +private: + int* H; + int incrementSize; +public: + ShellSort(int* H, int incrementSize); + void sort(int* A, int size) override; +}; + + +#endif //DATASTRUCTURES_CPP_SHELLSORT_H diff --git a/src/Array/Sort/Sort.h b/src/Array/Sort/Sort.h new file mode 100644 index 0000000..48c6b1b --- /dev/null +++ b/src/Array/Sort/Sort.h @@ -0,0 +1,14 @@ +// +// Created by Olcay Taner YILDIZ on 27.05.2023. +// + +#ifndef DATASTRUCTURES_CPP_SORT_H +#define DATASTRUCTURES_CPP_SORT_H + + +class Sort { + virtual void sort(int* A, int size) = 0; +}; + + +#endif //DATASTRUCTURES_CPP_SORT_H