为什么在单链表删除操作中int delete(slnode *p,int x);会报错
#include<stdio.h>#definenull0#include<malloc.h>typedefstructnode{intdata;structnode*n...
#include<stdio.h>
#define null 0
#include<malloc.h>
typedef struct node{
int data;
struct node *next;
}
slnode;
void *initiate(slnode **h){
*h=(slnode *)malloc(sizeof(slnode));
(*h)->next=null;
}
slnode append(slnode *p,int x){
slnode *s;
s=(slnode *)malloc(sizeof(slnode));
s->data=x;
s->next=p->next;
p->next=s;
}
void travel(slnode *p){
slnode *s;
s=p->next;
while(s!=null)
{
putchar(s->data);
s=s->next;
}
putchar('\n');
}
int del(slnode *p,int x){
slnode *s,*q;
q=p;
s=p->next;
while(s!=null)
if(s->data!=x){
q=s;
s=s->next;}
else{
q->next=s->next;
free(s);
return(0);
}
printf("%c:not found.\n",x);
return(-1);
}
int main(){
int i,ch1,ch2,n;
slnode *q,*p;
initiate(&p);
for(i=0;i<4;i++)
{printf("ch%d=",i);
ch1=getchar();
getchar();
append(p,ch1);
printf("point ch%d=%c\n",i,ch1);
}
travel(p);
printf("ch1=");
ch1=getchar();
getchar();
del(p,ch1);
travel(p);} 展开
#define null 0
#include<malloc.h>
typedef struct node{
int data;
struct node *next;
}
slnode;
void *initiate(slnode **h){
*h=(slnode *)malloc(sizeof(slnode));
(*h)->next=null;
}
slnode append(slnode *p,int x){
slnode *s;
s=(slnode *)malloc(sizeof(slnode));
s->data=x;
s->next=p->next;
p->next=s;
}
void travel(slnode *p){
slnode *s;
s=p->next;
while(s!=null)
{
putchar(s->data);
s=s->next;
}
putchar('\n');
}
int del(slnode *p,int x){
slnode *s,*q;
q=p;
s=p->next;
while(s!=null)
if(s->data!=x){
q=s;
s=s->next;}
else{
q->next=s->next;
free(s);
return(0);
}
printf("%c:not found.\n",x);
return(-1);
}
int main(){
int i,ch1,ch2,n;
slnode *q,*p;
initiate(&p);
for(i=0;i<4;i++)
{printf("ch%d=",i);
ch1=getchar();
getchar();
append(p,ch1);
printf("point ch%d=%c\n",i,ch1);
}
travel(p);
printf("ch1=");
ch1=getchar();
getchar();
del(p,ch1);
travel(p);} 展开
2个回答
展开全部
#include<stdio.h>
#define null 0
#include<malloc.h>
typedef struct node{
int data;
struct node *next;
}
slnode;
void *initiate(slnode **h){
*h=(slnode *)malloc(sizeof(slnode));
(*h)->next=null;
return 0;
}
slnode append(slnode *p,int x){
slnode *s;
s=(slnode *)malloc(sizeof(slnode));
s->data=x;
s->next=p->next;
p->next=s;
return *s;
}
void travel(slnode *p){
slnode *s;
s=p->next;
while(s!=null)
{
putchar(s->data);
s=s->next;
}
putchar('\n');
}
int del(slnode *p,int x){
slnode *s,*q;
q=p;
s=p->next;
while(s!=null)
if(s->data!=x){
q=s;
s=s->next;}
else{
q->next=s->next;
free(s);
return(0);
}
printf("%c:not found.\n",x);
return(-1);
}
int main(){
int i,ch1,ch2,n;
slnode *q,*p;
initiate(&p);
for(i=0;i<4;i++)
{printf("ch%d=",i);
ch1=getchar();
getchar();
append(p,ch1);
printf("point ch%d=%c\n",i,ch1);
}
travel(p);
printf("ch1=");
ch1=getchar();
getchar();
del(p,ch1);
travel(p);}
#define null 0
#include<malloc.h>
typedef struct node{
int data;
struct node *next;
}
slnode;
void *initiate(slnode **h){
*h=(slnode *)malloc(sizeof(slnode));
(*h)->next=null;
return 0;
}
slnode append(slnode *p,int x){
slnode *s;
s=(slnode *)malloc(sizeof(slnode));
s->data=x;
s->next=p->next;
p->next=s;
return *s;
}
void travel(slnode *p){
slnode *s;
s=p->next;
while(s!=null)
{
putchar(s->data);
s=s->next;
}
putchar('\n');
}
int del(slnode *p,int x){
slnode *s,*q;
q=p;
s=p->next;
while(s!=null)
if(s->data!=x){
q=s;
s=s->next;}
else{
q->next=s->next;
free(s);
return(0);
}
printf("%c:not found.\n",x);
return(-1);
}
int main(){
int i,ch1,ch2,n;
slnode *q,*p;
initiate(&p);
for(i=0;i<4;i++)
{printf("ch%d=",i);
ch1=getchar();
getchar();
append(p,ch1);
printf("point ch%d=%c\n",i,ch1);
}
travel(p);
printf("ch1=");
ch1=getchar();
getchar();
del(p,ch1);
travel(p);}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询