Skip to content

Commit 29b1958

Browse files
authored
Different ways to insert in Singly Linked List
1 parent 526fd2e commit 29b1958

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// A complete working C program to demonstrate all insertion methods
2+
// on Linked List
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
6+
// A linked list node
7+
struct Node
8+
{
9+
int data;
10+
struct Node *next;
11+
};
12+
13+
/* Given a reference (pointer to pointer) to the head of a list and
14+
an int, inserts a new node on the front of the list. */
15+
void push(struct Node** head_ref, int new_data)
16+
{
17+
/* 1. allocate node */
18+
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
19+
20+
/* 2. put in the data */
21+
new_node->data = new_data;
22+
23+
/* 3. Make next of new node as head */
24+
new_node->next = (*head_ref);
25+
26+
/* 4. move the head to point to the new node */
27+
(*head_ref) = new_node;
28+
}
29+
30+
/* Given a node prev_node, insert a new node after the given
31+
prev_node */
32+
void insertAfter(struct Node* prev_node, int new_data)
33+
{
34+
/*1. check if the given prev_node is NULL */
35+
if (prev_node == NULL)
36+
{
37+
printf("the given previous node cannot be NULL");
38+
return;
39+
}
40+
41+
/* 2. allocate new node */
42+
struct Node* new_node =(struct Node*) malloc(sizeof(struct Node));
43+
44+
/* 3. put in the data */
45+
new_node->data = new_data;
46+
47+
/* 4. Make next of new node as next of prev_node */
48+
new_node->next = prev_node->next;
49+
50+
/* 5. move the next of prev_node as new_node */
51+
prev_node->next = new_node;
52+
}
53+
54+
/* Given a reference (pointer to pointer) to the head
55+
of a list and an int, appends a new node at the end */
56+
void append(struct Node** head_ref, int new_data)
57+
{
58+
/* 1. allocate node */
59+
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
60+
61+
struct Node *last = *head_ref; /* used in step 5*/
62+
63+
/* 2. put in the data */
64+
new_node->data = new_data;
65+
66+
/* 3. This new node is going to be the last node, so make next of
67+
it as NULL*/
68+
new_node->next = NULL;
69+
70+
/* 4. If the Linked List is empty, then make the new node as head */
71+
if (*head_ref == NULL)
72+
{
73+
*head_ref = new_node;
74+
return;
75+
}
76+
77+
/* 5. Else traverse till the last node */
78+
while (last->next != NULL)
79+
last = last->next;
80+
81+
/* 6. Change the next of last node */
82+
last->next = new_node;
83+
return;
84+
}
85+
86+
// This function prints contents of linked list starting from head
87+
void printList(struct Node *node)
88+
{
89+
while (node != NULL)
90+
{
91+
printf(" %d ", node->data);
92+
node = node->next;
93+
}
94+
}
95+
96+
/* Driver program to test above functions*/
97+
int main()
98+
{
99+
/* Start with the empty list */
100+
struct Node* head = NULL;
101+
102+
// Insert 6. So linked list becomes 6->NULL
103+
append(&head, 6);
104+
105+
// Insert 7 at the beginning. So linked list becomes 7->6->NULL
106+
push(&head, 7);
107+
108+
// Insert 1 at the beginning. So linked list becomes 1->7->6->NULL
109+
push(&head, 1);
110+
111+
// Insert 4 at the end. So linked list becomes 1->7->6->4->NULL
112+
append(&head, 4);
113+
114+
// Insert 8, after 7. So linked list becomes 1->7->8->6->4->NULL
115+
insertAfter(head->next, 8);
116+
117+
printf("\n Created Linked list is: ");
118+
printList(head);
119+
120+
return 0;
121+
}

0 commit comments

Comments
 (0)