-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSinglyLinkedList.cpp
More file actions
222 lines (198 loc) · 4.82 KB
/
Copy pathSinglyLinkedList.cpp
File metadata and controls
222 lines (198 loc) · 4.82 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*******************Singly Linked List***********************/
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *next;
node(int data)
{
this->data = data;
this->next = NULL;
}
~node()
{
int value = this->data;
if (this->next!=NULL)
{
this->next = NULL;
delete next;
}
cout<<"Memory is free for node with data "<<value<<endl;
}
};
static int count =0;
int getlength(node *&head)
{
int cnt=0;
node *temp = head;
while (temp != NULL)
{
cnt++;
temp=temp->next;
}
return cnt;
}
void traverse(node *head)
{
node *temp = head;
while (temp != NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
void insAtHead(node* &head, node* &tail, int data)
{
// if list is empty
if(head==NULL)
{
cout<<"Yaha aaya hu "<<++count<<endl;
node *newNode = new node(data);
tail = newNode;
head = newNode;
}
else
{
// Create a new node
node *newNode = new node(data);
// newNode ke next me first ke first node ka address store krado jo ki head me h
newNode->next = head;
// head ko newNode ke address se update krado
head = newNode;
}
}
void insAtTail(node* &head, node* &tail, int data)
{
// if list is empty
if(tail==NULL)
{
cout<<"Yaha aaya hu "<<++count<<endl;
node *newNode = new node(data);
tail = newNode;
head = newNode;
}
else
{
node *newNode = new node(data);
tail->next = newNode;
tail = newNode;
}
}
void insAtPos(node* &head, node* &tail, int data, int k)
{
// if k=1, then it will be same as insAtHead function
if(k==1)
{
insAtHead(head,tail,data);
return;
}
// if list is empty
if(head==NULL)
{
cout<<"Yaha aaya hu "<<++count<<endl;
node *newNode = new node(data);
tail = newNode;
head = newNode;
return;
}
//if k is equal to ((length of list)+1)th position, then call insAtTail function
if(k==(getlength(head)+1))
{
insAtTail(head,tail,data);
return;
}
//if k is greater than (length of list)+1
if(k>(getlength(head)+1))
{
cout<<"Please enter a valid position!"<<endl;
return;
}
int pos = k-1;
// Create a new node
node *newNode = new node(data);
// Traverse to (k-1)th position
node* temp=head;
while(pos>1)
{
temp=temp->next;
pos--;
}
// Update next of newnode to point to kth node and then update (k-1)th node to point to newnode
newNode->next=temp->next;
temp->next=newNode;
}
void deleteNode(node* &head,int value, int pos)
{
// if list is empty
if(head==NULL)
{
cout<<"List is empty!"<<endl;
return;
}
//if pos is greater than ((length of list)+1)th position
if(pos>(getlength(head)))
{
cout<<"Please enter a valid position!"<<endl;
return;
}
cout<<"Before Deleting:"<<endl;
traverse(head);
cout<<endl;
node* temp = head;
// For first node/Deleting node from head
if (value==temp->data && pos==1)
{
head=temp->next;
temp->next=NULL;
delete temp;
}
// For deleting nodes(including the node at tail) other than first node(node at head)
else
{
node* prev=NULL;
while (temp!= NULL && temp->data != value)
{
prev=temp;
temp=temp->next;
}
if(temp==NULL) // To check if the node containing data value is actually present in the list or not
{
cout<<"Node not found!"<<endl;
return;
}
else
{
prev->next=temp->next;
temp->next=NULL;
delete temp;
}
}
}
int main()
{
// Formation of the very first node
node *first = new node(3);
// head and tail formation
node *head = first;
node *tail = first;
// Inserting nodes at head
insAtHead(head,tail, 5);
insAtHead(head,tail, 0);
insAtHead(head,tail, 7);
insAtHead(head,tail, 10);
// Inserting nodes at tail
insAtTail(head,tail,6);
insAtTail(head,tail,1);
// Inserting nodes at kth position
insAtPos(head,tail,2,2);
// // Printing the nodes present in the singly linked list
// traverse(head);
//Deleting the kth node present in the singly linked list
deleteNode(head,10,1);
cout<<"After Deleting:"<<endl;
traverse(head);
return 0;
}