-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListOneLink.h
More file actions
140 lines (121 loc) · 2.56 KB
/
ListOneLink.h
File metadata and controls
140 lines (121 loc) · 2.56 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
#pragma once
#include <cstdlib>
#include <iostream>
#include <stdexcept>
#include <stack>
template <class T>
class ListOneLink {
private:
struct Node {
Node() {}
Node(const T& value)
:data(value) {}
T data;
Node* next = NULL;
};
void recursion_reverse_of_node(Node* n) {
if (n->next == NULL) {
head = n;
return;
}
recursion_reverse_of_node(n->next);
n->next->next = n;
n->next = NULL;
}
Node* head = NULL;
size_t elements = 0;
public:
~ListOneLink() {
while (head != NULL) {
auto next = head->next;
delete head;
head = next;
}
}
size_t size() {
return elements;
}
Node* insert_after(const T& data, Node* pos) { // insert after Node
Node* temp = new Node(data);
Node* pos_next = pos->next;
pos->next = temp;
temp->next = pos_next;
++elements;
return temp;
}
Node* insert_before(const T& data, Node* pos) { // insert before Node
Node* to_insert = new Node(data);
Node* temp = head;
while (temp != pos && temp->next != pos)
temp = temp->next;
temp == pos ? head = to_insert : temp->next = to_insert;
to_insert->next = pos;
++elements;
return to_insert;
}
Node* push_front(const T& value) {
Node* temp = new Node(value);
temp->next = head;
head = temp;
++elements;
return head;
}
void pop_front() {
if (head == NULL) throw std::length_error("list is empty");
Node* new_head = head->next;
delete head;
head = new_head;
--elements;
}
void reverse() {
Node* current = head;
Node* prev = NULL;
Node* adjacent;
while (current != NULL) {
adjacent = current->next;
current->next = prev;
prev = current;
current = adjacent;
}
head = prev;
}
void stack_reverse() {
std::stack<Node*> nodes;
Node* temp = head;
while (temp != NULL) {
nodes.push(temp);
temp = temp->next;
}
temp = nodes.top();
head = temp;
nodes.pop();
while (!nodes.empty()) {
temp->next = nodes.top();
nodes.pop();
temp = temp->next();
}
temp->next = NULL;
}
void print() {
Node* temp = head;
bool first = true;
std::cout << "List contains " << elements << " elements: ";
while (temp != NULL) {
if (!first) std::cout << ", ";
first = false;
std::cout << temp->data;
temp = temp->next;
}
std::cout << '\n';
}
bool empty() {
return head == NULL;
}
T get_first() {
if (empty()) throw std::underflow_error("no elements");
return head->data;
}
void reverse_recursion() {
recursion_reverse_of_node(head);
}
};