Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement MyList.cpp: 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);.

Implement MyList.cpp:

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#########################

#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_back 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; }

#######################Node.h#####################

#ifndef __NODE_H_ #define __NODE_H_

class Node { friend class MyList; private: Node* next; char value; }; #endif /* NODE_H_ */

#########################MyList.h##########################

#include "Node.h" #include

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); };

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Sams Teach Yourself Beginning Databases In 24 Hours

Authors: Ryan Stephens, Ron Plew

1st Edition

067232492X, 978-0672324925

More Books

Students also viewed these Databases questions

Question

define what is meant by the term human resource management

Answered: 1 week ago