linked list insertion in c 2b 2b

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

showing results for - "linked list insertion in c 2b 2b"
Aicha
12 May 2016
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}
Salomé
03 Aug 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
how to insert an item in a linked list in cprogram for inserting nodes into 2ffrom a linked list cinsert function linked list c 2b 2binsert element on linked list cc 2b 2b insertion sort linked listhow to insert in linked list in c 2b 2blinked list insertion processlinked list stl insertion c 2b 2binsert into a linked list cinsert element in singly linked list in csingly linked list insertion program in cinserting into int in a sorted linked list cinsertion linked list in chow to insert in a linked list c 2b 2bc 2b 2b insertatend linked listinsertion using a singly linked list in cinsertion in linked list in data structureinsertion in doubly linked list c 2b 2bc how to insert in a linked listinsertion node in linked list c 2b 2binsert in linked list c 2b 2bin order insertion linked listc linked list insert in order linked list in c program insertionc programming linked list insertlinkedlist insert cppgeneral linked list c 2b 2b insertioninsert element in linked list in c 2b 2blinked list insert using fun in clinked list insert at function implementation class c 2b 2bhow to add to a linked list in chow to do insertion in linked list in c 2b 2binsert element in a linked list c 2b 2binserting in the the linked list in cppinsertion in linked list c 2b 2blinked list insertion full tutorial c 2b 2bhow to add a new element to the start of a linked listinsertion order in the linked listlinked list insertion program in c 2b 2binsertion sort linked list c 2b 2binsert function in linked list cinsert element linked list in cinsert elements in linked list c 2b 2blinked list c insert by orderc program insert into linked listinsertion sort in linked list c 2b 2blinkedlist insertionlinked list insertiion cinsertion linked listhow to insert with order linked list in clinked list insertion in a sortedinsert linked list in cinsert at func in linked list in c 2b 2binsertion linked list program in cinsertion in linked list at beginning and in end in javainsert node at front of linked listinsertion of an element in linked listlinked list insert c 2b 2binsertion in a linked list in c 2b 2b 27c insert node into linked listinsertion of element in doubly linked listinsert operations in linked list in c 2b 2blinked list insertion c 2b 2binsertion sort c 2b 2b linked listinsertion in a linked listlinked list insertionlinked list program in c for insertion at beginningadd elements in linked list cnode insertion in linked list in c 2b 2binsertion in sinlgy linked listinsert in singly linked list c 2b 2binsert at linked list in clinked list insertion gfgcreation and insertion in linked list in cinserting nodes in chow to add a element to the last linked list in cinserting in a linked list chow can insertion sort use in linked listinsertion into sorted linked listinsertion in linked listin the linked list insertion can be done linkedlists insertion in clinked list insertion operation c 2b 2balgorithm for insertion in linked listinsertion in linked list and displayinsertion into linked listc 2b 2b linked list inserthow to insert in linked list ordenated in cinsertion of linked list implementation in c 2b 2b why is linked list insertion o 281 29linked list insertion 27insertion in linked listslinked list insertion from arrayinsertion in singly linked list in c programlinked list in c insertion at endhow to insert an element into a linked list c 2b 2blinked list insertion cppinsert and element in c 2b 2b linked listinsertion doubly linked list c 2b 2binsertion in doubly linked list in c 2b 2bc 2b 2b programming linked list insertc program how to insert linked listhow to create a list in linked list insertion 3finsert at function for a doubly linked list in cinsertion at end in doubly linked list in chow to insert a number ina linked listinput nodes in linked listinsertion in a singly linked listsingly linked list push in cinsert function in linked list c programminglinked list insertion in cinsert function linked list in cinsert function in linked list c 2b 2binsertion sort c 2b 2b code linked listinsertion in singly linked list c 2b 2bhow to add to linked list c 2b 2blinked list insert cppinsert function in linked list in clinked list c 2b 2b insertioninsertion in linked list in cpp using classesinsertion of element in linked listinsertion in linked list in clinked list insertion after a node in csingly linked list insertion algorithmlinked list insertion in data structureinsertion in doubly linked list in cinsertion in own linked listsingly linked list in c insertinsert an element in a sorted linked list cinserting into int in a sorted singly linked list cinsertion sort algorithm in a singly linked list in csingly linked list insert at end in clinked list insertiion c 2b 2binsert an element in sorted linked list in c 2b 2blinked list insertion in c 2b 2binsertion in sorte doubly linked list in c 2b 2blinked list insertion operation c 2b 2b and descriptionlinked list insertion sortinsert linked list cinsertion in linked list chow to insert element in linked list c 2b 2binsert to linked list c 2b 2binsertion in sorted linked listinsertion in singly linked listsingly linked list insertionhow linked list insertion worksadd to linked list in order c 2b 2binsert element in a sorted linked list cinsert linked list return linked list in cinsert in linked list clinked list insertion c insert in link list c 2b 2blinked list c 2b 2b insertion operationsinsertion operation at beginning in doubly linked list in clinked list insert in clinked list insertion functionadding onto a linked list in ccreate and insert and display to perform singly linked list in cc programming linked list insert exampleinsertion singly linked list in clinked list insertion in c 2b 2b using classhow to implement insertion sort in linked list in cinsertion sort linked list cppc program to insert an element in a linked listinsertion at linked listlinked list insertion in c 2b 2b