我发布了这个帖子到Reddit,但没有得到任何评论,所以我决定看看我能否在这里得到一些帮助。双链表列表问题(具体复制构造函数和删除函数)
我目前的项目中给出了一个双向链表.h文件和一个.cpp文件,我们需要在.cpp中实现.h。尽管我很担心链接列表,但我觉得即使我有权利或有点权利的事情都是sl and不驯的,并且不能满足所有的要求。这里是.h文件:
#include <string>
using namespace std;
struct Node{
string ssn;
string name;
Node* succ;
Node* pred;
};
class DLL{
private:
Node* headPtr;
int itemCount;
public:
DLL();
DLL(DLL& n);
virtual ~DLL();
Node* getHeadPtr();
int search(string ss)const;
bool insert(string ss, string name, int & count);
bool remove(string ss, int & count);
int size();
void display();
};
,这里是.cpp文件:
#include "DLL.h"
#include <iostream>
#include <string>
using namespace std;
// default constructor is already implemented
// do not modify the default constructor
DLL::DLL(){
headPtr = nullptr;
itemCount=0;
}
// return the head pointer of the list
// it is already implemented, do not modify it
Node* DLL::getHeadPtr(){
return headPtr;
}
// copy construct, which copies an existing list n
// the new list is a different object from n
// the new list and object n have the same contents
// Please implement it
DLL::DLL(DLL& n){
/*Node *first = n.getHeadPtr();
first = headPtr;
while(headPtr->succ)
headPtr = headPtr->succ;
while(headPtr){
first->succ = headPtr->succ;
headPtr->succ = headPtr->succ->succ;
}*/
}
// destructor
// release every node of the list
// Please implement it
DLL::~DLL(){
Node *tmp = this->headPtr;
Node *temp;
while(tmp->pred)
tmp = tmp->pred;
while(tmp)
{
temp = tmp->succ;
delete tmp;
tmp = temp;
}
tmp =NULL;
temp = NULL;
}
// if some node has SSN matcthes string ss
// return the index value of the node
// the index value of the first node is 0, the second node is 1, etc.
// if there is node matching ss, return -1
int DLL::search(string ss)const{
int count = 0;
Node *tmp = headPtr;
while(tmp != NULL){
count++;
if(tmp->ssn == ss)
return count -1;
tmp = tmp->succ;
}
return NULL;
}
// insert (ss, name) to the existing list
// the SSN values are each node are organized in INCREASING order
// if there is a node matching ss value, return false; otherwise true
// else create a node with (ss, name), insert the node to the proper position
// parameter count is the counter of number of valid insertion
// when you implement this method, consider the following situations:
// 1. list is empty
// 2. node should be inserted to the beginning of the list
// 3. node should be inserted to the middle of the list
// 4. node should be inserted to the end of the list
//I use the append function, then sort afterwards
bool DLL::insert(string ss, string name, int & count){
//Create Node
Node *newPtr = new Node;
newPtr->ssn = ss;
newPtr->succ = NULL;
//If list is empty
if(headPtr == NULL){
headPtr = newPtr;
return true;
}
//If list is not empty
else{
Node* temp = headPtr;
while(temp->succ != NULL){
temp = temp->succ;
itemCount++;
count++;
}
temp->succ = newPtr;
//Following part is to sort from least to greatest (based on Lab 7)
//Store head to temp node
temp = headPtr;
string tempVal;
int counter = 0;
//Set temp to next, increase count
while (temp){
temp = temp->succ;
counter++;
}
//Make temp head again
temp = headPtr;
//While less than count and has next node, check if val at temp is greater than val at next
for (int j=0; j<count; j++){
while (temp->succ){
if (temp->ssn > temp->succ->ssn){
//Store val at temp to temp val
tempVal = temp->ssn ;
//Make temp's val the value at temp->next
temp->ssn = temp->succ->ssn;
//Make temp->next's val, temp's old val
temp->succ->ssn = tempVal;
}
//Make temp the next value and check again
temp = temp->succ;
}
//Move temp back to head
temp = headPtr;
}
return true;
}
return false;
}
// remove node containing ss value
// if there is no node containing ss, return false; otherwise true
// consider the following situations:
// 1. list is empty
// 2. node containing ss value is the first node
// 3. node containing ss value is in the middle of the list
// 4. node containing ss value is the last node of the list
bool DLL::remove(string ss, int & count){
/*Node *temp = headPtr;
while(temp){
if(temp->ssn == ss){
temp->pred->succ = temp->succ;
temp->succ->pred = temp->pred;
count++;
return true;
}
temp = temp->succ;
}
return false;*/
}
// return the number of the nodes
// it is already implemented, do not modify it
int DLL::size(){
return itemCount;
}
// iterate through each node
// print out SSN and memory address of each node
// do not modify this method
void DLL::display(){
Node* temp;
temp = headPtr;
while (temp!= nullptr) {
cout << temp->ssn << "\t" << temp << endl;
temp = temp->succ;
}
}
我想我的搜索功能是好的,有评论说,说不要修改功能是由由教授,所以他们很好,和.h文件是好的,因为它也包括在内。尽管如此,我真的很努力地使用复制构造函数和删除函数。我的删除函数的当前内容被注释掉了,因为每次达到它时test.exe都会崩溃。然后复制构造函数我只是失去了,我做了大量的研究和阅读其他帖子,但我无法弄清楚。我有我的意见,因为每次它到达该功能程序崩溃,就像使用删除功能。我的插入函数应该按顺序插入所有内容,如果插入函数已插入,我会将每个项目追加到列表中,然后将它们按顺序排序,从不检查SSN是否已经在。它有效,但显然不完全。任何建议都将是非常有用的,因为你可以告诉我双向链接列表挣扎了很多。先谢谢你。
在旁注:我猜对返回值的评论是来自教授,你应该**严格遵循这些(例如:你的搜索函数应该返回'-1'时,找不到值,你的返回NULL) – ccKep
@ccKep它实际上返回-1当没有找到,我会说实话我不知道为什么,我知道它不应该,但由于某种原因它确实。 – paul5345
在这种情况下,您应该完全调试程序流(并检查实际上是您编译的代码),该函数无法产生小于0的值。 – ccKep