#include <iostream>
using namespace std;
struct Linked
{
/********* Begin *********/
//结构体的成员变量
int num;
float score;
Linked* next;
/********* End *********/
};
Linked* Create()
{
/********* Begin *********/
//创建并返回一个新链表
Linked* head = new Linked;
head->next = 0; //初始化为0
return head;
/********* End *********/
}
void InsertAfter(Linked* node, int num, float sc)
{
/********* Begin *********/
//在指定节点后插入一个新节点,内容由 num,sc 参数指定
Linked* n = new Linked;
n->num = num;
n->score = sc;
n->next = node->next;
node->next = n;
/********* End *********/
}
void DeleteAfter(Linked* node)
{
/********* Begin *********/
//删除此节点之后的一个节点
node->next = node->next->next;
/********* End *********/
}
Linked* GetByIndex(Linked* head, int index)
{
/********* Begin *********/
//返回指定索引处的节点
Linked* ptr = head->next;
while (index)
{
ptr = ptr->next;
index--;
}
return ptr;
/********* End *********/
}
void PrintAll(Linked* head)
{
/********* Begin *********/
//按格式打印此链表中所有节点的成员变量
Linked* ptr = head->next;
while (ptr)
{
cout << ptr->num << " " << ptr->score << endl;
ptr = ptr->next;
}
/********* End *********/
}
int main()
{
int num;
float score;
cin >> num >> score;
Linked* lk = Create();
InsertAfter(lk, num, score);
cin >> num >> score;
InsertAfter(GetByIndex(lk, 0), num, score);
cin >> num >> score;
InsertAfter(GetByIndex(lk, 1), num, score);
DeleteAfter(GetByIndex(lk, 0));
PrintAll(lk);
}
头歌练习题网址:C&C++ 面向过程编程综合练习 (educoder.net)