Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
22 kB
6
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
struct stackArray
{
    int top;
    int arr[MAX];
};
struct queue
{
    int front;
    int rear;
    int arr[MAX];
};
struct linkedList
{
    int data;
    struct linkedList *next;
};

struct stacklist
{
    int datasl;
    struct stacklist *next;
};

void inputsl(struct stacklist *list);
// struct stacklist *insertionsl(struct stacklist *list, int value);
struct stacklist *popsl(struct stacklist *s);
void linear_search();
void binary_search();
void bubble_sort();
void selection_sort();
void merge_sort(int[], int, int);
void input();
void display(int[], int);
void input_q();
void display_q(int[], int);
void merge(int[], int, int, int);
int split(int[], int, int);
void quick_sort(int[], int, int);
void insertion_sort();
void radix_sort();
void insertion();
void deletion();
void stack_using_array();
void stack_using_linked_list();
void single_queue();
void double_queue();
void circular_queue();
struct linkedList *insertion_at_begin(struct linkedList *, int);
void insertion_at_end();
void insertion_at_position();
void deletion_at_begin();
void deletion_at_end();
void deletion_at_position();
void reversing();
int isEmpty(struct stackArray *h);
int isFull(struct stackArray *head);
void dequeue(struct queue *q);
void displayq(struct queue *h);
void single_queue(struct queue *q);
// void push(struct stackArray *, int);
void pop(struct stackArray *hm);
void displaystack(struct stackArray *he);

void stack_using_array(struct stackArray *h);

int isFullq(struct queue *head);
int isEmptyq(struct queue *head);
void inputl(struct linkedList *q);
void traversal(struct linkedList *m);

void linear_search()
{
    int size;
    printf("Enter the Size\n");
    scanf("%d", &size);
    int arr[size];
    int i;
    printf("Enter Elements\n");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("Enter Element to be Searched\n");
    int k, flag = 0;
    scanf("%d", &k);
    for (i = 0; i < size; i++)
    {
        if (arr[i] == k)
        {
            flag = 1;
            break;
        }
    }
    if (flag == 1)
    {
        printf("Element Found At index %d\n", i);
    }
    else
    {
        printf("Element Found\n");
    }
}
void binary_search()
{
    int size;
    printf("Enter the Size\n");
    scanf("%d", &size);
    int arr[size];
    printf("Enter Elements\n");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("Enter Element to be Searched\n");
    int k, flag = 0;
    int left = 0;
    int right = size - 1;
    while (left <= right)
    {
        int mid = (left + right) / 2;
        if (arr[mid] == k)
        {
            printf("Element Found\n");
            break;
        }
        else if (arr[mid] < k)
        {
            left = mid + 1;
        }
        else
        {
            right = mid - 1;
        }
    }
    printf("Element Not Found\n");
}
void bubble_sort()
{
    int size;
    printf("Enter the Size\n");
    scanf("%d", &size);
    int arr[size];
    printf("Enter Elements\n");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }
    for (int i = 0; i < size; i++)
    {
        for (int j = 0; j < size - 1 - i; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }

    printf("After Sorting\n");
    for (int i = 0; i < size; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n");
}
void selection_sort()
{
    int size;
    printf("Enter the Size\n");
    scanf("%d", &size);
    int arr[size];
    printf("Enter Elements\n");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }
    for (int i = 0; i < size; i++)
    {
        int min = i;
        for (int j = i + 1; j < size; j++)
        {
            if (arr[min] > arr[j])
            {
                min = j;
            }
        }
        int temp = arr[min];
        arr[min] = arr[i];
        arr[i] = temp;
    }
    printf("After Sorting\n");
    for (int i = 0; i < size; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n");
}
void input()
{
    int size;
    printf("Enter the Size\n");
    scanf("%d", &size);
    int arr[size];
    printf("Enter Elements\n");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }
    merge_sort(arr, 0, size);
    display(arr, size);
}
void merge_sort(int arr[], int left, int right)
{
    if (left < right)
    {
        int mid = (left + right) / 2;
        merge_sort(arr, left, mid);
        merge_sort(arr, mid + 1, right);
        merge(arr, left, mid, right);
    }
}
void merge(int arr[], int left, int mid, int right)
{
    int i, j, k;
    int n1 = mid - left + 1;
    int n2 = right - mid;

    // Create temporary arrays
    int L[n1], R[n2];

    // Copy data to temporary arrays L[] and R[]
    for (i = 0; i < n1; i++)
        L[i] = arr[left + i];
    for (j = 0; j < n2; j++)
        R[j] = arr[mid + 1 + j];

    // Merge the temporary arrays back into arr[left..right]
    i = 0;    // Initial index of first subarray
    j = 0;    // Initial index of second subarray
    k = left; // Initial index of merged subarray
    while (i < n1 && j < n2)
    {
        if (L[i] <= R[j])
        {
            arr[k] = L[i];
            i++;
        }
        else
        {
            arr[k] = R[j];
            j++;
        }
        k++;
    }

    // Copy the remaining elements of L[], if there are any
    while (i < n1)
    {
        arr[k] = L[i];
        i++;
        k++;
    }

    // Copy the remaining elements of R[], if there are any
    while (j < n2)
    {
        arr[k] = R[j];
        j++;
        k++;
    }
}
void display(int arr[], int size)
{
    printf("After Sorting\n");
    for (int i = 0; i < size; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n");
}
void insertion_sort()
{
    int size;
    printf("Enter the Size\n");
    scanf("%d", &size);
    int arr[size];
    printf("Enter Elements\n");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }

    for (int i = 1; i < size; i++)
    {
        int j = i - 1;
        int key = arr[i];
        while (j >= 0 && arr[j] > key)
        {
            arr[j + 1] = arr[j];
            j--;
        }
        arr[j + 1] = key;
    }
    printf("After Sorting\n");
    for (int i = 0; i < size; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n");
}
void input_q()
{
    int size;
    printf("Enter the Size\n");
    scanf("%d", &size);
    int arr[size];
    printf("Enter Elements\n");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }
    quick_sort(arr, 0, size - 1);
    display_q(arr, size);
}

