链接覆盖的数据
chained overwritten data
我用C++编写了这个简单的链表。但是有一个奇怪的问题让这让我很困惑。在我的程序中,用户应该能够输入他们喜欢的任意多的名字和年龄,并且他们都应该相互链接。但是,如果我输入A和1,B和2,C和3,然后输入D和4,那么C and 3
将被D and 4
覆盖。有人能解释为什么会这样吗?
代码:
#include<iostream>
#include<string>
using namespace std;
struct T_Schueler
{
string Name;
string Age;
T_Schueler *pnext;
};
T_Schueler *pStart = NULL;
int main()
{
string name, age;
while(true)
{
system("cls");
cout<<"Please enter name: ";
cin>> name;
cout<<"Please enter age: ";
cin>> age;
T_Schueler *pAllok, *pRun, *pLoesch;
pAllok = new(T_Schueler);
pAllok->pnext = NULL;
cout<<"nnNew dataset created.nn";
if(pStart == NULL)
{
pStart = pAllok;
pStart->Name = name;
pStart->Age = age;
}
else
{
pRun = pStart;
if (pRun->pnext != NULL)
{
pRun = pRun->pnext;
}
pRun->pnext = pAllok;
pRun = pRun->pnext;
pRun->Name = name;
pRun->Age = age;
}
//OUtput
pRun = pStart;
cout<<"Names: "<<endl;
while(pRun != NULL)
{
cout<< pRun->Name << " -> ";
pRun = pRun->pnext;
}
pRun = pStart;
cout<<"nAges: "<<endl;
while(pRun != NULL)
{
cout<< pRun->Age << " -> ";
pRun = pRun->pnext;
}
cout<<endl;
system("PAUSE");
}
cin.get();cin.get();
return 0;
}
当您正在查找链接列表的末尾时
if (pRun->pnext != NULL)
{
pRun = pRun->pnext;
}
您希望使用while
而不是if
来处理列表中已有几个节点的情况。
打印时已经这样做了,但插入时没有。
相关文章:
- C++覆盖父结构中的数据不起作用
- 使用 SQLite 覆盖数据
- 调用Main将替换矢量对象并覆盖数据
- 覆盖复制/移动分配超载时,我是否需要删除当前的成员数据
- 仅覆盖自定义数据架构的迭代器的操作员*()
- C++ 在覆盖新运算符和删除运算符时不释放数据
- 将数据附加到C 中的文件,但如果将程序重新执行,则覆盖数据
- 成员数据在stack实例上覆盖
- C 的多态性问题 - 数据覆盖
- 覆盖.txt文件 C++ 中的数据
- 生成lcov的分支覆盖数据
- 在c++中,将数据插入到文件中的特定位置而不进行覆盖
- 如何在不覆盖其他数据的情况下编辑文件中的数据
- C++ 高效的数据结构,用于附加具有覆盖的数据
- C++ 覆盖文件数据
- 实现一套覆盖数据结构
- 我似乎无法在qt创建器中打印几行数据。程序覆盖除最后一行之外的所有输出
- 如何使用C ++覆盖文件中的特定数据
- 覆盖矢量数据
- std::vector不会为多个向量条目创建cv::Mat的新引用——初始化矩阵时,数据会被覆盖