|
1鱼币
#include "stdafx.h"
#include <iostream>
typedef struct Student{
unsigned short index;
unsigned short grade;
Student* next;
};
int _tmain(int argc, _TCHAR* argv[])
{
//input all information nutill input index-0
Student* head = nullptr;
Student* ptr = nullptr;
while (true)
{
Student* p;
Student* getStudent();
p = getStudent();
if (p->index == 0)
{
break;
}
if (head == nullptr)
{
head = p;
}
if (head != nullptr&&head->next == nullptr)
{
head->next = p;
ptr = p;
}
if (ptr != nullptr)
{
ptr->next = p;
ptr = p;
}
}
//output all information
while (head!=nullptr)
{
std::cout << head->index << " " << head->grade << std::endl;
head = head->next;
}
return 0;
}
//method to get Student*
Student* getStudent(){
Student a = {0,0,nullptr};
Student* p =&a;
std::cout << "please input index and grade of a student:";
std::cin >>p->index >> p->grade;
p->next = nullptr;
return p;
}
|
|