资源描述:
《数据结构C语言版 单链表源代码.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、/*单链表的各种操作*/#include#include#definenull0typedefintElemType;/*字符型数据*/structLNode{ElemTypedata;structLNode*next;};voidsetnull(structLNode**p);intlength(structLNode**p);ElemTypeget(structLNode**p,inti);voidinsert(structLNode**p,ElemTypex,inti);voiddele(st
2、ructLNode**p,inti);voiddisplay(structLNode**p);intlocate(structLNode**p,ElemTypex);voidmain(){structLNode*head,*q;/*定义静态变量*/intselect,x1,x2,x3,x4;inti,n;intm,g;chare,y;setnull(&head);/*建设链表并设置为空表*/printf("请输入数据长度:");scanf("%d",&n);for(i=1;i<=n;i++){printf("将数据插入到单链表中:");
3、scanf("%d",&y);insert(&head,y,i);}/*插入数据到链表*/display(&head);/*显示链表所有数据*/printf("select1求长度length()");printf("select2取结点get()");printf("select3求值查找locate()");printf("select4删除结点delete()");printf("select0退出");printf("inputyourselect:");scanf("%d",&select);while(s
4、elect!=0){switch(select){case1:{x1=length(&head);printf("输出单链表的长度%d",x1);display(&head);}break;case2:{printf("请输入要取得结点:");scanf("%d",&m);x2=get(&head,m);printf("%d",x2);display(&head);}break;case3:{printf("请输入要查找的数据:");scanf("%d",&e);x3=locate(&head,e);printf("%d",x3);di
5、splay(&head);}break;case4:{printf("请输入要删除的结点:");scanf("%d",&g);dele(&head,g);display(&head);}break;}printf("select1求长度length()");printf("select2取结点get()");printf("select3求值查找locate()");printf("select4删除结点delete()");printf("select0退出");printf("inputyourselect:")
6、;scanf("%d",&select);}}voidsetnull(structLNode**p){*p=null;}intlength(structLNode**p){intn=0;structLNode*q=*p;while(q!=null){n++;q=q->next;}return(n);}ElemTypeget(structLNode**p,inti){intj=1;structLNode*q=*p;while(jnext;j++;}if(q!=null)return(q->data);e
7、lse{printf("位置参数不正确!");return0;}}intlocate(structLNode**p,ElemTypex){intn=0;structLNode*q=*p;while(q!=null&&q->data!=x){q=q->next;n++;}if(q==null)return(-1);elsereturn(n+1);}voidinsert(structLNode**p,ElemTypex,inti){intj=1;structLNode*s,*q;s=(structLNode*)malloc(sizeof
8、(structLNode));s->data=x;q=*p;if(i==1){s->next=q;*p=s;}else{while(jnext!=null){q=q->next;j++;}