int split(int arr[], int left, int right)
{
    int pivotgreater = right;
    int pivotless = left + 1;
    int pivot = arr[left];
    while (pivotgreater >= pivotless)
    {
        while (arr[pivotgreater] > pivot)
        {
            pivotgreater--;
        }
        while (arr[pivotless] < pivot)
        {
            pivotless++;
        }
        if (pivotgreater > pivotless)
        {
            int temp = arr[pivotgreater];
            arr[pivotgreater] = arr[pivotless];
            arr[pivotless] = temp;
        }
    }
    int temp = arr[left];
    arr[left] = arr[pivotgreater];
    arr[pivotgreater] = temp;
    return pivotgreater;
}
void quick_sort(int arr[], int left, int right)
{
    if (left < right)
    {
        int i = split(arr, left, right);
        quick_sort(arr, left, i - 1);
        quick_sort(arr, i + 1, right);
    }
}

void display_q(int arr[], int size)
{
    printf("After Sorting\n");
    for (int i = 0; i < size; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n");
}

void insertion()
{
    int size;
    printf("Enter the Size\n");
    scanf("%d", &size);
    int arr[size];
    printf("Enter Elements\n");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("Enter Position\n");
    int pos;
    scanf("%d", &pos);
    printf("Enter the Element\n");
    int ele;
    scanf("%d", &ele);
    for (int i = size - 1; i >= pos; i--)
    {
        arr[i + 1] = arr[i];
    }
    arr[pos] = ele;
    printf("After Sorting\n");
    for (int i = 0; i < size; i++)
    {
        printf("%d\t", arr[i]);
    }
    printf("\n");
}
void deletion()
{
    int size;
    printf("Enter the Size\n");
    scanf("%d", &size);
    int arr[size];
    printf("Enter Elements\n");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &arr[i]);
    }
    printf("Enter the Element to be deleted\n");
    int del;
    int index;
    scanf("%d", &del);
    for (int i = 0; i < size; i++)
    {
        if (arr[i] == del)
        {
            index = i;
        }
    }
    for (int i = index; i < size; i++)
    {
        arr[i] = arr[i + 1];
    }
    size--;
    printf("Updated Array\n");
    for (int i = 0; i < size; i++)
    {
        printf("%d\t", arr[i]);
    }
}
void push(struct stackArray *hm, int value)
{
    if (isFull(hm))
    {
        printf("Stack Overflow\n");
        return;
    }
    hm->top++;
    hm->arr[hm->top] = value;
    while (1)
    {
        printf("Do u Want To add more Elements?\n");
        int chs;
        printf("1.Enter Elements\n");
        printf("2.Pop\n");
        printf("3.Display\n");
        printf("4.Quit\n");
        scanf("%d", &chs);
        if (chs == 1)
        {
            int es;
            scanf("%d", &es);
            hm->top++;
            hm->arr[hm->top] = es;
        }
        else if (chs == 2)
        {
            pop(hm);
        }
        else if (chs == 3)
        {
            displaystack(hm);
        }
        else
        {
            break;
        }
    }
}

void pop(struct stackArray *hm)
{
    if (isEmpty(hm))
    {
        printf("Stack Underflow\n");
        return;
    }
    int data = hm->arr[hm->top];
    hm->top--;
    printf("Popped element: %d\n", data);
}

void displaystack(struct stackArray *he)
{
    if (isEmpty(he))
    {
        printf("Stack Underflow\n");
        return;
    }
    // printf("");
    printf("Stack Elements\n\n");
    for (int i = he->top; i >= 0; i--)
    {
        printf("%d\t", he->arr[i]);
    }
    printf("\n");
    printf("\n");
}

int isFull(struct stackArray *head)
{
    if (head->top == MAX - 1)
    {
        return 1;
    }
    return 0;
}

int isEmpty(struct stackArray *head)
{
    if (head->top == -1)
    {
        return 1;
    }
    return 0;
}

void stack_using_array(struct stackArray *h)
{
    printf("1.Push\n");
    printf("2.Pop\n");
    printf("3.Display\n");
    int chs;
    printf("Enter Choice\n");
    scanf("%d", &chs);
    if (chs == 1)
    {
        int elem;
        printf("Enter element to push: ");
        scanf("%d", &elem);
        push(h, elem);
    }
    else if (chs == 2)
    {
        pop(h);
    }
    else if (chs == 3)
    {
        displaystack(h);
    }
    else
    {
        printf("Invalid Choice\n");
    }
}

void traversalsl(struct stacklist *stack)
{
    struct stacklist *temp = stack;
    if (temp == NULL)
    {
        printf("Stack Is Empty\n");
        return;
    }
    while (temp != NULL)
    {
        printf("%d\t", temp->datasl);
        temp = temp->next;
    }
    printf("\n");
}

void inputsl(struct stacklist *l)
{

    printf("1.Enter Elements\n");
    int e;
    scanf("%d", &e);
    l->datasl = e;
    struct stacklist *temp = l;
    struct stacklist *op;
    while (1)
    {
        printf("Do u want To add more elements ?\n");
        printf("1.Enter Elements\n");
        printf("2.POP\n");
        printf("3.Display\n");
        printf("3.Quit\n");
        int cl;
        scanf("%d", &cl);
        if (cl == 1)
        {
            struct stacklist *second = (struct stacklist *)malloc(sizeof(struct stacklist));
            int ele;
            scanf("%d", &ele);
            second->datasl = ele;
            second->next = NULL; // Initialize the next pointer to NULL
            temp->next = second; // Link the new node to the current node
            temp = temp->next;
        }
        else if (cl == 2)
        {
            op = popsl(l);
        }
        else if (cl == 3)
        {
            traversalsl(op);
        }
        else
        {
            break;
        }
    }
}
struct stacklist *popsl(struct stacklist *s)
{
    struct stacklist *cur;
    struct stacklist *temp;
    cur = s;
    if (cur == NULL)
    {
        printf("Stack Is Empty\n");
        return NULL;
    }
    while (cur != NULL)
    {
        if (cur->next == NULL)
        {
            int data = cur->datasl;
            printf("Popped Element : %d\n", data);
            break;
        }
        cur = cur->next;
    }
    s = cur;
    return s;
}

int isFullq(struct queue *h)
{
    if (h->rear == MAX - 1)
    {
        return 1;
    }
    return 0;
}
void enqueue(struct queue *q, int value)
{
    if (isFullq(q))
    {
        printf("Queue is full\n");
        return;
    }
    q->rear++;
    q->arr[q->rear] = value;
    while (1)
    {
    }
}
int isEmptyq(struct queue *h)
{
    if (h->front == -1 && h->rear == -1)
    {
        return 1;
    }
    return 0;
}
void dequeue(struct queue *q)
{
    if (isEmptyq(q))
    {
        printf("Queue is empty\n");
        return;
    }
    q->front++;
    int data = q->arr[q->front];
    printf("Dequeued = %d\n", data);
}
void displayq(struct queue *h)
{
    if (isEmptyq(h))
    {
        printf("Queue is empty\n");
        return;
    }
    for (int i = h->front; i <= h->rear; i++)
    {
        printf("%d\t", h->arr[i]);
    }
    printf("\n");
}
void single_queue(struct queue *q)
{
    printf("1.Enqueue\n");
    printf("2.Dequeue\n");
    printf("3.Display\n");
    int ch;
    printf("Enter Choice\n");
    scanf("%d", &ch);
    if (ch == 1)
    {
        int el;
        printf("Enter Element\n");
        scanf("%d", &el);
        enqueue(q, el);
    }
    else if (ch == 2)
    {
        dequeue(q);
    }
    else
    {
        displayq(q);
    }
}

struct linkedList *insertion_at_begin(struct linkedList *ptr, int value)
{
    struct linkedList *temp;
    temp = (struct linkedList *)malloc(sizeof(struct linkedList));
    temp->next = ptr;
    temp->data = value;
    ptr = temp;
    return ptr;
}
void inputl(struct linkedList *p)
{
    printf("1.Enter Elements\n");
    int e;
    scanf("%d", &e);
    p->data = e;
    while (1)
    {
        printf("Do u want To add more elements ?\n");
        printf("1.Enter Elements\n");
        printf("2.Quit\n");
        int cl;
        scanf("%d", &cl);
        if (cl == 1)
        {
            struct linkedList *second;
            second = (struct linkedList *)malloc(sizeof(struct linkedList));
            int ele;
            scanf("%d", &ele);
            second->data = ele;
            p->next = second;
        }
        else
        {
            break;
        }
    }
    printf("Now to Insert Element At beginning\n");
    printf("Enter the Element\n");
    int el2;
    scanf("%d", &el2);
    struct linkedList *r = insertion_at_begin(p, el2);
    traversal(r);
}

void traversal(struct linkedList *m)
{
    if (m == NULL)
    {
        printf("List is empty\n");
        return;
    }
    while (m != NULL)
    {
        printf(" %d\t", m->data);
        m = m->next;
    }
}

