Advertisement
Guest User

lab4

a guest
Nov 13th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. main:
  2. #include <iostream>
  3.  
  4. int main()
  5. {
  6.  
  7. }
  8.  
  9. list.h:
  10. #pragma once
  11. #include "Point.h"
  12.  
  13.  
  14. class List
  15. {
  16. private:
  17. struct Node
  18. {
  19. Point point;
  20. Node* next;
  21. Node(const Point point) : next(nullptr), point(point) {}
  22. };
  23.  
  24. Node* first;
  25. size_t size;
  26.  
  27. Node* find(const size_t index);
  28. void pop(const size_t index);
  29. void push(const Point& point, const size_t index);
  30.  
  31. public:
  32.  
  33. List();
  34. ~List();
  35. };
  36.  
  37.  
  38.  
  39. List .cpp:
  40. #include "List.h"
  41.  
  42. List::List() : first(nullptr), size(0) {}
  43.  
  44. List::Node* List::find(const size_t index)
  45. {
  46. List::Node* it = first;
  47. for (size_t i = 0; i < index; i++)
  48. {
  49. it = it->next;
  50. }
  51. return it;
  52. }
  53. //V nie wiem czy to tu mialo byc dopisane
  54. void List::pop(const size_t index)
  55. {
  56. if (index == 0) {
  57. List::Node* node = find(index);
  58. first = node->next;
  59. delete node;
  60. size--;
  61. }
  62. else {
  63. List::Node* to_pop = find(index);
  64. List::Node* last = find(index - 1);
  65. last->next = to_pop->next;
  66. delete to_pop;
  67. size--
  68. }
  69. }
  70.  
  71. void List::push(const Point& point, size_t index)
  72. {
  73. List::Node* node = new List::Node(point);
  74.  
  75. if (index == 0)
  76. {
  77. node->next = first;
  78. first = node;
  79. }
  80. else
  81. {
  82. List::Node* last = find(index - 1);
  83. node->next = last->next;
  84. last->next = node;
  85. }
  86. size++;
  87. }
  88.  
  89. point.h:
  90. #pragma once
  91.  
  92. struct Point {
  93. double x, y, z;
  94. Point() : x(0.0), y(0.0), z(0.0) {}
  95. Point(double x, double y, double z) : x(x), y(y), z(z) {}
  96. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement