ccoommmmiitt 33 iinn ccpppp
This commit is contained in:
1 parent
32af7721e0
commit
5ebfed9781
5 files changed
+269
-83
No files matched your search
@@ -0,0 +1,156 @@
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
class node
|
||||
{
|
||||
public:
|
||||
int data;
|
||||
std::unique_ptr<node> next;
|
||||
|
||||
node(int data) : data(data), next(nullptr) {}
|
||||
node(int data, std::unique_ptr<node> next) : data(data), next(std::move(next)) {}
|
||||
~node() {}
|
||||
};
|
||||
class linked_list
|
||||
{
|
||||
public:
|
||||
std::unique_ptr<node> head;
|
||||
linked_list() : head(nullptr) {}
|
||||
|
||||
linked_list(std::vector<int> v) : head(nullptr)
|
||||
{
|
||||
for (int i : v)
|
||||
{
|
||||
append(i);
|
||||
}
|
||||
}
|
||||
|
||||
linked_list(std::initializer_list<int> v) : head(nullptr)
|
||||
{
|
||||
for (int i : v)
|
||||
{
|
||||
append(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void append(int x) // prepend
|
||||
{
|
||||
auto temp = std::make_unique<node>(x, std::move(head));
|
||||
head = std::move(temp);
|
||||
}
|
||||
/// Pops the head of the linked list and returns the value in x.
|
||||
///
|
||||
/// If the linked list is empty, returns false. Otherwise, returns true.
|
||||
/// @param x The value popped from the linked list, if not empty.
|
||||
/// @return true if a value was popped, false if the list was empty.
|
||||
std::optional<int> pop()
|
||||
{
|
||||
if (!head)
|
||||
return std::nullopt;
|
||||
int value = head->data;
|
||||
head = std::move(head->next);
|
||||
return value;
|
||||
}
|
||||
|
||||
/// Initialize a linked list with a vector of values
|
||||
///
|
||||
/// The linked list is created by prepending each item in the vector
|
||||
/// to the list in order. The resulting list is a linked list where
|
||||
/// each node contains one item from the vector, and the order of the
|
||||
/// items in the vector is preserved in the linked list.
|
||||
linked_list &add(std::vector<int> &v)
|
||||
{
|
||||
for (int i : v)
|
||||
{
|
||||
append(i);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
node *tail() const
|
||||
{
|
||||
node *temp = head.get();
|
||||
while (temp->next != nullptr)
|
||||
{
|
||||
temp = temp->next.get();
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
linked_list &add(const linked_list &l)
|
||||
{
|
||||
std::vector<int> v = l.view();
|
||||
while (v.size() > 0)
|
||||
{
|
||||
append(v.back());
|
||||
v.pop_back();
|
||||
}
|
||||
// delete v?
|
||||
|
||||
return *this;
|
||||
}
|
||||
linked_list &add(std::initializer_list<int> l)
|
||||
{
|
||||
for (int i : l)
|
||||
{
|
||||
append(i);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
std::vector<int> view() const
|
||||
{
|
||||
std::vector<int> ret;
|
||||
node *temp = head.get();
|
||||
while (temp != nullptr)
|
||||
{
|
||||
ret.push_back(temp->data);
|
||||
temp = temp->next.get();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
friend std::ostream &operator<<(std::ostream &os, const linked_list &list)
|
||||
{
|
||||
node *temp = list.head.get();
|
||||
while (temp)
|
||||
{
|
||||
os << temp->data << " -> ";
|
||||
temp = temp->next.get();
|
||||
}
|
||||
os << "nullptr";
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
linked_list l;
|
||||
l.append(1);
|
||||
l.append(2);
|
||||
l.append(3);
|
||||
l.append(4);
|
||||
l.append(5);
|
||||
std::cout << l << std::endl;
|
||||
int x = l.pop().value();
|
||||
std::cout << x << std::endl;
|
||||
linked_list l2 = {6, 7, 8, 9};
|
||||
l.add(l2);
|
||||
l2.add({17, 18, 19, 20, 21});
|
||||
std::cout << l2 << std::endl;
|
||||
std::cout << l << std::endl;
|
||||
std::vector v = l.view();
|
||||
for (int i : v)
|
||||
{
|
||||
std::cout << i << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
v = l2.view();
|
||||
for (int i : v)
|
||||
{
|
||||
std::cout << i << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user