C++编程:编写一个函数creat,用来建立一个动态链表。所谓建立动态链表是指在程序执行过程中从无到有地建立一个链表,即一个一个地开辟结点和输入各结点,并建立起前后相连的关系。各结点的数据由键盘输入。

以下是此题的【c++源代码】
#include <iostream>
using namespace std;
#define NULL 0    
struct student
{long num;
 float score;
 student *next;
};   
                    //定义n为全局变量,本文件模块中各函数均可使用它
student *creat(void)     //定义函数。此函数带回一个指向链表头的指针
{student *head;
 student *p1,*p2;
 int n=0;
 p1=p2=new student;       //开辟一个新单元,并使p1,p2指向它
cin>>p1->num>>p1->score;
head=NULL;
while(p1->num!=0)
{n=n+1;
 if(n==1) head=p1;
 else p2->next=p1;
 p2=p1;
 p1=new student;
 cin>>p1->num>>p1->score;
}
p2->next=NULL;
return(head);
}   


来源:c++程序设计第三版谭浩强课后答案

本文链接:http://www.wb98.com/cjia/post/cjia_7.6.html


本站文章搜索:

<< 上一篇下一篇 >>

搜索

Tags列表

赞助商链接