C 链接列表仅输出其他每个元素
c++ linked list only outputting every other element
我正在尝试创建一个充满字符的链接列表。以下代码仅保存其他所有元素,我可以修改如何解决此问题?附件是用户阅读输入中使用的两个功能。
void LList :: InsertTail(element thing) {
// PRE : the N.O. LList is valid
// POST : the N.O. LList is unchanged, except that a
// new listnode containing element thing has been
// inserted at the tail end of the list
listnode * temp;
temp = new listnode;
temp -> data = thing;
temp -> next = NULL;
if(head == NULL)
head = temp;
else
tail -> next = temp;
tail = temp;
}
void LList :: ReadForward() {
// PRE: the N.O. LList is valid
// POST : the N.O. LList is valid, all of its
// previous listnodes have been deleted, and
// it now consists of new listnodes containing
// elements given by the user in foward order
char userval;
Clean();
cout << "Enter the message: ";
userval = cin.get();
cout << userval;
while (cin.get()!= SENTINEL) {
InsertTail(userval);
userval = cin.get();
cout << userval;
}
cin.clear();
cin.ignore(80, 'n');
}
问题是您的读书。
每次致电CIN.Get()您正在阅读另一个字符 - 因此跳过该角色而被添加。
将其更改为:
while(userval) {
问题在您的while()
循环中ReadForward()
方法:
while (cin.get() != SENTINEL) { <----
InsertTail(userval);
userval = cin.get();
cout << userval;
}
在标记的行上,您调用cin.get()
,但切勿将其存储在任何地方。这会丢弃所有其他字符,因为您只是在读之后仅存储一个字符。
修复程序是每次运行循环时都会在userval
内存储get()
的结果。
cout << "Enter the message: ";
while (cin >> userval) {
cout << userval;
InsertTail(userval);
}
相关文章:
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 为什么这个选择排序算法仍然切换一个元素,当它已经是其他元素中最小的元素时?
- 将 Vector<String > 元素复制到其他 Vector<String>* (1 作为
- 将向量C++中的每个其他元素累加
- 如果类包含基类类型的成员作为第一个元素,后跟其他成员,编译器是否可以优化空基?
- 我有一系列阵列.可以从一个数组中阅读一些元素和另一个数组中的其他元素
- 在一个类中向向量添加元素不适用于其他类
- 当从其他方法返回 vector 时,C++无法访问矢量元素
- 我无法将二维数组的两个元素的差异分配给C++中其他数组的元素?
- 将多种数据类型写入文件时的其他行和额外的数组元素
- 如何在unordered_map中插入 3 个元素或其他奇数个元素
- 检查unordered_set是否包含其他unordered_set中的所有元素 - C++
- C++11 在列表到映射(或其他容器)之间移动元素
- C++:从动态结构数组中删除元素并移动其他元素
- 为什么 size() 在与其他堆栈交换元素后提供错误的堆栈大小?
- 使用对其他枚举值的操作定义枚举元素值
- C 链接列表仅输出其他每个元素
- 从两个列之一返回值,或跳过数组中的所有其他元素
- 检查两个数组中的相似元素,为某些测试返回正确的值,为其他测试返回不正确的值
- 子级访问同一父级的其他子级的元素