我正在上学的实验室工作,而且我仍然坚持使用双重链接列表的功能。当我尝试使用removeFront()函数从列表中删除前节点时,它返回NULL并为我创建一个couts,即使已经有一个列表。我会在下面发布我的cpp文件和主文件,希望有人可以帮我理解它出了什么问题。
//DLinkedList.h
#pragma once
#include <string>
using namespace std;
typedef string Elem;
struct DNode
{
Elem value;
DNode* next;
DNode* prev;
};
class DLinkedList
{
public:
DLinkedList() { header_ = NULL; }
~DLinkedList() { };
bool empty() const;
const Elem& front() const;
const Elem& back() const;
void addFront(const Elem& e);
void addBack(const Elem& e);
void removeFront();
void removeBack();
private:
DNode* header_;
DNode* trailer_;
protected:
void add(DNode* v, const DNode& e);
void remove(DNode* v);
};
//DLinkedList.cpp
#include <iostream>
#include "DLinkedList.h"
using namespace std;
const Elem& DLinkedList::front() const
{
if (header_ == NULL)
{
cout << "Please create a doubly linked list first.\n\n";
}
else
{
return header_->value;
}
}
void DLinkedList::addFront(const Elem& e) //DONE
{
// If there is no header
// create a temporary node and set
// the header to it
if (header_ == NULL)
{
DNode *temp;
temp = new(struct DNode);
temp->prev = NULL;
temp->value = e;
temp->next = NULL;
header_ = temp;
trailer_ = temp;
}
else
{
//Create current node to point to header
// and temp node to be the new front node
DNode *current;
DNode *temp;
current = header_;
temp = new(struct DNode);
temp->prev = NULL;
temp->value = e;
temp->next = current->next;
header_->prev = temp->next;
header_ = temp;
}
cout << "Element Inserted at the front." << endl;
}
void DLinkedList::removeFront()
{
// Check to see if there is anything
// in the list first
if (header_ == NULL)
{
cout << "Create a doubly linked list first.";
}
// Check to see if the list has more than one item.
// If it only has one node erase it.
DNode *current;
current = header_;
if (current->next == NULL)
{
header_->next = NULL;
header_->value = "";
header_->prev = NULL;
header_ = NULL;
}
else
{
current = current->next;
//header_->next = NULL;
//header_->value = "";
//header_->prev = NULL;
header_ = current;
header_->prev = NULL;
}
}
//Main.cpp
#include <iostream>
#include <string>
#include <cassert>
#include "DLinkedList.h"
using namespace std;
int main()
{
DLinkedList album;
album.addFront("Word");
album.addFront("Turn");
album.addFront("Bird");
album.addFront("Weird");
cout << album.front() << endl;
album.removeFront();
cout << album.front() << endl;
system("pause");
return 0;
}
答案 0 :(得分:-1)
这是我怎么做的。
void DLinkedList::removeFront()
{
// Check to see if there is anything
// in the list first
if (header_ == NULL)
{
cout << "Create a doubly linked list first.";
}
// Check to see if the list has more than one item.
// If it only has one node erase it.
if (header_->next == NULL)
{
delete header_;
}
else
{
DNode* next = header_->next;
delete header_;
header_ = next;
header_->prev = NULL;
}
}
答案 1 :(得分:-1)
我认为你的addFront也错了,我会这样做:
void DLinkedList::addFront(const Elem& content) //DONE
{
// If there is no header
// create a temporary node and set
// the header to it
if (header_ == NULL)
{
header_ = new(struct DNode);
header_->prev = NULL;
header_->value = content;
header_->next = NULL;
trailer_ = header_;
}
else
{
//Create current node to point to header
// and temp node to be the new front node
DNode *tmp;
tmp = new(struct DNode);
tmp->prev = NULL;
tmp->value = content;
tmp->next = header_;
header_->prev = tmp;
header_ = tmp;
}
cout << "Element Inserted at the front." << endl;
}