Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //SLL.h
- #pragma once
- class List
- {
- public:
- int Element;
- List* Next;
- static int Count;
- static List* Head;
- static List* Tail;
- List(int InElement = 0);
- ~List();
- static bool AddFront(int InElement);
- static bool AddBack(int InElement);
- static bool RemoveFront();
- static bool RemoveBack();
- void Print();
- static void PrintAll();
- };
- //SLL.cpp
- #include "SLL.h"
- #include <iostream>
- using namespace std;
- List* List::Head = nullptr;
- List* List::Tail = nullptr;
- int List::Count = 0;
- List::List(int InElement) : Element(InElement), Next(nullptr)
- {
- Count++;
- }
- List::~List()
- {
- Count--;
- }
- bool List::AddFront(int InElement)
- {
- List* NewList = new List(InElement);
- if (!NewList)
- return false;
- if ((Head == Tail) && (Head == nullptr))
- {
- //cout << "AddFront: case 0" << endl;
- Head = NewList;
- Tail = NewList;
- }
- else
- {
- //cout << "AddFront: default" << endl;
- NewList->Next = Head;
- Head = NewList;
- }
- return true;
- }
- bool List::AddBack(int InElement)
- {
- List* NewList = new List(InElement);
- if (!NewList)
- return false;
- if ((Head == Tail) && (Head == nullptr))
- {
- //cout << "AddFront: case 0" << endl;
- Head = NewList;
- Tail = NewList;
- }
- else
- {
- //cout << "AddFront: default" << endl;
- Tail->Next = NewList;
- Tail = NewList;
- }
- return true;
- }
- bool List::RemoveFront()
- {
- if (!Head)
- return false;
- List* ToDelete = Head;
- if (ToDelete == Tail)
- {
- Tail = nullptr;
- Head = nullptr;
- }
- else
- {
- Head = Head->Next;
- }
- delete ToDelete;
- return true;
- }
- bool List::RemoveBack()
- {
- if (!Tail)
- return false;
- List* ToDelete = Tail;
- if (ToDelete == Head)
- {
- Tail = nullptr;
- Head = nullptr;
- }
- else
- {
- List* BeforeTail = Head;
- while (BeforeTail->Next != Tail)
- {
- BeforeTail = BeforeTail->Next;
- }
- Tail = BeforeTail;
- BeforeTail->Next = nullptr;
- }
- delete ToDelete;
- return true;
- }
- void List::Print()
- {
- cout << "Element = " << Element << endl;
- if (Next)
- {
- Next->Print();
- }
- else
- {
- cout << "Last element\n";
- }
- }
- void List::PrintAll()
- {
- if (Head)
- {
- Head->Print();
- }
- else
- {
- cout << "List is empty\n";
- }
- }
- //Main.cpp
- #include <iostream>
- #include "SLL.h"
- using namespace std;
- int main()
- {
- //List* L = new List(5);
- List::AddFront(4);
- List::AddFront(5);
- List::AddBack(3);
- List::PrintAll();
- List::RemoveBack();
- List::PrintAll();
- List::RemoveFront();
- List::PrintAll();
- List::RemoveFront();
- List::PrintAll();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement