您的位置:首页 > 编程语言 > C语言/C++

C++ 链表操作

2004-08-17 16:27 369 查看
#include <iostream.h>

struct student
{
int num;
int score;
struct student *next;
};

int n;

struct student *CreateLink()
{
struct student *head;
struct student *p1=NULL;
struct student *p2=NULL;
int num = 1;
int score = 11;
n = 0;
p1 = new struct student();
p2 = p1;
if (p1 == NULL)
{
cout<<"/nCann't create it, try it again in a moment!/n";
return NULL;
}
else
{
head = NULL;
p1->num = num++;
p1->score = score++;
}

while(p1->num < 10)
{
n += 1;
if (n==1)
{
head = p1;
p2->next = NULL;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = new struct student();
p1->num = num++;
p1->score = score++;
}
p2->next = NULL;
delete(p1);
p1 = NULL;
return head;
}

void Print(struct student *head)
{
struct student *p;
p = head;
if(head != NULL)
{

do
{
cout<num<<'/t'<score<<'/t'<next<
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: