链表操作实例.doc
上传人:sy****28 上传时间:2024-09-14 格式:DOC 页数:7 大小:39KB 金币:16 举报 版权申诉
预览加载中,请您耐心等待几秒...

链表操作实例.doc

链表操作实例.doc

预览

在线预览结束,喜欢就下载吧,查找使用更方便

16 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

实现单项链表的数据删除、插入、排序等功能*********************************************************************/#include<stdio.h>#include"windows.h"#include"malloc.h"#defineLENsizeof(structstudent)#defineNULL0#defineN5//N为所要创建的链表的长度intn=0;//定义全局变量n,表示链表的节点个数/*******************************************************************Description:声明一个结构体作为链表的节点.tip:student只是一个标签,是不能声明变量的*********************************************************************/structstudent{intnum;floatcj;structstudent*Pnext;};/*******************************************************************Description:创建一个动态链表参数:返回链表的第一个节点的地址,x为链表的长度*********************************************************************/structstudent*pa(intx){structstudent*head;structstudent*p1,*p2;n=0;p1=p2=(structstudent*)malloc(LEN);//开辟一个结构体内存fflush(stdin);//清除缓冲区数据避免直接读入缓冲区数据scanf("%d,%f",&p1->num,&p1->cj);head=NULL;while(p1->Pnext!=NULL){n=n+1;if(n==1){head=p1;//链表的头地址}else{p2->Pnext=p1;//链接链表数据}/*判断是否最后一个*/if(n>=x){p1->Pnext=NULL;}else{p2=p1;p1=(structstudent*)malloc(LEN);fflush(stdin);scanf("%d,%f",&p1->num,&p1->cj);}}return(head);}/*******************************************************************Description:输出一个链表参数:head为第一个节点的地址*********************************************************************/voidprint(structstudent*head){structstudent*p;inti=0;printf("\nNow,These%drecordsare:\n",n);p=head;if(head!=NULL)//如果链表不是空的,则进行数据输出{do{printf("%d\t",i++);printf("%5d%5.1f\n",p->num,p->cj);//输出链表数据p=p->Pnext;}while(p!=NULL);}}/*******************************************************************Description:释放动态链表的地址空间参数:链表的头地址head,无返回值*********************************************************************/voidfreelinkspace(structstudent*head){structstudentLtemp;Ltemp.Pnext=head->Pnext;//Ltemp用来存放->next,避免空间被//释放后,找不到下一个结点的地址while(head->Pnext