Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <stdio.h>
- #include <iostream>
- template<class T> class Node
- {
- public:
- T data;
- Node* next;
- };
- template<class T> class StaticLinkedList
- {
- public:
- Node<T>* first;
- void PushStart(T data)
- {
- Node<T>* newfirst = new Node<T>();
- newfirst->data = data;
- newfirst->next = first;
- first = newfirst;
- }
- void PushEnd(T data)
- {
- Node<T>* newLast = new Node<T>();
- newLast->next = NULL;
- newLast->data = data;
- Node<T>* last = Last();
- last->next = newLast;
- }
- void PushIndex(T data, uint32_t index)
- {
- Node<T>* prevInd = AtIndex(index - 1);
- Node<T>* oldIndex = AtIndex(index);
- Node<T>* newIndex = new Node<T>();
- newIndex->next = oldIndex;
- newIndex->data = data;
- prevInd->next = newIndex;
- }
- void RemoveStart()
- {
- first = first->next;
- }
- void RemoveEnd()
- {
- Node<T>* tmp = first;
- while (tmp->next->next != nullptr)
- tmp = tmp->next;
- tmp->next = nullptr;
- }
- void RemoveIndex(uint32_t index)
- {
- Node<T>* prevInd = AtIndex(index - 1);
- prevInd->next = prevInd->next->next;
- }
- Node<T>* AtIndex(const uint32_t i)
- {
- Node<T>* tmp = first;
- for (int j = 0; j < i; j++)
- tmp = tmp->next;
- return tmp;
- }
- Node<T>* Last()
- {
- Node<T>* tmp = first;
- while (tmp->next != nullptr)
- tmp = tmp->next;
- return tmp;
- }
- void PrintList()
- {
- Node<T>* nz = first;
- while (nz != nullptr)
- {
- std::cout << nz->data << " -> ";
- nz = nz->next;
- }
- std::cout << "null" << "\r\n";
- }
- void operator<< (const Node<T>* n) const
- {
- PushEnd(n);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement