Advertisement
Guest User

Untitled

a guest
Oct 24th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <stdio.h>
  4. #include <iostream>
  5.  
  6.  
  7. template<class T> class Node
  8. {
  9. public:
  10. T data;
  11. Node* next;
  12.  
  13. };
  14.  
  15. template<class T> class StaticLinkedList
  16. {
  17.  
  18. public:
  19. Node<T>* first;
  20.  
  21. void PushStart(T data)
  22. {
  23. Node<T>* newfirst = new Node<T>();
  24.  
  25. newfirst->data = data;
  26. newfirst->next = first;
  27.  
  28. first = newfirst;
  29. }
  30. void PushEnd(T data)
  31. {
  32. Node<T>* newLast = new Node<T>();
  33. newLast->next = NULL;
  34. newLast->data = data;
  35.  
  36. Node<T>* last = Last();
  37. last->next = newLast;
  38.  
  39. }
  40. void PushIndex(T data, uint32_t index)
  41. {
  42. Node<T>* prevInd = AtIndex(index - 1);
  43. Node<T>* oldIndex = AtIndex(index);
  44. Node<T>* newIndex = new Node<T>();
  45.  
  46. newIndex->next = oldIndex;
  47. newIndex->data = data;
  48.  
  49. prevInd->next = newIndex;
  50.  
  51. }
  52.  
  53. void RemoveStart()
  54. {
  55. first = first->next;
  56. }
  57.  
  58. void RemoveEnd()
  59. {
  60. Node<T>* tmp = first;
  61.  
  62. while (tmp->next->next != nullptr)
  63. tmp = tmp->next;
  64.  
  65. tmp->next = nullptr;
  66. }
  67.  
  68. void RemoveIndex(uint32_t index)
  69. {
  70. Node<T>* prevInd = AtIndex(index - 1);
  71.  
  72. prevInd->next = prevInd->next->next;
  73. }
  74.  
  75. Node<T>* AtIndex(const uint32_t i)
  76. {
  77. Node<T>* tmp = first;
  78. for (int j = 0; j < i; j++)
  79. tmp = tmp->next;
  80.  
  81. return tmp;
  82. }
  83. Node<T>* Last()
  84. {
  85. Node<T>* tmp = first;
  86.  
  87. while (tmp->next != nullptr)
  88. tmp = tmp->next;
  89.  
  90. return tmp;
  91. }
  92.  
  93. void PrintList()
  94. {
  95. Node<T>* nz = first;
  96. while (nz != nullptr)
  97. {
  98. std::cout << nz->data << " -> ";
  99. nz = nz->next;
  100. }
  101.  
  102. std::cout << "null" << "\r\n";
  103. }
  104. void operator<< (const Node<T>* n) const
  105. {
  106. PushEnd(n);
  107. }
  108. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement