Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. //*****************************//
  2. // Felix Hu 12/6/2019 //
  3. // //
  4. // ListLab1 //
  5. //*****************************//
  6.  
  7. //-------Import-------//
  8. #include <iostream> //
  9. #include <string> //
  10. using namespace std; //
  11. //=======Import=======//
  12.  
  13. #ifndef LISTLAB1_H
  14. #define LISTLAB1_H
  15.  
  16. class ListNode {
  17. private:
  18. //Initial Variables
  19. string value; //value
  20. ListNode* next; //pointer
  21. public:
  22. //constructor that takes a string and a location for the next node
  23. ListNode(string value = "", ListNode* next = NULL);
  24. //returns the value of the current node
  25. string getValue();
  26. //return the pointer that points to the next node
  27. ListNode* getNext();
  28. //set the location of the next node
  29. void setNext(ListNode* arg);
  30. // returns a new node that is a copy of the argument node
  31. ListNode* copyNode(ListNode* arg);
  32. // returns a new list that is a copy of the original list
  33. ListNode* copyList(ListNode* arg);
  34. //returns a new linked list containing copies of each node in
  35. //the original list except the first node, maintaining the
  36. //order of the original list
  37. ListNode* rest(ListNode* h);
  38. // returns the value of the first node, or NULL if the list is empty
  39. string first(ListNode* head);
  40. // returns the value of the second node, or NULL if the list is empty or if there is only one node
  41. string second(ListNode* head);
  42. //returns a reference to the last node in the list, or NULL if the list is empty
  43. ListNode* pointerToLast(ListNode* head);
  44. //returns a copy of the last node (not just its value!). copyofLast can be recursive
  45. ListNode* copyOfLast(ListNode* head);
  46. //returns a reference to a list whose first node's value is specified by the argument, and the
  47. //first node's next links to the original list.
  48. ListNode* insertFirst(ListNode* head, string arg);
  49. //returns a reference to a list whose last node's value is specified by the argument, such
  50. //that this last node has been appended to the original list and had its next is set to NULL
  51. ListNode* insertLast(ListNode* head, string arg);
  52. };
  53.  
  54. ListNode* ListNode::copyList(ListNode* arg) {
  55. //if it is the last element return the argument
  56. if (arg->getNext() == NULL) {
  57. return copyNode(arg);
  58. }
  59. //start result with head(argument)
  60. ListNode* result = copyNode(arg);
  61. //set pointer next to point to the head of the rest of the list (recursive)
  62. result->setNext(copyList(arg->getNext()));
  63. //return the result
  64. return result;
  65. }
  66. ListNode* ListNode::rest(ListNode* h) {
  67. //copy the list starting from the second element
  68. return copyList(h->getNext());
  69. }
  70. string ListNode::first(ListNode* head) {
  71. //return the value using accessor method
  72. return head->getValue();
  73. }
  74. string ListNode::second(ListNode* head) {
  75. //get the pointer to the next node and get the value of the pointed value
  76. return head->getNext()->getValue();
  77. }
  78. ListNode* ListNode::pointerToLast(ListNode* head) {
  79. //create temporary node
  80. ListNode* temp = head;
  81. //while the node is not the last node continue
  82. while (temp->getNext() != NULL) {
  83. temp = temp->getNext();
  84. }
  85. //when exit, temp will be the last node
  86. return temp;
  87. }
  88. ListNode* ListNode::copyOfLast(ListNode* head) {
  89. //use makeCopy and pointerToLast methods
  90. return copyNode(pointerToLast(head));
  91. }
  92. ListNode* ListNode::insertFirst(ListNode* head, string arg) {
  93. //create node with value of argument and pointer pointing to the head of the original list
  94. return new ListNode(arg, head);
  95. }
  96. ListNode* ListNode::insertLast(ListNode* head, string arg) {
  97. //set the pointer of the last node(found by pointerToLast) to a new node with value of argument and pointer poiting to NULL
  98. pointerToLast(head)->setNext(new ListNode(arg, NULL));
  99. return head;
  100. }
  101. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement