C program to insert an element to the right of key node in a linked list

PROGRAM:

#include <stdio.h>
#include <stdlib.h>
//Creating structure
struct node
{
    int data;
    struct node *link;
}*first=NULL;

struct node * create(struct node *);
void disp(struct node *);
struct node * insRight(struct node *,int);

//main function
int main()
{
    int key;
    printf("\nEnter elements to create linked list\n");
    first=create(first);
    printf("\nLinked list created!\n");
    disp(first);
    printf("\nEnter key node\n");
    scanf("%d",&key);
    first=insRight(first,key);
    disp(first);
    return 0;
}

//function to create linked list
struct node * create(struct node *first)
{
    struct node *new_node;
    int choice=0;

    do
    {
        new_node=(struct node *)malloc(sizeof(struct node));
        printf("\nEnter  an element\n");
        scanf("%d",&new_node->data);
        new_node->link=NULL;
        if(first==NULL)
            first=new_node;
        else
        {
            new_node->link=first;
            first=new_node;
        }
        printf("\nEnter 1 to continue, 0 to stop\n");
        scanf("%d",&choice);
    }while(choice==1);
    return first;
}

//function to display linked list
void disp(struct node *first)
{
    struct node *temp;
    if(first==NULL)
        printf("\nDisplay not possible, Linked list empty\n");
    else if(first->link==NULL)
    {
        printf("Displaying linked list elements:\n");
        printf("%d",first->data);
    }
    else
    {
        printf("Displaying linked list elements:\n");
        temp=first;
        while(temp!=NULL)
        {
            printf("%d\t",temp->data);
            temp=temp->link;
        }
    }
}

//function to search and insert elements to the right of key node in the linked list
struct node * insRight(struct node * first,int key)
{
    int cnt=0;
    struct node *temp=NULL,*new_node;
    if(first==NULL)
    {
        printf("\nKey not found\n");
        return first;
    }
    else
    {
        if(first->data==key)
        {
        printf("\nKey found\n");
        new_node=(struct node *)malloc(sizeof(struct node));
        printf("Enter an element\n");
        scanf("%d",&new_node->data);
        new_node->link=first->link;
        first->link=new_node;
        ++cnt;
        }
        temp=first->link;
        while(temp!=NULL)
        {
            if(temp->data==key)
            {
                printf("\nKey found\n");
                new_node=(struct node *)malloc(sizeof(struct node));
                printf("Enter an element\n");
                scanf("%d",&new_node->data);
                new_node->link=temp->link;
                temp->link=new_node;
                ++cnt;
                temp=temp->link;
            }
            else
            {
                temp=temp->link;
            }
        }
        if(cnt==0)
            printf("\nKey not found\n");
        else
            printf("%d keys inserted\n",cnt);
        return first;
    }

}



OUTPUT:

TRIAL 1:

Enter elements to create linked list

Enter  an element
10

Enter 1 to continue, 0 to stop
1

Enter  an element
20

Enter 1 to continue, 0 to stop
1

Enter  an element
10

Enter 1 to continue, 0 to stop
1

Enter  an element
30

Enter 1 to continue, 0 to stop
1

Enter  an element
10

Enter 1 to continue, 0 to stop
1

Enter  an element
40

Enter 1 to continue, 0 to stop
0

Linked list created!
Displaying linked list elements:
40      10      30      10      20      10
Enter key node
10

Key found
Enter an element
50

Key found
Enter an element
60

Key found
Enter an element
70
3 keys inserted
Displaying linked list elements:
40      10      50      30      10      60      20      10      70



TRIAL 2:

Enter elements to create linked list

Enter  an element
10

Enter 1 to continue, 0 to stop
1

Enter  an element
20

Enter 1 to continue, 0 to stop
1

Enter  an element
30

Enter 1 to continue, 0 to stop
1

Enter  an element
40

Enter 1 to continue, 0 to stop
1

Enter  an element
50

Enter 1 to continue, 0 to stop
0

Linked list created!
Displaying linked list elements:
50      40      30      20      10
Enter key node
60

Key not found
Displaying linked list elements:
50      40      30      20      10

Popular Posts