zelekkartofelek

list.cpp

Nov 15th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. #include "list.h"
  2.  
  3. void List::append(int x) {
  4. if (_size == 0) {
  5. first->data = x;
  6. _size = 1;
  7. return;
  8. }
  9. node *save = last;
  10. node * new_node = new node;
  11. last->next = new_node;
  12. last = new_node;
  13. last->before = save;
  14. last->next = nullptr;
  15. last->data = x;
  16. _size++;
  17. }
  18.  
  19. List::List() {
  20. _size = 0;
  21. first = new node;
  22. first->data = 0;
  23. first->next = nullptr;
  24. last = first;
  25. }
  26.  
  27. int List::get(int index) {
  28. node *current = first;
  29. for (int i = 0; i != index; i++) {
  30. current = current->next;
  31. }
  32. return current->data;
  33. }
  34.  
  35. void List::set(int index, int value) {
  36. node *current = first;
  37. for (int i = 0; i != index; i++) {
  38. current = current->next;
  39. }
  40. current->data = value;
  41. }
Add Comment
Please, Sign In to add comment