链表插入
Linked Lists Insert
我正在努力解决这个链表的例子,我无法更改下面第二个代码块中的任何代码,但是我可以自由编辑这个函数。因此,我相信节点的使用将不起作用,因为我无法在任何地方添加它们。有什么想法吗?
// Insert the given string into the linked-list such that the
// entries in the linked-list are in alphabetical order
bool List::insert(const char *string_p)
{
// Please write the list insert function
return SUCCESS;
}
无法修改的代码如下
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;
}
// List constructor
List::List()
{
this->entryCount = 0;
this->head_p = NULL;
this->tail_p = NULL;
}
// List destructor
List::~List()
{
// Delete all entries in the list
ListEntry *entry_p = this->head_p;
ListEntry *current_p = this->head_p;
while (entry_p != NULL)
{
current_p = entry_p;
entry_p = entry_p->getNext();
delete current_p;
}
}
// Output linked list in order from head to tail
// printing out the string data from each list entry
bool List::printForward()
{
ListEntry *entry_p = this->head_p;
int count = 0;
cout << "FORWARD: " << this->entryCount << " entriesn";
while (entry_p != NULL)
{
cout << entry_p->getData() << " ";
if (++count % 5 == 0 || entry_p == this->tail_p)
{
cout << endl;
}
entry_p = entry_p->getNext();
}
return SUCCESS;
}
// Output linked list in reverse order from tail to head
// printing out the string data from each list entry
bool List::printReverse()
{
ListEntry *entry_p = this->tail_p;
int count = 0;
cout << "REVERSE: " << this->entryCount << " entriesn";
while (entry_p != NULL)
{
cout << entry_p->getData() << " ";
if (++count % 5 == 0 || entry_p == this->head_p)
{
cout << endl;
}
entry_p = entry_p->getPrevious();
}
return SUCCESS;
}
所以你已经解决了大部分困难的东西。 你要做的是完成插入功能女巫有2个特殊情况。
1( 当head_p为空时(列表为空(2( 当head_p不为空时(列表不为空(
在插入方法中使用它,您可以获取给定const char *string_p
并从中创建ListEntry。 从那里,您将创建的ListEntry插入到列表中。
如果head_p为 null,那么您基本上是在创建列表,并且需要设置指向新 ListEntry 的头和结束指针。 如果列表不为空,则必须将其添加到末尾。 这需要更新 ListItem 中的prev_p和next_p指针(将此留给您练习(。
相关文章:
- 链表c++插入,所有情况都已检查,但没有任何工作
- 如何按数字顺序插入链表节点?
- 在链链表中手动插入第五个元素
- 在链表中的第 n 位插入显示分割错误
- 将二叉树的节点插入链表 (C++)
- 在将第一个值插入链表时尝试访问 head 时出现分段错误
- 插入链表和无限循环的末尾
- C++中的数据结构,插入链表中节点的开头
- 将节点插入链表只会转到开头或结尾
- 如何将唯一的随机整数插入链表中
- 按排序顺序将节点插入链表
- C++内存从将数据插入链表中泄漏
- 将整数按升序插入链表
- 将节点正确插入链表C++中
- 尝试将节点插入链表中,但我需要一个重载运算符<函数
- 插入链表的后面
- 在间隔树中插入链表元素
- 将单词插入链表时出错
- 如何在head中的特定节点后插入链表?c++
- 将项插入链表后进行排序