insertion singly linked list in c

/**
 * C program to insert a new node at the beginning of a Singly Linked List
 */

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


/* Structure of a node */
struct node {
    int data;          // Data 
    struct node *next; // Address 
}*head;


void createList(int n);
void insertNodeAtBeginning(int data);
void displayList();


int main()
{
    int n, data;

    /*
     * Create a singly linked list of n nodes
     */
    printf("Enter the total number of nodes: ");
    scanf("%d", &n);
    createList(n);

    printf("\nData in the list \n");
    displayList();

    /*
     * Insert data at the beginning of the singly linked list
     */
    printf("\nEnter data to insert at beginning of the list: ");
    scanf("%d", &data);
    insertNodeAtBeginning(data);

    printf("\nData in the list \n");
    displayList();

    return 0;
}


/*
 * Create a list of n nodes
 */
void createList(int n)
{
    struct node *newNode, *temp;
    int data, i;

    head = (struct node *)malloc(sizeof(struct node));

    /*
     * If unable to allocate memory for head node
     */
    if(head == NULL)
    {
        printf("Unable to allocate memory.");
    }
    else
    {
        /*
         * Input data of node from the user
         */
        printf("Enter the data of node 1: ");
        scanf("%d", &data);

        head->data = data; // Link data field with data
        head->next = NULL; // Link address field to NULL

        temp = head;

        /*
         * Create n nodes and adds to linked list
         */
        for(i=2; i<=n; i++)
        {
            newNode = (struct node *)malloc(sizeof(struct node));

            /* If memory is not allocated for newNode */
            if(newNode == NULL)
            {
                printf("Unable to allocate memory.");
                break;
            }
            else
            {
                printf("Enter the data of node %d: ", i);
                scanf("%d", &data);

                newNode->data = data; // Link data field of newNode with data
                newNode->next = NULL; // Link address field of newNode with NULL

                temp->next = newNode; // Link previous node i.e. temp to the newNode
                
                temp = temp->next; 
            }
        }

        printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
    }
}


/*
 * Create a new node and inserts at the beginning of the linked list.
 */
void insertNodeAtBeginning(int data)
{
    struct node *newNode;

    newNode = (struct node*)malloc(sizeof(struct node));

    if(newNode == NULL)
    {
        printf("Unable to allocate memory.");
    }
    else
    {
        newNode->data = data; // Link data part
        newNode->next = head; // Link address part

        head = newNode;          // Make newNode as first node

        printf("DATA INSERTED SUCCESSFULLY\n");
    }
}


/*
 * Display entire list
 */
void displayList()
{
    struct node *temp;

    /*
     * If the list is empty i.e. head = NULL
     */
    if(head == NULL)
    {
        printf("List is empty.");
    }
    else
    {
        temp = head;
        while(temp != NULL)
        {
            printf("Data = %d\n", temp->data); // Print data of current node
            temp = temp->next;                 // Move to next node
        }
    }
}

3.33
6

                                    #include &lt;iostream&gt;
// Linked list
struct node {
    int data ;
    node * link;
};


node * Node(int data) {
    node * temp = new node();
    temp-&gt;data = data;
    temp-&gt;link = NULL;
    return temp;
}

void append(node ** head, int data) {

    if(*head == NULL) {
        *head =  Node(data);
    }else {
        node * temp = * head;
        while (temp-&gt;link != NULL) {
            temp=temp-&gt;link;
        }
        temp-&gt;link = Node(data);

    }

}


// insertion at begining

void insertBeg(node **head , int data) {
    if(*head == NULL) {
        * head = Node(data);
    }else {
        node * temp = Node(data);
        temp-&gt;link = *head;
        *head = temp;

    }


}
// insert at specific position

void addafter(node * head , int loc , int data) {
    node * temp , * r ;
    temp = head ;
    for( int i = 0 ; i&lt;loc;i++ ) {
        temp = temp-&gt;link;
        if(temp == NULL) {
            cout&lt;&lt;&quot;there ar less elemtns&quot; ;
            return;
        }

    }
    // insert new node
    r = Node(data);
    r-&gt;link = temp-&gt;link;
    temp-&gt;link = r;


}

void display(node * head) {

    node * temp = head;
    while(temp!= NULL) {
        cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
        temp = temp-&gt;link;
    }
}



int main() {
    node * head = NULL;
    append(&amp;head,5);
    append(&amp;head,5);
    append(&amp;head,5);
    append(&amp;head,5);
    display(head);
    cout&lt;&lt;endl;
    insertBeg(&amp;head,6);
    insertBeg(&amp;head,6);
    insertBeg(&amp;head,6);
    display(head);
addafter(head,4,7);
cout&lt;&lt;endl;
display(head);

    return 0;
}

3.33 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
insertion linked list in c how to insert element in linked list c++ insert linked list return linked list in c insertion in linked list and display insertion in sorte doubly linked list in c++ inserting in a linked list c insertion operation at beginning in doubly linked list in c linked list c++ insertion operations insert an element in sorted linked list in c++ node insertion in linked list in c++ insertion node in linked list c++ insert function linked list c++ how can insertion sort use in linked list insertion linked list program in c insert in singly linked list c++ insert linked list c linked list insert at function implementation class c++ linked list insertion from array Linked List Insertion gfg program for inserting nodes into/from a linked list c insertion at linked list insert element in linked list in c++ singly linked list push in c singly linked list in c insert insert element linked list in c insert element in singly linked list in c insertion sort in linked list c++ why is linked list insertion o(1) linked list insertiion c++ linked list insertiion c how to do insertion in linked list in C++ Linked list insertion in a sorted insert function in linked list c programming linked list stl insertion c++ insertion in linked list c insertion sort c++ linked list insertion in singly linked list in c program insertion linked list c program how to insert linked list insert function in linked list c++ how linked list insertion works insert element in a linked list c++ insert and element in c++ linked list linked list c insert by order insert element on linked list c linked list insertion after a node in c linked list insertion function linked list program in c for insertion at beginning linked list insertion' insertion doubly linked list c++ how to add to linked list c++ linked list insertion c__ how to insert with order linked list in c linked list insertion operation c++ and description insertion singly linked list in c how to insert in linked list ordenated in c linked list insertion operation c++ linkedlist insert cpp how to insert an item in a linked list in c linked list insertion cpp how to add to a linked list in c in the linked list insertion can be done insertion into sorted linked list insertion in doubly linked list c++ insertion at end in doubly linked list in c linked list in c insertion at end insertion in doubly linked list in c insertion in sinlgy linked list singly linked list insertion program in c linked list insertion process insert to linked list c++ c program Insert into linked list insertion in singly linked list c++ insertion of linked list implementation in c++ add elements in linked list c add to linked list in order c++ insertion in sorted linked list insert linked list in c general linked list c++ insertion insertion into linked list linked list insertion c++ how to insert in a linked list c++ linked list insertion sort insertion in a linked list linked list insert cpp insertion sort algorithm in a singly linked list in c insertion using a singly linked list in c linked list insert c++ linked list c++ insertion how to insert an element into a linked list c++ insertion in linked list in data structure c++ insertion sort linked list linked list insertion in data structure how to implement insertion sort in linked list in c inserting in the the linked list in cpp insert at func in linked list in c++ Insert element in a sorted Linked List c insertion in linked list c++ c linked list insert in order c++ linked list insert insertion order in the linked list insert in linked list c++ insertion of element in doubly linked list insertion of element in linked list linked list insertion insertion sort linked list c++ insertion in linked list Insertion in doubly linked list in C++ insertion in a singly linked list linked list insertion in c++ linked list insertion in c insertion in singly linked list insert at function for a doubly linked list in C singly linked list insertion insert an element in a sorted linked list c singly linked list insert at end in c inserting nodes in c insert function in linked list in c insert function in linked list c insert node at front of linked list insertion in linked list in c creation and insertion in linked list in c linked list insertion program in c++ insert in linked list c how to add a element to the last linked list in c insertion in linked list at beginning and in end in java how to insert a number ina linked list input nodes in linked list how to add a new element to the start of a linked list c insert node into linked list
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source