具有多个数据项的链接列表 [英] Linked List with Multiple Data Items
本文介绍了具有多个数据项的链接列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个链接列表,其中不仅包含一个数据。该程序将运行并允许我输入尽可能多的结构,但是当我退出它时,它将正确打印输入的最后一个结构,两次,然后崩溃。
我假设我正在做一些不正确的指针。
这里是代码:
I''m trying to create a linked list which contains more than just a single piece of data. The program will run and allow me to enter as many structs as I want but when I quit it, it will correctly print out the last structure entered, twice, and then crash.
I assume I''m doing something not right with the pointers.
Here''s the code:
void main(void)
{
int x;
struct Student
{
int id;
string name;
string courseCode;
struct Student *next;
} *start, *p;
Student student;
start = NULL;
printf("Enter a sequence of integers, letter to end: ");
while(cin>>x)
{
p = &student;
start = (Student *)malloc(sizeof(struct Student));
if(start == NULL)
{
cout<<"Not enough memory";
exit(1);
}
cout<<"Enter Student ID: ";
cin>>student.id;
cin.sync();
cout<<endl<<"Enter Student Name: ";
getline(cin, student.name);
cin.sync();
cout<<endl<<"Enter Course Code: ";
getline(cin, student.courseCode);
cin.sync();
student.next = p;
}
cout<<endl<<"In reverse order, the following details were entered: "<<endl;
for(p = start; p != NULL; p = p->next)
{
cout<<student.id<<endl;
cout<<student.name<<endl;
cout<<student.courseCode<<endl;
}
}
推荐答案
在最后一个循环中,你打印<$ c $的数据c>学生而不是p
。
学生
在循环中没有变化,它的变化是p
所以你应该打印它。
另外,在C ++中,当你有new
时,你真的不想打电话给malloc
,这些链接会指出你正确的方向;
http://msdn.microsoft.com/en-us/library/kewsb8ba(v = vs.71).aspx [ ^ ]
http://stackoverflow.com/questions/655065/when-should-i- use-the-new-keyword-in-c [ ^ ]
这个稍微改变的实现类型 - 我想你想做什么;
In the last loop you''re printing the data forstudent
instead of forp
.
student
doesn''t change in the loop, it''sp
that changes so you should be printing that instead.
Also, in C++ you really don''t want to callmalloc
when you havenew
, these links will point you in the right direction;
http://msdn.microsoft.com/en-us/library/kewsb8ba(v=vs.71).aspx[^]
http://stackoverflow.com/questions/655065/when-should-i-use-the-new-keyword-in-c[^]
This slightly changed implementation kind-of does what you want I think;
#include<iostream>
#include<string>
using namespace std;
struct Student
{
int id;
string name;
string courseCode;
struct Student* next;
};
void main(void)
{
int x;
Student* previous = NULL;
Student* start = NULL;
printf("Enter a sequence of integers, letter to end: ");
while(cin>>x)
{
Student* student = new Student;
student->next = NULL;
if (start == NULL)
start = student;
cout<<"Enter Student ID: ";
cin>>student->id;
cin.sync();
cout<<endl<<"Enter Student Name: ";
getline(cin, student->name);
cin.sync();
cout<<endl<<"Enter Course Code: ";
getline(cin, student->courseCode);
cin.sync();
if (previous != NULL)
previous->next = student;
previous = student;
}
cout<<endl<<"In reverse order, the following details were entered: "<<endl;
for(Student* p = start; p != NULL; p = p->next)
{
cout<<p->id<<endl;
cout<<p->name<<endl;
cout<<p->courseCode<<endl;
}
}
希望这会有所帮助,
Fredrik
Hope this helps,
Fredrik
这篇关于具有多个数据项的链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文