DSA(linked list)

Linked List Program

Linked List Program in C

write a menu driven program for all the operations in linked list


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

// Definition of the node structure
struct node {
    int info; // Data to store
    struct node *link; // Pointer to the next node
};

// Function to create a new node
struct node *createnode(int x) {
    struct node *new_node = (struct node *) malloc(sizeof(struct node));
    if (new_node == NULL) {
        printf("Memory allocation failed\\n");
        exit(1);
    }
    new_node->info = x;
    new_node->link = NULL;
    return new_node;
}

// Function to insert a node at the beginning of the list
struct node *insert_beg(struct node *first, int x) {
    struct node *new_node = createnode(x);
    new_node->link = first;
    return new_node;
}

// Function to insert a node at the end of the list
struct node *insert_end(struct node *first, int x) {
    struct node *new_node = createnode(x);
    if (first == NULL) {
        return new_node;
    }
    struct node *ptr = first;
    while (ptr->link != NULL) {
        ptr = ptr->link;
    }
    ptr->link = new_node;
    return first;
}

// Function to insert a node after a given node
struct node *insert_after(struct node *first, int x, int n) {
    struct node *ptr = first;
    while (ptr != NULL && ptr->info != n) {
        ptr = ptr->link;
    }
    if (ptr == NULL) {
        printf("Node %d not found\\n", n);
        return first;
    }
    struct node *new_node = createnode(x);
    new_node->link = ptr->link;
    ptr->link = new_node;
    return first;
}

// Function to insert a node before a given node
struct node *insert_before(struct node *first, int x, int n) {
    if (first == NULL) {
        printf("List is empty\\n");
        return first;
    }
    if (first->info == n) {
        return insert_beg(first, x);
    }
    struct node *ptr = first;
    struct node *prev = NULL;
    while (ptr != NULL && ptr->info != n) {
        prev = ptr;
        ptr = ptr->link;
    }
    if (ptr == NULL) {
        printf("Node %d not found\\n", n);
        return first;
    }
    struct node *new_node = createnode(x);
    prev->link = new_node;
    new_node->link = ptr;
    return first;
}

// Function to display the linked list
void display(struct node *first) {
    struct node *ptr = first;
    if (ptr == NULL) {
        printf("Linked list is empty\\n");
        return;
    }
    while (ptr != NULL) {
        printf("%d\\t", ptr->info);
        ptr = ptr->link;
    }
    printf("\\n");
}

// Function to delete the first node
struct node *delete_beg(struct node *first) {
    if (first == NULL) {
        printf("Linked list is empty\\n");
        return first;
    }
    struct node *temp = first;
    first = first->link;
    free(temp);
    return first;
}

// Function to delete the last node
struct node *delete_end(struct node *first) {
    if (first == NULL) {
        printf("Linked list is empty\\n");
        return first;
    }
    if (first->link == NULL) {
        free(first);
        return NULL;
    }
    struct node *ptr = first;
    while (ptr->link->link != NULL) {
        ptr = ptr->link;
    }
    free(ptr->link);
    ptr->link = NULL;
    return first;
}

// Function to delete a specific node
struct node *delete_given_node(struct node *first, int x) {
    if (first == NULL) {
        printf("Linked list is empty\\n");
        return first;
    }
    if (first->info == x) {
        return delete_beg(first);
    }
    struct node *ptr = first;
    while (ptr->link != NULL && ptr->link->info != x) {
        ptr = ptr->link;
    }
    if (ptr->link == NULL) {
        printf("Node %d not found\\n", x);
        return first;
    }
    struct node *temp = ptr->link;
    ptr->link = temp->link;
    free(temp);
    return first;
}

// Function to count the number of nodes in the list
int count_node(struct node *first) {
    int count = 0;
    struct node *ptr = first;
    while (ptr != NULL) {
        count++;
        ptr = ptr->link;
    }
    return count;
}

// Function to search for a node by value
void search_node(struct node *first, int x) {
    struct node *ptr = first;
    while (ptr != NULL) {
        if (ptr->info == x) {
            printf("Node %d found\\n", x);
            return;
        }
        ptr = ptr->link;
    }
    printf("Node %d not found\\n", x);
}

int main() {
    int choice, x, n;
    struct node *first = NULL;
    do {
        printf("\\nPress 1 to insert node at the beginning\\n");
        printf("Press 2 to insert node at the end\\n");
        printf("Press 3 to insert node after the given node\\n");
        printf("Press 4 to insert node before the given node\\n");
        printf("Press 5 to delete first node\\n");
        printf("Press 6 to delete last node\\n");
        printf("Press 7 to count nodes\\n");
        printf("Press 8 to search node\\n");
        printf("Press 9 to delete given node\\n");
        printf("Press 10 to display\\n");
        printf("Press 0 to exit\\n");

        scanf("%d", &choice);
        switch (choice) {
            case 1:
                printf("Enter node to insert: ");
                scanf("%d", &x);
                first = insert_beg(first, x);
                break;
            case 2:
                printf("Enter node to insert: ");
                scanf("%d", &x);
                first = insert_end(first, x);
                break;
            case 3:
                printf("Enter node to insert: ");
                scanf("%d", &x);
                printf("Enter the node to insert after it: ");
                scanf("%d", &n);
                first = insert_after(first, x, n);
                break;
            case 4:
                printf("Enter node to insert: ");
                scanf("%d", &x);
                printf("Enter the node to insert before it: ");
                scanf("%d", &n);
                first = insert_before(first, x, n);
                break;
            case 5:
                first = delete_beg(first);
                break;
            case 6:
                first = delete_end(first);
                break;
            case 7:
                printf("Number of nodes: %d\\n", count_node(first));
                break;
            case 8:
                printf("Search node: ");
                scanf("%d", &x);
                search_node(first, x);
                break;
            case 9:
                printf("Node to delete: ");
                scanf("%d", &x);
                first = delete_given_node(first, x);
                break;
            case 10:
                printf("\\nNodes are:\\n");
                display(first);
                break;
            case 0:
                exit(0);
            default:
                printf("Enter a valid choice\\n");
                break;
        }
    } while (1);
    return 0;
}
    

Comments

Popular posts from this blog

OOPS

JAVA Basic program(practical-2)

java basic program(practical-1)