forked from codescoop/Play-with-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_linked_list_insertion_2.cpp
201 lines (165 loc) · 4.15 KB
/
04_linked_list_insertion_2.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
TOPIC: Linked List - Insertion-II
[Insertion at Middle & Tail]
we can create linked list using:
- Linked List Class (Object Oriented Programming)
- Function (Procedural Programming)
(functions to perform different operations in linked list)
Insertion in linked list can be done at:
- head
- middle
- tail
Why Dynamic Allocation not Static Allocation in Linked List ?
- In static allocation, when the function call is over, all the variables that are created statically
are destroyed.
Thus, the Node which we create inside the function will also get destroyed, when the function call is over.
Like: insertAtHead(head,3);
insertAtHead(head,99);
insertAtHead(head,10);
- So, to create chain of Nodes, we use dynamic memory allocation.
*/
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node *next;
// constructor
Node(int d)
{
data = d;
next = NULL;
}
};
// Linked List Class (Object Oriented Programming)-----------------------------
/*
class LinkedList
{
Node *head;
Node *tail;
public:
LinkedList()
{
...
}
void insert(int d)
{
...
}
...
...
};
*/
// Functions (Procedural Programming)-----------------------------------------
// pass by value
// void insertAtHead(Node *headCopy, int d)
// pass a pointer by reference (because we want to make changes to the original head pointer)
void insertAtHead(Node *&head, int d)
{
if(head == NULL)
{
head = new Node(d);
return;
}
Node *n = new Node(d);
n->next = head;
head = n;
}
// function to find length of linked list
int length(Node*head)
{
int cnt = 0;
while(head != NULL)
{
cnt++;
head = head->next;
}
return cnt;
}
// function to insert data at last position of linked list
void insertAtTail(Node*&head, int data)
{
// corner case
if(head == NULL)
{
// Node *n = new Node(data);
// head = n;
head = new Node(data);
return;
}
Node *tail = head; // using temp pointer
// Moving head towards tail
while(tail->next != NULL)
{
tail = tail->next;
}
// create & attach new node
tail->next = new Node(data);
}
// function to insert data at middle of linked list
void insertAtMiddle(Node*&head, int data, int pos)
{
//corner case
if(head==NULL or pos==0)
{
insertAtHead(head, data);
return;
}
else if (pos >= length(head))
{
insertAtTail(head, data);
return;
}
Node *temp = head; // temp pointer is static. So, it will be deleted at the end of function call
// take pos-1 jumps
int i=1;
while(i < pos)
{
temp = temp->next;
i++;
}
// create a new node
Node *n = new Node(data);
n->next = temp->next;
temp->next = n;
}
// pass by value (because we don't want to change the original head pointer)
void print(Node *head)
{
while(head != NULL)
{
cout << head->data << " -> ";
head = head->next;
}
cout << endl;
}
int main()
{
// since we don't have linked list class, we have to keep the track of head pointer
Node *head = NULL; // to store address of the first node
// insert at head of linked list
insertAtHead(head,3);
insertAtHead(head,19);
insertAtHead(head,10);
cout << "Linked List [Insert at Head] : ";
print(head);
// insert at middle postion of linked list
insertAtMiddle(head,99,1);
cout << "Linked List [Insert at Pos=1] : ";
print(head);
insertAtMiddle(head,44,3);
cout << "Linked List [Insert at Pos=3] : ";
print(head);
// insert at tail of linked list
insertAtTail(head,88);
cout << "Linked List [Insert at Tail] : ";
print(head);
return 0;
}
/*
OUTPUT:
Linked List [Insert at Head] : 10 -> 19 -> 3
Linked List [Insert at Pos=1] : 10 -> 99 -> 19 -> 3
Linked List [Insert at Pos=3] : 10 -> 99 -> 19 -> 44 -> 3
Linked List [Insert at Tail] : 10 -> 99 -> 19 -> 44 -> 3 -> 88
*/