Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node {
  6. int value = 0;
  7. Node *next = NULL;
  8. Node *prev = NULL;
  9.  
  10. Node(int value) {
  11. this->value = value;
  12. }
  13. };
  14.  
  15. class List {
  16. public:
  17. void pushTail(int valToAdd) {
  18. Node * newNode = new Node(valToAdd);
  19. if (head == NULL) {
  20. head = newNode;
  21. tail = newNode;
  22. return;
  23. }
  24. tail->next = newNode;
  25. tail = newNode;
  26. };
  27.  
  28. void popTail()
  29. {
  30. Node * temp = tail;
  31. tail = tail -> prev;
  32. tail -> next = NULL;
  33. delete temp;
  34. return;
  35. };
  36.  
  37. void popHead()
  38. {
  39. Node *temp = head;
  40. head = head->next;
  41. return;
  42. };
  43.  
  44. void pushHead(int valToAdd) {
  45. Node * newNode = new Node(valToAdd);
  46. if (head == NULL) {
  47. head = newNode;
  48. tail = newNode;
  49. return;
  50. }
  51. newNode->next = head;
  52. head = newNode;
  53. };
  54.  
  55. void printAll() {
  56. Node * buff = head;
  57. cout<<"print List:" << endl;
  58. while (buff != NULL) {
  59. cout<< buff->value << endl;
  60. buff = buff->next;
  61. }
  62. }
  63.  
  64. private:
  65. Node * head = NULL;
  66. Node * tail = NULL;
  67. };
  68.  
  69. int main() {
  70. List list;
  71. list.pushHead(1);
  72. list.pushHead(4);
  73. list.pushTail(8);
  74. list.printAll();
  75.  
  76. list.popTail();
  77. list.printAll();
  78. }
  79.  
  80. /*Node *temp = tail;
  81. tail = NULL;
  82. tail -> next = tail;
  83. return;*/ //как вырезать весь список сконца LUL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement