| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2100 人关注过本帖
标题:[求助]c程序的一个错误
只看楼主 加入收藏
远去的列车
Rank: 1
等 级:新手上路
威 望:2
帖 子:205
专家分:0
注 册:2007-8-7
收藏
得分:0 
int DelList(LinkList L,int i2,char e2)
{
Node *p,*r;
int k;
p=L;
k=0;
while(p->next!=NULL&&k<i2-1)
{
p=p->next;
k=k+1;
}
if(k!=i2-1)
{
printf("The position is wrong!\n");
return 0;
}
r=p->next;
p->next=p->next->next;
e2=r->data; //这句话好像没起什么作用,可无?
free(r);
return 1;
}

C++学习
2007-09-29 08:44
andself
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-9-23
收藏
得分:0 
以下是引用远去的列车在2007-9-29 8:41:12的发言:
L=InsList(L, int i , char e);
L=DelList(L, int i2 , char e2);

改成
L=InsList(L, i , e);
L=DelList(L, i2 , e2);

另外一点:L 与 函数返回值不对应

L 不对应? 那应该怎么改?


2007-09-29 13:35
andself
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-9-23
收藏
得分:0 

我已经改好了 谢谢大家帮忙 呵呵 这里把改好的发一下 大家运行下
关于链表 的 求长 插入 建立 打印 删除 查找
#include<stdio.h>
#include<malloc.h>
menu()
{
printf(" ____________________________ \n");
printf(" | * MENU * |\n");
printf(" | (1)Creat a new linklist. |\n");
printf(" | (2)Locate. |\n");
printf(" | (3)Get element. |\n");
printf(" | (4)The length of linklist. |\n");
printf(" | (5)Inside an element. |\n");
printf(" | (6)Delete an element. |\n");
printf(" | (7)EXIT. |\n");
printf(" |____________________________|\n");
printf("Please input the number of your choose.\n");
}

int choose()
{
int a;
scanf("%d",&a);
return(a);
}

typedef struct Node
{ char data;
struct Node *next;
}Node,*LinkList;


Node *CreatL()
{
LinkList L;
Node *r,*s;
int flag=1;
char c;
L=(Node *)malloc(sizeof(Node));
L->next=NULL;
r=L;
while(flag)
{
c=getchar();
if(c!='$')
{ s=(Node *)malloc(sizeof(Node));
s->data=c;
r->next=s;
r=s;
}
else
{ flag=0;
r->next=NULL;
}
}
return L;
}

Node *Locate(LinkList L)
{
Node *p;
int b=1;
char i;
scanf("%c",&i);
p=L->next;
while(p!=NULL)
if(p->data!=i)
{
p=p->next;
b++;
}
else break;
printf("This element's locate is %d\n",b);
}

Node *Get(LinkList L)
{
int j,i;
char a;
Node *p;
scanf("%d",&i);
p=L;
j=0;
while(p->next!=NULL&&j<i)
{
p=p->next;
j++;
}
if(i==j)
{
a=p->data;
printf("The i'th element is :%c\n",a);
}
else printf("The i is wrong.\n");
}

int Listlength(LinkList L)
{
Node *p;
int j;
p=L->next;
j=0;
while(p!=NULL)
{
p=p->next;
j++;
}
printf("This Linklist's length is %d\n",j);
}

int InsList(LinkList L,int i,char e)
{
Node *pre,*s;
int k;
pre=L;
k=0;
while(pre!=NULL&&k<i-1)
{
pre=pre->next;
k=k+1;
}
if(k!=i-1)
{
printf("The i is wrong!\n");
return 0;
}
s=(Node *)malloc(sizeof(Node));
s->data=e;
s->next=pre->next;
pre->next=s;
return 1;
}

int DelList(LinkList L,int i2)
{
Node *p,*r;
int k;
p=L;
k=0;
while(p->next!=NULL&&k<i2-1)
{
p=p->next;
k=k+1;
}
if(k!=i2-1)
{
printf("The position is wrong!\n");
return 0;
}
r=p->next;
p->next=p->next->next;
free(r);
return 1;
}


void Print_Node(LinkList L)
{
Node *p;
p=L->next;
while(p)
{
printf("%c--->",p->data);
p=p->next;
}
if(p==NULL)
{
printf("^\n\n\n");
}
}

void Free_Node(LinkList L)
{
Node *p;
p=L->next;
while(p)
{
L->next=p->next;
free(p);
p=L->next;
}
free(L);
}


main()
{
char c,e;
LinkList L;
int i,i2,x=1,y,*p,*h;
printf("Please creat a linklist.\n");
printf("Please input something and enter '$'to end.\n");
L=CreatL();
printf("LinkList has founded!\n");
h=L;
while(x)
{
menu();
y=choose();
if(y>0&&y<8)
{
if(y==1) { printf("please input something and enter '$' to end.\n");
L=CreatL();
printf("LinkList has founded!\n");
}
else
if(y==2) { printf("Please enter the element which you find.\n");
Locate(L);
}

else
if(y==3)
{ printf("Please enter the position of the element which you find.\n");
Get(L);
}
else
if(y==4) Listlength(L);
else
if(y==5)
{
printf("Please input the element e and the locate i.\n");
printf("i :");
scanf("%d",&i);
getchar();
printf("\n");
printf("e :");
scanf("%c",&e);
printf("\n");
L=InsList(L, i , e);
printf("The new LinkList is:\n");
L=h;
Print_Node(L);
}
else
if(y==6)
{
printf("Please input the locate i.\n");
printf("i="); scanf("%d",&i2);
printf("\n");
L=DelList(L, i2);
printf("The new LinkList is:\n");
L=h;
Print_Node(L);
}
else if(y==7) return(1);
printf("(1)Go on (0)EXIT\n");
scanf("%d",&x);
}
else printf("ERROR");
}
Free_Node(L);

}


2007-09-29 20:57
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
还没改好.

倚天照海花无数,流水高山心自知。
2007-09-29 21:17
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 

#include<stdio.h>
#include<malloc.h>

typedef struct Node
{ char data;
struct Node *next;
}Node,*LinkList;

void menu()
{
printf(" ____________________________ \n");
printf(" | * MENU * |\n");
printf(" | (1)Creat a new linklist. |\n");
printf(" | (2)Locate. |\n");
printf(" | (3)Get element. |\n");
printf(" | (4)The length of linklist. |\n");
printf(" | (5)Inside an element. |\n");
printf(" | (6)Delete an element. |\n");
printf(" | (7)EXIT. |\n");
printf(" |____________________________|\n");
printf("Please input the number of your choose.\n");
}

int choose()
{
int a;
scanf("%d",&a);
return(a);
}


Node *CreatL()
{
LinkList L;
Node *r,*s;
int flag=1;
char c;
L=(Node *)malloc(sizeof(Node));
L->next=NULL;
r=L;
while(flag)
{
c=getchar();
if(c!='$')
{ s=(Node *)malloc(sizeof(Node));
s->data=c;
r->next=s;
r=s;
}
else
{ flag=0;
r->next=NULL;
}
}
return L;
}

Node *Locate(LinkList L)
{
Node *p;
int b=1;
char i;
scanf("%c",&i);
p=L->next;
while(p!=NULL)
if(p->data!=i)
{
p=p->next;
b++;
}
else break;
printf("This element's locate is %d\n",b);
}

Node *Get(LinkList L)
{
int j,i;
char a;
Node *p;
scanf("%d",&i);
p=L;
j=0;
while(p->next!=NULL&&j<i)
{
p=p->next;
j++;
}
if(i==j)
{
a=p->data;
printf("The i'th element is :%c\n",a);
}
else printf("The i is wrong.\n");
}

int Listlength(LinkList L)
{
Node *p;
int j;
p=L->next;
j=0;
while(p!=NULL)
{
p=p->next;
j++;
}
printf("This Linklist's length is %d\n",j);
//你的返回值呢?
}

