Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #pragma once
  2. #include "node.h"
  3.  
  4. template <typename typo>
  5. class list {
  6. protected:
  7. node <typo> *head; // wskaznik na pierwszy element listy
  8.  
  9. public:
  10. list() {
  11. head = nullptr;
  12. }
  13. ~list() {}
  14.  
  15. /* sprawdza czy lista jest pusta*/
  16. bool empty() const {
  17. if (head == nullptr) {
  18. return true;
  19. }
  20. else {
  21. return false;
  22. }
  23. }
  24.  
  25. /* zwraca pierwszy element z listy*/
  26. const typo & front() const {
  27. if(empty()==true){
  28. error();
  29. return 0;
  30. }
  31. else{
  32. return head->element;
  33. }
  34. }
  35.  
  36. /* dodaje element na poczatek listy*/
  37. virtual void add(const typo& element) {
  38. node<typo> *newNode = new node <typo>;
  39. newNode->element = element;
  40. newNode->next = head;
  41. head = newNode;
  42. //cout << " Dodano element" << endl;
  43. }
  44.  
  45. /* usuwa i zwraca pierwszy element z listy*/
  46. typo removeFront() {
  47. typo returnValue = 0;
  48.  
  49. if (empty()==true) {
  50. error();
  51. }
  52. else {
  53. node<typo> *oldHead = head;
  54. returnValue = oldHead->element;
  55. head = oldHead->next;
  56. delete oldHead;
  57. // cout << " Usunieto element" << endl;
  58. }
  59. return returnValue;
  60. }
  61.  
  62. /* wypisuje liste od poczatku*/
  63. void print() {
  64. if (empty() == true) {
  65. cout << " Struktura jest pusta!" << endl;
  66. }
  67. else {
  68. node<typo> *temp = head;
  69. while (temp != nullptr) {
  70. cout << temp->element << " ";
  71. temp = temp->next;
  72. }
  73. cout << endl;
  74. }
  75. }
  76.  
  77. /* usuwa wszystkie elementy z listy*/
  78. void remove() {
  79. if (empty()==true) {
  80. error();
  81. }
  82. else {
  83. node<typo> *oldHead;
  84. while (head != nullptr) {
  85. oldHead = head;
  86. head = oldHead->next;
  87. delete oldHead;
  88. }
  89. cout << " Usunieto wszystkie elementy" << endl;
  90. }
  91. }
  92.  
  93. /* wypisuje komunikat o niepoprawnej operacji*/
  94. void error() {
  95. cout << " Nie mozna wykonac tej operacji" << endl;
  96. }
  97.  
  98. /* menu operacji*/
  99. virtual void menu() {
  100. cout << " 1. Dodaj element na poczatek listy" << endl;
  101. cout << " 2. Usun element z pocztku listy" << endl;
  102. cout << " 3. Wyswietl zawartosc listy" << endl;
  103. cout << " 4. Usun zawartosc listy" << endl;
  104. cout << " 5. Wroc" << endl;
  105. }
  106. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement