-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingly_linked_list.cpp
More file actions
181 lines (144 loc) · 2.74 KB
/
singly_linked_list.cpp
File metadata and controls
181 lines (144 loc) · 2.74 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
#include <iostream>
using namespace std;
class node
{
public:
int data;
node *next;
node(int data)
{ // this is constructor
this->data = data;
this->next = NULL;
}
};
void insertAtHead(node *&head, int value)
{
node *temp = new node(value);
temp->next = head;
head = temp;
}
void inserAtTail(node *&head, int value)
{
node *temp = new node(value);
node *current = head;
while (current->next != NULL)
{
current = current->next;
}
current->next = temp;
}
void insertAtanyPos(node *head, int value, int position)
{
node *temp = new node(value);
int count = 1;
if (position == 1)
{
insertAtHead(head, value);
}
node *current = head;
while (count < position - 1)
{
current = current->next;
count++;
}
if (current->next == NULL)
{
inserAtTail(head, value);
}
else
{
temp->next = current->next;
current->next = temp;
}
}
void print(node *&head)
{
node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
void deleteAtHead(node *&head)
{
node *temp = head;
head = head->next;
temp->next = NULL;
delete temp;
}
void deleteAtTail(node *&head)
{
node *temp = head;
node *current = NULL;
while (temp->next != NULL)
{
current = temp;
temp = temp->next;
}
current->next = temp->next;
delete temp;
}
void deleteAtanyPos(node *&head, int position)
{
node *temp = head;
node *current = NULL;
int count = 1;
if (position == 1)
{
deleteAtHead(head);
}
while (count < position)
{
current = temp;
temp = temp->next;
count++;
}
if (temp->next == NULL)
{
deleteAtTail(head);
}
else
{
current->next = temp->next;
temp->next = NULL;
delete temp;
}
}
int main()
{
node *head = new node(50);
// cout<<head->data<<endl;
// cout<<head->next<<endl;
insertAtHead(head, 49);
insertAtHead(head, 48);
insertAtHead(head, 47);
insertAtHead(head, 46);
// print(head);
// cout<<endl;
inserAtTail(head, 51);
inserAtTail(head, 52);
inserAtTail(head, 53);
inserAtTail(head, 54);
// print(head);
// cout<<endl;
insertAtanyPos(head, 500, 3);
print(head);
cout << endl;
insertAtanyPos(head, 500, 11);
print(head);
cout << endl;
deleteAtHead(head);
print(head);
cout << endl;
deleteAtTail(head);
print(head);
cout << endl;
deleteAtanyPos(head, 5);
print(head);
cout << endl;
deleteAtanyPos(head, 8);
print(head);
cout << endl;
return 0;
}