Question: Problem Definition You will implement singly-linked list in a MyList class. Your class definition will be implemented in a MyList.cpp. Each function's expected behavior is

Problem Definition You will implement singly-linked list in a MyList class. Your class definition will be implemented in a MyList.cpp. Each function's expected behavior is described below. You will find the following files in the specs: Node.h, MyList.h, and main.cpp (for early testing). Node.h is read-only in both develop and submit mode of this assignment. The main.cpp and MyList.h are able to be edited in developer mode. If you make changes to either of these files that cause you to be unable to compile an executable successfully, revert back to the read-only versions in the assignment specs. Hints A reasonable plan of attack is to implement and test the more straight-forward functions (constructors, destructor, push_back etc.) prior to working on the more challenging parts (find, swap, reverse). Your goal should be to complete a portion of the program each day. You have one-week to complete this assignment. Start early! Node.h There is no need to make any changes to this file either for debugging or testing. It is provided as read-only in both develop and submit mode.

#ifndef __NODE_H_

#define __NODE_H_

class Node {

friend class MyList;

private:

Node* next;

char value; };

#endif /* NODE_H_ */ MyList.h

You can comment out sections of MyList.cpp in developer mode. This will allow piece-wise compilation and testing of your solution as it is developed.

#include "Node.h"

using namespace std;

#ifndef MYLIST_H_

#define MYLIST_H_ class

MyList

{

private:

Node* head;

public:

MyList();

MyList(const MyList& str);

MyList(const string& str);

MyList(const char* str);

~MyList();

/* Mutators */ void push_front(char value);

void push_back(char value);

void pop_front(); void pop_back();

void swap(int i, int j);

void insert_at_pos(int i, char value);

void reverse();

/* Accessors */

int size()const;

void print()const;

int find(char value)const;

int find(MyList& query_str)const;

/* Operator overloaders */

MyList& operator=(const MyList& str);

char& operator[](const int i);

MyList operator+(const MyList& str); };

#endif /* MYLIST_H_ */

Constructors and Destructor MyList();

Default constructor MyList(const MyList& str); Copy constructor. Constructs a list from a passed in MyList object, e.g. MyList l1(l2);. Performs a deep-copy, where memory separate from the argument object is allocated from the heap, using the keyword new.

MyList(const string& str); Constructs a list from a passed in string object, e.g. string username = "FLYNN"; MyList l1(username);.

MyList(const char* str); Constructs a list from a passed in string literal, e.g. MyList l1("Red pill, or Blue pill?");

~MyList(); Destructor Mutators

void push_front(char value); Inserts value at the front of the list.

void push_back(char value); Inserts value at the back of the list.

void pop_front(); Removes the first item from the list. No action on empty list.

void pop_back(); Removes the last item from the list. No action on empty list.

void swap(int i, int j); Swaps the value of the nodes at positions i and j. Program should take no action if i or j is out-of-bounds.

void insert_at_pos(int i, char value); Inserts a node with value at position i in the list. Program should abort via an assert statement if i is out-of-bounds.

void reverse(); Reverses the items in the list.

Accessors int size() const; Returns the number of nodes in the list.

void print() const; Prints the contents of the list.

int find(char value) const; Finds the position of the first occurrence of a character value in the list and returns that position. If the character is not in the list, the function returns -1.

int find(MyList& query_str) const; Finds the position of the first occurrence of MyList query_str in the list and return that position. If query_str is not in the list, the function returns -1.

Operator Overloaders MyList& operator=(const MyList& str); Overloaded assignment operator. Assigns the contents rhs (r-value) list to lhs (l-value) list, e.g. l1 = l2; Check for self-assignment.

MyList& operator+(const MyList str); Concatenates two lists together, i.e. l1 + l2;

**const char& operator[ ](const int i) const; ** Overloaded [ ] operator. Reads the character located at l1[i] in list l1. Program should abort via an assert statement if i is out-of-bounds.

**char& operator[ ](const int i); ** Overloaded [ ] operator. Returns writable reference to memory location for list l1 at l1[i]. Program should abort via an assert statement if i is out-of-bounds. main.cpp

The main.cpp is provided for testing purposes. To submit your solution for tests, comment out the body of the main function provided therein. #include #include #include "MyList.h" using namespace std; int main() { char c = 'a'; MyList l1; cout << "push_back z: " ; l1.push_back('z'); l1.print(); l1.pop_back(); cout << "pop_back z: " ; l1.print(); cout << "push_front 0 0 z 0 z: " ; l1.push_front('0'); l1.push_front('0'); l1.push_front('z'); l1.push_front('0'); l1.push_front('z'); l1.print(); int loc_size = l1.size(); cout << "array style print: " ; for(int i = 0; i < loc_size; i++) cout << l1[i] << " "; cout << endl; cout << "insert_at_pos a 0 on left and right of middle z: " ; l1.insert_at_pos(2, '0'); l1.insert_at_pos(5, '0'); l1.print(); cout << "push_back letters f-j: "; for(int i = 0; i < 10; i++) l1.push_back(c+i); l1.print(); cout << "pop_front z z: "; for(int i = 0; i < 2; i++) l1.pop_front(); l1.print(); cout << "pop_back f-j: "; for(int i = 0; i < 5; i++) l1.pop_back(); l1.print(); cout << "reverse odd sized list: "; l1.reverse(); l1.print(); cout << "push_front f: "; l1.push_front('f'); l1.print(); cout << "reverse even sized list: "; l1.reverse(); l1.print(); // make some copies for later use cout << "Construct new lists l2,l3,l4: "; cout << "MyList l4(l1); MyList l3 = l4; MyList l2(l3);" << endl; MyList l4(l1); MyList l3 = l4; MyList l2(l3); // test pop_back cout << "pop_back entire list, l1: "; loc_size = l1.size(); for(int i = 0; i < loc_size; i++) l1.pop_back(); l1.print(); // test reverse cout << "reverse empty list, l1: "; l1.reverse(); l1.print(); cout << "pop_back entire list but one item, l2: "; loc_size = l2.size(); for(int i = 0; i < loc_size-1; i++) l2.pop_back(); l2.print();; cout << "reverse one item list, l2: "; l2.reverse(); l2.print(); cout << "pop_back entire list but two items, l3: "; loc_size = l3.size(); for(int i = 0; i < loc_size-2; i++) l3.pop_back(); l3.print(); cout << "reverse two item list, l3: "; l3.reverse(); l3.print(); cout << "list l4: "; l4.print(); cout << "list l4 + l3: "; l4 + l3; l4.print(); cout << "list l4 + l2: "; l4 + l2; l4.print(); cout << "list l4 + l1: "; l4 + l1; l4.print(); cout << "Good Bye!" << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!