-
Notifications
You must be signed in to change notification settings - Fork 2
/
insert_singly_linkedlist.c
153 lines (124 loc) · 2.99 KB
/
insert_singly_linkedlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/****************************************************************************
File name: insert_singly_linkedlist.cpp
Author: babajr
*****************************************************************************/
/*
1. Insert new node of given value at given position in the Singly Linked List.
2. Insert new node at last position of the singly Linked List.
*/
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
struct Node
{
int data;
struct Node *next;
};
typedef struct Node Node;
Node *head = NULL; // global head pointer
/*
API to display contents of the linkedlist using iterative approach.
*/
void display(Node *ptr)
{
if(ptr == NULL)
{
printf("LINKED LIST is EMPTY\n");
return;
}
while(ptr != NULL)
{
printf("%d\t", ptr->data);
ptr = ptr->next;
}
printf("\n");
}
/*
API to count number of nodes in the linked list.
*/
int count(Node *ptr)
{
int nodeCount = 0;
while(ptr != NULL)
{
nodeCount++;
ptr = ptr->next;
}
return nodeCount;
}
/*
API to insert node at particular position.
*/
void insert(Node *ptr, int pos, int value)
{
// create new node
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
// check for valid position
if(pos < 0 || pos > count(ptr))
{
printf("Enter valid position. Either %d or less than %d\n", 0, count(ptr));
return;
}
// inserting at the start of the list
if(pos == 0)
{
newNode->next = head;
head = newNode;
}
else
{
// make the ptr to point previous node of mentioned position.
for(int i = 0; i < pos - 1; i++)
ptr = ptr->next;
// first make the link between new node to next node.
// Then make link from previous node to new node.
newNode->next = ptr->next;
ptr->next = newNode;
}
}
/*
API to insert node always at the last position.
*/
void insertAtLast(Node *ptr, int value)
{
Node *last = NULL; // pointer to point to last node of list.
// create new node
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->data = value;
newNode->next = NULL;
// if list is empty i.e. head or ptr = NULL
if(ptr == NULL)
{
head = newNode;
last = newNode;
}
else
{
last = ptr; // start last pointer from head
while(last->next != NULL) // traverse the list until last pointer reach to last node of list.
{
last = last->next;
}
// create the link between last node to the new node and point
// the last pointer to the new last node.
last->next = newNode;
last = newNode;
}
}
/* Driver Code */
int main(void)
{
insert(head, 0, 0);
insert(head, 1, 1);
insert(head, 2, 2);
insert(head, 3, 3);
insert(head, 3, 4);
display(head);
insertAtLast(head, 20);
insertAtLast(head, 25);
insertAtLast(head, 30);
display(head);
return 0;
}