在链表末尾插入字符串
Inserting string at end of linked list
我在链表的末尾插入一个字符串。当我编译文件时,我收到 2 个错误:
错误:"setData"未在此范围内声明 设置数据(*string_p(;
错误:"getNext"未在此范围内声明 newNode = getNext((;
但是,它们是在使用它们之前定义的(在上述方法中定义(,所以我不明白错误。
#include <iostream>
#include <string>
using std::string;
using std::cout;
using std::endl;
#define SUCCESS 0
#define FAIL 1
// Represents an entry object in the linked-list
class ListEntry
{
public:
explicit ListEntry();
explicit ListEntry(const char *string_p);
~ListEntry();
string getData();
void setData(const char* string_p);
void setData(string string);
ListEntry *getNext();
ListEntry *getPrevious();
ListEntry *prev_p; // pointer to previous entry in the linked-list
ListEntry *next_p; // pointer to next entry in the linked-list
private:
string data; // entry's string
};
// Represents the linked-list object
class List
{
public:
List();
~List();
bool printForward();
bool printReverse();
bool insert(const char *string_p);
private:
int entryCount; // number of entries present in the linked-list
ListEntry *head_p; // pointer to the first entry in the list
ListEntry *tail_p; // pointer to the last entry in the list
};
// ListEntry constructor
ListEntry::ListEntry()
{
this->prev_p = NULL;
this->next_p = NULL;
return;
}
// ListEntry constructor
ListEntry::ListEntry(const char *string_p)
{
this->data = string_p;
this->prev_p = NULL;
this->next_p = NULL;
return;
}
// List entry destructor
ListEntry::~ListEntry()
{
return;
}
// Return the stored string object
string ListEntry::getData()
{
return this->data;
}
// Set the internal string data from a char*
void ListEntry::setData(const char* string_p)
{
this->data = string_p;
}
// Set the internal string data from a string
void ListEntry::setData(string string)
{
this->data = string;
}
// Returns reference to the next entry in the list
ListEntry *ListEntry::getNext()
{
return this->next_p;
}
// Returns reference to the previous entry in the list
ListEntry *ListEntry::getPrevious()
{
return this->prev_p;
}
还有我的插入函数(在我的程序中低于上述方法(:
bool List::insert(const char *string_p)
{
// Please write the list insert function
//new node to be inserted
ListEntry* newNode = new ListEntry();
//List *newList = new List();
if(newNode == NULL)
{
cout << "FAILED";
}
else
{
setData(*string_p); //////ERROR HERE
if(this->head_p = NULL)
{
newNode = getNext(); //////ERROR HERE
newNode = this->head_p;
this->head_p = newNode; // newNode now points to the head node
this->entryCount++;
return SUCCESS;
}
else
{
ListEntry* temp = this->head_p;
while(temp -> next_p != NULL)
{
temp = temp -> next_p;
}
temp -> next_p = newNode;
this->entryCount++;
return SUCCESS;
}
}
}
您已经定义了函数,但没有按照定义的方式使用它们:
setData(*string_p); // Takes a const char*, but you have provided a char.
// *string_p dereferences the string pointer, giving the
// first char.
newNode = getNext(); // getNext is a ListEntry function, but you are trying
// to use it in the context of List. This is also true of the
// above function.
您的insert()
方法实现得完全错误。 它应该看起来更像这样:
int List::insert(const char *string_p)
{
//new node to be inserted
ListEntry* newNode = new ListEntry(string_p);
if (newNode == NULL)
{
cout << "FAILED";
return FAIL;
}
if (this->head_p == NULL) {
this->head_p = newNode;
}
if (this->tail_p != NULL)
{
this->tail_p->next_p = newNode;
newNode->prev_p = this->tail_p;
}
this->tail_p = newNode;
this->entryCount++;
return SUCCESS;
}
函数
setData
和getNext
是类ListEntry
的非静态成员函数。因此,必须使用成员访问表达式调用它们。
此外,此调用提供的参数
setData(*string_p);
具有与函数预期不同的类型。
你必须至少写像
newNode->setFata( string_p );
和
newNode->getNext();
尽管即使从语法的角度来看函数的调用是正确的,此代码片段也没有意义
if(this->head_p = NULL)
{
newNode = newNode->getNext();
newNode = this->head_p;
因为至少存在内存泄漏。
还有这个如果语句
if(newNode == NULL)
如果您使用新运算符的以下调用,这将是有意义的
ListEntry* newNode = new ( std::nothrow ) ListEntry();
该函数可以如下所示
bool List::insert( const char *string_p )
{
//new node to be inserted
ListEntry *newNode = new ( std::nothrow ) ListEntry( string_p );
bool success = newNode != nullptr;
if ( success )
{
if ( tail_p )
{
tail_p->next_p = newNode;
newNode->prev_p = tail_p;
}
else
{
head_p = newNode;
}
tail_p = newNode;
entryCount++;
}
return success;
}
相关文章:
- 如何在C++中.txt文件的开头插入字符串
- 在字符串的每个单词的末尾插入字符串
- 我正在尝试将 int 值插入字符串,而不是 1,我得到 10
- 如何在C++的路径中插入字符串?
- 在子字符串后插入字符串
- 删除空格后如何将其插入字符串中
- 将元素插入字符串集 c++ 的向量中
- 在链接时间插入字符串
- 在链表末尾插入字符串
- 链接列表并按字母顺序插入字符串
- C 链接列表,插入字符串
- 将字符插入字符串
- 在二叉搜索树C++中插入字符串时出错
- 将字符插入字符串中
- 插入字符串时出错<<过载C++
- 列表数组<string>,插入字符串行为奇怪
- 如何使用C++中的插入函数将整数插入字符串
- 插入字符串时增加二进制搜索树的大小
- 在特定索引c++/Qt的另一个字符串中插入字符串
- 为什么从Mac OSX管道(2)读取(2)会将0x7F插入字符串中