| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 441 人关注过本帖
标题:[求助]链表
只看楼主 加入收藏
donald
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2006-11-13
收藏
 问题点数:0 回复次数:2 
[求助]链表

我弄了很久才把报错调为零。
可是运行没反应阿。
麻烦各位帮忙看一下。
辛苦辛苦
#include <stdio.h>
#include <malloc.h>
struct intNode{
int value;
struct intNode *next;
};
int menu()
{
int choice;
do{
printf("\n\n\n");
printf("(1)创建一个新链表");
printf("(2)向某链表插入一个新元\n");
printf("(3)从某链表删除一个表元\n");
printf("(4)输出某链表\n");
printf("(5)将某链表排序,变成按表元值排序的有序链表\n");
printf("(6)将某两个有序链表合并成一个链表\n");
printf("(7)按某链表复制出一个新表\n");
printf("(8)结束程序\n");
printf("输入你的选择\n");
scanf("%d",&choice);
if(choice>=1&&choice<=8)
return choice;
printf("选择时出错!重新选择\n");
}while(1);
}
struct intNode *searchDOLink(struct intNode *h,int key,struct intNode **pp)
{
struct intNode *v=h,*u=NULL;
while((v!=NULL)&&(v->value!=key)){
u=v;
v=v->next;
}
*pp=u;
return v;
}
struct intNode *creat()
{
struct intNode *head=NULL,*tail=NULL,*p;
int n;
printf("input data\n");
while(scanf("%d",&n)>0){
p=(struct intNode *)malloc(sizeof(struct intNode));
p->value=n;
p->next=NULL;
if(head==NULL)head=tail=p;
else tail=tail->next=p;
}
return head;
}
int rInserte(struct intNode **hpt,int key)
{
struct intNode *u,*w,*p;
if((u=searchDOLink(*hpt,key,&w))==NULL||u->value!=key){
p=(struct intNode *)malloc(sizeof(struct intNode));
p->value=key;
p->next=u;
if(w==NULL) *hpt=p;
else w->next=p;
return 1;
}
return 0;
}
struct intNode *sDelete(struct intNode**hpt,int key)
{
struct intNode *u,*w;
u=*hpt;
while((u!=NULL)&&(u->value!=key)){
w=u;u=u->next;
}
if(u!=NULL){
if(u==*hpt)
*hpt=u->next;
else w->next=u->next;
u->next=NULL;
}
return u;
}
void output(struct intNode *h)
{
struct intNode *p=h;
while(p!=NULL){
printf("%d\t",p->value);
p=p->next;
}
printf("\n");
}
void rank(struct intNode **hpt)
{
struct intNode *p,*u,*t;
p=*hpt;
u=p->next;
if(p->value>u->value){
t=p->next;
p->next=u->next;
p=t;
u->next=p;
}
}
struct intNode *connect(struct intNode *h1,struct intNode *h2)
{
struct intNode *ne,*t,*p;
ne=t=NULL;
while(h1||h2){
p=(struct intNode *)malloc(sizeof(struct intNode));
if(h2==NULL||h1!=NULL&&h1->value<h2->value){
p->value=h1->value;h1=h1->next;
}
else {p->value=h2->value;h2=h2->next;
}
p->next=NULL;
if(t==NULL) ne=t=p;
else t=t->next=p;
}
return ne;
}
struct intNode *copy(struct intNode *hpt)
{
struct intNode *q,*head=NULL,*tail=NULL;
while(hpt!=NULL){
q=(struct intNode*)malloc(sizeof(struct intNode));
q->value=hpt->value;
q->next=NULL;
if(tail==NULL)head=tail=q;
else tail=tail->next=q;
hpt=hpt->next;
}
return head;
}
void main()
{
int choice;
int i=1;
struct intNode *h1=NULL,*t1=NULL,*h2=NULL,*t2=NULL,*h3=NULL,*t3=NULL,*p;
p=(struct intNode*)malloc(sizeof(struct intNode));
p->value=9;
p->next=NULL;
h1=t1=p;
p=(struct intNode*)malloc(sizeof(struct intNode));
p->value=2;
p->next=NULL;
t1=t1->next=p;
p=(struct intNode*)malloc(sizeof(struct intNode));
p->value=5;
p->next=NULL;
t1=t1->next=p;
while(i<6){
p=(struct intNode*)malloc(sizeof(struct intNode));
p->value=2*i-1;
p->next=NULL;
if(h2==NULL)h2=t2=p;
else t2=t2->next=p;
}
i=1;
while(i<6){
p=(struct intNode*)malloc(sizeof(struct intNode));
p->value=2*i;
p->next=NULL;
if(h3==NULL)h3=t3=p;
else t3=t3->next=p;
}
while(1){
choice=menu();
switch(choice){
case 1: output(creat());break;
case 2: rInserte(&h1,4);output(h1);break;
case 3: sDelete(&h1,3);break;
case 4: output(h1);break;
case 5: rank(&h1);output(h1);break;
case 6: output(connect(h2,h3));break;
case 7: output(copy(h1));break;
case 8:return;
}
}
}



搜索更多相关主题的帖子: 链表 
2006-12-13 15:11
abcBoy
Rank: 1
等 级:新手上路
帖 子:57
专家分:0
注 册:2006-12-13
收藏
得分:0 
[CODE]while(i<6){
p=(struct intNode*)malloc(sizeof(struct intNode));
p->value=2*i-1;
p->next=NULL;
if(h2==NULL)h2=t2=p;
else t2=t2->next=p;
}
i=1;
while(i<6){
p=(struct intNode*)malloc(sizeof(struct intNode));
p->value=2*i;
p->next=NULL;
if(h3==NULL)h3=t3=p;
else t3=t3->next=p;
}[/CODE]
i你加1了吗?
2006-12-13 15:18
donald
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2006-11-13
收藏
得分:0 
sorry!!
2006-12-13 18:07
快速回复:[求助]链表
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013928 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved