Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //*****************************//
- // Felix Hu 12/6/2019 //
- // //
- // ListLab1 //
- //*****************************//
- //-------Import-------//
- #include <iostream> //
- #include <string> //
- using namespace std; //
- //=======Import=======//
- #ifndef LISTLAB1_H
- #define LISTLAB1_H
- class ListNode {
- private:
- //Initial Variables
- string value; //value
- ListNode* next; //pointer
- public:
- //constructor that takes a string and a location for the next node
- ListNode(string value = "", ListNode* next = NULL);
- //returns the value of the current node
- string getValue();
- //return the pointer that points to the next node
- ListNode* getNext();
- //set the location of the next node
- void setNext(ListNode* arg);
- // returns a new node that is a copy of the argument node
- ListNode* copyNode(ListNode* arg);
- // returns a new list that is a copy of the original list
- ListNode* copyList(ListNode* arg);
- //returns a new linked list containing copies of each node in
- //the original list except the first node, maintaining the
- //order of the original list
- ListNode* rest(ListNode* h);
- // returns the value of the first node, or NULL if the list is empty
- string first(ListNode* head);
- // returns the value of the second node, or NULL if the list is empty or if there is only one node
- string second(ListNode* head);
- //returns a reference to the last node in the list, or NULL if the list is empty
- ListNode* pointerToLast(ListNode* head);
- //returns a copy of the last node (not just its value!). copyofLast can be recursive
- ListNode* copyOfLast(ListNode* head);
- //returns a reference to a list whose first node's value is specified by the argument, and the
- //first node's next links to the original list.
- ListNode* insertFirst(ListNode* head, string arg);
- //returns a reference to a list whose last node's value is specified by the argument, such
- //that this last node has been appended to the original list and had its next is set to NULL
- ListNode* insertLast(ListNode* head, string arg);
- };
- ListNode* ListNode::copyList(ListNode* arg) {
- //if it is the last element return the argument
- if (arg->getNext() == NULL) {
- return copyNode(arg);
- }
- //start result with head(argument)
- ListNode* result = copyNode(arg);
- //set pointer next to point to the head of the rest of the list (recursive)
- result->setNext(copyList(arg->getNext()));
- //return the result
- return result;
- }
- ListNode* ListNode::rest(ListNode* h) {
- //copy the list starting from the second element
- return copyList(h->getNext());
- }
- string ListNode::first(ListNode* head) {
- //return the value using accessor method
- return head->getValue();
- }
- string ListNode::second(ListNode* head) {
- //get the pointer to the next node and get the value of the pointed value
- return head->getNext()->getValue();
- }
- ListNode* ListNode::pointerToLast(ListNode* head) {
- //create temporary node
- ListNode* temp = head;
- //while the node is not the last node continue
- while (temp->getNext() != NULL) {
- temp = temp->getNext();
- }
- //when exit, temp will be the last node
- return temp;
- }
- ListNode* ListNode::copyOfLast(ListNode* head) {
- //use makeCopy and pointerToLast methods
- return copyNode(pointerToLast(head));
- }
- ListNode* ListNode::insertFirst(ListNode* head, string arg) {
- //create node with value of argument and pointer pointing to the head of the original list
- return new ListNode(arg, head);
- }
- ListNode* ListNode::insertLast(ListNode* head, string arg) {
- //set the pointer of the last node(found by pointerToLast) to a new node with value of argument and pointer poiting to NULL
- pointerToLast(head)->setNext(new ListNode(arg, NULL));
- return head;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement