this is so ass tho ngl
This commit is contained in:
1 parent
2c87094755
commit
a329f16cec
4 files changed
+181
-151
No files matched your search
+15
-21
@@ -12,7 +12,7 @@ public:
|
||||
|
||||
node(int data) : data(data), next(nullptr) {}
|
||||
node(int data, std::unique_ptr<node> next) : data(data), next(std::move(next)) {}
|
||||
~node() {}
|
||||
// ~node() {}
|
||||
};
|
||||
class linked_list
|
||||
{
|
||||
@@ -28,20 +28,14 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
linked_list(std::initializer_list<int> v) : head(nullptr)
|
||||
{
|
||||
for (int i : v)
|
||||
{
|
||||
append(i);
|
||||
}
|
||||
}
|
||||
|
||||
linked_list(std::initializer_list<int> v) : linked_list(std::vector<int>(v)) {}
|
||||
|
||||
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.
|
||||
@@ -70,27 +64,27 @@ public:
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
node *tail() const
|
||||
{
|
||||
node *temp = head.get();
|
||||
if (!temp) return nullptr;
|
||||
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;
|
||||
// what did i do to my argument
|
||||
void add(const linked_list &l)
|
||||
{
|
||||
if (!l.head) return;
|
||||
node* l_tail = l.tail(); // valid cuz check above
|
||||
l_tail->next = std::move(head);
|
||||
head = std::make_unique<node>(l.head);
|
||||
// return *this;
|
||||
}
|
||||
|
||||
linked_list &add(std::initializer_list<int> l)
|
||||
{
|
||||
for (int i : l)
|
||||
@@ -133,7 +127,7 @@ int main(int argc, char const *argv[])
|
||||
l.append(4);
|
||||
l.append(5);
|
||||
std::cout << l << std::endl;
|
||||
int x = l.pop().value();
|
||||
int x = l.pop().value(); // should be 5
|
||||
std::cout << x << std::endl;
|
||||
linked_list l2 = {6, 7, 8, 9};
|
||||
l.add(l2);
|
||||
|
||||
Reference in new issue
Block a user