C++代写:CS32 Felina

按照要求补充实现Doubly Linked List的所有函数,不能使用STL中现有的方法。然后使用Doubly Linked List完成一个小应用程序,并通过所有测试。

Background

In this project, you will write the implementation of the PeopleList using a doubly linked list. You will also implement a couple of algorithms that operate on a PeopleList.

Implement PeopleList

Consider the following PeopleList interface:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
typedef std::string InfoType;

class PeopleList
{
public:
PeopleList(); // Create an empty In (i.e., one with no InfoType values)

bool empty() const; // Return true if the list is empty, otherwise false.

int size() const; // Return the number of elements in the linked list.

bool add(const std::string& firstName, const std::string& lastName, const InfoType& value);
// If the full name (both the first and last name) is not equal to any full name currently
// in the list then add it and return true. Elements should be added according to
// their last name. Elements with the same last name should be added according to
// their first names. Otherwise, make no change to the list and return false
// (indicating that the name is already in the list).

bool change(const std::string &firstName, const std::string &lastName, const InfoType& value);
// If the full name is equal to a full name currently in the list, then make that full
// name no longer map to the value it currently maps to, but instead map to
// the value of the third parameter; return true in this case.
// Otherwise, make no change to the list and return false.

bool addOrChange(std::string firstName, std::string lastName, const InfoType& value);
// If full name is equal to a name currently in the list, then make that full name no
// longer map to the value it currently maps to, but instead map to
// the value of the third parameter; return true in this case.
// If the full name is not equal to any full name currently in the list then add it
// and return true. In fact this function always returns true.

bool remove(const std::string& firstName, cont std::string& lastName);
// If the full name is equal to a full name currently in the list, remove the
// full name and value from the list and return true. Otherwise, make
// no change to the list and return false.

bool contains(const std::string firstName, const std::string lastName) const;
// Return true if the full name is equal to a full name currently in the list, otherwise
// false.

bool lookup(const std::string& firstName, const std::string& lastName, InfoType& value) const;
// If the full name is equal to a full name currently in the list, set value to the
// value in the list that that full name maps to, and return true. Otherwise,
// make no change to the value parameter of this function and return
// false.

bool get(int i, std::string& firstName, std::string& lastName, InfoType& value) const;
// If 0 <= i < size(), copy into firstName, lastName and value parameters the
// corresponding information of the element at position i in the list and return
// true. Otherwise, leave the parameters unchanged and return false.
// (See below for details about this function.)

void swap(PeopleList& other);
// Exchange the contents of this list with the other one.
};

The add function primarily places elements in a the list based on last name. If there are multiple entries with the same last name then those elements, with the same last name, are added according to their first name. In other words, this code fragment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
PeopleList m;

m.add("Skyler", "White", 45);
m.add("James", "McGill", 49);
m.add("Charles", "McGill", 58);
m.add("Walter", "White", 52);
m.add("Jesse", "Pinkman", 27);

for (int n = 0; n < m.size(); n++)
{
string f;
string l;
int v;
m.get(n, f, l, v);
cout << f << " " << l << " " << v << endl;
}

must result in the output:

Charles McGill 58
James McGill 49
Jesse Pinkman 27
Skyler White 45
Walter White 52

Notice that the empty string is just as good a string as any other; you should not treat it in any special way:

1
2
3
4
5
6
7
8
9
10
11
PeopleList gpas;

gpas.add("Gus", "Fring", 3.756);
assert(!gpas.contains("",""));
gpas.add("Jane", "Margolis", 3.538);
gpas.add("", "", 4.000);
gpas.add("Jesse", "Pinkman", 2.256);
assert(gpas.contains("", ""));
gpas.remove("Gus", "Fring");
assert(gpas.size() == 3 && gpas.contains("Jesse", "Pinkman") &&
gpas.contains("Jane", "Margolis") && gpas.contains("", ""));

When comparing keys for add, change, addOrChange, remove, contains, and lookup, just use the == or != operators provided for the string type by the library. These do case-sensitive comparisons, and that’s fine.

For this project, implement this PeopleList interface using a doubly-linked list. (You must not use any container from the C++ library.)

For your implementation, if you let the compiler write the destructor, copy constructor, and assignment operator, they will do the wrong thing, so you will have to declare and implement these public member functions as well:

Destructor

When a PeopleList is destroyed, all dynamic memory must be deallocated.

Copy constructor

When a brand new PeopleList is created as a copy of an existing PeopleList, a deep copy should be made.

Assignment operator

When an existing PeopleList (the left-hand side) is assigned the value of another PeopleList (the right-hand side), the result must be that the left-hand side object is a duplicate of the right-hand side object, with no memory leak (i.e. no memory from the old value of the left-hand side should be still allocated yet inaccessible).

Notice that there is no a priori limit on the maximum number of elements in the PeopleList (so addOrChange should always return true). Also, if a PeopleList has a size of n, then the values of the first parameter to the get member function are 0, 1, 2, …, n-1; for other values, it returns false without setting its parameters.

Implement some non member functions

Using only the public interface of PeopleList, implement the following two functions. (Notice that they are non-member functions; they are not members of PeopleList or any other class.)

1
bool combine(const PeopleList& m1, const PeopleList& m2, PeopleList& result);

When this function returns, result must consist of pairs determined by these rules:

  • If a full name appears in exactly one of m1 and m2, then result must contain an element consisting of that full name and its corresponding value.
  • If a full name appears in both m1 and m2, with the same corresponding value in both, then result must contain an element with that full name and value.

When this function returns, result must contain no elements other than those required by these rules. (You must not assume result is empty when it is passed in to this function; it might not be.)

If there exists a full name that appears in both m1 and m2, but with different corresponding values, then this function returns false; if there is no full name like this, the function returns true. Even if the function returns false, result must be constituted as defined by the above rules.

For example, suppose a PeopleList maps the full name to integers. If m1 consists of these three elements

"Ethel" "Mertz"  456      "Fred" "Mertz"  123      "Lucy" "Ricardo"  789

and m2 consists of

"Lucy" "Ricardo"  789      "Ricky" "Ricardo" 321

then no matter what value it had before, result must end up as a list consisting of

"Ethel" "Mertz"  456     "Fred" "Mertz"  1231      "Lucy" "Ricardo"  789      "Ricky" "Ricardo"  32

and combine must return true. If instead, m1 were as before, and m2 consisted of

"Lucy" "Ricardo" 654      "Ricky" "Ricardo"  321

then no matter what value it had before, result must end up as a list consisting of

"Ethel" "Mertz" 456          "Fred" "Mertz"  123      "Ricky" "Ricardo"  321

and combine must return false.

1
2
void search (const std::string& fsearch, const std::string& lsearch, 
const PeopleList& p1, PeopleList& result)
;

When this function returns, result must contain a copy of all the elements in m1 that match the search terms; it must not contain any other elements. You can wildcard the first name, last name or both by supplying “*”. (You must not assume result is empty when it is passed in to this function; it may not be.)

For example, if p consists of the three elements

"Gustavo" "Fring"  57      "Skyler" "White"  45      "Walter" "White" 52 

and the following call is made:

search("*", "White", p, result)

then no matter what value it had before, result must end up as a PeopleList consisting of

"Skyler" "White"  45       "Walter" "White" 52

If instead, p1 were

"Jane" "Doe" 35            "Marie" "Schrader" 37      "Jane" "Margolis" 27

and the following call is made:

1
search("Jane", "*", p, result)

then no matter what value it had before, result must end up as a list consisting of

"Jane" "Doe" 35            "Jane" "Margolis" 27

and if the following call is made:

1
search("*", "*", p, result)

then no matter what value it had before, result must end up being a copy of p

Be sure these functions behave correctly in the face of aliasing: What if m1 and result refer to the same PeopleList, for example?

Other Requirements

Regardless of how much work you put into the assignment, your program will receive a low score for correctness if you violate these requirements:

  • Your class definition, declarations for the two required non-member functions, and the implementations of any functions you choose to inline must be in a file named PeopleList.h, which must have appropriate include guards. The implementations of the functions you declared in PeopleList.h that you did not inline must be in a file named PeopleList.cpp. Neither of those files may have a main routine (unless it’s commented out). You may use a separate file for the main routine to test your PeopleList class; you won’t turn in that separate file.

  • Except to add a destructor, copy constructor, assignment operator, and dump function (described below), you must not add functions to, delete functions from, or change the public interface of the PeopleList class. You must not declare any additional struct/class outside the PeopleList class, and you must not declare any public struct/class inside the PeopleList class. You may add whatever private data members and private member functions you like, and you may declare private structs/classes inside the PeopleList class if you like. The source files you submit for this project must not contain the word friend. You must not use any global variables whose values may be changed during execution.

If you wish, you may add a public member function with the signature void dump() const. The intent of this function is that for your own testing purposes, you can call it to print information about the map; we will never call it. You do not have to add this function if you don’t want to, but if you do add it, it must not make any changes to the map; if we were to replace your implementation of this function with one that simply returned immediately, your code must still work correctly. The dump function must not write to cout, but it’s allowed to write to cerr.

  • Your code must build successfully (under both g32 and either clang++ or Visual C++) if linked with a file that contains a main routine.

  • You must have an implementation for every member function of PeopleList, as well as the non-member functions combine and search. Even if you can’t get a function implemented correctly, it must have an implementation that at least builds successfully. For example, if you don’t have time to correctly implement PeopleList::remove or search, say, here are implementations that meet this requirement in that they at least build successfully:

1
2
3
4
5
6
7
8
9
10
bool PeopleList::remove(const std::string& fname, cont std::string& lname)
{
return false; // not correct, but at least this compiles
}

void search(const std::string& fsearch, const std::string& lsearch,
const PeopleList& p1, PeopleList& result)

{

// does nothing; not correct, but at least this compiles
}

You’ve probably met this requirement if the following file compiles and links with your code. (This uses magic beyond the scope of CS 32.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "PeopleList.h"
#include <type_traits>

#define CHECKTYPE(f, t) { auto p = (t)(f); (void)p; }

static_assert(std::is_default_constructible<PeopleList>::value,
"Map must be default-constructible.");
static_assert(std::is_copy_constructible<PeopleList>::value,
"Map must be copy-constructible.");

void ThisFunctionWillNeverBeCalled()
{

CHECKTYPE(&PeopleList::operator=, PeopleList& (PeopleList::*)(const PeopleList&));
CHECKTYPE(&PeopleList::empty, bool (PeopleList::*)() const);
CHECKTYPE(&PeopleList::size, int (PeopleList::*)() const);
CHECKTYPE(&PeopleList::add, bool (PeopleList::*)(const std::string&, const std::string&, const InfoType&));
CHECKTYPE(&PeopleList::change, bool (PeopleList::*)(const std::string&, const std::string&, const InfoType&));
CHECKTYPE(&PeopleList::addOrChange, bool (PeopleList::*)(const std::string&, const std::string&, const InfoType&));
CHECKTYPE(&PeopleList::remove, bool (PeopleList::*)(const std::string&, const std::string&));
CHECKTYPE(&PeopleList::contains, bool (PeopleList::*)(const std::string&, const std::string&) const);
CHECKTYPE(&PeopleList::lookup, bool (PeopleList::*)(const std::string&, const std::string&, InfoType&) const);
CHECKTYPE(&PeopleList::get, bool (PeopleList::*)(int, const std::string&, const std::string&, InfoType&) const);
CHECKTYPE(&PeopleList::swap, void (PeopleList::*)(PeopleList&));

CHECKTYPE(combine, bool (*)(const PeopleList&, const PeopleList&, PeopleList&));
CHECKTYPE(search, void (*)(const std::string&, const std::string&, const PeopleList&, PeopleList&));
}

int main()
{}

  • If you add #include to PeopleList.h, have the typedef define InfoType as std::string, and link your code to a file containing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "PeopleList.h"
#include <string>
#include <iostream>
#include <cassert>
using namespace std;

void test()
{

PeopleList m;
assert(m.add("Fred", "Mertz", "[email protected]"));
assert(m.add("Ethel", "Mertz", "[email protected]"));
assert(m.size() == 2);
string first, last, e;
assert(m.get(0, first, last, e) && e == "[email protected]");
string s1;
assert(m.get(1, first, last, e) &&
(first == "Fred" && e == "[email protected]"));
}

int main()
{

test();
cout << "Passed all tests" << endl;
}

the linking must succeed. When the resulting executable is run, it must write Passed all tests to cout and nothing else to cout.

  • If we successfully do the above, then make no changes to PeopleList.h other than to change the typedefs for PeopleList so that InfoType specifies int, recompile PeopleList.cpp, and link it to a file containing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "PeopleList.h"
#include <string>
#include <iostream>
#include <cassert>
using namespace std;

void test()
{

PeopleList m;
assert(m.add("Fred", "Mertz", 52));
assert(m.add("Ethel", "Mertz", 49));
assert(m.size() == 2);
string first, last;
int a;
assert(m.get(0, first, last, a) && a == 49);
string s1;
assert(m.get(1, first, last, a) &&
(first == "Fred" && a == 52));
}

int main()
{

test();
cout << "Passed all tests" << endl;
}

the linking must succeed. When the resulting executable is run, it must write Passed all tests to cout and nothing else to cout.

  • During execution, if a client performs actions whose behavior is defined by this spec, your program must not perform any undefined actions, such as dereferencing a null or uninitialized pointer.

  • Your code in PeopleList.h and PeopleList.cpp must not read anything from cin and must not write anything whatsoever to cout. If you want to print things out for debugging purposes, write to cerr instead of cout. cerr is the standard error destination; items written to it by default go to the screen. When we test your program, we will cause everything written to cerr to be discarded instead - we will never see that output, so you may leave those debugging output statements in your program if you wish.

Turn it in

There will be a link on CCLE that will enable you to turn in your source files and report. You will turn in a zip file containing these files:

  • PeopleList.h. When you turn in this file, the typedefs must specify string as the InfoType.

  • PeopleList.cpp. Function implementations should be appropriately commented to guide a reader of the code.

On the class website there will also be questions for you to provide the following:

  • a description of the design of your implementation and why you chose it. (A couple of sentences will probably suffice, perhaps with a picture of a typical List and an empty List. Is your list circular? Does it have a dummy node? What’s in your nodes?)

  • A brief description of notable obstacles you overcame.

  • pseudocode for non-trivial algorithms (e.g., PeopleList::remove and combine).

  • a list of test cases that would thoroughly test the functions. Be sure to indicate the purpose of the tests. For example, here’s the beginning of a presentation in the form of code:

The tests were performed on a map from strings to doubles

1
2
3
4
5
6
// default constructor
PeopleList m;
// For an empty list:
assert(m.size() == 0); // test size
assert(m.empty()); // test empty
assert(!m.remove("Ricky", "Ricardo")); // nothing to erase

Even if you do not correctly implement all the functions, you must still list test cases that would test them. Don’t lose points by thinking “Well, I didn’t implement this function, so I won’t bother saying how I would have tested it if I had implemented it.”