【发布时间】:2021-11-20 00:14:43
【问题描述】:
问候堆栈溢出。我的程序应该采用用户输入的字符行并将它们附加到列表中。如果在输入中读取主题标签,该程序还应该删除最近附加的字符。
我的程序大部分都可以运行,但是当我尝试通过添加太多主题标签来破坏它时遇到了错误。执行此操作后,如果使用了太多主题标签,则列表停止接受附加内容将不显示任何内容。
我希望我只包含我认为有用的代码,如果不需要 main 函数,请见谅。
#include <iostream>
using namespace std;
class doubleList
{
public:
doubleList() { first = NULL; } // constructor
void append(char); // adds entry to the end of the list
void remove_last(); // removes the last item from a list
friend ostream& operator<<(ostream& out, const doubleList& l); // outputs in forward order
private:
struct Node
{
char data;
Node *next;
Node *prev;
};
Node *first;
Node *last;
};
void doubleList::append(char entry)
{
Node* temp = new Node();
temp -> data = entry;
temp -> next = NULL;
if (first == NULL)
{
first = temp;
last = temp;
}
else
{
last -> next = temp;
temp -> prev = last;
last = temp;
}
}
void doubleList::remove_last()
{
if (first -> next == NULL)
{
delete first;
}
else if (first != NULL)
{
last = last -> prev;
delete last -> next;
last -> next = NULL;
}
}
ostream& operator<<(ostream& out, const doubleList& l)
{
doubleList::Node* q;
q = l.first;
while (q != NULL)
{
out << q -> data;
q = q -> next;
}
return out;
}
int main()
{
doubleList list;
char ch[100];
cout << "Enter a line of characters; # will delete the most recent character." << endl;
for (int i = 0; i < 100; i++)
{
cin.get(ch[i]);
list.append(ch[i]);
if (ch[i] == '#')
{
list.remove_last();
list.remove_last(); // called twice becaue it removes the hashtag from the list
} // and i was too lazy to make it so it doesnt do that so this
// is simply an easier fix
if (ch[i] == '\n') // exits the loop when enter is clicked
break;
}
cout << list;
return 0;
}
我的程序成功运行如下所示:
Enter a line of characters; # will delete the most recent character.
abcd##fg
abfg
添加过多标签时我的程序:
Enter a line of characters; # will delete the most recent character.
ab#####efgh
在用户输入后没有显示任何内容。提前致谢。
【问题讨论】:
-
请发帖minimal reproducible example。什么是附加()?您还缺少包含 to 以便编译。
-
如前所述,我已经删除了很多代码,只显示了我认为有问题的功能。主函数中列出的附加函数会将数据添加到列表的末尾。很抱歉忘记了那个。这能澄清什么吗?
-
注意:
first -> next更常见(总是?)写成first->next。 -
使用更新的代码和输入 `ab#####efgh` t 在
doubleList::append()上的operator new(unsigned long)上为我崩溃。这是一个完全不同的问题。 -
minimal reproducible example 的提示: 不要依赖用户输入。您有导致崩溃的示例输入,因此只需假设这是输入。 (不过,更短的输入会更好。根据问题描述,我猜
a##d的输入足以重现问题。)也就是说,您的主要功能可以减少到int main() { doubleList list; list.append('a'); list.append('#'); list.remove_last(); list.remove_last(); list.append('#'); list.remove_last(); list.remove_last(); list.append('d'); cout << list; }。保持简单,忽略您的最终功能,并专注于错误。
标签: c++ class linked-list doubly-linked-list function-definition