Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- struct PersonListNode
- {
- public:
- Person Value;
- PersonListNode* Next = nullptr;
- PersonListNode()
- {
- }
- };
- struct LinkedList
- {
- public: // private:
- PersonListNode* _head = nullptr;
- LinkedList()
- {
- }
- void Add(Person* value)
- {
- auto newNode = new PersonListNode();
- newNode->Value = value;
- if (_head == nullptr)
- {
- _head = newNode;
- return;
- }
- auto last = GetLast();
- last->Next = newNode;
- }
- PersonListNode* GetHead()
- {
- return _head;
- }
- PersonListNode* GetLast()
- {
- auto h = _head;
- while (h->Next != nullptr)
- {
- h = h->Next;
- }
- return h;
- }
- PersonListNode* ElementAt(int pos)
- {
- auto h = _head;
- if (pos == 0) return h;
- int i = 0;
- while (i != pos && h->Next != nullptr)
- {
- h = h->Next;
- i++;
- }
- return h;
- }
- bool IsEmpty()
- {
- return _head == nullptr;
- }
- void RemoveLastNode()
- {
- auto last = GetLast();
- if (_head == last)
- {
- _head = nullptr;
- delete last;
- return;
- }
- auto preLastNode = _head;
- while (preLastNode->Next != last)
- {
- preLastNode = preLastNode->Next;
- }
- delete last;
- preLastNode->Next = nullptr;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement