Sharing of my labs carried out during the TDDE18 course at Linköping University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sorted_list.h 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #ifndef SORTED_LIST_H
  2. #define SORTED_LIST_H
  3. #include <string>
  4. /*
  5. Lab 3 - TDDE18 - Sorted list lab - Header file
  6. Last modification : 2020-09-30
  7. */
  8. // Note: The other TODO remarks are in the test_list.cc file
  9. //TODO: Complementary work needed, 8-8: The link class and any functions pertaining it
  10. // should be stashed away and inaccessible to user. --> done (by moving Node struct declaration
  11. // in Sorted_List private part).
  12. //TODO: Complementary work needed, 8-8: Should have a function at() which should get
  13. // a value from place n in list. --> done (by implementing at() function and creating tests).
  14. //COMMENT: Good job with no memory leaks!
  15. /// A structure representing a list node
  16. /*
  17. struct Node {
  18. int value;
  19. Node* next;
  20. };*/
  21. /// A class representing a sorted list
  22. /// The list is sorted from smallest to largest value
  23. class Sorted_List{
  24. public:
  25. /// Constructors
  26. // Default constructor
  27. Sorted_List();
  28. // Copy constructor
  29. Sorted_List(Sorted_List const& other);
  30. // "Nice" constructor (actually, no idea how to name it)
  31. Sorted_List(std::initializer_list<int> list);
  32. // Move constructor
  33. Sorted_List(Sorted_List&& other);
  34. /// Destructor
  35. ~Sorted_List();
  36. // Copy assignment operator
  37. Sorted_List& operator=(Sorted_List const& other);
  38. // Move assignment operator
  39. Sorted_List& operator=(Sorted_List&& other);
  40. // Insert a value in the list
  41. void insert(int val);
  42. // Remove a value of the list, raise error if the value does not exist
  43. void remove(int val);
  44. // Check if a list is empty
  45. bool is_empty() const;
  46. // Return the size of a list
  47. int size() const;
  48. // Return the value at a given position
  49. int at(int pos) const;
  50. // Convert a list to string
  51. std::string to_string() const;
  52. private:
  53. struct Node {
  54. int value;
  55. Node* next;
  56. };
  57. Node* first{};
  58. void insert(Node*& first, int val);
  59. };
  60. #endif