在第9章例9.9和例9.10的基础上,写一个函数del,用来删除动态链表中指定的结点。

2016-06-20 09:07:46  分类: c程序设计第四版谭浩强课后答案  参与:

在第9章例9.9和例9.10的基础上,写一个函数del,用来删除动态链表中指定的结点。

c程序设计第四版谭浩强课后答案   c程序设计(第四版)学习辅导 谭浩强 编著
【c源程序】
#include <stdio.h>
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("\nlist null!\n");
      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("delete:%ld\n",num);
   n=n-1;
     }
  else printf("%ld not been found!\n",num);    //找不到该结点
  return(head);
}

 

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

本文链接:http://www.wb98.com/c/post/tanhaoqiang_9.7.html

本站文章搜索:

<< 上一篇下一篇 >>

搜索

网站分类

Tags列表

赞助商链接