c程序结点基本操作.doc_第1页
c程序结点基本操作.doc_第2页
c程序结点基本操作.doc_第3页
c程序结点基本操作.doc_第4页
c程序结点基本操作.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#include #define NULL 0typedef int ElemType;typedef struct LNode ElemType data; struct LNode *next;LNode,*LinkList;LinkList La,La_address,Lb,Lb_address,Lc_address;void init_node(LinkList L) L=(LinkList)malloc(sizeof(LNode); L-data=0; L-next=NULL; return;LinkList create_node_list_from_tail_to_head(LinkList L,int n) LinkList p; int i; L=(LinkList)malloc(sizeof(LNode); L-next=NULL; printf(input node La elements from tail to head:n); for(i=n;i0;-i) p=(LinkList)malloc(sizeof(LNode); scanf(%d,&p-data); p-next=L-next;L-next=p; return L;ElemType get_node_data(LinkList L,int i) LinkList p; int j; ElemType e; p=L-next; j=1; while(p&jnext;+j; if(!p|ji) printf(get data errorn); return 0; e=p-data; return e;void insert_node(LinkList L,int i,ElemType e) LinkList p,s; int j=0; p=L; while(p&jnext;+j; if(!p|ji-1) printf(insert errorn); return; s=(LinkList)malloc(sizeof(LNode); s-data=e;s-next=p-next; p-next=s; return;void delete_node(LinkList L,int i) LinkList p=L,q; int j=0; while(p-next&jnext;+j; if(!(p-next)&ji+1) printf(delete errorn);return; q=p-next;p-next=q-next; free(q); return;LinkList merge_node_list(LinkList La,LinkList Lb) LinkList pa,pb,pc,Lc; pa=La-next;pb=Lb-next;Lc=pc=NULL; while(pa&pb) if(pa-datadata) pc-next=pa;pc=pa;pa=pa-next; else pc-next=pb;pc=pb;pb=pb-next; pc-next=pa?pa:pb; free(Lb); return Lc;int get_list_node_number(LinkList L) int i=0; LinkList p=L; while(p!=0) p=p-next; i+; return i-1;void bubble_sort_node_list(LinkList L) int i,j,num,num_1,temp; num_1=num=get_list_node_number(L); for(j=1;jnum;j+) for(i=1;i(temp=get_node_data(L,i+1) delete_node(L,i+1); insert_node(L,i,temp); return;void show_node_list(LinkList L) LinkList p; ElemType e; p=L-next; printf(nnt006: L-); while(p!=NULL) e=p-data; printf(%d-,e); p=p-next; printf(NULL :006nnn); return;int main(void) int i,m,n,val,temp,num; char c,ch; clrscr(); printf(here have some opperations about node list:nn); while(10) printf( 1-create node list La from tail to head.n 2-create node list Lb from tail to head.n 3-get node position_i data.n 4-insert node before position_i.n 5-delete node position_i.n 6-merge node list La and Lb,and elements from little to big.n 7-show node list.n 8-exit.n); c=getchar(); switch(c) case 1:printf(input node list La element amounts:n); scanf(%d,&m); La_address=create_node_list_from_tail_to_head(La,m); printf(create over.n);getchar();clrscr();break; case 2:printf(input node list Lb element amounts:n); scanf(%d,&n); Lb_address=create_node_list_from_tail_to_head(Lb,n); printf(create over.n);getchar();clrscr();break; case 3:printf(witch node list do you want to operate:n1-Lat2-Lbn); getchar();ch=getchar(); switch(ch) case 1:printf(tell me the node position:n); scanf(%d,&i); temp=get_node_data(La_address,i); printf(the data you want is %dn,temp); system(pause);clrscr();break; case 2:printf(tell me the node position:n); scanf(%d,&i); temp=get_node_data(Lb_address,i); printf(the data you want is %dn,temp); system(pause);clrscr();break; default: printf(error input007n); system(pause);clrscr();break; break; case 4:printf(witch node list do you want to operate:n1-Lat2-Lbn); getchar();ch=getchar(); switch(ch) case 1:printf(tell me the node position before you want to insert:n); scanf(%d,&i); printf(tell me the data you want to insert:n); scanf(%d,&temp); insert_node(La_address,i,temp); printf(insert over.n); system(pause);clrscr();break; case 2:printf(tell me the node position before you want to insert:n); scanf(%d,&i); printf(tell me the data you want to insert:n); scanf(%d,&temp); insert_node(Lb_address,i,temp); printf(insert over.n); system(pause);clrscr();break; default: printf(insert error007n); system(pause);clrscr();break; break; case 5:printf(witch node list do you want to operate:n1-Lat2-Lbn); getchar();ch=getchar(); switch(ch) case 1:printf(tell me the node position:n); scanf(%d,&i); delete_node(La_address,i); printf(delete over.n); system(pause);clrscr();break; case 2:printf(tell me the node position:n); scanf(%d,&i); delete_node(Lb_address,i); printf(delete over.n); system(pause);clrscr();break; default: printf(delete error007n); system(pause);clrscr();break; break; case 6:printf(it will merge La,Lb ignore its elements order:n); bubble_sort_node_list(La_address); bubble_sort_node_list(Lb_address); Lc_address=merge_node_list(La_address,Lb_address); printf(the merged node list is Lc,but the list La and Lb will point everywhere!n); system(pause);clrscr();break; case 7:printf(witch node list do you want to show:n1-Lat2-Lbt3-Lcn); getchar();ch=getchar(); switch(ch) case 1:show_node_list(La_address);system(pause);clrscr();break; case 2:show_node_list(Lb_address);system(pause);clrscr();break;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论