int InsList(LinkList L,int i,char e)
{
Node *pre,*s;
int k;
pre=L;
k=0;
while(pre!=NULL&&k<i-1)
{
pre=pre->next;
k=k+1;
}
if(k!=i-1)
{
printf("The i is wrong!\n");
return 0;
}
s=(Node *)malloc(sizeof(Node));
s->data=e;
s->next=pre->next;
pre->next=s;
return 1;
}

int DelList(LinkList L,int i2)
{
Node *p,*r;
int k;
p=L;
k=0;
while(p->next!=NULL&&k<i2-1)
{
p=p->next;
k=k+1;
}
if(k!=i2-1)
{
printf("The position is wrong!\n");
return 0;
}
r=p->next;
p->next=p->next->next;
free(r);
return 1;
}


void Print_Node(LinkList L)
{
Node *p;
p=L->next;
while(p)
{
printf("%c--->",p->data);
p=p->next;
}
if(p==NULL)
{
printf("^\n\n\n");
}
}

void Free_Node(LinkList L)
{
Node *p;
p=L->next;
while(p)
{
L->next=p->next;
free(p);
p=L->next;
}
free(L);
}


int main()
{
char c,e;
LinkList L;
int i,i2,x=1,y;
Node *p,*h;
printf("Please creat a linklist.\n");
printf("Please input something and enter '$'to end.\n");
L=CreatL();
printf("LinkList has founded!\n");
h=L;
while(x)
{
menu();
y=choose();
if(y>0&&y<8)
{
if(y==1) { printf("please input something and enter '$' to end.\n");
L=CreatL();
printf("LinkList has founded!\n");
}
else
if(y==2) { printf("Please enter the element which you find.\n");
Locate(L);
}

else
if(y==3)
{ printf("Please enter the position of the element which you find.\n");
Get(L);
}
else
if(y==4) Listlength(L);
else
if(y==5)
{
printf("Please input the element e and the locate i.\n");
printf("i :");
scanf("%d",&i);
getchar();
printf("\n");
printf("e :");
scanf("%c",&e);
printf("\n");
L=InsList(L, i , e); //函数是 什么类型
printf("The new LinkList is:\n");
L=h;
Print_Node(L);
}
else
if(y==6)
{
printf("Please input the locate i.\n");
printf("i="); scanf("%d",&i2);
printf("\n");
L=DelList(L, i2); //同上
printf("The new LinkList is:\n");
L=h;
Print_Node(L);
}
else if(y==7) return(1);
printf("(1)Go on (0)EXIT\n");
scanf("%d",&x);
}
else printf("ERROR");
}
Free_Node(L);
return 0;
}


倚天照海花无数,流水高山心自知。
2007-09-29 21:19
andself
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-9-23
收藏
得分:0 
需要 类型么?  只是 运行很正常啊  链表么?

2007-09-29 21:47
andself
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-9-23
收藏
得分:0 

int Listlength(LinkList L)
{
Node *p;
int j;
p=L->next;
j=0;
while(p!=NULL)
{
p=p->next;
j++;
}
printf("This Linklist's length is %d\n",j);
//你的返回值呢?
}

这个 不用返回值 啊 只是输出而已 是不是至少加个 return 1?


2007-09-29 21:51
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
没有返回值就把函数定义成void.

倚天照海花无数,流水高山心自知。
2007-09-29 21:52
andself
Rank: 1
等 级:新手上路
帖 子:60
专家分:0
注 册:2007-9-23
收藏
得分:0 
  知道了  谢谢  那 有什么 危害么?

2007-09-29 21:55
nuciewth
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:我爱龙龙
等 级:贵宾
威 望:104
帖 子:9786
专家分:208
注 册:2006-5-23
收藏
得分:0 
危害倒没有.

倚天照海花无数,流水高山心自知。
2007-09-29 21:56
快速回复:[求助]c程序的一个错误
数据加载中...
 
   



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

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