线性链表的建立与显示

线性链表的建立与显示

ID:33721819

大小:56.50 KB

页数:7页

时间:2019-02-28

线性链表的建立与显示_第1页
线性链表的建立与显示_第2页
线性链表的建立与显示_第3页
线性链表的建立与显示_第4页
线性链表的建立与显示_第5页
资源描述:

《线性链表的建立与显示》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库

1、实验一:线性链表的建立与显示#include#include#defineNULL0typedefstructLNode//线性表存储结构{intdata; structLNode*next;}LNode,*LinkList;voidCreate_LinkList(LinkList&L)//建立单链表并顺序输入{L=(LinkList)malloc(sizeof(LNode)); L->next=NULL; intj=0,i,n; printf("请输入将要输入的元素个数n:"); scanf("%d",&n); printf("请输入元素:

2、n"); LinkListq=L,p; for(i=1;i<=n;i++) {p=(LinkList)malloc(sizeof(LNode)); q->next=p; p->next=NULL; q=q->next; scanf("%d",&p->data); j++; }}voidPrintf_L(LinkList&L){LinkListp; p=L->next; while(p!=NULL) {printf("%d",p->data);  p=p->next; } }intmain(void){LinkListL; Create_LinkList(L); Printf_L(L);

3、  system("pause"); return0;}#include"stdio.h"#include"stdlib.h"#include"malloc.h"/*假设输入的数据为3个*/#definesize3typedefstructList{intLoc;//位置,这里不以head开始,head->next才表示0;intData;//存储的数据structList*next;}list;/*头结点*/list*setup_list(list*head){printf("setup_list");head=malloc(sizeof(list));if(head==NULL

4、){printf("setupfail");getchar();exit(0);}head->Loc=-1;head->Data=0;head->next=NULL;printf("setupsuccess");returnhead;}/*输入数据,这里的head并不存储实际的数据。所以长度不算他。*/voidinput(list*head,intLoc){list*q=NULL;list*s=NULL;s=malloc(sizeof(list));s->next=NULL;printf("inputthe%ddata:",Loc);scanf("%d",&s->Data);s

5、->Loc=Loc;q=head;while(q->next!=NULL){q=q->next;}q->next=s;}/*浏览,显示出所有数据*/voidbrowse(list*head){list*q=NULL;q=head->next;while(q!=NULL){printf("Loc:%d",q->Loc);printf("Data:%d",q->Data);q=q->next;}}/*插入num值有主函数控制*/voidinsert_list(list*head,intLoc){list*q=NULL;list*p=NULL;list*s=NULL;q=head;p=q

6、->next;while(p!=NULL&&p->Loc!=Loc){q=p;p=q->next;}s=malloc(sizeof(list));printf("inputData");scanf("%d",&s->Data);s->Loc=Loc;q->next=s;s->next=p;/*整理num后面的数后推*/while(p!=NULL){Loc++;p->Loc=Loc;p=p->next;}}/*删除指定Loc位置上的数据*/voiddelete_list(list*head,intLoc){list*q=NULL;list*p=NULL;q=head;p=q->next;

7、while(p!=NULL&&p->Loc!=Loc){q=p;p=q->next;}q->next=p->next;free(p);printf("Loc%d",Loc);p=q->next;while(p!=NULL){p->Loc=Loc;Loc++;p=p->next;}}/*查找元素并返回位置*/list*find_list(list*head,intdata){list*q=NULL;list*p=NULL;q=head;p=q

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。