LINK LIST



linked list is a way to store a collection of elements. Like an array these can be character or integers. Each element in a linked list is stored in the form of a node.


singly link list:- 


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

typedef struct node
{
    int info;
    struct node *next;
} NODE;

int creatlist(NODE *head)
{
    NODE *newnode, *last;
    int n, i;
    last = head;
    printf("\n how many node u enter:");
    scanf("%d", &n);
    for (i = 0; i < n; i++)
    {
        newnode = (NODE *)malloc(sizeof(NODE));
        printf("\n enter the node:");
        scanf("%d", &newnode->info);

        newnode->next = NULL;
        last ->next = newnode;
        last  = newnode;
    }
}

int display(NODE *head)
{
    NODE *temp = head;
    for (temp = head->next; temp != NULL; temp = temp->next)
    {
        printf("\n%d\n", temp->info);
    }
}
int search(NODE *head, int n)
{
    int pos = 1;
    NODE *temp = head;
    for (temp = head->next; temp != NULL; temp = temp->next)
    {
        if (temp->info == n)
        {
            return pos;
        }
        pos++;
    }
    return -1;
}

void insert(NODE *head, int num, int pos)
{
    NODE *temp, *newnode;
    int i;
    for (temp = head, i = 1; (temp != NULL) && (i <= pos - 1); i++)
        temp = temp->next;
    if (temp == NULL)
    {
        printf("\nposition out of range");
        return;
    }
    newnode = (NODE *)malloc(sizeof(NODE));
    newnode->info = num;
    newnode->next = temp->next;
    temp->next = newnode;
}

int sort(NODE *head)
{
    NODE *temp, *temp1;
    int num;
    for (temp = head->next; temp->next != NULL; temp = temp->next)
        for (temp1 = temp->next; temp1 != NULL; temp1 = temp1->next)
            if (temp->info > temp1->info)
            {
                num = temp->info;
                temp->info = temp1->info;
                temp1->info = num;
            }
}
void deletebyvalue(NODE *head, int num)
{
    NODE *temp, *temp1;
    for (temp = head; temp->next != NULL; temp = temp->next)
        if (temp->next->info == num)
        {
            temp1 = temp->next;
            temp->next = temp1->next;
            free(temp1);
            return;
        }
    printf("\n element not found!!\n");
}
void deletebyposition(NODE *head, int p)
{
    NODE *temp, *nextnode;
    int i = 1;
    temp = head;
    while (i <= p - 1)
    {
        temp = temp->next;
        if (temp->next == NULL)
        {
            printf("\n position out of range!!");
            return;
        }
        i++;
    }
    nextnode = temp->next;
    temp->next = nextnode->next;
    free(nextnode);
}

int main()
{
    NODE *head;
    head = (NODE *)malloc(sizeof(NODE));
    int choice, key, ans, n, p;
    do
    {
        printf("\n1:createlist");
        printf("\n2:display");
        printf("\n3:search");
        printf("\n4:insert");
        printf("\n5:sort");
        printf("\n6:delete by value");
        printf("\n7:delete by position");

        printf("\nheyy pls. enter your choice:");
        scanf("%d", &choice);

        switch (choice)
        {
        case 1:
            creatlist(head);
            break;
        case 2:
            display(head);
            break;
        case 3:
            printf("\nenter the key:");
            scanf("%d", &key);
            ans = search(head, key);
            if (ans == -1)
                printf("\n ohh ur key not found!!!");
            else
                printf("\n ur key found at position %d", ans);
            break;
        case 4:
            printf("\nenter the element and position:");
            scanf("%d%d", &n, &p);
            insert(head, n, p);
            display(head);
            break;
        case 5:
            sort(head);
            display(head);
            break;
        case 6:
            printf("\n enter the value to be deleted: ");
            scanf("%d", &n);
            deletebyvalue(head, n);
            display(head);
            break;
        case 7:
            printf("\n enter the position to be deleted:");
            scanf("%d", &p);
            deletebyposition(head, p);
            display(head);
            break;
        default:
            printf("\nInvalid input!!!!");
        }
    } while (choice <= 7);

    return 0;
}

Comments

Popular posts from this blog

Full Stack Developement Practical Slips Programs

Practical slips programs : Machine Learning

Some slips for full stacks