Advertisement
Raphael404

SingleLinkedList

Jun 23rd, 2018
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. //SingleLinkedList
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. struct Node
  8. {
  9.     int Value;
  10.     Node *Next;
  11. };
  12.  
  13. class SLL //SLL = SingleLinkedList
  14. {
  15. private:
  16.     Node *head;
  17.     Node *tail;
  18.     int size;
  19. public:
  20.     SLL()
  21.     {
  22.         head = tail = NULL;
  23.         size = 0;
  24.     }
  25.     ~SLL()
  26.     {
  27.         DeleteAll();
  28.     }
  29.  
  30.     void AddTail(int number)
  31.     {
  32.         Node *tmp = new Node();
  33.         tmp->Next = NULL;
  34.         tmp->Value = number;
  35.         if (head == NULL)
  36.         {
  37.             head = tail = tmp;
  38.         }
  39.         else
  40.         {
  41.             tail->Next = tmp;
  42.             tail = tmp;
  43.         }
  44.         size++;
  45.     }
  46.     int getSize()
  47.     {
  48.         return size;
  49.     }
  50.     void DeleteOne()
  51.     {
  52.         head = head->Next;
  53.     }
  54.     void DeleteAll()
  55.     {
  56.         while (head != NULL)
  57.         {
  58.             DeleteOne();
  59.         }
  60.     }
  61.     void Print()
  62.     {
  63.         Node *tmp = new Node();
  64.         tmp = head;
  65.         while (tmp->Next != NULL)
  66.         {
  67.             cout << tmp->Value << "  ";
  68.             tmp = tmp->Next;
  69.         }
  70.         cout << endl;
  71.     }
  72. };
  73.  
  74. int main()
  75. {
  76.     //SLL = SingleLinkedList
  77.     cout << "Single Linked List SSL" << endl;
  78.     SLL list;
  79.     for (int i = 0; i < 20; i++)
  80.         list.AddTail(i);
  81.     list.Print();
  82.  
  83.     cin.get();
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement