DATA STRUCTURE-LINKED LIST(DELETE,INSERT,CREATE,DISPLAY OPERATIONS):MENU DRIVEN PEOGRAM:-

 #include <stdio.h>

#include <conio.h>

#include <stdlib.h>

typedef struct node

{

    int data;

    struct node *next;

} NODE;


NODE *create_list(NODE *list)

{

    NODE *temp, *new_node;

    int n, i;

    printf("\n Enter how many nodes you want:");

    scanf("%d", &n);

    for (i = 1; i <= n; i++)

    {

        new_node = (NODE *)malloc(sizeof(struct node));

        new_node->next = NULL;

        printf("\n Enter [%d] list element:", i);

        scanf("%d", &new_node->data);

        if (list == NULL)

        {

            list = temp = new_node;

        }

        else

        {

            temp->next = new_node;

            temp = new_node;

        }

    }

    return list;

}

void display(NODE *list)

{

    while (list != NULL)

    {

        printf("\t%d", list->data);

        list = list->next;

    }

}

NODE *insert(NODE *list, int p, int n)

{

    NODE *temp, *new_node, *temp1;

    int i;

    new_node = (NODE *)malloc(sizeof(struct node));

    new_node->data = n;

    new_node->next = NULL;

    if (p == 1)

    {

        new_node->next = list;

        list = new_node;

    }

    else

    {

        for (i = 1, temp = list; i < p - 1 && temp != NULL; i++, temp = temp->next)

            ;


        if (temp == NULL)

        {

            printf("\n position out of range!");

            return list;

        }

        else

        {

            new_node->next = temp->next;

            temp->next = new_node;

            return list;

        }

    }

}

NODE *deleteByValue(NODE *list, int n)

{

    NODE *temp = list, *temp1;

    if (list->data == n)

    {

        list = temp->next;

        free(temp);

        return list;

    }


    while (temp->next != NULL)

    {

        if (temp->next->data == n)

        {

            temp1 = temp->next;

            temp->next = temp1->next;

            free(temp1);

            return list;

        }

        temp = temp->next;

    }

    // return list;//if all similar element have to remove from list!

    if (temp->next == NULL)

    {

        printf("\n The element not found!");

        return list;

    }

}

NODE *deleteByPos(NODE *list, int p)

{

    NODE *temp = list, *temp1;

    int i;

    if (p == 1)

    {

        list = temp->next;

        free(temp);

        return list;

    }


    for (i = 1, temp = list; temp->next != NULL && i < p - 1; i++, temp = temp->next)

        ;


    if (temp == NULL)

    {

        printf("\n Position out of range!");

        return list;

    }

    temp1 = temp->next;

    temp->next = temp1->next;

    free(temp1);

    return list;

}

int main()

{

    NODE *list = NULL;

    int choice, pos, num;

    do

    {

        printf("\n------------------------------------");

        printf("\n 1.create \n 2.display \n3.insert \n4.delete by value \n 5.delete by position \nExit");

        printf("\n------------------------------------");

        printf("\n Enter your choice:");

        scanf("%d", &choice);

        switch (choice)

        {

        case 1:

            list = create_list(list);

            break;

        case 2:

            display(list);

            break;

        case 3:

            printf("\n Enter position and data for inserting in list:");

            scanf("%d%d", &pos, &num);

            list = insert(list, pos, num);

            break;

        case 4:

            printf("\n Enter value for deleting into list:");

            scanf("%d", &num);

            list = deleteByValue(list, num);

            break;

        case 5:

            printf("\n Enter position for delete the element:");

            scanf("%d", &pos);

            list = deleteByPos(list, pos);

            break;

        }

    } while (choice != 6);

    getch();

    return 0;

}

Comments

Popular posts from this blog

PHP ALL ASSIGNMENT PDF

DATA STRUCTURE ALL PDF(LAB ASSIGNMENTS)

DATA STRUCTURE :ASSIGNMENT NO.8:TREE