欢迎来到天天文库
浏览记录
ID:33721819
大小:56.50 KB
页数:7页
时间:2019-02-28
《线性链表的建立与显示》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
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
此文档下载收益归作者所有