Skip to main content

Linked List Insertion

 Linked List Insertion at Begin, Insertion at End, Insertion at Position

Program:

#include<stdlib.h>
#include <stdio.h>
     
void create();
void display();
void insert_at_begin();
void insert_at_end();
void insert_at_position();
 
struct node
{
        int data;
        struct node *next;
};
struct node *head=NULL;
int main()     
{
        int choice;
        while(1){
               
                
                printf("\n 1.Create \n");
                printf("\n 2.Display \n");
                printf("\n 3.Insert at the beginning \n");
                printf("\n 4.Insert at the end \n");
                printf("\n 5.Insert at specified position \n");
                printf("\n 6.Exit \n");
                printf("\n--------------------------------------\n");
                printf("Enter your choice:\t");
                scanf("%d",&choice);
                switch(choice)
                {
                        case 1:
                                        create();
                                        break;
                        case 2:
                                        display();
                                        break;
                        case 3: 
                                        insert_at_begin();
                                        break;
                        case 4:
                                        insert_at_end();
                                        break;
                        case 5:
                                        insert_at_position();
                                        break;
                        
                        case 6:
                                        exit(0);
                                        break;
                             
                        default:
                                        printf("Invalid choice");
                                        break;
                }
        }
        return 0;
}
void create()
{
        struct node *newnode,*temp;
        newnode=(struct node *)malloc(sizeof(struct node));
        if(newnode==NULL)
        {
                printf("\nOut of Memory Space:\n");
                exit(0);
        }
        printf("\nEnter the data value for the node:\t");
        scanf("%d",&newnode->data);
        newnode->next=NULL;
        if(head==NULL)
        {
                head=newnode;
        }
        else
        {
                temp=head;
                while(temp->next!=NULL)
                {
                        temp=temp->next;
                }
                temp->next=newnode;
        }
}
void display()
{
        struct node *temp;
        if(head==NULL)
        {
                printf("\nList is empty:\n");
                return;
        }
        else
        {
                temp=head;
                printf("\nThe List elements are:\n");
                while(temp!=NULL)
                {
                        printf("%d\t",temp->data );
                        temp=temp->next ;
                }
        }
}
void insert_at_begin()
{
        struct node *newnode;
        newnode=(struct node *)malloc(sizeof(struct node));
        if(newnode==NULL)
        {
                printf("\nOut of Memory Space:\n");
                return;
        }
        printf("\nEnter the data value for the node:\t" );
        scanf("%d",&newnode->data);
        newnode->next =NULL;
        if(head==NULL)
        {
                head=newnode;
        }
        else
        {
                newnode->next=head;
                head=newnode;
        }
}
void insert_at_end()
{
        struct node *newnode,*temp;
        newnode=(struct node *)malloc(sizeof(struct node));
        if(newnode==NULL)
        {
                printf("\nOut of Memory Space:\n");
                return;
        }
        printf("\nEnter the data value for the node:\t" );
        scanf("%d",&newnode->data);
        newnode->next =NULL;
        if(head==NULL)
        {
                head=newnode;
        }
        else
        {
                temp=head;
                while(temp->next !=NULL)
                {
                        temp=temp->next ;
                }
            temp->next =newnode;
        }
}
void insert_at_position()
{
        struct node *temp,*newnode;
        int i,pos;
        newnode=(struct node *)malloc(sizeof(struct node));
        if(newnode==NULL)
        {
                printf("\nOut of Memory Space\n");
                return;
        }
        printf("\nEnter the position for the new node to be inserted:\t");
        scanf("%d",&pos);
        printf("\nEnter the data value of the node:\t");
        scanf("%d",&newnode->data) ;
  
        newnode->next=NULL;
        if(pos==0)
        {
                newnode->next=head;
                head=newnode;
        }
        else
        {
                for(i=0,temp=head;i<pos-1;i++) { temp=temp->next;
                        if(temp==NULL)
                        {
                                printf("\nInvalid Position!!!\n");
                                return;
                        }
                }
                newnode->next =temp->next ;
                temp->next=newnode;
        }
}



Output:




Comments

Popular posts from this blog

CLASSIFICATION OF EMBEDDED SYSTEMS

  CLASSIFICATION OF EMBEDDED SYSTEMS Some of the criteria used in the classification of embedded systems are as follows: (1) Based on generation (2) Complexity and performance requirements (3) Based on deterministic behavior (4) Based on triggering Classification Based on Generation First Generation :  The early embedded systems were built around 8bit microprocessors like 8085 and Z80, and 4bit microcontrollers. Simple in hardware circuits with firmware developed in Assembly code. Digital telephone keypads, stepper motor control units etc. are examples of this. Second Generation   :  These are embedded systems built around 16bit microprocessors and 8 or 16 bit microcontrollers, following the first generation embedded systems. The instruction set for the second generation processors/controllers were much more complex and powerful than the first generation processors/controllers. Some of the second generation embedded systems contained embedded operating...

Microprocessor vs Microcontroller

  Difference between a microprocessor and a microcontroller Microprocessor A silicon chip representing a central processing unit (CPU), which is capable of performing arithmetic as well as logical operations according to a predefined set of instructions. It is a dependent unit. It requires the combination of other chips like timers, program and data memory chips, interrupt controllers, etc. for functioning. Most of the time general purpose in design and operation. Doesn't contain a built in I/O port. The I/O port functionality needs to be implemented with the help of external programmable peripheral interface chips like 8255. Targeted for high end market where performance is important. Limited power saving options compared to microcontrollers. Microcontroller A microcontroller is a highly integrated chip that contains a CPU, scratchpad RAM, special and general purpose register arrays, on chip ROM/FLASH memory for program storage, timer and interrupt control units and dedicated I/O ...

ChatGPT

 What is ChatGPT ? ChatGPT  is an AI (Artificial Intelligence) chatbot , which is developed by OpenAI.  It takes text as input and responds to it like a human. It was launched in NOVEMBER 2022. In ChatGPT ,GPT stands for Generative Pre-trained Transformer. It is among the latest sensations in the field of AI. How to access ChatGPT ? To use ChatGPT the first step is to sign up on OpenAI and create an account on it. Here is the link " https://openai.com/blog/chatgpt/ ". By clicking the above link the interface looks like, Now, click on the "TRY CHATGPT" and signup/login using your credentials. Is ChatGPT free to use ? Free ChatGPT service is not available anywhere,but ChatGPT will only be available to U.S customers for now. On February 2023 OpenAI has announced that it was introducing "ChatGPT Plus"  by launching a pilot subscription plan for ChatGPT. The new subscription plan, ChatGPT Plus, will be available for $20/month, and subscribers will receive a num...