-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist.cpp
More file actions
185 lines (176 loc) · 3.05 KB
/
Copy pathlinkedlist.cpp
File metadata and controls
185 lines (176 loc) · 3.05 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
#include <iostream>
using namespace std;
class linkedlist {
private:
struct node {
int item;
node* next;
};
node* first;
node* last;
int length;
public:
linkedlist() {
first = last = NULL;
length = 0;
}
bool isempthy() {
return length == 0;
}
void insertfirst(int element) {
node* newnode = new node;
newnode ->item = element;
if (isempthy()) {
first = last = newnode;
newnode ->next = NULL;
}
else {
newnode->next = first;
first = newnode;
}
length++;
}
void insertend(int element) {
node* newnode = new node;
newnode->item = element;
if (isempthy()) {
first = last = newnode;
newnode->next = NULL;
}
else {
last->next = newnode;
newnode->next = NULL;
last = newnode;
}
length++;
}
void insertatpos(int pos, int element) {
if (pos<0 || pos>length) {
cout << "out of range";
}
else {
node* newnode = new node;
newnode->item = element;
if (pos == 0) {
insertfirst(element);
}
else if (pos == length) {
insertend(element);
}
else {
node* cur = first;
for (size_t i = 1; i < pos; i++) {
cur = cur->next;
}
newnode->next = cur->next;
cur->next = newnode;
}
length++;
}
}
void print(){
node* curr = first;
while (curr != NULL) {
cout << curr->item << " ";
curr = curr->next;
}
}
void removefirst() {
if (length == 0) {
cout << "is empthy";
}
else if (length == 1) {
delete first;
first = last = NULL;
length--;
}
else {
node* temp = first;
first = first->next;
delete temp;
}
}
void removeend() {
if (length == 0) {
cout << "is empthy";
}
else if (length == 1) {
delete first;
first = last = NULL;
length--;
}
else {
node* tempp = first->next;
node* prev = first;
while (tempp != last) {
prev = tempp;
tempp = tempp->next;
}
delete tempp;
prev->next = NULL;
last = prev;
length--;
}
}
void remove_specific_element(int element)
{
if (length == 0)
cout << "List is empty";
else if (element == first->item)
removefirst();
else if (element == last->item)
removeend();
else {
node* cur = first->next;
node* prev = first;
for (int i = 1; i < length; i++)
{
if (cur->item == element)
break;
prev = cur;
cur = cur->next;
}
if (cur == NULL)
cout << "can't delete Element " << element << " becuse it dose not exist on the list" << endl;
else
prev->next = cur->next;
delete cur;
length--;
}
}
void reverse() {
node* prev, *nextt,*currr;
prev = NULL;
currr = first;
nextt = currr->next;
while (nextt != NULL) {
nextt = currr->next;
currr->next = prev;
prev = currr;
currr = nextt;
}
first = prev;
}
int search(int element) {
node* curr = first;
int pos = 0;
while (curr != NULL) {
if (curr->item == element) {
return pos;
curr = curr->next;
pos++;
}
return -1;
}
}
};
int main()
{
linkedlist ob;
ob.insertfirst(10);
ob.insertfirst(11);
ob.insertfirst(12);
ob.insertatpos(2,13);
ob.remove_specific_element(10);
ob.print();
}