linked list insert at rear and front and display.

 hey 


/*
    author: patel divyakumar bharatbhai
    date: 27/11/2022
    problem: insertion of singly Linked List
*/

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

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

void insertAtFront();
void insertAtRear();
void displayLinkedList();
void deleteAtFront();
void insertAtMid();

int main()
{
    int x, ch;
    printf("1. Insert value at rear(end) \n2. Insert value at at front \n3. Display \n4. Delete at front\n5. Insert at middle(ascending order) \n6. exit \n");
    do
    {
        printf("Enter your choice: ");
        scanf("%d", &ch);

        switch (ch)
        {
        case 1:
            printf("Enter the value: ");
            scanf("%d", &x);
            insertAtRear(x);
            break;

        case 2:
            printf("Enter the value: ");
            scanf("%d", &x);
            insertAtFront(x);
            break;

        case 3:
            displayLinkedList();
            break;

        case 4:
            deleteAtFront();
            break;

        case 5:
            printf("Enter the value: ");
            scanf("%d", &x);
            insertAtMid(x);
            break;

        default:
            printf("BYE...");
            free(head);
            exit(0);
            break;
        }
    } while (1);
    return 0;
}

void insertAtFront(int x)
{
    struct node *new;

    new = (struct node *)malloc(sizeof(struct node));
    new->data = x;
    new->next = head;

    head = new;
}

void insertAtRear(int x)
{
    struct node *new, *temp;

    new = (struct node *)malloc(sizeof(struct node));
    new->data = x;
    new->next = NULL;

    if (head == NULL)
    {
        head = new;
    }
    else
    {
        temp = head;
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        temp->next = new;
    }
}

void displayLinkedList()
{
    struct node *temp;
    if (head == NULL)
    {
        printf("\nLinked List is Empty \n");
    }
    else
    {
        temp = head;
        while (temp->next != NULL)
        {
            printf("%d\n", temp->data);
            temp = temp->next;
        }
        printf("%d\n", temp->data);
    }
}

void deleteAtFront()
{
    struct node *new;
    if (head == NULL)
    {
        printf("Linked List is Empty\n");
    }
    else
    {
        new = head;
        printf("Deleted number is %d", new->data);
        new = new->next;
        head = new;
    }
}

void insertAtMid(int x)
{
    struct node *new, *temp, *temp2;
    
    new = (struct node *)malloc(sizeof(struct node));
    new->data = x;

    temp = head;
    if (head == NULL)
    {
        head = new;
        new->next = NULL;
    }
    else if (temp->data > new->data)
    {
        new->next = head;
        head = new;
    }
    else
    {
        temp2 = temp;
        while (temp->next != NULL)
        {
            if (temp->data < new->data)
            {
                temp2 = temp;
                temp = temp->next;
            }
            else
            {
                break;
            }
        }
        if (temp->next == NULL && temp->data < new->data)
        {
            temp->next = new;
            new->next = NULL;
        }
        else
        {
            temp2->next = new;
            new->next = temp;
        }
    }
} 

Post a Comment

0 Comments