数据结构课程计报告链表.docVIP

  • 2
  • 0
  • 约4.39千字
  • 约 13页
  • 2018-11-03 发布于福建
  • 举报
数据结构课程计报告链表

班级: 姓名: 学号: 设计时间: 一、应用程序的名称:链表 二、应用程序的主题与设计目的:实现一个链表的建立、输出,并且完成节点的插入、删除操作。 三、应用程序简介: 1、基本结构 A、功能模块图 B、各模块流程图 (1) 链表的建立: 开辟一个新节点,使p1、P2指向它 读入一个学生数据给p1所指的结点 Y N 输出p指向的节点并且p 指向下一个节点 (3) 链表结点的删除 Y N P2后移一个位置 P1后移一个位置 N Y N p1是要删除的结点 Y (4) 链表节点的插入 Y N N Y 2、基本内容:(源代码及注释) #includestdio.h #includemalloc.h #define LEN sizeof(struct student) int n; struct student {int num; int score; struct student *next;}; struct student *creat(void) /*定义函数,此函数带回一个指向链表头的指针*/ {struct student *head; struct student *p1,*p2; n=0; p1=p2=(struct student *)malloc(LEN); /*开辟一个新单元*/ scanf(%d,%d,p1-num,p1-score); head=NULL; while(p1-num!=0) { n=n+1; if(n==1)head=p1; else p2-next=p1; /*把p1所指的结点连接在p2所指的结点后面*/ p2=p1; p1=(struct student*)malloc(LEN); scanf(%d,%d,p1-num,p1-score);} p2-next=NULL; return(head); /*函数返回head的值,即链表中第一个节点的起始地址*/ } void print(struct student*head) {struct student*p; printf(\nNow,these %d records are:\n,n); p=head; if(head!=NULL) do { printf(%d %d\n,p-num,p-score); p=p-next; }while(p!=NULL); } struct student*del(struct student*head,int num) {struct student*p1,*p2; if(head==NULL) {printf(\nlist null! \n); return head; } p1=head; 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:%d\n,num); n=n-1; } else printf(%d not beed found!\n,num); /*找不到该节点*/ return(head); } struct student*insert(struct student*head,struct student*stud) {struct student*p0,*p1,*p2; p1=head; /*使p1指向第一个节点*/ p0=stud; /*p0指向要插入的节点*/ if(head==NULL) /*原来的链表是空表*/ {head=p0; p0-next=NULL; } /*使p0指向的节点作为头结点*/ else {while((p0-nump1

文档评论(0)

1亿VIP精品文档

相关文档