| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2670 人关注过本帖
标题:[求助]单链表的插入与删除?
取消只看楼主 加入收藏
csz88999
Rank: 1
来 自:云南
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-6-1
收藏
 问题点数:0 回复次数:3 
[求助]单链表的插入与删除?
下面这个程序是我在TC2.0下编辑的,但是它有个错误,我不知道应该怎么改!麻烦大家帮我看一下!
#include<stdio.h>
#define NULL 0
typedef int elemtype;
typedef struct node
{elemtype data;
struct node *next;
}slink;
int n=1;
int m;
slink *creat(void)
{
int i,m;
slink *head,*p,*sq;
sq=(slink*)malloc(sizeof(slink));
sq->next=NULL;
p=sq->next;
printf(“please input the slink longs:\n”);
scanf(“%d”,&m);
if(m==0)
printf(“please input again:\n”);
for(i=1;i<=m;i++)
{
scanf(“%f”,&p->data);
p=p->next;
}
}
Void displist(slink *sq)
{
slink *p=sq->next;
while(p!=null)
Printf(“p->data “);
)
Int Inselem(slink*sq,elemtype x,int i)
{
int j=1;
printf(“please input the locate you want inset\n”)
scanf(“%d”,&i);
printf("please input the number you want inset:\n");
scanf("%f",&x);
slink *p=sq,*s;
s=(slink *)malloc(sizeof(slink));
s->data=x;
s->next=NULL;
if(i<1 || i>m+1)
printf(“your input is wrong!")
return 0;
while(j<i)
{p=p->next;j++;}
s->next=p->next;
p->next=s;
return 1;
}
int Delelem(slink *sq,int i)
{
int j =1;
slink *p=sq,*q;
printf("please input the locate you want delelate:\n");
scanf ("%d",&i);
if(i<1 || i>m)
printf("your enter is wrong!");
return 0;
while (j<i)
{p=p->next;j++;}
q=p->next;
p->next=q->next;
free(q);
return 1;
}
mian()
{
int j ,i;
slink *q,*p;
p=q=(slink*)malloc(sizlf(slink));
q=creat();
Displist(q);
q=insert(q,p);
Displist(q);
Delelem(q,j);
Displist(q);
getch();
}
下划线红色的字是它出现有错误的行.显示的错误是{ Error C:\TC\NONAME.C 27: Declaration syntax error}

搜索更多相关主题的帖子: 单链 slink int next node 
2007-10-18 10:49
csz88999
Rank: 1
来 自:云南
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-6-1
收藏
得分:0 
哈哈,\是呀,我的那个错误太多了,我简直无法相信呀,郁闷
谢谢楼上的了,不过我在TC上运行,上面的程序还是有点不对!

2007-10-25 08:28
csz88999
Rank: 1
来 自:云南
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-6-1
收藏
得分:0 

我上面那个直接不用了,这是我另编的一个,不过它还是有错误!再麻烦大家看一下!
#include<stdio.h>
#include<malloc.h>
typedef int elemtype;
typedef struct node
{elemtype data;
struct node *next;
}slink;
int n;
slink *creatlist(int n)
{
int x,k;
slink *head,*p,*r;
p=(slink*)malloc(sizeof(slink));
p->next=NULL;
r=p;
if(n==0)
printf("please input again:\n");
for(k=1;k<=n;k++)
{
Printf("input value:\n");
scanf("%d",&x);
p=(slink*)malloc(sizeof(slink));
p->data=x;
p->next=NULL;
r->next=p;
r=r->next;
}
}
int insert(slink*sq,elemtype x,int i)
{
int j=1;
slink *p=sq,*s;
s=(slink *)malloc(sizeof(slink));
s->data=x;
s->next=NULL;
if(i<1 || i>n+1)
printf("your input is wrong!");
while(j<i)
{p=p->next;j++;}
s->next=p->next;
p->next=s;
n++;
return 1;
}

int Delelem(slink *sq,int i)
{
int j=1;
slink *p=sq,*q;
if(i<1 || i>n)
printf("your enter is wrong!");
return 0;
while (j<i)
{p=p->next;j++;}
q=p->next;
p->next=q->next;
free(q);
n--;
return 1;
}
mian()
{
slink *head,*p;
int n,m,j,x;
printf("input the length of list:\n");
scanf("%d",&n);
head=creatlist(n);
printf("output the list:\n");
p=head->next;
while(p)
{
printf("%d",p->data );
p=p->next;
}
printf("input insert locate:\n");
Scanf("%d",&m);
printf("\ninput the inset data:\n");
scanf("%d",&x);
insert(head,x,m);
printf("output the list:\n");
p=head->next;
while(p)
{
printf("%d",p->data );
p=p->next;
}
Printf("input delet locate:\n");
Scanf("%d", &j);
Delelem(head,j);
printf("output the list:\n");
p=head->next;
while(p)
{
printf("%d",p->data );
p=p->next;
}
getch();
}

它显示的错误是Unreachable code in function Delelem
红色的那行是它显示错误的行,但也可能不是那里错误!


2007-10-25 08:49
csz88999
Rank: 1
来 自:云南
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-6-1
收藏
得分:0 

哦,谢谢了,我会继续努力的!


2007-10-30 08:16
快速回复:[求助]单链表的插入与删除?
数据加载中...
 
   



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

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