所以,我正在尝试实现一个复制构造函数,在实例化一个对象时,我可以选择从头到尾(正常)或尾到头(反向)复制列表。现在,当我使用复制构造函数时,它总是从头到尾复制,即使它进入相反的条件。
这是我的头文件:
#include <iostream>
#include "Student.h"
#include "Node.h"
using namespace std;
class List {
public:
List(); // Default constructor
List(const List&); // Copy constructor (2-in-1, see lab sheet)
~List(); // Destructor
bool isEmpty(); // List is empty: TRUE or FALSE?
int getNumNodes() {return numNodes;} // How many Nodes in List
void append(Student *); // Append new Node to head or tail of List
void insert(Student *); // Inserts new Node in the
// Appropriate location in List
void deleteNode(string); //Search for and delete specific Node
void displayAscending();// Display List HEAD to TAIL
void displayDescending(); // Display List TAIL to HEAD
// input Student::data into Student pointer.
void input(Student*, string, string, string, string, string);
Node *getHead() const {return head;} // ptr to head.
Node *getTail() const {return tail;} //ptr to tail.
private:
void printer(Node *); //recursive function w/in displayDescending()
Node *head;
Node *tail;
bool empty;
bool forward; // forward = head-to-tail i.e. true
int numNodes; // number of nodes in the list
};
这是我的复制构造函数。
List::List(List &list) { // Copy constructor
head = NULL; // Head pointer set to NULL initially
tail = NULL; // Tail pointer set to NULL initially
empty = true;
forward = true; // Copies head-to-tail by default.
numNodes = 0;
string flag; // Stores prompt value.
cout << "Copy from head to tail? (y/n): ";
cin >> flag; // prompt for user.
if(flag == "n")
forward = false;
Node *curr = NULL; //initialize curr in function scope.
if(flag == "n") {
forward = false;
curr = list.getTail(); // curr points to list tail.
cout << "Copying in reverse order...\n" << endl;
} else { // forward == true
curr = list.getHead(); // curr points to list head.
cout << "Copying from head-to-tail...\n" << endl;
} // end if/else
while(curr) {
string f = ( curr->getData()->getFirst() );
string m = ( curr->getData()->getMid() );
string l = ( curr->getData()->getLast() );
string s = ( curr->getData()->getSocial() );
string a = ( curr->getData()->getAge() );
Node *nodePtr = NULL; // a node that's pointing
// using it to point to creation of
// a new node
Student *stuPtr = new Student; // creates a stud pointer on
// heap of Student class in
// order to store stud info
input(stuPtr,f,m,l,s,a); // input Student::data into stuPtr.
append(stuPtr); // append the node with stuPtr to head or tail
// of list.
if(!forward)
curr = curr->getPrev();
else
curr = curr->getNext();
} // end while*/
cout << "Done copying!\n" << endl;
} // end copy constructor
此外,如果您需要查看它如何附加到列表,这里是 append() 函数。
void List::append(Student *newStudent) {
Node *newNode = new Node(newStudent); // new Node containing student arg.
newNode->getData(); // get data of student arg.
if(isEmpty()) { // tail=NULL, no list.
cout << "List is empty. Inserting first Node.\n" << endl;
head = newNode;
tail = newNode; // new Node becomes head & tail.
} else {
if(forward) { // append to tail of list.
tail->setNext(newNode); // NEXT ptr of tail points to newNode.
newNode->setPrev(tail); // newNode's PREV points to former tail.
tail = newNode; // newNode becomes the new tail.
} else { // append to head of list.
head->setPrev(newNode); // PREV ptr of head points to newNode.
newNode->setNext(head); // newNode's NEXT points to former head.
head = newNode; // newNode becomes the new head.
} // end if/else
} // end if/else
numNodes++;
}