Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <typename T>class LinkedList {
- private:
- LinkedList(T item) {
- this->Containment = item;
- };
- LinkedList* Next = NULL;
- T Containment;
- LinkedList* getLast() {
- LinkedList* Node = this;
- while (Node->Next != NULL) {
- Node = Node->Next;
- }
- return Node;
- }
- public:
- LinkedList() {};
- void push_back(T item) {
- LinkedList* Last = this->getLast();
- Last->Next = new LinkedList(item);
- delete Last;
- }
- T pop_back() {
- return this->getLast()->Containment;
- }
- void push_forward(T item) {
- LinkedList* First = this;
- this->Containment = item;
- this->Next = First;
- delete First;
- }
- T pop_forward() {
- return this->Containment;
- }
- void remove_last() {
- Next = this->Next;
- LinkedList* OneBefore;
- while (Next != NULL) {
- OneBefore = Next;
- Next = Next->Next;
- }
- OneBefore->Next = NULL;
- delete Next;
- }
- void remove_first() {
- LinkedList* Second = this->Next;
- this->Next = Second->Next;
- this->Containment = Second->Containment;
- delete Second;
- }
- int count() {
- LinkedList* Second = this;
- int c = 0;
- while(Second->Next != NULL) {
- Second = Second->Next;
- c++;
- }
- return c + 1;
- }
- T operator[](int number) {
- int i = 0;
- int c = this->count();
- if (i + 1 > c) throw exception("Out of range");
- LinkedList* Second = this;
- while (Second->Next != NULL) {
- if (i == number) {
- break;
- }
- Second = Second->Next;
- i++;
- }
- return Second->Containment;
- }
- };
- int main() {
- LinkedList<int> list;
- list.push_back(3);
- list.push_back(4);
- cout << list.count();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement