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 186
|
#include <iostream>
#include <cassert>
#include <initializer_list>
#include <utility>
using namespace std;
template <class type>
class node {
public:
type info {};
node* link {};
node(const type& t) : info(t) {}
};
template <class type>
class Linkedlist {
private:
node<type> *head {}, *tail {};
size_t counter {};
public:
Linkedlist() {}
~Linkedlist() {
while (head) {
auto curr {head->link};
delete head;
head = curr;
}
}
Linkedlist(const Linkedlist&) = delete;
Linkedlist& operator=(const Linkedlist&) = delete;
Linkedlist(std::initializer_list<type> il) {
for (const auto& i : il)
append(i);
}
bool isEmpty() const { return (counter == 0); }
type getLast() const {
if (isEmpty()) {
cout << "Linked list is empty !!!\n";
assert(0);
}
return tail->info;
}
type getFirst() const {
if (isEmpty()) {
cout << "Linked list is empty !!!\n";
assert(0);
}
return head->info;
}
void append(const type& e) {
const auto newnode {new node<type>(e)};
if (isEmpty())
head = tail = newnode;
else {
tail->link = newnode;
tail = newnode;
}
++counter;
}
void print() const {
if (isEmpty()) {
cout << "Linked list is empty !!!\n";
return;
}
for (auto curr = head; curr != nullptr; curr = curr->link)
cout << (curr != head ? "," : "") << curr->info;
cout << '\n';
}
bool search(const type& e) const {
for (auto curr = head; curr != nullptr; curr = curr->link)
if (curr->info == e)
return true;
return false;
}
void insert(const type& element, size_t index) {
if (index < 1 || index > counter + 1) {
cout << " invalid index !!!!\n";
return;
}
auto newnode {new node<type>(element)};
if (index == 1) {
newnode->link = head;
if (isEmpty())
tail = newnode;
head = newnode;
} else {
auto current {head};
for (size_t i = 1; i != index - 1; ++i)
current = current->link;
newnode->link = current->link;
current->link = newnode;
if (tail == current)
tail = newnode;
}
++counter;
}
void erase(const type& e) {
if (isEmpty()) {
cout << "List is empty !!!\n";
return;
}
bool found {};
auto current {head};
node<type>* prev_current {};
while (!found && current != nullptr) {
if (current->info == e)
found = true;
else {
prev_current = current;
current = current->link;
}
}
if (!found) {
cout << "The item to be deleted is not in the list !!!";
return;
}
if (current == head) {
head = current->link;
if (head == nullptr)
tail = nullptr;
delete current;
} else {
prev_current->link = current->link;
if (current == tail)
tail = prev_current;
delete current;
}
--counter;
}
void sort() {
auto hi {head}, hj {head};
for (size_t i = 0, j = 0, swapped = true; swapped && i < counter - 1; ++i, hi = hi->link)
for (j = 0, swapped = false, hj = head; j < counter - i - 1; ++j, hj = hj->link)
if (hj->info > hj->link->info) {
swap(hj->info, hj->link->info);
swapped = true;
}
}
};
int main()
{
Linkedlist<int> ll {1, 3, 2, 5, 4, 7, 6};
ll.print();
ll.sort();
ll.print();
}
| |