Advertisement
Guest User

List

a guest
Sep 21st, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. //SLL.h
  2.  
  3. #pragma once
  4.  
  5. class List
  6. {
  7.  
  8. public:
  9.  
  10.     int Element;
  11.  
  12.     List* Next;
  13.  
  14.     static int Count;
  15.  
  16.     static List* Head;
  17.  
  18.     static List* Tail;
  19.  
  20.     List(int InElement = 0);
  21.  
  22.     ~List();
  23.  
  24.     static bool AddFront(int InElement);
  25.  
  26.     static bool AddBack(int InElement);
  27.  
  28.     static bool RemoveFront();
  29.  
  30.     static bool RemoveBack();
  31.  
  32.     void Print();
  33.  
  34.     static void PrintAll();
  35. };
  36.  
  37. //SLL.cpp
  38.  
  39. #include "SLL.h"
  40. #include <iostream>
  41.  
  42. using namespace std;
  43.  
  44. List* List::Head = nullptr;
  45. List* List::Tail = nullptr;
  46. int List::Count = 0;
  47.  
  48. List::List(int InElement) : Element(InElement), Next(nullptr)
  49. {
  50.     Count++;
  51. }
  52. List::~List()
  53. {
  54.     Count--;
  55. }
  56.  
  57. bool List::AddFront(int InElement)
  58. {
  59.     List* NewList = new List(InElement);
  60.  
  61.     if (!NewList)
  62.         return false;
  63.  
  64.     if ((Head == Tail) && (Head == nullptr))
  65.     {
  66.         //cout << "AddFront: case 0" << endl;
  67.         Head = NewList;
  68.         Tail = NewList;
  69.     }
  70.     else
  71.     {
  72.         //cout << "AddFront: default" << endl;
  73.         NewList->Next = Head;
  74.         Head = NewList;
  75.     }
  76.     return true;
  77. }
  78.  
  79. bool List::AddBack(int InElement)
  80. {
  81.     List* NewList = new List(InElement);
  82.  
  83.     if (!NewList)
  84.         return false;
  85.  
  86.     if ((Head == Tail) && (Head == nullptr))
  87.     {
  88.         //cout << "AddFront: case 0" << endl;
  89.         Head = NewList;
  90.         Tail = NewList;
  91.     }
  92.     else
  93.     {
  94.         //cout << "AddFront: default" << endl;
  95.         Tail->Next = NewList;
  96.         Tail = NewList;
  97.     }
  98.     return true;
  99. }
  100.  
  101. bool List::RemoveFront()
  102. {
  103.     if (!Head)
  104.         return false;
  105.  
  106.     List* ToDelete = Head;
  107.     if (ToDelete == Tail)
  108.     {
  109.         Tail = nullptr;
  110.         Head = nullptr;
  111.     }
  112.     else
  113.     {
  114.         Head = Head->Next;
  115.     }
  116.  
  117.     delete ToDelete;
  118.  
  119.     return true;
  120. }
  121.  
  122. bool List::RemoveBack()
  123. {
  124.     if (!Tail)
  125.         return false;
  126.  
  127.     List* ToDelete = Tail;
  128.     if (ToDelete == Head)
  129.     {
  130.         Tail = nullptr;
  131.         Head = nullptr;
  132.     }
  133.     else
  134.     {
  135.         List* BeforeTail = Head;
  136.         while (BeforeTail->Next != Tail)
  137.         {
  138.             BeforeTail = BeforeTail->Next;
  139.         }
  140.         Tail = BeforeTail;
  141.         BeforeTail->Next = nullptr;
  142.     }
  143.    
  144.     delete ToDelete;
  145.  
  146.     return true;
  147. }
  148.  
  149. void List::Print()
  150. {
  151.     cout << "Element = " << Element << endl;
  152.     if (Next)
  153.     {
  154.         Next->Print();
  155.     }
  156.     else
  157.     {
  158.         cout << "Last element\n";
  159.     }
  160. }
  161.  
  162. void List::PrintAll()
  163. {
  164.     if (Head)
  165.     {
  166.         Head->Print();
  167.     }
  168.     else
  169.     {
  170.         cout << "List is empty\n";
  171.     }
  172. }
  173.  
  174. //Main.cpp
  175.  
  176. #include <iostream>
  177. #include "SLL.h"
  178.  
  179. using namespace std;
  180.  
  181. int main()
  182. {
  183.     //List* L = new List(5);
  184.     List::AddFront(4);
  185.     List::AddFront(5);
  186.     List::AddBack(3);
  187.  
  188.     List::PrintAll();
  189.  
  190.     List::RemoveBack();
  191.  
  192.     List::PrintAll();
  193.  
  194.     List::RemoveFront();
  195.  
  196.     List::PrintAll();
  197.  
  198.     List::RemoveFront();
  199.    
  200.     List::PrintAll();
  201.  
  202.     system("pause");
  203.     return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement