Write a program to implement Circular Queue using Array in c language 
ChatGPT
Certainly! Here's a simple implementation of a Circular Queue using an array in the C language:

c
Copy code
#include <stdio.h>
#define MAX_SIZE 5

// Structure to represent the Circular Queue
struct CircularQueue {
    int items[MAX_SIZE];
    int front, rear;
};

// Function to initialize the Circular Queue
void initializeQueue(struct CircularQueue* queue) {
    queue->front = -1;
    queue->rear = -1;
}

// Function to check if the Circular Queue is empty
int isEmpty(struct CircularQueue* queue) {
    return (queue->front == -1 && queue->rear == -1);
}

// Function to check if the Circular Queue is full
int isFull(struct CircularQueue* queue) {
    return ((queue->rear + 1) % MAX_SIZE == queue->front);
}

// Function to enqueue an element into the Circular Queue
void enqueue(struct CircularQueue* queue, int value) {
    if (isFull(queue)) {
        printf("Queue is full. Cannot enqueue.\n");
        return;
    }

    if (isEmpty(queue)) {
        queue->front = 0;
        queue->rear = 0;
    } else {
        queue->rear = (queue->rear + 1) % MAX_SIZE;
    }

    queue->items[queue->rear] = value;
    printf("Enqueued: %d\n", value);
}

// Function to dequeue an element from the Circular Queue
int dequeue(struct CircularQueue* queue) {
    int dequeuedItem;

    if (isEmpty(queue)) {
        printf("Queue is empty. Cannot dequeue.\n");
        return -1;
    } else if (queue->front == queue->rear) {
        dequeuedItem = queue->items[queue->front];
        initializeQueue(queue);
    } else {
        dequeuedItem = queue->items[queue->front];
        queue->front = (queue->front + 1) % MAX_SIZE;
    }

    printf("Dequeued: %d\n", dequeuedItem);
    return dequeuedItem;
}

// Function to display the elements of the Circular Queue
void display(struct CircularQueue* queue) {
    if (isEmpty(queue)) {
        printf("Queue is empty.\n");
        return;
    }

    int i = queue->front;
    printf("Queue elements: ");
    do {
        printf("%d ", queue->items[i]);
        i = (i + 1) % MAX_SIZE;
    } while (i != (queue->rear + 1) % MAX_SIZE);
    printf("\n");
}

// Sample usage of the Circular Queue
int main() {
    struct CircularQueue myQueue;
    initializeQueue(&myQueue);

    enqueue(&myQueue, 10);
    enqueue(&myQueue, 20);
    enqueue(&myQueue, 30);
    display(&myQueue);

    dequeue(&myQueue);
    display(&myQueue);

    enqueue(&myQueue, 40);
    display(&myQueue);

    return 0;
}
This program initializes a Circular Queue, enqueues and dequeues elements, and displays the current state of the queue. Adjust the MAX_SIZE macro according to your requirements.

User
Write a program to convert Infix Expression to Postfix Expression
ChatGPT
Certainly! Here's a simple program in C to convert an infix expression to a postfix expression using a stack:

c
Copy code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 100

// Structure to represent a stack
struct Stack {
    int top;
    char items[MAX_SIZE];
};

// Function to initialize the stack
void initializeStack(struct Stack* stack) {
    stack->top = -1;
}

// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
    return stack->top == -1;
}

// Function to push an element onto the stack
void push(struct Stack* stack, char value) {
    if (stack->top == MAX_SIZE - 1) {
        printf("Stack Overflow\n");
        exit(EXIT_FAILURE);
    }

    stack->items[++stack->top] = value;
}

// Function to pop an element from the stack
char pop(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack Underflow\n");
        exit(EXIT_FAILURE);
    }

    return stack->items[stack->top--];
}

// Function to return the top element of the stack without popping
char peek(struct Stack* stack) {
    if (isEmpty(stack)) {
        return '\0';  // Empty stack
    }

    return stack->items[stack->top];
}

// Function to check if a character is an operator
int isOperator(char ch) {
    return (ch == '+' || ch == '-' || ch == '*' || ch == '/');
}

// Function to determine the precedence of an operator
int precedence(char op) {
    if (op == '+' || op == '-')
        return 1;
    if (op == '*' || op == '/')
        return 2;
    return 0;
}

// Function to convert infix expression to postfix expression
void infixToPostfix(char* infix, char* postfix) {
    struct Stack operatorStack;
    initializeStack(&operatorStack);

    int i, j;
    char ch;

    for (i = 0, j = 0; infix[i] != '\0'; i++) {
        if (infix[i] >= 'a' && infix[i] <= 'z' || infix[i] >= 'A' && infix[i] <= 'Z') {
            postfix[j++] = infix[i];
        } else if (isOperator(infix[i])) {
            while (!isEmpty(&operatorStack) && precedence(infix[i]) <= precedence(peek(&operatorStack))) {
                postfix[j++] = pop(&operatorStack);
            }
            push(&operatorStack, infix[i]);
        } else if (infix[i] == '(') {
            push(&operatorStack, infix[i]);
        } else if (infix[i] == ')') {
            while (!isEmpty(&operatorStack) && peek(&operatorStack) != '(') {
                postfix[j++] = pop(&operatorStack);
            }
            pop(&operatorStack);  // Pop '('
        }
    }

    while (!isEmpty(&operatorStack)) {
        postfix[j++] = pop(&operatorStack);
    }

    postfix[j] = '\0';  // Null-terminate the postfix expression
}

// Sample usage of infixToPostfix
int main() {
    char infix[MAX_SIZE], postfix[MAX_SIZE];

    printf("Enter an infix expression: ");
    fgets(infix, MAX_SIZE, stdin);
    infixToPostfix(infix, postfix);

    printf("Postfix expression: %s\n", postfix);

    return 0;
}
This program takes an infix expression as input, converts it to postfix using a stack, and then prints the resulting postfix expression. Adjust the MAX_SIZE macro according to your requirements.

User
Write a program to implement the Merge sort
ChatGPT
Certainly! Here's a simple program in C to implement the Merge Sort algorithm:

c
Copy code
#include <stdio.h>
#include <stdlib.h>

// Function to merge two subarrays of arr[]
void merge(int arr[], int left, int middle, int right) {
    int i, j, k;
    int n1 = middle - left + 1;
    int n2 = right - middle;

    // 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[middle + 1 + j];

    // Merge the temporary arrays back into arr[left..right]
    i = 0;
    j = 0;
    k = left;

    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++;
    }
}

// Main function to perform merge sort on arr[left..right]
void mergeSort(int arr[], int left, int right) {
    if (left < right) {
        // Same as (left+right)/2, but avoids overflow for large left and right
        int middle = left + (right - left) / 2;

        // Sort first and second halves
        mergeSort(arr, left, middle);
        mergeSort(arr, middle + 1, right);

        // Merge the sorted halves
        merge(arr, left, middle, right);
    }
}

// Function to print an array
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

// Sample usage of mergeSort
int main() {
    int arr[] = {12, 11, 13, 5, 6, 7};
    int arr_size = sizeof(arr) / sizeof(arr[0]);

    printf("Given array is \n");
    printArray(arr, arr_size);

    mergeSort(arr, 0, arr_size - 1);

    printf("\nSorted array is \n");
    printArray(arr, arr_size);

    return 0;
}
This program defines functions to perform the Merge Sort algorithm on an array. It then demonstrates the sorting process with a sample array. Adjust the array values and size as needed.

User
Write a menu-driven program to implement the Single Link List and perform the following operations i. Creation (with the following data 12,55,67,45,23,78,99,32) ii.Display

iii. Deletion (delete the following data 12,45,32)
ChatGPT
Certainly! Here's a menu-driven program in C to implement a singly linked list and perform creation, display, and deletion operations:

c
Copy code
#include <stdio.h>
#include <stdlib.h>

// Node structure for the singly linked list
struct Node {
    int data;
    struct Node* next;
};

// Function to create a new node
struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Memory allocation failed\n");
        exit(EXIT_FAILURE);
    }
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

// Function to insert a node at the end of the linked list
void insertEnd(struct Node** head, int value) {
    struct Node* newNode = createNode(value);

    if (*head == NULL) {
        *head = newNode;
    } else {
        struct Node* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

// Function to delete a node with the given data from the linked list
void deleteNode(struct Node** head, int value) {
    if (*head == NULL) {
        printf("List is empty. Cannot delete.\n");
        return;
    }

    struct Node* temp = *head;
    struct Node* prev = NULL;

    // Check if the node to be deleted is the head
    if (temp != NULL && temp->data == value) {
        *head = temp->next;
        free(temp);
        printf("Deleted: %d\n", value);
        return;
    }

    // Search for the node to be deleted
    while (temp != NULL && temp->data != value) {
        prev = temp;
        temp = temp->next;
    }

    // If the node is not present
    if (temp == NULL) {
        printf("Data not found. Cannot delete.\n");
        return;
    }

    // Unlink the node from the linked list
    prev->next = temp->next;

    // Free the memory of the node
    free(temp);

    printf("Deleted: %d\n", value);
}

// Function to display the linked list
void displayList(struct Node* head) {
    if (head == NULL) {
        printf("List is empty.\n");
        return;
    }

    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

// Function to delete all nodes from the linked list
void deleteList(struct Node** head) {
    struct Node* current = *head;
    struct Node* next;

    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }

    *head = NULL;
}

// Main function for the menu-driven program
int main() {
    struct Node* head = NULL;

    int choice, data;

    do {
        printf("\n----- Menu -----\n");
        printf("1. Create linked list\n");
        printf("2. Display linked list\n");
        printf("3. Delete nodes\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                insertEnd(&head, 12);
                insertEnd(&head, 55);
                insertEnd(&head, 67);
                insertEnd(&head, 45);
                insertEnd(&head, 23);
                insertEnd(&head, 78);
                insertEnd(&head, 99);
                insertEnd(&head, 32);
                printf("Linked list created.\n");
                break;
            case 2:
                displayList(head);
                break;
            case 3:
                deleteNode(&head, 12);
                deleteNode(&head, 45);
                deleteNode(&head, 32);
                break;
            case 4:
                deleteList(&head);
                printf("Exiting program.\n");
                break;
            default:
                printf("Invalid choice. Please enter a valid option.\n");
        }
    } while (choice != 4);

    return 0;
}
This program allows you to create a singly linked list, display it, and delete nodes with specific data. The menu-driven interface lets you choose the operation you want to perform. Adjust as needed for your specific requirements.

User
Write a menu-driven program to Create a Binary Search Tree and traverse that tree in an Inorder fashion
ChatGPT
Certainly! Here's a menu-driven program in C to create a Binary Search Tree (BST) and traverse it in an Inorder fashion:

c
Copy code
#include <stdio.h>
#include <stdlib.h>

// Structure for a Binary Search Tree node
struct Node {
    int data;
    struct Node* left;
    struct Node* right;
};

// Function to create a new BST node
struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Memory allocation failed\n");
        exit(EXIT_FAILURE);
    }
    newNode->data = value;
    newNode->left = newNode->right = NULL;
    return newNode;
}

// Function to insert a value into the BST
struct Node* insertNode(struct Node* root, int value) {
    if (root == NULL) {
        return createNode(value);
    }

    if (value < root->data) {
        root->left = insertNode(root->left, value);
    } else if (value > root->data) {
        root->right = insertNode(root->right, value);
    }

    return root;
}

// Function to perform Inorder traversal of the BST
void inorderTraversal(struct Node* root) {
    if (root != NULL) {
        inorderTraversal(root->left);
        printf("%d ", root->data);
        inorderTraversal(root->right);
    }
}

// Function to delete all nodes from the BST
void deleteTree(struct Node* root) {
    if (root != NULL) {
        deleteTree(root->left);
        deleteTree(root->right);
        free(root);
    }
}

// Main function for the menu-driven program
int main() {
    struct Node* root = NULL;
    int choice, value;

    do {
        printf("\n----- Menu -----\n");
        printf("1. Insert into BST\n");
        printf("2. Inorder Traversal\n");
        printf("3. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter value to insert: ");
                scanf("%d", &value);
                root = insertNode(root, value);
                printf("Value %d inserted into BST.\n", value);
                break;
            case 2:
                printf("Inorder Traversal: ");
                inorderTraversal(root);
                printf("\n");
                break;
            case 3:
                deleteTree(root);
                printf("Exiting program.\n");
                break;
            default:
                printf("Invalid choice. Please enter a valid option.\n");
        }
    } while (choice != 3);

    return 0;
}
This program allows you to create a Binary Search Tree, insert values into it, and traverse the tree in an Inorder fashion. The menu-driven interface lets you choose the operation you want to perform. Adjust as needed for your specific requirements.

User
Write a menu-driven program for Stack implementation (PUSH, POP, and DISPLAY) using an array data structure
ChatGPT
Certainly! Here's a menu-driven program in C for Stack implementation using an array data structure with PUSH, POP, and DISPLAY operations:

c
Copy code
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 5

// Structure to represent the stack
struct Stack {
    int items[MAX_SIZE];
    int top;
};

// Function to initialize the stack
void initializeStack(struct Stack* stack) {
    stack->top = -1;
}

// Function to check if the stack is full
int isFull(struct Stack* stack) {
    return stack->top == MAX_SIZE - 1;
}

// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
    return stack->top == -1;
}

// Function to push an element onto the stack
void push(struct Stack* stack, int value) {
    if (isFull(stack)) {
        printf("Stack Overflow. Cannot push %d.\n", value);
        return;
    }

    stack->items[++stack->top] = value;
    printf("Pushed: %d\n", value);
}

// Function to pop an element from the stack
void pop(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack Underflow. Cannot pop.\n");
        return;
    }

    int poppedItem = stack->items[stack->top--];
    printf("Popped: %d\n", poppedItem);
}

// Function to display the elements of the stack
void display(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty.\n");
        return;
    }

    printf("Stack elements: ");
    for (int i = 0; i <= stack->top; i++) {
        printf("%d ", stack->items[i]);
    }
    printf("\n");
}

// Main function for the menu-driven program
int main() {
    struct Stack myStack;
    initializeStack(&myStack);

    int choice, value;

    do {
        printf("\n----- Menu -----\n");
        printf("1. Push\n");
        printf("2. Pop\n");
        printf("3. Display\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter value to push: ");
                scanf("%d", &value);
                push(&myStack, value);
                break;
            case 2:
                pop(&myStack);
                break;
            case 3:
                display(&myStack);
                break;
            case 4:
                printf("Exiting program.\n");
                break;
            default:
                printf("Invalid choice. Please enter a valid option.\n");
        }
    } while (choice != 4);

    return 0;
}
This program allows you to perform PUSH, POP, and DISPLAY operations on a stack using an array data structure. The menu-driven interface lets you choose the operation you want to perform. Adjust as needed for your specific requirements.

User
Write a Menu driven program to implement STACK using a Single Link List.
ChatGPT
Certainly! Here's a menu-driven program in C to implement a stack using a single linked list with PUSH, POP, and DISPLAY operations:

c
Copy code
#include <stdio.h>
#include <stdlib.h>

// Structure for a node in the linked list
struct Node {
    int data;
    struct Node* next;
};

// Structure to represent the stack
struct Stack {
    struct Node* top;
};

// Function to create a new node
struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Memory allocation failed\n");
        exit(EXIT_FAILURE);
    }
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

// Function to initialize the stack
void initializeStack(struct Stack* stack) {
    stack->top = NULL;
}

// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
    return stack->top == NULL;
}

// Function to push an element onto the stack
void push(struct Stack* stack, int value) {
    struct Node* newNode = createNode(value);
    newNode->next = stack->top;
    stack->top = newNode;
    printf("Pushed: %d\n", value);
}

// Function to pop an element from the stack
void pop(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack Underflow. Cannot pop.\n");
        return;
    }

    struct Node* poppedNode = stack->top;
    int poppedItem = poppedNode->data;
    stack->top = poppedNode->next;
    free(poppedNode);
    printf("Popped: %d\n", poppedItem);
}

// Function to display the elements of the stack
void display(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack is empty.\n");
        return;
    }

    printf("Stack elements: ");
    struct Node* current = stack->top;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

// Function to delete all nodes from the stack
void deleteStack(struct Stack* stack) {
    while (stack->top != NULL) {
        struct Node* temp = stack->top;
        stack->top = temp->next;
        free(temp);
    }
}

// Main function for the menu-driven program
int main() {
    struct Stack myStack;
    initializeStack(&myStack);

    int choice, value;

    do {
        printf("\n----- Menu -----\n");
        printf("1. Push\n");
        printf("2. Pop\n");
        printf("3. Display\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter value to push: ");
                scanf("%d", &value);
                push(&myStack, value);
                break;
            case 2:
                pop(&myStack);
                break;
            case 3:
                display(&myStack);
                break;
            case 4:
                deleteStack(&myStack);
                printf("Exiting program.\n");
                break;
            default:
                printf("Invalid choice. Please enter a valid option.\n");
        }
    } while (choice != 4);

    return 0;
}
This program allows you to perform PUSH, POP, and DISPLAY operations on a stack implemented using a single linked list. The menu-driven interface lets you choose the operation you want to perform. Adjust as needed for your specific requirements.

User
Write a program to Implement Binary Searching using Recursion
ChatGPT
Certainly! Here's a simple C program that implements binary searching using recursion:

c
Copy code
#include <stdio.h>

// Recursive binary search function
int binarySearch(int arr[], int low, int high, int target) {
    if (low <= high) {
        int mid = low + (high - low) / 2;

        // If the target is present at the middle
        if (arr[mid] == target)
            return mid;

        // If the target is smaller than the middle element, search in the left subarray
        if (arr[mid] > target)
            return binarySearch(arr, low, mid - 1, target);

        // If the target is larger than the middle element, search in the right subarray
        return binarySearch(arr, mid + 1, high, target);
    }

    // Target is not present in the array
    return -1;
}

// Driver program to test the binarySearch function
int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int n = sizeof(arr) / sizeof(arr[0]);
    int target;

    printf("Enter the element to search: ");
    scanf("%d", &target);

    int result = binarySearch(arr, 0, n - 1, target);

    if (result != -1)
        printf("Element %d found at index %d.\n", target, result);
    else
        printf("Element %d not found in the array.\n", target);

    return 0;
}
This program defines a binarySearch function that performs binary search recursively. The main function initializes an array, takes user input for the element to search, and then calls the binarySearch function to find the element's index. Adjust the array and target values as needed.

User
Write a menu-driven program for Queue implementation (ENQUEU, DEQUEUE, and DISPLAY) using an array data structure
ChatGPT
Certainly! Here's a menu-driven program in C for Queue implementation using an array data structure with ENQUEUE, DEQUEUE, and DISPLAY operations:

c
Copy code
#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 5

// Structure to represent the queue
struct Queue {
    int items[MAX_SIZE];
    int front, rear;
};

// Function to initialize the queue
void initializeQueue(struct Queue* queue) {
    queue->front = -1;
    queue->rear = -1;
}

// Function to check if the queue is empty
int isEmpty(struct Queue* queue) {
    return (queue->front == -1 && queue->rear == -1);
}

// Function to check if the queue is full
int isFull(struct Queue* queue) {
    return ((queue->rear + 1) % MAX_SIZE == queue->front);
}

// Function to enqueue an element into the queue
void enqueue(struct Queue* queue, int value) {
    if (isFull(queue)) {
        printf("Queue is full. Cannot enqueue.\n");
        return;
    }

    if (isEmpty(queue)) {
        queue->front = 0;
        queue->rear = 0;
    } else {
        queue->rear = (queue->rear + 1) % MAX_SIZE;
    }

    queue->items[queue->rear] = value;
    printf("Enqueued: %d\n", value);
}

// Function to dequeue an element from the queue
int dequeue(struct Queue* queue) {
    int dequeuedItem;

    if (isEmpty(queue)) {
        printf("Queue is empty. Cannot dequeue.\n");
        return -1;
    } else if (queue->front == queue->rear) {
        dequeuedItem = queue->items[queue->front];
        initializeQueue(queue);
    } else {
        dequeuedItem = queue->items[queue->front];
        queue->front = (queue->front + 1) % MAX_SIZE;
    }

    printf("Dequeued: %d\n", dequeuedItem);
    return dequeuedItem;
}

// Function to display the elements of the queue
void display(struct Queue* queue) {
    if (isEmpty(queue)) {
        printf("Queue is empty.\n");
        return;
    }

    int i = queue->front;
    printf("Queue elements: ");
    do {
        printf("%d ", queue->items[i]);
        i = (i + 1) % MAX_SIZE;
    } while (i != (queue->rear + 1) % MAX_SIZE);
    printf("\n");
}

// Main function for the menu-driven program
int main() {
    struct Queue myQueue;
    initializeQueue(&myQueue);

    int choice, value;

    do {
        printf("\n----- Menu -----\n");
        printf("1. Enqueue\n");
        printf("2. Dequeue\n");
        printf("3. Display\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter value to enqueue: ");
                scanf("%d", &value);
                enqueue(&myQueue, value);
                break;
            case 2:
                dequeue(&myQueue);
                break;
            case 3:
                display(&myQueue);
                break;
            case 4:
                printf("Exiting program.\n");
                break;
            default:
                printf("Invalid choice. Please enter a valid option.\n");
        }
    } while (choice != 4);

    return 0;
}
This program allows you to perform ENQUEUE, DEQUEUE, and DISPLAY operations on a queue using an array data structure. The menu-driven interface lets you choose the operation you want to perform. Adjust as needed for your specific requirements.

User
Write a menu-driven program to implement the Double Link List and perform the following operations i.Creation ii. Display iii. Deleting a specific node
ChatGPT
Certainly! Here's a menu-driven program in C to implement a doubly linked list and perform creation, display, and deleting a specific node operations:

c
Copy code
#include <stdio.h>
#include <stdlib.h>

// Structure for a node in the doubly linked list
struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
};

// Function to create a new node
struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Memory allocation failed\n");
        exit(EXIT_FAILURE);
    }
    newNode->data = value;
    newNode->prev = newNode->next = NULL;
    return newNode;
}

// Function to insert a node at the end of the doubly linked list
void insertEnd(struct Node** head, int value) {
    struct Node* newNode = createNode(value);

    if (*head == NULL) {
        *head = newNode;
    } else {
        struct Node* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
        newNode->prev = temp;
    }
}

// Function to delete a specific node with the given data
void deleteNode(struct Node** head, int value) {
    if (*head == NULL) {
        printf("List is empty. Cannot delete.\n");
        return;
    }

    struct Node* current = *head;

    // Search for the node with the given data
    while (current != NULL && current->data != value) {
        current = current->next;
    }

    // If the node is not present
    if (current == NULL) {
        printf("Data not found. Cannot delete.\n");
        return;
    }

    // Update the next pointer of the previous node
    if (current->prev != NULL) {
        current->prev->next = current->next;
    } else {
        *head = current->next;  // If the node to delete is the head
    }

    // Update the prev pointer of the next node
    if (current->next != NULL) {
        current->next->prev = current->prev;
    }

    // Free the memory of the deleted node
    free(current);

    printf("Deleted: %d\n", value);
}

// Function to display the elements of the doubly linked list
void display(struct Node* head) {
    if (head == NULL) {
        printf("List is empty.\n");
        return;
    }

    struct Node* temp = head;
    printf("Doubly Linked List elements (forward): ");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->next;
    }
    printf("\n");

    printf("Doubly Linked List elements (backward): ");
    while (temp != NULL) {
        printf("%d ", temp->data);
        temp = temp->prev;
    }
    printf("\n");
}

// Main function for the menu-driven program
int main() {
    struct Node* myDoublyList = NULL;

    int choice, value;

    do {
        printf("\n----- Menu -----\n");
        printf("1. Insert into doubly linked list\n");
        printf("2. Delete node from doubly linked list\n");
        printf("3. Display doubly linked list\n");
        printf("4. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                printf("Enter value to insert: ");
                scanf("%d", &value);
                insertEnd(&myDoublyList, value);
                break;
            case 2:
                printf("Enter value to delete: ");
                scanf("%d", &value);
                deleteNode(&myDoublyList, value);
                break;
            case 3:
                display(myDoublyList);
                break;
            case 4:
                printf("Exiting program.\n");
                break;
            default:
                printf("Invalid choice. Please enter a valid option.\n");
        }
    } while (choice != 4);

    return 0;
}
This program allows you to create a doubly linked list, insert values into it, delete nodes with specific data, and display the list. The menu-driven interface lets you choose the operation you want to perform. Adjust as needed for your specific requirements.

User
Write a program to implement Insertion Sort
ChatGPT
Certainly! Here's a simple C program to implement the Insertion Sort algorithm:

c
Copy code
#include <stdio.h>

// Function to perform Insertion Sort
void insertionSort(int arr[], int n) {
    int i, key, j;
    for (i = 1; i < n; i++) {
        key = arr[i];
        j = i - 1;

        // Move elements of arr[0..i-1] that are greater than key to one position ahead of their current position
        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j = j - 1;
        }
        arr[j + 1] = key;
    }
}

// Function to print an array
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

// Sample usage of insertionSort
int main() {
    int arr[] = {12, 11, 13, 5, 6};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: ");
    printArray(arr, n);

    insertionSort(arr, n);

    printf("Sorted array: ");
    printArray(arr, n);

    return 0;
}
This program defines a function insertionSort to perform the Insertion Sort algorithm on an array. The printArray function is used to display the contents of an array. The main function demonstrates the sorting process with a sample array. Adjust the array values and size as needed.

User
. Write a program to implement Heap Sort
ChatGPT
Certainly! Here's a C program to implement Heap Sort:

c
Copy code
#include <stdio.h>

// Function to perform Heapify on a subtree rooted with the given index
void heapify(int arr[], int n, int i) {
    int largest = i; // Initialize largest as the root
    int left = 2 * i + 1; // Left child
    int right = 2 * i + 2; // Right child

    // If left child is larger than root
    if (left < n && arr[left] > arr[largest])
        largest = left;

    // If right child is larger than the largest so far
    if (right < n && arr[right] > arr[largest])
        largest = right;

    // If the largest is not the root
    if (largest != i) {
        // Swap the root with the largest element
        int temp = arr[i];
        arr[i] = arr[largest];
        arr[largest] = temp;

        // Recursively heapify the affected sub-tree
        heapify(arr, n, largest);
    }
}

// Function to perform Heap Sort
void heapSort(int arr[], int n) {
    // Build a max heap
    for (int i = n / 2 - 1; i >= 0; i--)
        heapify(arr, n, i);

    // Extract elements from the heap one by one
    for (int i = n - 1; i > 0; i--) {
        // Swap the root with the last element
        int temp = arr[0];
        arr[0] = arr[i];
        arr[i] = temp;

        // Heapify the reduced heap
        heapify(arr, i, 0);
    }
}

// Function to print an array
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

// Sample usage of heapSort
int main() {
    int arr[] = {12, 11, 13, 5, 6, 7};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: ");
    printArray(arr, n);

    heapSort(arr, n);

    printf("Sorted array: ");
    printArray(arr, n);

    return 0;
}
This program defines functions heapify and heapSort to implement the Heap Sort algorithm on an array. The printArray function is used to display the contents of an array. The main function demonstrates the sorting process with a sample array. Adjust the array values and size as needed.

User
Write a program to implement Quick Sort
ChatGPT
Certainly! Here's a C program to implement the Quick Sort algorithm:

c
Copy code
#include <stdio.h>

// Function to partition the array and return the pivot index
int partition(int arr[], int low, int high) {
    int pivot = arr[high]; // Choose the rightmost element as the pivot
    int i = low - 1; // Index of smaller element

    for (int j = low; j < high; j++) {
        // If the current element is smaller than or equal to the pivot
        if (arr[j] <= pivot) {
            i++;

            // Swap arr[i] and arr[j]
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    // Swap arr[i+1] and arr[high] (put the pivot in its correct place)
    int temp = arr[i + 1];
    arr[i + 1] = arr[high];
    arr[high] = temp;

    return i + 1;
}

// Function to perform Quick Sort
void quickSort(int arr[], int low, int high) {
    if (low < high) {
        // Find the pivot index such that elements smaller than the pivot are on the left,
        // and elements greater than the pivot are on the right
        int pivotIndex = partition(arr, low, high);

        // Recursively sort the sub-arrays before and after the pivot
        quickSort(arr, low, pivotIndex - 1);
        quickSort(arr, pivotIndex + 1, high);
    }
}

// Function to print an array
void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}

// Sample usage of quickSort
int main() {
    int arr[] = {12, 11, 13, 5, 6, 7};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("Original array: ");
    printArray(arr, n);

    quickSort(arr, 0, n - 1);

    printf("Sorted array: ");
    printArray(arr, n);

    return 0;
}
This program defines functions partition and quickSort to implement the Quick Sort algorithm on an array. The printArray function is used to display the contents of an array. The main function demonstrates the sorting process with a sample array. Adjust the array values and size as needed.

User
Write a program to evaluate the postfix expression.
ChatGPT
Certainly! Here's a C program to evaluate a postfix expression:

c
Copy code
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

// Structure to represent a stack
struct Stack {
    int top;
    int capacity;
    int* array;
};

// Function to create a stack
struct Stack* createStack(int capacity) {
    struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));
    if (stack == NULL) {
        printf("Memory allocation failed\n");
        exit(EXIT_FAILURE);
    }
    stack->top = -1;
    stack->capacity = capacity;
    stack->array = (int*)malloc(stack->capacity * sizeof(int));
    if (stack->array == NULL) {
        printf("Memory allocation failed\n");
        exit(EXIT_FAILURE);
    }
    return stack;
}

// Function to check if the stack is empty
int isEmpty(struct Stack* stack) {
    return stack->top == -1;
}

// Function to push an element onto the stack
void push(struct Stack* stack, int value) {
    stack->array[++stack->top] = value;
}

// Function to pop an element from the stack
int pop(struct Stack* stack) {
    if (isEmpty(stack)) {
        printf("Stack Underflow. Cannot pop.\n");
        exit(EXIT_FAILURE);
    }
    return stack->array[stack->top--];
}

// Function to evaluate a postfix expression
int evaluatePostfix(char postfix[]) {
    struct Stack* stack = createStack(strlen(postfix));
    int i;

    for (i = 0; postfix[i]; ++i) {
        if (isdigit(postfix[i]))
            push(stack, postfix[i] - '0');
        else {
            int operand2 = pop(stack);
            int operand1 = pop(stack);

            switch (postfix[i]) {
                case '+':
                    push(stack, operand1 + operand2);
                    break;
                case '-':
                    push(stack, operand1 - operand2);
                    break;
                case '*':
                    push(stack, operand1 * operand2);
                    break;
                case '/':
                    push(stack, operand1 / operand2);
                    break;
            }
        }
    }
    return pop(stack);
}

// Sample usage of evaluatePostfix
int main() {
    char postfix[] = "23*5+";
    printf("Postfix expression: %s\n", postfix);
    printf("Result: %d\n", evaluatePostfix(postfix));
    return 0;
}
This program defines a Stack structure and functions to create, push, and pop elements from the stack. The evaluatePostfix function uses a stack to evaluate a postfix expression. Adjust the postfix array as needed with your postfix expression.

User
Write a program to create a Single link list, display the list, reverse that single link list and show the reversed list.
ChatGPT
Certainly! Here's a C program that creates a singly linked list, displays the original list, reverses the linked list, and displays the reversed list:

c
Copy code
#include <stdio.h>
#include <stdlib.h>

// Structure for a node in the linked list
struct Node {
    int data;
    struct Node* next;
};

// Function to create a new node
struct Node* createNode(int value) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Memory allocation failed\n");
        exit(EXIT_FAILURE);
    }
    newNode->data = value;
    newNode->next = NULL;
    return newNode;
}

// Function to insert a node at the end of the linked list
void insertEnd(struct Node** head, int value) {
    struct Node* newNode = createNode(value);

    if (*head == NULL) {
        *head = newNode;
    } else {
        struct Node* temp = *head;
        while (temp->next != NULL) {
            temp = temp->next;
        }
        temp->next = newNode;
    }
}

// Function to display the elements of the linked list
void displayList(struct Node* head) {
    if (head == NULL) {
        printf("List is empty.\n");
        return;
    }

    struct Node* temp = head;
    while (temp != NULL) {
        printf("%d -> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

// Function to reverse the linked list
struct Node* reverseList(struct Node* head) {
    struct Node *prev, *current, *next;
    prev = NULL;
    current = head;

    while (current != NULL) {
        next = current->next;
        current->next = prev;
        prev = current;
        current = next;
    }

    return prev; // 'prev' is now the new head of the reversed list
}

// Main function for the program
int main() {
    struct Node* myList = NULL;

    // Insert elements into the linked list
    insertEnd(&myList, 12);
    insertEnd(&myList, 55);
    insertEnd(&myList, 67);
    insertEnd(&myList, 45);
    insertEnd(&myList, 23);

    // Display the original linked list
    printf("Original Linked List:\n");
    displayList(myList);

    // Reverse the linked list
    myList = reverseList(myList);

    // Display the reversed linked list
    printf("\nReversed Linked List:\n");
    displayList(myList);

    return 0;
}

Comments

Popular posts from this blog

Code