| 网站首页 | 业界新闻 | 群组 | 交易 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
 跟大牛学C++学算法数据结构

已结贴   问题点数：10  回复次数：2

#include<stdio.h>
struct employee
{
int no;
char name[20];
struct employee *next;
};
struct employee *insert(struct employee *head, struct employee *pb)
{
struct employee *pn, *pt;
pn = head;
if (pn == NULL)
{
head = pb;
pb->next = NULL;
}
else
{
while ((pn->no != pb->no)&&(pb->next != NULL))
{
pt = pn;
pn = pn->next;
}
if (pn->no == pb->no)
{
if (head == pn)
head = pb;
else
pt->next = pb;                                          //???????
pb->next = pn;
}
else
{
pn->next = pb;
pb->next = NULL;
}
}
return head;
得分:10

#include <stdio.h>

struct employee
{
int no;
char name[20];
struct employee *next;
};

struct employee *insert(struct employee *head, struct employee *pb)
{
struct employee *pn, *pt;
pn = head;
pt = NULL;
while (pn)
{
if (pn->no == pb->no)
{
pb->next = pn;
break;
}
pt = pn;
pn = pn->next;
}
if (pt)
pt->next = pb;
else
head = pb;
return head;
}

main()
{
struct employee a[6] =
{
101, "a101", NULL,
103, "a103", NULL,
102, "a102", NULL,
101, "a101", NULL,
103, "a103", NULL,
102, "a102", NULL
};
struct employee *head = NULL;
int i;
for (i=0; i<6; ++i)
head = insert(head, &a[i]);
struct employee *h;
for (h=head; h; h=h->next)
printf("%d\t%s\n",h->no, h->name);
}
得分:0

• 3
• 1/1页
• 1

Powered by Discuz, Processed in 0.053326 second(s), 8 queries.
Copyright©2004-2019, BCCN.NET, All Rights Reserved