-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomwork2-2.cpp
More file actions
127 lines (125 loc) · 3.19 KB
/
homwork2-2.cpp
File metadata and controls
127 lines (125 loc) · 3.19 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
#include <iostream>
#include <iomanip>
#include <cmath>
#include <numeric>
#include <string>
using namespace std;
#define endl '\n'
//#define haha cin.tie(0), cout.tie(0), cin.sync_with_stdio(0), cout.sync_with_stdio(0);
#define ll long long
struct node {
int data;
node* next = nullptr;
node(int data) :data(data) {}
};
class linkedlist {
private:
node* head{};node* tail{};
int length = 0;
public:
linkedlist() {}
linkedlist(const linkedlist&) = delete;
linkedlist& operator=(const linkedlist& another) = delete;
void insert_end(int value) {
node* entry = new node(value);
if (head == nullptr) {
head = tail = entry;
}
else {
tail->next = entry;
tail = entry;
}
length++;
}
void print() {
node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
node* get_node(int value){
int indx=0;
for(node*cur=head;cur;cur=cur->next,indx++){
if(value==indx){
return cur;
}
}
return nullptr;
}
void delete_with_key(int value) {
value--;
if (length < 1)return;
if (length <= 2) {
delete get_node(value);
head = tail = get_node(abs(value - 1));
} else {
if (value == 0) {
node*current=head->next;
delete head;
head = current;
} else if (value == length - 1) {
node*before_tail=get_node(value-1);
delete tail;
tail = before_tail;
tail->next=nullptr;
} else {
node *before = get_node(value - 1);
node *after = get_node(value + 1);
delete get_node(value);
before->next = after;
}
}
length--;
}
void swap_pairs(){
if(length==1)return;
for(node*cur=head;cur&&cur->next;cur=cur->next->next) {
if (cur->next != nullptr) {
node *after = cur->next;
swap(cur->data, after->data);
}
}
}
void delete_even_positions() {
if (length <= 1) return;
int indx = 1;
node* cur = head;
while (cur && cur->next) {
if (indx % 2 == 0) {
node* next_node = cur->next;
delete_with_key(indx);
cur = next_node;
} else {
cur = cur->next;
}
indx++;
}
}
void reverse(){
node* pre = nullptr;
node* cur = head;
node* after = nullptr;
while (cur != nullptr) {
after = cur->next;
cur->next = pre;
pre = cur;
cur = after;
}
tail = head;
head = pre;
}
};
int main() {
linkedlist list;
list.insert_end(1);
list.insert_end(2);
list.insert_end(3);
list.insert_end(4);
list.delete_with_key(3);
list.swap_pairs();
list.reverse();
list.delete_even_positions();
list.print();
}