Question: LList.h code #ifndef LLIST_H #define LLIST_H #include #include #define int int using namespace std; struct node_t { int data; node_t* next; }; // This implementation
LList.h code
| #ifndef LLIST_H | |
| #define LLIST_H | |
| #include | |
| #include | |
| #define int int | |
| using namespace std; | |
| struct node_t { | |
| int data; | |
| node_t* next; | |
| }; | |
| // This implementation will use a head pointer, | |
| // allowing O(1) insertion on the front, | |
| // and O(n) on the end. | |
| class LList { | |
| public: | |
| LList(){ | |
| head = NULL; | |
| } | |
| ~LList(){ | |
| clear(); | |
| } | |
| LList(const LList& other){ | |
| // Do the same as the default constructor | |
| head = NULL; | |
| // Check if the other LList is empty | |
| if(other.head == NULL){ | |
| return; | |
| } | |
| // Not empty? Iterate through the other list | |
| // and push_back on myself. | |
| node_t* temp = other.head; | |
| while(temp){ | |
| push_back(temp->data); | |
| temp = temp->next; | |
| } | |
| } | |
| // Similar to copy constructor, but check for self | |
| // assignment, if not, clear and copy all data. | |
| LList& operator= (const LList& other){ | |
| if(this == &other){ | |
| return *this; | |
| } | |
| clear(); | |
| if(other.head == NULL){ | |
| return *this; | |
| } | |
| node_t* temp = other.head; | |
| while(temp){ | |
| push_back(temp->data); | |
| temp = temp->next; | |
| } | |
| return *this; | |
| } | |
| bool empty() const { | |
| // TODO: Fill me in | |
| } | |
| unsigned int size() const { | |
| // TODO: Fill me in | |
| } | |
| void push_back(int value){ | |
| // TODO: Fill me in | |
| } | |
| void push_front(int value){ | |
| // Empty list? | |
| if(head == NULL){ | |
| head = new node_t; | |
| head->data = value; | |
| head->next = NULL; | |
| }else{ // Not empty | |
| node_t* temp = new node_t; | |
| temp->data = value; | |
| temp->next = head; | |
| head = temp; | |
| } | |
| } | |
| void pop_front(){ | |
| if(head == NULL) return; | |
| node_t* temp = head; | |
| head = head->next; | |
| delete temp; | |
| } | |
| void pop_back(){ | |
| // TODO: Fill me in | |
| } | |
| // Overload [] operator | |
| // Return logic error if index out of bounds | |
| int& operator[](unsigned pos){ | |
| node_t* temp = head; | |
| while(temp != NULL && pos > 0){ | |
| temp = temp->next; | |
| pos--; | |
| } | |
| // As long as I don't have a null pointer, assign. | |
| if(pos == 0 && temp != NULL){ | |
| return temp->data; | |
| } | |
| throw logic_error("Index invalid"); | |
| } | |
| LList reverse() const { | |
| // TODO: Fill me in | |
| return LList(); // Remove me! | |
| } | |
| bool operator==(const LList& other) const { | |
| } | |
| bool operator!=(const LList& other) const { | |
| return !operator==(other); | |
| } | |
| void clear(){ | |
| node_t* last = head; | |
| while(head){ | |
| head = head->next; | |
| delete last; | |
| last = head; | |
| } | |
| // Normaly you never want to change head or you'll orphan part | |
| // of the list, but in this case we are wiping the list, | |
| // so it is ok to so and saves us a variable. | |
| head = NULL; | |
| } | |
| private: | |
| node_t* head; | |
| }; | |
| // Note this function is O(n^2) because getAt is O(n) and we are | |
| // doing it n times. | |
| ostream& operator<<(ostream& out, const LList other){ | |
| out << "["; | |
| for(unsigned int i = 1; i < other.size(); i++){ | |
| out << other.getAt(i-1) << ", "; | |
| } | |
| if(other.size() > 0){ | |
| out << other.getAt(other.size() - 1); | |
| } | |
| out << "]"; | |
| return out; | |
| } | |
| #endif |
Modify LList.h to implement the functions or operator overloading listed below. Most of the functions are stubbed out so you just need to add the proper logic.
Here is a list of the required methods you must fill in: LList.h
push_back(int) - Place the given value on the end of the linked-list.
reverse() const - Return a new linked-list which is the current one reversed.
size() - Returns the size (length) of the linked-list.
empty() - Return true if the list is empty otherwise return false.
pop_back() - Removes the last element of the linked-list. If the list is empty do nothing.
operator==(LListR) - Return true if the current linked-list contains identical values to the passed one.
Overload the plus (+) operator to add two lists together.
getAt(unsigned) - Return the value at the given position, or throw a logic error if the index is invalid. See the overloaded [] operator for hints on how to throw an exception.
setAt(int, unsigned) - Assign the given value to the given position (0-based). Throw a logic error if the index is invalid.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
