2022年2022年链表操作实例 .pdf
实现单项链表的数据删除、插入、排序等功能*/ #include #include windows.h #include malloc.h #define LEN sizeof(struct student) #define NULL 0 #define N 5 /N 为所要创建的链表的长度int n = 0; /定义全局变量n,表示链表的节点个数/* Description: 声明一个结构体作为链表的节点. tip: student 只是一个标签 ,是不能声明变量的*/ struct student intnum; floatcj; struct student *Pnext; ; /* Description: 创建一个动态链表参数 : 返回链表的第一个节点的地址,x 为链表的长度*/ struct student *pa(int x) struct student *head; struct student *p1,*p2; n = 0; p1 = p2 = (struct student *)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 = (struct student *)malloc(LEN); fflush(stdin); scanf(%d,%f,&p1-num,&p1-cj); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 7 页 - - - - - - - - - return(head); /* Description: 输出一个链表参数 : head为第一个节点的地址*/ void print(struct student *head) struct student *p; int i = 0; printf(nNow,These %d records are:n,n); p = head; if(head != NULL) / 如果链表不是空的,则进行数据输出 do printf(%dt,i+); printf(%5d %5.1fn,p-num,p-cj); / 输出链表数据p = p-Pnext; while(p != NULL); /* Description: 释放动态链表的地址空间参数 : 链表的头地址head,无返回值*/ voidfreelinkspace(struct student *head) struct student Ltemp; Ltemp.Pnext = head-Pnext; / Ltemp 用来存放 -next,避免空间被 / 释放后 ,找不到下一个结点的地址while(head-Pnext != NULL) / 判断是否已经空间释放到最后一个 free(head); head = Ltemp.Pnext; Ltemp.Pnext = head-Pnext; free(head); / 释放最后一个结点空间 /* Description: 删除链表链表中的一个结点参数 : head 为第一个节点的地址,num为要删除结点的序号(head-num)*/ struct student *del(struct student *head,intnum) struct student *p1,*p2; p1 = head; while(p1-num!=num& p1-Pnext!=NULL) / 寻找要删除的结点 p2 = p1; / p2 存放删除结点的前一个结点地址p1 = p1-Pnext; / p1 存放要删除的结点 if(num = p1-num) / 是否找到要删除结点 if(p1 = head) / 删除的是第一个结点名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 7 页 - - - - - - - - - head = p1-Pnext; else if(p1-Pnext = NULL) / 删除的是最后一个结点 p2-Pnext = NULL; else / 删除中间的结点 p2-Pnext = p1-Pnext; p1-Pnext = NULL; printf(delete: %dn,num); n = n-1; / 链表长度 - 1 else printf(%d not been found! n,num); delete(p1); return(head); /* Description: 添加一个结点到链表中参数 : head 为第一个结点的地址指针 stud 为要插入的结点的地址指针*/ struct student *insert(struct student *head,struct student *stud) struct student *p0,*p1,*p2; p0 = stud; p1 = head; while(p0-nump1-num& p1-Pnext!=NULL) / 找到添加结点的位置 p2 = p1; / p2 存放要添加的前一个结点的地址p1 = p1-Pnext; / p1 存放要添加的后一个结点的地址 if(p0-numnum& p1-Pnext!=NULL) if(p1 = head) / 添加结点到第一个位置 p0-Pnext = p1; head = p0; else p2-Pnext = p0; p0-Pnext = p1; else / 添加结点到最后一个位置 p1-Pnext = p0; p0-Pnext = NULL; n = n+1; / 结点数目 + 1 return(head); /* 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 7 页 - - - - - - - - - Description: 链表的重新排序 =按照 .num(不能重复)的大小从小到大排列链表数据参数 : head 接收链表第一个节点的地址指针,返回值为新生成链表的第一个节点的地址指针*/ struct student *linkpaixu(struct student *head) struct student *stemp,*ltemp,*shead,*head_h; struct student *p,*q; /* 申请两个链表指针,用来储存链表交换过程的中间值 */ head_h = head; ltemp = head; p = (struct student *) malloc(LEN); q = (struct student *) malloc(LEN); /* 为 p,q开辟动态存储空间 */ /* -= 先将链表的第一个数据与其他数据比较 =- */ while(head-Pnext != NULL) shead = head; head = head-Pnext; if(ltemp-num head-num) if(ltemp = shead) ltemp -Pnext = head -Pnext; head -Pnext = ltemp; else p-Pnext = head -Pnext; q-Pnext = ltemp -Pnext; head -Pnext = q-Pnext; shead -Pnext = ltemp; ltemp -Pnext = p-Pnext; head_h = head; head = ltemp; ltemp = head_h; /* 比较链表第一个以外的数据*/ while(ltemp -Pnext != NULL) stemp = ltemp; ltemp = ltemp -Pnext; head = ltemp; while(head-Pnext != NULL) shead = head; head = head-Pnext; if(ltemp-num head-num) if(ltemp = shead) p-Pnext = head -Pnext; stemp -Pnext = head; head -Pnext = ltemp; ltemp -Pnext = p-Pnext; else 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 7 页 - - - - - - - - - p-Pnext = head -Pnext; q-Pnext = ltemp -Pnext; stemp -Pnext = head; head -Pnext = q-Pnext; shead -Pnext = ltemp; ltemp -Pnext = p-Pnext; head = ltemp; ltemp = stemp -Pnext; free(p); free(q); / 释放 p,q 的动态存储空间return(head_h); void main() struct student *phead,*pins; / 定义 2 个链表指针intdelnum, selflog,flog_a = 1,Nf = 1; / 要删除的对象delnum char delflog ; / 删除标志 y/n charinsflog, flog_nx = n; char flog_free ; / 释放标志printf(please input %d numbers:n,N); phead = pa(N); / 创建一个动态链表,并赋值print(phead); / 输出链表数据intNx; / Nx 想要输入的链表长度printf(How long do you want establish? t); fflush(stdin); scanf(%d,&Nx); while(Nx = 0) /* -= 判断创建的是否是一个空链表 =- */ printf( 您要创建的是一个空链表,是否确定?y/n t); fflush(stdin); scanf(%c,&flog_nx); if(flog_nx = n) printf(How long do you want input?t); fflush(stdin); scanf(%d,&Nx); else if(flog_nx = y) gotoendl; else printf(wrong input!n); printf(How long do you want input?t); fflush(stdin); scanf(%d,&Nx); printf(please input %d numbers: ,Nx); printf( 如:1,3 两个数中间以 ,隔开 n); phead = pa(Nx); / 创建一个动态链表,并赋值print(phead); / 输出链表数据while(flog_a) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 7 页 - - - - - - - - - if(phead = NULL) printf(链表已空 ,无法操作 n); flog_a = 0; break; printf(n 操作n1:t 插入数据 n2:t 删除数据 n3:t 排序 n4:t 清屏n5:t 输出现在的链表数据n0:texitn); printf(nPlease input:t); fflush(stdin); if(scanf(%d,&selflog) / select flog 选择项switch(selflog) case 1 : printf(insert someone? y/nt); fflush(stdin); scanf(%c,&insflog); while(insflog != n) while(insflog != y&insflog != n) printf(wrnonginput,please input again. n); printf(another one? y/nt); fflush(stdin); scanf(%c,&insflog); printf(please input the date:n); pins = (struct student *)malloc(LEN); fflush(stdin); scanf(%d,%f,&pins-num,&pins-cj); phead = insert(phead,pins); print(phead); printf(another one? y/nt); fflush(stdin); scanf(%c,&insflog); while(insflog != y&insflog != n) printf(wrnonginput,please input again. n); printf(another one? y/nt); fflush(stdin); scanf(%c,&insflog); break; case 2 : printf(del someone? y/nt); fflush(stdin); scanf(%c,&delflog); while(delflog != n &phead != NULL) while(delflog != y&delflog != n) printf(wrnonginput,please input again. n); printf(del someone? y/nt); fflush(stdin); scanf(%c,&delflog); printf(please input the student what you want del:n); fflush(stdin); scanf(%d,&delnum); phead = del(phead,delnum); print(phead); printf(another one? y/nt); fflush(stdin); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 7 页 - - - - - - - - - scanf(%c,&delflog); if(n+1)=0) printf(There is no more num could be del!n); break; break; case 3 : printf(n 排序之后: ); phead = linkpaixu(phead); print(phead); / 排序该数据链break; case 4 : / clrscr(); system(cls); break; case 5 : print(phead); break; case 0 : flog_a = 0 ; break; /* 退出 */ default : printf(wrong inputnPlease input again); break; else printf( 非法输入 !n); endl: while(1) if(Nx = 0) printf(Cant establish the link!n); break; printf(n 保留数据? y/nt); / 是否释放地址空间fflush(stdin); scanf(%c,&flog_free); if(flog_free = y) break; else if(flog_free = n) freelinkspace(phead); break; else printf(wrong input!n); printf(OVER!nGOOD LUCK!n); 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 7 页,共 7 页 - - - - - - - - -