Every DS Code Ever 2nd BME

 avatar
itsLu
c_cpp
a year ago
11 kB
62
Indexable
#include <iostream>
#include <ctime>
using namespace std;

//Fill an array with random numbers:
void randArray(arr[], int arraySize, int minNum, int maxNum)
{
    for (int k = 0 ; k < arraySize ; k++)
        arr[k] = minNum + rand() % (maxNum - minNum + 1);
}

//Fill an array with random unrepeated numbers:
void unrepeatedRandArray(arr[], int arraySize, int minNum, int maxNum)
{
    int temp, k, m;
    bool isRepeated;
    for (k = 0 ; k < arraySize ; k++)
    {
        isRepeated = false;
        temp = minNum + rand() % (maxNum - minNum + 1);
        for (m = 0 ; m < k ; m++)
        {
            if arr[m] == temp
            {
                isRepeated = true;
                break;
            }
        }
        if (isRepeated == false)
            arr[k] = temp;
        else
            k--;
    }
}

//Print the array:
void printArray (arr[], int arraySize)
{
    for (int k = 0 ; k < arraySize ; k++)
        cout << arr[k] << "\t";
}

//Linear search:
int Linear_Search(int arr[], int arraySize, int key)
{
    for (int k = 0 ; k < arraySize ; k++)
    {
        if (arr[k] == key)
            return k;
    }
    return -1;
}

//Binary search function using loops:
int Binary_Search(int arr[], int arraySize, int key)
{
    int first = 0, last = arraysize - 1;
    while (first <= last)
    {
        int mid = (first + last) / 2;
        if (arr[mid] == key)
            return mid;
        else if (key < arr[mid])
            last = mid - 1;
        else
            first = mid + 1;
    }
    return -1;
}

//Binary search function without using loops (Recursion):
int recBinary_Search(int arr[], int first, int last, int key)
{
    if (first > last)
        return -1;
    int mid = (first + last) / 2;
    if (arr[mid] == key)
        return mid;
    else if (key < arr [mid])
        return recBinary_Search(arr, first, mid - 1, key);
    else
        return recBinary_Search(arr, mid + 1, last, key);
}

//Selection Sort:
void Selection_Sort(int arr[], int arraySize)
{
    int minIndex, temp;
    for (int p = 0 ; p < arraySize - 1 ; p++)
    {
        minIndex = p;
        for (int c = p + 1 ; c < arraySize ; c++)
        {
            if (arr[c] < arr[p])
                minIndex = c;
        }
        if (minIndex != p)
        {
            temp = arr[p];
            arr[p] = arr[minIndex];
            arr[minIndex] = temp;
        }
    }

}

//Bubble Sort:
void Bubble_Sort(int arr[], int arraySize)
{
    int temp;
    bool isSwapped;
    for (int p = 0 ; p < arraySize - 1 ; p++)
    {
        isSwapped = false;
        for (int c = 0 ; c < arraySize - p - 1 ; c++)
        {
            if (arr[c] > arr[c+1])
            {
                temp = arr[c];
                arr[c] = arr[c+1];
                arr[c+1] = temp;
                isSwapped = true;
            }
        }
        if (isSwapped == false)
            break;
    }
}

//Insertion Sort:
void Insertion_Sort(int arr[], int arraySize)
{
    int key, k, m;
    for (k = 0 ; k < arraySize ; k++)
    {
        key = arr[k];
        m = k - 1;
        while (m >= 0 && arr[m] > key)
        {
            arr[m + 1] = arr[m];
            m--;
        }
        arr[m+1] = key;
    }
}

//Quick Sort:
void Quick_Sort(int arr[], int first, int last)
{
    if (last > first)
    {
        int pivot = arr[first], leftCounter = first + 1, rightCounter = last, temp;
        while (leftCounter <= rightCounter)
        {
            while (arr[leftCounter] <= pivot && leftCounter <= last)
                leftCounter++;
            while (arr[rightCounter] >= pivot && rightCounter > first)
                rightCounter--;
            if (leftCounter < rightCounter)
            {
                temp = arr[leftCounter];
                arr[leftCounter] = arr[rightCounter];
                arr[rightCounter] = temp;
            }
        }
        temp = arr[first];
        arr[first] = arr[rightCounter];
        arr[rightCounter] = temp;
        Quick_Sort(arr, first, rightCounter - 1);
        Quick_Sort(arr, rightCounter + 1, last);
    }
}

//Merge Sort:
void Merge2Arrays(int arr[], int first1, int last1, int first2, int last2)
{
    int *tempArray = new int [(last2 - first1) + 1], tempCounter = 0, counter1 = first1, counter2 = first2;
    while (counter1 <= last1 && counter2 <= last2)
    {
        if (arr[counter1] < arr[counter2])
        {
            tempArray[tempCounter] = arr[counter1];
            tempCounter++;
            counter1++;
        }
        else
        {
            tempArray[tempCounter] = arr[counter2];
            tempCounter++;
            counter2++;
        }
    }
    while (counter1 <= last1)
    {
        tempArray[tempCounter] = arr[counter1];
        tempCounter++;
        counter1++;
    }
    while (counter2 <= last2)
    {
        tempArray[tempCounter] = arr[counter2];
        tempCounter++;
        counter2++;
    }
    for (int k = 0 ; k < ((last2 - first1) + 1) ; k++)
        arr[k + first1] = tempArray[k];
    delete[] tempArray;
}

void Merge_Sort(int arr[], int first, int last)
{
    if (first >= last)
        return;
    int mid = ((first + last) / 2);
    Merge_Sort(arr, first, mid);
    Merge_Sort(arr, mid + 1, last);
    Merge2Arrays(arr, first, mid, mid + 1, last);
}

