我在Reddit发布了同样的帖子,但是没有得到任何评论,所以我决定看看我是否可以在这里得到一些帮助。
我当前的项目给出了一个双向链表.h文件和一个.cpp文件,我们需要在.cpp中实现.h。我现在真的在与链接列表挣扎,我觉得即使我有正确或有点正确的事情都是草率的,并没有满足全部要求。这是.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每次到达时都会崩溃。然后复制构造函数我只是迷失了,我做了很多研究并阅读了其他帖子,但我无法弄明白。我已经注释掉了,因为程序每次到达该函数时都会崩溃,就像使用remove函数一样。我的插入函数应该按顺序插入所有内容,如果已插入则不插入,我将每个项目附加到列表中,然后将它们按顺序排序,从不检查SSN是否已经存在。它有效,但显然不完全。任何建议都会非常有用,因为你可以说我在双重链表上苦苦挣扎。提前谢谢。
答案 0 :(得分:0)
似乎构造函数中的空指针有问题
/*Node *first = n.getHeadPtr();
first = headPtr;
while(headPtr->succ)
此处headPtr
是nullptr,因此headPtr->succ
会引发运行时错误。