Data Structure Using C ( LEAK Code 💦 )

 Experiment 1: Insertion and Deletion in Array (C Program)


//Experiment 1: Insertion and Deletion in Array (C Program)

#include <stdio.h>
int main() {
    int arr[10], n, pos, val;

    printf("Enter the number of elements: ");
    scanf("%d", &n);

    printf("Enter %d elements:\n", n);
    for (int i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    printf("\nEnter position and value to insert: ");
    scanf("%d %d", &pos, &val);

    for (int i = n; i > pos; i--)
        arr[i] = arr[i - 1];

    arr[pos] = val;
    n++;

    printf("Array after insertion: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);
    printf("\n");

    printf("\nEnter position to delete: ");
    scanf("%d", &pos);
    n--;

    for (int i = pos; i < n - 1; i++)
        arr[i] = arr[i + 1];

    printf("Array after deletion: ");
    for (int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    return 0;
}











 Experiment 2.1: Factorial using Recursion

#include <stdio.h>

int fact(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    return n * fact(n - 1);
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Factorial of %d is: %d\n", n, fact(n));
    return 0;
}







 Experiment 2.2: Fibonacci Series using Recursion



#include <stdio.h>

int fact(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    return n * fact(n - 1);
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Factorial of %d is: %d\n", n, fact(n));

    return 0;
}





Experiment 2.3: Sum of First N Numbers using Recursion


#include <stdio.h>

int sum(int n) {
    if (n == 0)
        return 0;
    else
        return n + sum(n - 1);
}

int main() {
    int n;
    printf("Enter a number: ");
    scanf("%d", &n);
    printf("Sum of first %d numbers is: %d\n", n, sum(n));
    return 0;
}






Experiment 3: Stack Operations


#include <stdio.h>
#define MAX 100

int top = -1, c, stack[MAX];

void push();
void pop();
void peek();
void display();

int main() {
    printf("\n1. Push\n2. Pop\n3. Peek\n4. Display\n5. Exit\n");
    while(1) {
        printf("Enter your choice(1-5): ");
        scanf("%d", &c);
        switch(c) {
            case 1: push(); break;
            case 2: pop(); break;
            case 3: peek(); break;
            case 4: display(); break;
            case 5: return 0;
            default: printf("Enter correct choice\n");
        }
    }
}

void push() {
    int val;
    if(top == MAX - 1)
        printf("Stack is full.\n");
    else {
        printf("Enter the element to insert into stack: ");
        scanf("%d", &val);
        top = top + 1;
        stack[top] = val;
    }
}

void pop() {
    if(top == -1)
        printf("Stack is empty.\n");
    else {
        printf("\nThe deleted element is %d.\n", stack[top]);
        top = top - 1;
    }
}

void peek() {
    if(top == -1)
        printf("Stack is empty.\n");
    else
        printf("\nThe topmost element in the stack is %d.\n", stack[top]);
}

void display() {
    int i;
    if(top == -1)
        printf("Stack is empty.\n");
    else {
        printf("Elements in stack are..\n");
        for(i = 0; i <= top; i++)
            printf("%d\n", stack[i]);
    }
}





 Experiment 4: Queue Operations


#include <stdio.h>
#define MAX 10

int queue[MAX];
int front = -1, rear = -1;

void insert() {
    int num;
    printf("Enter number to insert: ");
    scanf("%d", &num);
    if (rear == MAX - 1) {
        printf("Queue is full.\n");
    } else {
        if (front == -1) front = 0;
        rear++;
        queue[rear] = num;
        printf("%d inserted.\n", num);
    }
}

int delete_element() {
    if (front == -1 || front > rear) {
        printf("Queue is empty.\n");
        return -1;
    } else {
        int val = queue[front];
        front++;
        return val;
    }
}

int peek() {
    if (front == -1 || front > rear) {
        printf("Queue is empty.\n");
        return -1;
    } else {
        return queue[front];
    }
}

void display() {
    if (front == -1 || front > rear) {
        printf("Queue is empty.\n");
    } else {
        for (int i = front; i <= rear; i++) {
            printf("%d ", queue[i]);
        }
        printf("\n");
    }
}

int main() {
    int option, val;
    do {
        printf("\n1. Insert\n2. Delete\n3. Peek\n4. Display\n5. Exit\n");
        printf("Enter option: ");
        scanf("%d", &option);

        switch(option) {
            case 1: insert(); break;
            case 2: val = delete_element();
                    if (val != -1) printf("Deleted: %d\n", val);
                    break;
            case 3: val = peek();
                    if (val != -1) printf("First element: %d\n", val);
                    break;
            case 4: display(); break;
            case 5: printf("Exiting...\n"); break;
            default: printf("Invalid option.\n");
        }
    } while(option != 5);

    return 0;
}


output apni G*nd me dal lo

No abusive langauge , be kind , be humble , be a good person


Experiment 5: Circular Queue Operations


#include <stdio.h>
#define MAX 5

int queue[MAX];
int front = -1, rear = -1;

void insert() {
    int num;
    printf("Enter number to insert: ");
    scanf("%d", &num);

    if ((front == 0 && rear == MAX - 1) || (rear + 1 == front)) {
        printf("Queue is full.\n");
    } else {
        if (front == -1) front = 0;
        rear = (rear + 1) % MAX;
        queue[rear] = num;
        printf("%d inserted.\n", num);
    }
}

int delete_element() {
    if (front == -1) {
        printf("Queue is empty.\n");
        return -1;
    } else {
        int val = queue[front];
        if (front == rear) {
            front = rear = -1;
        } else {
            front = (front + 1) % MAX;
        }
        return val;
    }
}

int peek() {
    if (front == -1) {
        printf("Queue is empty.\n");
        return -1;
    } else {
        return queue[front];
    }
}

void display() {
    if (front == -1) {
        printf("Queue is empty.\n");
    } else {
        int i = front;
        while (1) {
            printf("%d ", queue[i]);
            if (i == rear) break;
            i = (i + 1) % MAX;
        }
        printf("\n");
    }
}

int main() {
    int option, val;
    do {
        printf("\n1. Insert\n2. Delete\n3. Peek\n4. Display\n5. Exit\n");
        printf("Enter option: ");
        scanf("%d", &option);

        switch(option) {
            case 1: insert(); break;
            case 2: val = delete_element();
                    if (val != -1) printf("Deleted: %d\n", val);
                    break;
            case 3: val = peek();
                    if (val != -1) printf("First element: %d\n", val);
                    break;
            case 4: display(); break;
            case 5: printf("Exiting...\n"); break;
            default: printf("Invalid option.\n");
        }
    } while(option != 5);

    return 0;
}

Experiment no 5. Circular Queue Operations


#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *next;
};

struct node *start = NULL;

void insert_at_first(int val) {
    struct node *p = (struct node*)malloc(sizeof(struct node));
    p->data = val;
    p->next = start;
    start = p;
    printf("%d inserted at first.\n", val);
}

void insert_at_last(int val) {
    struct node *p = (struct node*)malloc(sizeof(struct node));
    p->data = val;
    p->next = NULL;
    if (start == NULL) {
        start = p;
    } else {
        struct node *q = start;
        while (q->next != NULL) {
            q = q->next;
        }
        q->next = p;
    }
    printf("%d inserted at last.\n", val);
}

void delete_first() {
    if (start == NULL) {
        printf("List is empty.\n");
    } else {
        struct node *p = start;
        printf("Deleted: %d\n", p->data);
        start = start->next;
        free(p);
    }
}

void delete_last() {
    if (start == NULL) {
        printf("List is empty.\n");
    } else if (start->next == NULL) {
        printf("Deleted: %d\n", start->data);
        free(start);
        start = NULL;
    } else {
        struct node *q = start;
        while (q->next->next != NULL) {
            q = q->next;
        }
        printf("Deleted: %d\n", q->next->data);
        free(q->next);
        q->next = NULL;
    }
}

void display() {
    if (start == NULL) {
        printf("List is empty.\n");
    } else {
        struct node *q = start;
        printf("List elements: ");
        while (q != NULL) {
            printf("%d ", q->data);
            q = q->next;
        }
        printf("\n");
    }
}

int main() {
    int choice, val;
    do {
        printf("\n1. Insert at first\n2. Insert at last\n3. Delete first\n4. Delete last\n5. Display\n6. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1: printf("Enter value: "); scanf("%d", &val); insert_at_first(val); break;
            case 2: printf("Enter value: "); scanf("%d", &val); insert_at_last(val); break;
            case 3: delete_first(); break;
            case 4: delete_last(); break;
            case 5: display(); break;
            case 6: printf("Exiting...\n"); break;
            default: printf("Invalid choice.\n");
        }
    } while(choice != 6);

    return 0;
}




Experiment 6: Singly Linked List


#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *next;
};

struct node *start = NULL;

void insert_at_first(int val) {
    struct node *p = (struct node*)malloc(sizeof(struct node));
    p->data = val;
    p->next = start;
    start = p;
    printf("%d inserted at first.\n", val);
}

void insert_at_last(int val) {
    struct node *p = (struct node*)malloc(sizeof(struct node));
    p->data = val;
    p->next = NULL;
    if (start == NULL) {
        start = p;
    } else {
        struct node *q = start;
        while (q->next != NULL) {
            q = q->next;
        }
        q->next = p;
    }
    printf("%d inserted at last.\n", val);
}

void delete_first() {
    if (start == NULL) {
        printf("List is empty.\n");
    } else {
        struct node *p = start;
        printf("Deleted: %d\n", p->data);
        start = start->next;
        free(p);
    }
}

void delete_last() {
    if (start == NULL) {
        printf("List is empty.\n");
    } else if (start->next == NULL) {
        printf("Deleted: %d\n", start->data);
        free(start);
        start = NULL;
    } else {
        struct node *q = start;
        while (q->next->next != NULL) {
            q = q->next;
        }
        printf("Deleted: %d\n", q->next->data);
        free(q->next);
        q->next = NULL;
    }
}

void display() {
    if (start == NULL) {
        printf("List is empty.\n");
    } else {
        struct node *q = start;
        printf("List elements: ");
        while (q != NULL) {
            printf("%d ", q->data);
            q = q->next;
        }
        printf("\n");
    }
}

int main() {
    int choice, val;
    do {
        printf("\n1. Insert at first\n2. Insert at last\n3. Delete first\n4. Delete last\n5. Display\n6. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1: printf("Enter value: "); scanf("%d", &val); insert_at_first(val); break;
            case 2: printf("Enter value: "); scanf("%d", &val); insert_at_last(val); break;
            case 3: delete_first(); break;
            case 4: delete_last(); break;
            case 5: display(); break;
            case 6: printf("Exiting...\n"); break;
            default: printf("Invalid choice.\n");
        }
    } while(choice != 6);

    return 0;
}


Experiment 7: Doubly Linked List (Insertion, Deletion, Display)


#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *prev;
    struct node *next;
};

struct node *start = NULL;

void insert_at_begin(int val) {
    struct node *p = (struct node*)malloc(sizeof(struct node));
    p->data = val;
    p->prev = NULL;
    p->next = start;
    if (start != NULL) start->prev = p;
    start = p;
    printf("%d inserted at beginning.\n", val);
}

void insert_at_end(int val) {
    struct node *p = (struct node*)malloc(sizeof(struct node));
    p->data = val;
    p->next = NULL;
    if (start == NULL) {
        p->prev = NULL;
        start = p;
    } else {
        struct node *q = start;
        while (q->next != NULL) q = q->next;
        q->next = p;
        p->prev = q;
    }
    printf("%d inserted at end.\n", val);
}

void insert_after(int element, int val) {
    struct node *q = start;
    while (q != NULL && q->data != element) q = q->next;
    if (q != NULL) {
        struct node *p = (struct node*)malloc(sizeof(struct node));
        p->data = val;
        p->next = q->next;
        p->prev = q;
        if (q->next != NULL) q->next->prev = p;
        q->next = p;
        printf("%d inserted after %d.\n", val, element);
    } else {
        printf("Element not found.\n");
    }
}

void delete_begin() {
    if (start == NULL) {
        printf("List is empty.\n");
    } else {
        struct node *r = start;
        printf("Deleted: %d\n", r->data);
        start = start->next;
        if (start != NULL) start->prev = NULL;
        free(r);
    }
}

void delete_end() {
    if (start == NULL) {
        printf("List is empty.\n");
    } else {
        struct node *r = start;
        while (r->next != NULL) r = r->next;
        printf("Deleted: %d\n", r->data);
        if (r->prev != NULL) r->prev->next = NULL;
        else start = NULL;
        free(r);
    }
}

void delete_specific(int element) {
    struct node *r = start;
    while (r != NULL && r->data != element) r = r->next;
    if (r != NULL) {
        printf("Deleted: %d\n", r->data);
        if (r->prev != NULL) r->prev->next = r->next;
        else start = r->next;
        if (r->next != NULL) r->next->prev = r->prev;
        free(r);
    } else {
        printf("Element not found.\n");
    }
}

void display() {
    if (start == NULL) {
        printf("List is empty.\n");
    } else {
        struct node *q = start;
        printf("List: ");
        while (q != NULL) {
            printf("%d ", q->data);
            q = q->next;
        }
        printf("\n");
    }
}

int main() {
    int choice, val, element;
    do {
        printf("\n1. Insert at beginning\n2. Insert at end\n3. Insert after element\n4. Delete beginning\n5. Delete end\n6. Delete specific\n7. Display\n8. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1: printf("Enter value: "); scanf("%d", &val); insert_at_begin(val); break;
            case 2: printf("Enter value: "); scanf("%d", &val); insert_at_end(val); break;
            case 3: printf("Enter element: "); scanf("%d", &element);
                    printf("Enter value: "); scanf("%d", &val);
                    insert_after(element, val); break;
            case 4: delete_begin(); break;
            case 5: delete_end(); break;
            case 6: printf("Enter element to delete: "); scanf("%d", &element); delete_specific(element); break;
            case 7: display(); break;
            case 8: printf("Exiting...\n"); break;
            default: printf("Invalid choice.\n");
        }
    } while(choice != 8);

    return 0;
}


Experiment 9: Stack using Linked List



#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node *next;
};

struct node *top = NULL;

void push(int val) {
    struct node *p = (struct node*)malloc(sizeof(struct node));
    p->data = val;
    p->next = top;
    top = p;
    printf("%d pushed into stack.\n", val);
}

void pop() {
    if (top == NULL) {
        printf("Stack is empty.\n");
    } else {
        struct node *temp = top;
        printf("Popped: %d\n", temp->data);
        top = top->next;
        free(temp);
    }
}

void display() {
    if (top == NULL) {
        printf("Stack is empty.\n");
    } else {
        struct node *q = top;
        printf("Stack elements:\n");
        while (q != NULL) {
            printf("%d\n", q->data);
            q = q->next;
        }
    }
}

int main() {
    int choice, val;
    do {
        printf("\n1. Push\n2. Pop\n3. Display\n4. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1: printf("Enter value: "); scanf("%d", &val); push(val); break;
            case 2: pop(); break;
            case 3: display(); break;
            case 4: printf("Exiting...\n"); break;
            default: printf("Invalid choice.\n");
        }
    } while(choice != 4);

    return 0;
}


Experiment 10: Binary Search Tree Traversals (Preorder, Inorder, Postorder)



#include <stdio.h>
#include <stdlib.h>

struct tree {
    int data;
    struct tree *lchild;
    struct tree *rchild;
};

struct tree *root = NULL;

void insert(int val) {
    struct tree *newNode = (struct tree*)malloc(sizeof(struct tree));
    newNode->data = val;
    newNode->lchild = NULL;
    newNode->rchild = NULL;

    if (root == NULL) {
        root = newNode;
        return;
    }

    struct tree *curr = root, *prev = NULL;
    while (curr != NULL) {
        prev = curr;
        if (val < curr->data)
            curr = curr->lchild;
        else
            curr = curr->rchild;
    }

    if (val < prev->data)
        prev->lchild = newNode;
    else
        prev->rchild = newNode;
}

void preorder(struct tree *curr) {
    if (curr != NULL) {
        printf("%d ", curr->data);
        preorder(curr->lchild);
        preorder(curr->rchild);
    }
}

void inorder(struct tree *curr) {
    if (curr != NULL) {
        inorder(curr->lchild);
        printf("%d ", curr->data);
        inorder(curr->rchild);
    }
}

void postorder(struct tree *curr) {
    if (curr != NULL) {
        postorder(curr->lchild);
        postorder(curr->rchild);
        printf("%d ", curr->data);
    }
}

int main() {
    int choice, val;
    do {
        printf("\n1. Insert\n2. Preorder\n3. Inorder\n4. Postorder\n5. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        switch(choice) {
            case 1: printf("Enter value: "); scanf("%d", &val); insert(val); break;
            case 2: printf("Preorder Traversal: "); preorder(root); printf("\n"); break;
            case 3: printf("Inorder Traversal: "); inorder(root); printf("\n"); break;
            case 4: printf("Postorder Traversal: "); postorder(root); printf("\n"); break;
            case 5: printf("Exiting...\n"); break;
            default: printf("Invalid choice.\n");
        }
    } while(choice != 5);

    return 0;
}


 Experiment 11: Reversing a List using Stack



#include <stdio.h>

int stack[100], top = -1;

void push(int val) {
    stack[++top] = val;
}

int pop() {
    return stack[top--];
}

int main() {
    int n = 4, i;
    int list[] = {10, 20, 30, 40};

    printf("Original List: ");
    for(i = 0; i < n; i++) {
        printf("%d ", list[i]);
        push(list[i]);
    }

    printf("\nReversed List: ");
    for(i = 0; i < n; i++) {
        printf("%d ", pop());
    }

    return 0;
}





Comments