//Stack using Array:
class stackArray
{
    int *stackArr, stackSize, top;
public:
    stackArray(int s = 1)
    {
        stackSize = s;
        stackArr = new int [stackSize];
        top = 0;
    }
    bool isEmpty()
    {
        return top == 0;
    }
    bool isFull()
    {
        return top == stackSize;
    }
    void doubleSize()
    {
        int *tempArr = new int [stackSize * 2];
        for (int k = 0 ; k < top < k++)
            tempArr[k] = stackArr[k];
        stackSize *= 2;
        delete[] stackArr;
        stackArr = tempArr;
    }
    void push(int item)
    {
        if (isFull())
            doubleSize();
            arr[top] = item;
            top++;
    }
    int pop()
    {
        if (isEmpty())
        {
            cout << "Stack is empty!\n";
            return -1;
        }
        top--;
        return arr[top];
    }
    int get_length()
    {
        return top;
    }
    int getMaxSize()
    {
        return stackSize;
    }
    int get_top_value()
    {
        return arr[top - 1];
    }
    void deleteStack()
    {
        top = 0;
    }
    void printStack()
    {
        for (int k = top - 1 ; k >= 0 ; k--)
            cout << arr[k] << "\t";
    }
    ~stackArray()
    {
        delete[] stackArr;
        deleteStack();
        cout << "\nStack is deleted\n";
    }
};

//Circular queue using Array:
class queueArray
{
    int *queueArr, queueSize, frnt, rear, counter;
public:
    queueArray(int s = 1)
    {
        queueSize = s;
        queueArr = new int [queueSize];
        frnt = rear = counter = 0;
    }
    bool isEmpty()
    {
        return counter == 0;
    }
    bool isFull()
    {
        return counter == queueSize;
    }
    void doubleSize()
    {
        int *tempArr = new int [queueSize * 2];
        for (int k = 0 ; k < queueSize ; k++)
            tempArr[k] = queueArr[((k+frnt) % queueSize)];
        queueSize *= 2;
        delete[] queueArr;
        queueArr = tempArr;
        frnt = 0;
        rear = counter;
    }
    void enqueue(int item)
    {
        if (isFull())
            doubleSize();
        queueArr[rear] = item;
        rear = ((rear + 1) % queueSize);
        counter++;
    }
    int dequeue()
    {
        if (isEmpty())
        {
            cout << "\nQueue is empty!\n";
            return -1;
        }
        int temp = frnt;
        frnt = ((frnt + 1) % queueSize);
        counter --;
        return queueArr[temp];
    }
    int getMaxSize()
    {
        return queueSize;
    }
    int get_length()
    {
        return counter;
    }
    int getFront()
    {
        return queueArr[frnt];
    }
    void printQueue()
    {
        for (int k = 0 ; k < counter ; k++)
            cout << queueArr[((k + frnt) % queueSize)] << "\t";
    }
    void deleteQueue()
    {
        frnt = rear = counter = 0;
    }
    ~queueArray()
    {
        delete[] queueArr;
        cout "\nQueue is deleted\n";
    }
};

//For Linked List:
struct node
{
    int data;
    node *next;
};

//Stack using Linked List:
class LinkedStack
{
    node *top;
public:
    LinkedStack()
    {
        top = NULL;
    }
    bool isEmpty()
    {
        return top == NULL:
    }
    void push (int item)
    {
        node *item = new node;
        item->data = data;
        item->next = top;
        top = item;
    }
    int pop()
    {
        if (isEmpty())
        {
            cout << "\nStack is empty!\n";
            return -1;
        }
        node *temp = top;
        int tempData = temp->data;
        top = temp->next;
        delete temp;
        return tempData;
    }
    int get_length()
    {
        int counter = 0;
        node *temp;
        for (temp = top ; temp != NULL ; temp = temp->next)
            counter++;
        return counter;
    }
    void printStack()
    {
        node *temp;
        for (temp = top ; temp != NULL ; temp = temp->next)
            cout << temp->data << "\t";
    }
    void deleteStack()
    {
        node *temp;
        while (top != NULL)
        {
            temp = top;
            top = temp->next;
            delete temp;
        }
    }
    ~LinkedStack()
    {
        deleteStack();
        cout << "\nStack is deleted\n";
    }
};

//Queue using Linked List:
class LinkedQueue
{
    node *frnt, *rear;
public:
    LinkedQueue()
    {
        frnt = rear = NULL;
    }
    bool isEmpty()
    {
        return frnt == NULL;
    }
    void enqueue(int item)
    {
        node *temp = new node;
        temp->data = item;
        temp->next = NULL;
        if (isEmpty())
            frnt = rear = temp;
        else
        {
            rear->next = temp;
            rear = temp;
        }
    }
    int dequeue()
    {
        if (isEmpty())
        {
            cout << "\nQueue is empty!\n";
            return -1;
        }
        node *temp = frnt;
        int tempData = temp->data;
        frnt = frnt->next;
        delete temp;
        if (isEmpty())
            rear = NULL;
        return tempData;
    }
    int get_length()
    {
        int counter = 0;
        node *temp;
        for (temp = frnt ; temp != NULL ; temp = temp->next)
            counter++;
        return counter;
    }
    void printQueue()
    {
        node *temp;
        for (temp = frnt ; temp != NULL ; temp = temp->next)
            cout << temp->data << "\t";
    }
    void deleteQueue()
    {
        node *temp;
        while (frnt != NULL)
        {
            temp = frnt;
            frnt = frnt->next;
            delete temp;
        }
        rear = NULL;
    }
    ~LinkedQueue()
    {
        deleteQueue();
        cout << "\nQueue is deleted\n";
    }
};
Editor is loading...
Leave a Comment