| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2672 人关注过本帖
标题:[求助]单链表的插入与删除?
只看楼主 加入收藏
csz88999
Rank: 1
来 自:云南
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-6-1
收藏
 问题点数:0 回复次数:11 
[求助]单链表的插入与删除?
下面这个程序是我在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
shlg1229
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2007-9-24
收藏
得分:0 
LZ你的程序风格不是怎么好啊。看起来很不舒服

个人意见,不代表官方看法
2007-10-18 11:12
shlg1229
Rank: 1
等 级:新手上路
帖 子:107
专家分:0
注 册:2007-9-24
收藏
得分:0 

我编译了一下。LZ的程序不只这一个错误。我建议你先把一些简单的语法错误改了在发上来让大家看


个人意见,不代表官方看法
2007-10-18 11:30
xiaofkg
Rank: 1
等 级:新手上路
威 望:1
帖 子:77
专家分:0
注 册:2007-9-17
收藏
得分:0 

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define NUM 10
typedef struct{
int *elem;
int length ;
int listsize;
}sqlist;
initlist(sqlist *L)
{L->elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L->elem) exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
creat (sqlist *L)
{int i;
for(i=0;i<NUM;i++)
{scanf("%d",&L->elem[i]);
L->length++;
}


}
print(sqlist *L)
{int i;
for(i=0;i<L->length;i++)
printf("%d ",L->elem[i]);
printf("\n");
}
insert(sqlist *L,int i,int e)
{int *newbase;
int *q,*p;
if(i<1||i>L->length+1) return ERROR;
if(L->length>L->listsize) {
newbase=(int *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(int));
if(!newbase) exit (OVERFLOW);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
q=&(L->elem[i-1]);
p=&L->elem[L->length-1];
for(;p>q||(p==q);--p) *(p+1)=*p;
*q=e;
++L->length;
return OK;
}
main()
{ sqlist L1;
int i,e;
initlist ( &L1);
printf("input 10 integer:\n");
creat( &L1);
printf("input the position i and input the number e:\n");

scanf("%d%d",&i,&e ) ;

printf("before insert list:\n");
print( &L1);
insert( &L1, i, e);
printf("after insert list:\n");
print( &L1);
getch();

}

2007-10-18 22:08
xiaofkg
Rank: 1
等 级:新手上路
威 望:1
帖 子:77
专家分:0
注 册:2007-9-17
收藏
得分:0 

这是删除表
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
#define OK 1
#define ERROR 0
#define OVERFLOW -2


typedef struct{
int *elem;
int length ;
int listsize;
}sqlist;
initlist(sqlist *L,int num)
{L->elem=(int *)malloc(num*sizeof(int));
if(!L->elem) exit(OVERFLOW);
L->length=0;
L->listsize=LIST_INIT_SIZE;
return OK;
}
creat (sqlist *L,int num)
{int i;
for(i=0;i<num;i++)
{scanf("%d",&L->elem[i]);
L->length++;
}


}
print(sqlist *L)
{int i;
for(i=0;i<L->length;i++)
printf("%d ",L->elem[i]);
printf("\n");
}
/*insert(sqlist *L,int i,int e)
{int *newbase;
int *q,*p;
if(i<1||i>L->length+1) return ERROR;
if(L->length>L->listsize) {
newbase=(int *)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(int));
if(!newbase) exit (OVERFLOW);
L->elem=newbase;
L->listsize+=LISTINCREMENT;
}
q=&(L->elem[i-1]);
p=&L->elem[L->length-1];
for(;p>q||(p==q);--p) *(p+1)=*p;
*q=e;
++L->length;
return OK;
} */
listdelete(sqlist *L,int i,int *e){
int *p,*q;
if((i<1)||(i>L->length))return ERROR;
p=&(L->elem[i-1]);
e=p ;
q=L->elem+L->length-1;
for(++p;p<=q;++p)*(p-1)=*p;
--L->length;
return OK;
}

main()
{ sqlist L1;
int i,*e;
int num;
printf("please input the number of the list :\n");
scanf("%d",&num);

initlist( &L1,num);
printf("please input the conent of the list:\n");
creat( &L1,num);
/*printf("input the position i and input the number e:\n"); */
printf("input the delete position i :\n");
scanf("%d",&i);

/*scanf("%d %d\n",&i,&e ) ; */

printf("before insert list:\n");
print( &L1);
listdelete(&L1,i,e);

/*insert( &L1, i, e);
printf("after insert list:\n"); */
printf("after delete list:\n");
print( &L1);
getch();

}

2007-10-18 22:11
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
>小北<
该用户已被删除
收藏
得分:0 
提示: 作者被禁止或删除 内容自动屏蔽
2007-10-26 13:21
csz88999
Rank: 1
来 自:云南
等 级:新手上路
帖 子:45
专家分:0
注 册:2007-6-1
收藏
得分:0 

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


2007-10-30 08:16
zhangyg_cn
Rank: 1
等 级:新手上路
帖 子:50
专家分:0
注 册:2007-10-22
收藏
得分:0 

单链表的插入与删除是不。我编好了一个,在2005下运行成功
这个是插入
#include<stdio.h>
#include<stdlib.h>
struct List
{
int Number;
struct List *Next;
};
typedef struct List Node;
typedef Node* Link;
int Data[10]={9,8,7,6,5,4,3,2,1};
Link Create_List(Link Head)
{
Link Pointer;
Link New;

Head=(Link)malloc(sizeof(Node));
if(Head==NULL)
{
printf("Memory allocate Failure!!!");
}
else
{
Head->Number = Data[0];
Head->Next = NULL;

Pointer = Head;

for(int i=1;i<10;i++)
{
New = (Link)malloc(sizeof(Node));
New->Number = Data[i];
New->Next = NULL;

Pointer->Next = New;
Pointer = New;
}
}
return Head;

}
void Free_List(Link Head)
{
Link Pointer;
while(Head!=NULL)
{
Pointer = Head;
Head = Head->Next;
free(Pointer);
}
}
void Print_List(Link Head)
{
Link Pointer;
Pointer = Head;
while(Pointer!=NULL)
{

printf("Number is [%d]\n",Pointer->Number);
Pointer = Pointer->Next;
}
}
Link Insert_List(Link Head,Link New,int Insert_Number)
{
Link Pointer;
Pointer = Head;

while(1)
{
if(Pointer->Number == Insert_Number)
{
New->Next = Pointer->Next;
Pointer->Next = New;
break;
}
Pointer = Pointer->Next;

if(Pointer==NULL)
{
New->Next = Head;
Head = New;
break;
}
}
return Head;
}
Link Search_List(Link Head,int Search_Number)
{
Link Pointer;
Pointer = Head;

while(1)
{
if(Pointer->Number == Search_Number )
{
printf("Search Number is [%d]\n",Pointer->Number);
break;
}
Pointer = Pointer->Next;
if(Pointer->Next==NULL)
{
printf("Not Found!!!");
break;
}
}
return Head;

}
int main()
{
Link Head=NULL;
Link New;
int Insert_Number;
Head = Create_List(Head);
if(Head!=NULL)
{
Print_List(Head);
// Free_List(Head);
}
printf("Please input Insert Number : ");
scanf("%d",&Insert_Number);
New = (Link)malloc(sizeof(Node));
scanf("%d",&New->Number);
// Search_List(Head,Search);
Head=Insert_List(Head,New,Insert_Number);
Print_List(Head);
return 0;
}
下来是删除
#include<stdlib.h>
#include<stdio.h>
struct List
{
int Number;
struct List *Next;
};
typedef struct List Node;
typedef Node* Link;
int Data[10] = {9,8,7,6,5,4,3,2,1,0};
void Free_List(Link Head)
{
Link Pointer;
while(Head!=NULL)
{
Pointer = Head;
Head = Head->Next;
free(Pointer);
}
}
Link Print_List(Link Head)
{

Link Pointer;
Pointer = Head;

if((Head->Number >-32762)&&(Head ->Number <32768) )
{
while(Pointer!=NULL)
{
printf("Number is [%d]\n",Pointer->Number);
Pointer= Pointer->Next;
}
}
else
return NULL;
}
Link Delete_List(Link Head,int Delete_Number)
{
Link Pointer;
Link Back;
Pointer = Head;

while(1)
{
if(Head->Number == Delete_Number )
{
Head = Pointer->Next;
free(Pointer);
break;
}
Back = Pointer;
Pointer = Pointer->Next;
if(Pointer->Number == Delete_Number)
{
Back->Next = Pointer->Next;
break;
}
if(Pointer == NULL)
{
printf("没有找到该节点\n");
break;
}

}
return Head;
}
Link Create_List(Link Head)
{
Link Pointer;
Link New;

Head = (Link)malloc(sizeof(Node));
if(Head == NULL)
{
printf("Memory allcoate Failure!!!\n");
}
else
{
Head->Number = Data[0];
Head->Next = NULL;

Pointer = Head;
for(int i=1 ;i<10; i++)
{
New = (Link)malloc(sizeof(Node));
New->Number= Data[i];
New->Next = NULL;

Pointer->Next = New;
Pointer= New;
}
}
return Head;
}
int main()
{
int Del_Number;
Link Head;
Head=Create_List(Head);
if(Head!=NULL)
{
Print_List(Head);
// Free_List(Head);
}
while(1)
{
printf("请输入您所要删除的节点:\n");
scanf("%d",&Del_Number);
if(Del_Number>=32767)
break;
Head = Delete_List(Head,Del_Number);
if(Head!=NULL)
Head=Print_List(Head);
else
break;
}
printf("链表已经空\n");
return 0;
}


删蒢┈记亿﹎ 镏下啲х`祗惿噎份 ┊ ☆ 曾经啲◇ˋ美恏徊亿`ヽ
2007-10-30 17:14
快速回复:[求助]单链表的插入与删除?
数据加载中...
 
   



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

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