好文档 - 专业文书写作范文服务资料分享网站

《C语言程序设计》课后习题答案(第四版)谭浩强23019

天下 分享 时间: 加入收藏 我要投稿 点赞

struct student {long num; float score;

struct student *next; }; int n;

struct student *del(struct student *head,long num) {struct student *p1,*p2;

if (head==NULL) // 是空表 {printf(\ return(head); }

p1=head; //使p1指向第一个结点

while(num!=p1->num && p1->next!=NULL) //p1指向的不是所要找的结点且后面还有结点 */

{p2=p1;p1=p1->next;} // p1后移一个结点 if(num==p1->num) // 找到了

{if(p1==head)head=p1->next; //若p1指向的是首结点,把第二个结点地址赋予head */ else p2->next=p1->next; // 否则将下一结点地址赋给前一结点地址 printf(\ n=n-1; }

else printf(\ //找不到该结点 return(head); } 9-8

#include struct student {long num; float score;

struct student*next; }; int n;

struct student *insert(struct student *head,struct student *stud) {struct student *p0,*p1,*p2;

p1=head; //使p1指向第一个结点 p0=stud; //指向要插入的结点 if(head==NULL) //原来的链表是空表

{head=p0;p0->next=NULL;} //使p0指向的结点作为头结点 else

{while((p0->num>p1->num) && (p1->next!=NULL))

{p2=p1; //使p2指向刚才p1指向的结点 p1=p1->next;

} //p1后移一个结点 if(p0->num<=p1->num)

{if(head==p1) head=p0; //插到原来第一个结点之前 else p2->next=p0; //插到p2指向的结点之后 p0->next=p1; } else {p1->next=p0; p0->next=NULL; //插到最后的结点之后 } }

n=n+1; //结点数加1 return (head); }

9-9-1

#include #include

#define LEN sizeof(struct student) struct student {long num;

float score; struct student *next; }; int n;

int main()

{struct student *creat();

struct student *del(struct student * ,long);

struct student *insert(struct student *, struct student *); void print(struct student *); struct student *head,stu; long del_num;

printf(\ head=creat();

print(head); printf(\ scanf(\ head=del(head,del_num); print(head);

printf(\ scanf(\ head=insert(head,&stu); print(head); return 0; }

struct student *creat() {struct student *head; struct student *p1,*p2; n=0;

p1=p2=( struct student*) malloc(LEN); scanf(\ head=NULL;

while(p1->num!=0) {n=n+1;

if(n==1)head=p1; else p2->next=p1; p2=p1; p1=(struct student*)malloc(LEN); scanf(\ }

p2->next=NULL; return(head); }

struct student *del(struct student *head,long num) {struct student *p1,*p2; if (head==NULL)

{printf(\ return(head); }

p1=head;

while(num!=p1->num && p1->next!=NULL) {p2=p1;p1=p1->next;} if(num==p1->num) {if(p1==head)head=p1->next; else p2->next=p1->next; printf(\ n=n-1; }

else printf(\ return(head); }

struct student *insert(struct student *head, struct student *stud) {struct student *p0,*p1,*p2; p1=head;

p0=stud; if(head==NULL) {head=p0; p0->next=NULL;} else

{while((p0->num>p1->num) && (p1->next!=NULL)) {p2=p1; p1=p1->next; }

if(p0->num<=p1->num) {if(head==p1) head=p0; else p2->next=p0; p0->next=p1; } else

{p1->next=p0; p0->next=NULL;} }

n=n+1; return(head); }

void print(struct student *head) {struct student *p;

printf(\ p=head;

if(head!=NULL) do {printf(\ p=p->next;

}while(p!=NULL); }

9-9-2

#include #include #define NULL 0

#define LEN sizeof(struct student) struct student {long num;

float score; struct student *next; }; int n;

int main()

{struct student *creat();

struct student *del(struct student * ,long );

struct student *insert(struct student *, struct student *); void print(struct student *); struct student *head,stu; long del_num;

printf(\ head=creat();

print(head); printf(\ scanf(\ head=del(head,del_num); print(head);

printf(\ scanf(\ head=insert(head,&stu); print(head);

printf(\ scanf(\ head=insert(head,&stu); print(head); return 0; }

struct student *creat() {struct student *head; struct student *p1,*p2; n=0;

p1=p2=( struct student*) malloc(LEN); scanf(\ head=NULL;

while(p1->num!=0) {n=n+1;

if(n==1)head=p1; else p2->next=p1; p2=p1; p1=(struct student*)malloc(LEN); scanf(\ }

《C语言程序设计》课后习题答案(第四版)谭浩强23019

structstudent{longnum;floatscore;structstudent*next;};intn;structstudent*del(structstudent*head,longnum){structstudent*p1,*p2;if(head==N
推荐度:
点击下载文档文档为doc格式
87azb13lxw6x2111f20r4n7xz5ee5l00bm8
领取福利

微信扫码领取福利

微信扫码分享