-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingly_linked_list.cpp
176 lines (151 loc) · 2.87 KB
/
singly_linked_list.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
#include <iostream>
//node class
class node
{
public:
int data;
node *next;
};
//linked list class
class linked_list
{
private:
node *head, *tail;
public:
linked_list();
void create_node(int);
void display();
void insert_first(int);
void insert_at(int, int);
void delete_first();
void delete_at(int);
void delete_last();
};
//constructor
linked_list::linked_list()
{
head = NULL;
tail = NULL;
}
//create_node
void linked_list::create_node(int item)
{
node *temp = new node;
temp->data = item;
temp->next = NULL;
if (head == NULL)
{
head = temp;
tail = temp;
}
else
{
//assigning temp->next as NULL
tail->next = temp;
tail = tail->next;
}
std::cout << "Inserting => " << item << std::endl;
}
//display
void linked_list::display()
{
node *temp = new node;
temp = head;
while (temp != NULL)
{
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
}
//insert_first
void linked_list::insert_first(int item)
{
node *temp = new node;
temp->data = item;
temp->next = head;
head = temp;
std::cout << "Inserting => " << item << " at head." << std::endl;
}
//insert_at
void linked_list::insert_at(int position, int item)
{
node *previous = new node;
node *current = new node;
node *temp = new node;
current = head;
for (int i = 1; i < position; i++)
{
previous = current;
current = current->next;
}
temp->data = item;
previous->next = temp;
temp->next = current;
std::cout << "Inserting => " << item << " at " << position << " index." << std::endl;
}
//delete_first
void linked_list::delete_first()
{
node *temp = new node;
temp = head;
head = head->next;
std::cout << "Deleting => " << temp->data << std::endl;
delete temp;
}
//delete_at
void linked_list::delete_at(int position)
{
node *current = new node;
node *previous = new node;
current = head;
for (int i = 1; i < position; i++)
{
previous = current;
current = current->next;
}
previous->next = current->next;
std::cout << "Deleting from index => " << position << std::endl;
}
//delete last
void linked_list::delete_last()
{
node *current = new node;
node *previous = new node;
current = head;
while (current->next != NULL)
{
previous = current;
current = current->next;
}
tail = previous;
previous->next = NULL;
delete current;
std::cout << "Deleting last element." << std::endl;
}
int main()
{
linked_list test_obj;
test_obj.create_node(1);
test_obj.create_node(2);
test_obj.create_node(3);
test_obj.create_node(4);
test_obj.create_node(5);
test_obj.create_node(6);
test_obj.create_node(7);
test_obj.create_node(8);
test_obj.create_node(9);
test_obj.create_node(10);
test_obj.display();
test_obj.delete_first();
test_obj.display();
test_obj.delete_last();
test_obj.display();
test_obj.delete_at(3);
test_obj.display();
test_obj.insert_first(2473);
test_obj.display();
test_obj.insert_at(7, 239);
test_obj.display();
return 0;
}