int main()
{
    printf("1.Searching\n");
    printf("2.Sorting\n");
    printf("3.Array Operation\n");
    printf("4.Stack\n");
    printf("5.Queue\n");
    printf("6.Linked List\n");
    int ch;
    printf("Enter Your Choice\n");
    scanf("%d", &ch);
    struct stackArray *s;
    s->top = -1;
    struct queue *head = (struct queue *)malloc(sizeof(struct queue));
    head->front = -1;
    head->rear = -1;
    struct linkedList *l;
    l = (struct linkedList *)malloc(sizeof(struct linkedList));
    struct stacklist *sl;
    sl = (struct stacklist *)malloc(sizeof(struct stacklist));

    switch (ch)
    {
    case 1:
        printf("1.Linear Search\n");
        printf("1.Binary Search\n");
        int ch2;
        printf("Enter Your Choice\n");
        scanf("%d", &ch2);
        switch (ch2)
        {
        case 1:
            linear_search();
            break;
        case 2:
            binary_search();
            break;
        default:
            printf("Wrong Choice\n");
            break;
        }
        break;
    case 2:
    {
        int ch3;
        printf("1.Bubble Sort\n");
        printf("2.Selection Sort\n");
        printf("3.Merge Sort\n");
        printf("4.Insertion Sort\n");
        printf("5.Quick Sort\n");
        printf("6.Radix Sort\n");
        scanf("%d", &ch3);
        switch (ch3)
        {
        case 1:
            bubble_sort();
            break;
        case 2:
            selection_sort();
            break;
        case 3:
            input();
            // merge_sort();
            // display();
            break;
        case 4:
            insertion_sort();
            break;
        case 5:
            input_q();
            // quick_sort();
            break;
            // case 6:
            //     radix_sort();
            //     break;

        default:
            printf("Enter Correct Choice\n");
            break;
        }
        break;
    }
    case 3:
        printf("1.Insertion\n");
        printf("2.Deletion\n");
        int ch4;
        printf("Enter Your Choice\n");
        scanf("%d", &ch4);
        switch (ch4)
        {
        case 1:
            insertion();
            break;
        case 2:
            deletion();
            break;

        default:
            printf("Enter Correct Choice\n");
            break;
        }
        break;
    case 4:
        printf("1.Stack Using Array\n");
        printf("2.Stack Using Linked List\n");
        int ch5;
        printf("Enter Your Choice\n");
        scanf("%d", &ch5);
        switch (ch5)
        {
        case 1:
            stack_using_array(s);
            break;
        case 2:
            // stack_using_linked_list();
            inputsl(sl);
            break;
        default:
            printf("Enter Correct Choice\n");
            break;
        }
        break;
    case 5:
        printf("1.Single Ended Queue\n");
        printf("2.Double Ended Queue\n");
        printf("3.Circular Queue\n");
        int ch6;
        printf("Enter Your Choice\n");
        scanf("%d", &ch6);
        switch (ch6)
        {
        case 1:
            single_queue(head);
            break;
            // case 2:
            //     double_queue();
            //     break;
            // case 3:
            //     circular_queue();
            //     break;

        default:
            printf("Enter Correct Choice\n");
            break;
        }
        break;
    case 6:

        printf("1.Insertion\n");
        printf("2.Deletion\n");
        printf("3.Reverse Linked List\n");
        int ch7;
        printf("Enter Your Choice\n");
        scanf("%d", &ch7);
        switch (ch7)
        {
        case 1:
            printf("1.Insertion At Beginning\n");
            printf("2.Insertion At End\n");
            printf("3.Insertion At Specific Position\n");
            int ch71;
            scanf("%d", &ch71);
            printf("Enter Your Choice\n");
            switch (ch71)
            {
            case 1:
                inputl(l);
                break;
                // case 2:
                //     insertion_at_end();
                //     break;
                // case 3:
                //     insertion_at_position();
                //     break;

            default:
                printf("Enter Correct Choice\n");
                break;
            }
            break;
            // case 2:
            //     printf("1.Deletion At Beginning\n");
            //     printf("2.Deletion At End\n");
            //     printf("3.Deletion At Specific Position\n");
            //     int ch72;
            //     printf("Enter Your Choice\n");
            //     scanf("%d", &ch72);

            //     switch (ch72)
            //     {
            //     case 1:
            //         deletion_at_begin();
            //         break;
            //     case 2:
            //         deletion_at_end();
            //         break;
            //     case 3:
            //         deletion_at_position();
            //         break;

            //     default:
            //         printf("Enter Correct Choice\n");
            //         break;
            //     }
            //     break;
            // case 3:
            //     reversing();
            //     break;
            // default:
            //     printf("Enter Correct Choice\n");
            //     break;
            // }
            // break;

        default:
            printf("Enter Correct Choice\n");
            break;
        }
    }
}