单链表的的查找与删除(C)

uloborid 2007-12-30 03:57:09
感觉就是主函数有问题,请大家帮我看看应该怎么写??
题目是:
建立并输出单链表A,D,E,F,H,I,F;查找到F,并删除.输出结果为A D E H I
#include "string.h"
#include "stdio.h"
#define null 0
typedef char datatype;
typedef struct node
{
datatype data;
struct node *next;
}listnode;
typedef listnode *linklist;
listnode *p;
linklist head;

linklist createlistr(void)
{
char ch;
linklist head;
listnode *s,*r;
head=null;r=null;
while ((ch=getchar())!='\n')
{
s=(listnode *)malloc(sizeof(listnode));
s->data=ch;
if (head==null)
head=s;
else
r->next=s;
r=s;
}
if (r!=null)
r->next=null;
return head;
}

listnode* getnode(linklist head,int i)
{
int j;
listnode *p;
p=head;j=0;
while(p->next&&j<i)
{
p=p->next;
j++;
}
if(i==j)
return p;
else
return null;
}

void deletelist(linklist head ,int i)
{
listnode *p,*r;
p=getnode(head,i-1);
if(p==null||p->next==null)
error("position error");
r=p->next;
p->next=r->next;
free(r);
}

main()
{
linklist head;
listnode *p;
printf("please input which character are you search\n");
head=linklist createlistr();
p=deletelist(head,);
while(p=='')
{
printf("%c",p->datatype);
p=p->next;
}
}
...全文
164 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
uloborid 2008-04-28
  • 打赏
  • 举报
回复
啊哈...中间很久一段没上这个论坛了
今天来看下果然还是有大大给出答案了
非常感谢!
lyg_wangyushi 2007-12-31
  • 打赏
  • 举报
回复
对不起,上面的代码只给出了删除的部分
lyg_wangyushi 2007-12-30
  • 打赏
  • 举报
回复
代码比较乱整理一下

#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#define null 0
typedef char datatype;
typedef struct node
{
datatype data;
struct node *next;
}listnode;
typedef listnode *linklist;
listnode *p;
linklist head;

linklist createlistr(void)
{
char ch;
linklist head;
listnode *s,*r;
head=null;r=null;
while ((ch=getchar())!='\n')
{
s=(listnode *)malloc(sizeof(listnode));
s-> data=ch;
if (head==null)
head=s;
else
r-> next=s;
r=s;
}
if (r!=null)
r-> next=null;
return head;
}

listnode* getnode(linklist head,int i)
{
int j;
listnode *p;
p=head;j=0;
while(p&&j<i)
{
p=p-> next;
j++;
}
if(i==j)
return p;
else
return NULL;
}

linklist deletelist(linklist head ,int i)
{
listnode *p,*r;
p=getnode(head,i-1);
if(p==NULL)
printf("position error");

r=p-> next;
p-> next=r-> next;
free(r);
return p;
}

main()
{
int counter=1;
linklist head;
listnode *p;
printf("please input which character are you search\n");
head=createlistr();
p=head;
while(p!=NULL)
{
if(p->data!='F')
{
printf("%c",p->data);
counter++;
}
else
{
p=deletelist(head,counter);
counter--;
}
p=p->next;
}
return 0;
}
lyg_wangyushi 2007-12-30
  • 打赏
  • 举报
回复

#include "string.h"
#include "stdlib.h"
#include "stdio.h"
#define null 0
typedef char datatype;
typedef struct node
{
datatype data;
struct node *next;
}listnode;
typedef listnode *linklist;
listnode *p;
linklist head;

linklist createlistr(void)
{
char ch;
linklist head;
listnode *s,*r;
head=null;r=null;
while ((ch=getchar())!='\n')
{
s=(listnode *)malloc(sizeof(listnode));
s-> data=ch;
if (head==null)
head=s;
else
r-> next=s;
r=s;
}
if (r!=null)
r-> next=null;
return head;
}

listnode* getnode(linklist head,int i)
{
int j;
listnode *p;
p=head;j=0;
while(p&&j<i)
{
p=p-> next;
j++;
}
if(i==j)
return p;
else
return NULL;
}

linklist deletelist(linklist head ,int i)
{
listnode *p,*r;
p=getnode(head,i-1);
if(p==NULL)
printf("position error");

r=p-> next;
p-> next=r-> next;
free(r);
return p;
}

main()
{
int counter=1;
linklist head;
listnode *p;
printf("please input which character are you search\n");
head=createlistr();
p=head;
while(p!=NULL)
{
if(p->data!='F')
{
printf("%c",p->data);
counter++;
}
else
{
p=deletelist(head,counter);
counter--;
}
p=p->next;
}
return 0;
}

33,006

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