insertion singly linked list in c

Solutions on MaxInterview for insertion singly linked list in c by the best coders in the world

showing results for - "insertion singly linked list in c"
Eugénie
10 Oct 2017
1/**
2 * C program to insert a new node at the beginning of a Singly Linked List
3 */
4
5#include <stdio.h>
6#include <stdlib.h>
7
8
9/* Structure of a node */
10struct node {
11    int data;          // Data 
12    struct node *next; // Address 
13}*head;
14
15
16void createList(int n);
17void insertNodeAtBeginning(int data);
18void displayList();
19
20
21int main()
22{
23    int n, data;
24
25    /*
26     * Create a singly linked list of n nodes
27     */
28    printf("Enter the total number of nodes: ");
29    scanf("%d", &n);
30    createList(n);
31
32    printf("\nData in the list \n");
33    displayList();
34
35    /*
36     * Insert data at the beginning of the singly linked list
37     */
38    printf("\nEnter data to insert at beginning of the list: ");
39    scanf("%d", &data);
40    insertNodeAtBeginning(data);
41
42    printf("\nData in the list \n");
43    displayList();
44
45    return 0;
46}
47
48
49/*
50 * Create a list of n nodes
51 */
52void createList(int n)
53{
54    struct node *newNode, *temp;
55    int data, i;
56
57    head = (struct node *)malloc(sizeof(struct node));
58
59    /*
60     * If unable to allocate memory for head node
61     */
62    if(head == NULL)
63    {
64        printf("Unable to allocate memory.");
65    }
66    else
67    {
68        /*
69         * Input data of node from the user
70         */
71        printf("Enter the data of node 1: ");
72        scanf("%d", &data);
73
74        head->data = data; // Link data field with data
75        head->next = NULL; // Link address field to NULL
76
77        temp = head;
78
79        /*
80         * Create n nodes and adds to linked list
81         */
82        for(i=2; i<=n; i++)
83        {
84            newNode = (struct node *)malloc(sizeof(struct node));
85
86            /* If memory is not allocated for newNode */
87            if(newNode == NULL)
88            {
89                printf("Unable to allocate memory.");
90                break;
91            }
92            else
93            {
94                printf("Enter the data of node %d: ", i);
95                scanf("%d", &data);
96
97                newNode->data = data; // Link data field of newNode with data
98                newNode->next = NULL; // Link address field of newNode with NULL
99
100                temp->next = newNode; // Link previous node i.e. temp to the newNode
101                
102                temp = temp->next; 
103            }
104        }
105
106        printf("SINGLY LINKED LIST CREATED SUCCESSFULLY\n");
107    }
108}
109
110
111/*
112 * Create a new node and inserts at the beginning of the linked list.
113 */
114void insertNodeAtBeginning(int data)
115{
116    struct node *newNode;
117
118    newNode = (struct node*)malloc(sizeof(struct node));
119
120    if(newNode == NULL)
121    {
122        printf("Unable to allocate memory.");
123    }
124    else
125    {
126        newNode->data = data; // Link data part
127        newNode->next = head; // Link address part
128
129        head = newNode;          // Make newNode as first node
130
131        printf("DATA INSERTED SUCCESSFULLY\n");
132    }
133}
134
135
136/*
137 * Display entire list
138 */
139void displayList()
140{
141    struct node *temp;
142
143    /*
144     * If the list is empty i.e. head = NULL
145     */
146    if(head == NULL)
147    {
148        printf("List is empty.");
149    }
150    else
151    {
152        temp = head;
153        while(temp != NULL)
154        {
155            printf("Data = %d\n", temp->data); // Print data of current node
156            temp = temp->next;                 // Move to next node
157        }
158    }
159}
Lola
05 Mar 2016
1#include <iostream>
2// Linked list
3struct node {
4    int data ;
5    node * link;
6};
7
8
9node * Node(int data) {
10    node * temp = new node();
11    temp->data = data;
12    temp->link = NULL;
13    return temp;
14}
15
16void append(node ** head, int data) {
17
18    if(*head == NULL) {
19        *head =  Node(data);
20    }else {
21        node * temp = * head;
22        while (temp->link != NULL) {
23            temp=temp->link;
24        }
25        temp->link = Node(data);
26
27    }
28
29}
30
31
32// insertion at begining
33
34void insertBeg(node **head , int data) {
35    if(*head == NULL) {
36        * head = Node(data);
37    }else {
38        node * temp = Node(data);
39        temp->link = *head;
40        *head = temp;
41
42    }
43
44
45}
46// insert at specific position
47
48void addafter(node * head , int loc , int data) {
49    node * temp , * r ;
50    temp = head ;
51    for( int i = 0 ; i<loc;i++ ) {
52        temp = temp->link;
53        if(temp == NULL) {
54            cout<<"there ar less elemtns" ;
55            return;
56        }
57
58    }
59    // insert new node
60    r = Node(data);
61    r->link = temp->link;
62    temp->link = r;
63
64
65}
66
67void display(node * head) {
68
69    node * temp = head;
70    while(temp!= NULL) {
71        cout<<temp->data<<" ";
72        temp = temp->link;
73    }
74}
75
76
77
78int main() {
79    node * head = NULL;
80    append(&head,5);
81    append(&head,5);
82    append(&head,5);
83    append(&head,5);
84    display(head);
85    cout<<endl;
86    insertBeg(&head,6);
87    insertBeg(&head,6);
88    insertBeg(&head,6);
89    display(head);
90addafter(head,4,7);
91cout<<endl;
92display(head);
93
94    return 0;
95}
96
queries leading to this page
inserting in a linked list chow to insert in a linked list c 2b 2binsert function linked list c 2b 2bc linked list insert in order creation and insertion in linked list in clinked list insertion c linked list insertion after a node in clinked list insertiion cinserting into int in a sorted linked list cinserting in the the linked list in cppinsertion sort in linked list c 2b 2badding onto a linked list in clinked list insertion functionnode insertion in linked list in c 2b 2blinked list c 2b 2b insertioninsert element in linked list in c 2b 2bsingly linked list insert at end in cwhy is linked list insertion o 281 29insertion at end in doubly linked list in csingly linked list insertioninsertion in doubly linked list in c 2b 2binsertion of element in doubly linked listadd elements in linked list cinsertion in linked list in data structurelinked list insertion c 2b 2binsertion at linked listsingly linked list push in cinsertion in sinlgy linked listinsertion in sorte doubly linked list in c 2b 2bc programming linked list insert exampleinsert elements in linked list c 2b 2binsertion linked list program in cc 2b 2b linked list inserthow to add a element to the last linked list in cinsertion in singly linked listc program to insert an element in a linked listinsert element on linked list clinked list insert cppinsertion in linked list cinsertion into sorted linked listhow to insert element in linked list c 2b 2binsert in linked list c 2b 2bhow to create a list in linked list insertion 3fc 2b 2b insertion sort linked listinsertion linked list in clinked list insertiion c 2b 2bhow to add to a linked list in clinked list insert in cinsert and element in c 2b 2b linked listinsertion in a linked listlinked list c 2b 2b insertion operationslinked list insert using fun in clinked list stl insertion c 2b 2binsert function in linked list in chow to insert with order linked list in cinsertion sort c 2b 2b code linked listlinked list c insert by orderinsertion in a singly linked listlinked list insert c 2b 2binsertion linked listinsert at function for a doubly linked list in cinsertion in linked list and displayhow to do insertion in linked list in c 2b 2bsingly linked list insertion program in clinked list insertion 27algorithm for insertion in linked listadd to linked list in order c 2b 2blinked list insertion in data structureinsert into a linked list cin order insertion linked listinsertion node in linked list c 2b 2binsertion sort algorithm in a singly linked list in cinsert an element in sorted linked list in c 2b 2binsertion in doubly linked list in cin the linked list insertion can be done insertion order in the linked listinsert linked list in cinsert at linked list in cinsert into linked list in sorted order in cc how to insert in a linked listhow to insert an element into a linked list c 2b 2binsert an element in a sorted linked list clinked list insertion processsingly linked list in c insertinsert to linked list c 2b 2binserting into int in a sorted singly linked list clinked list in c program insertioninsertion in own linked listlinked list insertion in a sortedhow to insert in linked list ordenated in clinkedlists insertion in cinsert in linked list cinsertion sort linked list cppinsert function in linked list cinsertion singly linked list in chow to add a new element to the start of a linked listc program for insertion in linked list clinked list insertion from arrayinsert operations in linked list in c 2b 2blinked list program in c for insertion at beginninginsertion in linked list in cpp using classeslinked list insertion full tutorial c 2b 2binsertion operation at beginning in doubly linked list in cinsertion in singly linked list in c programinsert element in a linked list c 2b 2binsertion of linked list implementation in c 2b 2b insert function linked list in cinserting nodes in clinked list insertion in cc programming linked list insertinsertion in doubly linked list c 2b 2binsert element in a sorted linked list cinsert linked list return linked list in cinsert in link list c 2b 2binsert node at front of linked listinsert function in linked list c 2b 2binsertion of an element in linked listhow to add to linked list c 2b 2binsertion sort c 2b 2b linked listinsert linked list cinsert in singly linked list c 2b 2bc 2b 2b insertatend linked listlinked list in c insertion at endsingly linked list insertion algorithmlinked list insertion operation c 2b 2bc 2b 2b programming linked list insertinsertion in linked listhow to insert in linked list in c 2b 2bhow to insert a number ina linked listlinkedlist insert cppinsertion in linked list c 2b 2binsertion in linked list at beginning and in end in javac program insert into linked listlinked list insertion in c 2b 2binsert element in singly linked list in clinked list insertion sortc program how to insert linked listinsertion into linked listinsertion in linked list in cinsertion in linked listshow to implement insertion sort in linked list in clinked list insert at function implementation class c 2b 2binsert at func in linked list in c 2b 2bcreate and insert and display to perform singly linked list in cinsertion doubly linked list c 2b 2binput nodes in linked listinsertion using a singly linked list in chow to insert an item in a linked list in clinked list insertion program in c 2b 2binsertion of element in linked listgeneral linked list c 2b 2b insertionprogram for inserting nodes into 2ffrom a linked list clinked list insertion in c 2b 2b using classlinked list insertion operation c 2b 2b and descriptionlinked list insertion cppinsertion in sorted linked listinsert function in linked list c programminginsertion in singly linked list c 2b 2blinkedlist insertionc insert node into linked listinsert element linked list in chow can insertion sort use in linked listhow linked list insertion worksinsertion in a linked list in c 2b 2b 27insertion sort linked list c 2b 2blinked list insertion gfglinked list insertioninsertion singly linked list in c