首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

单链表的的查找与删除(C)解决方案

2012-03-13 
单链表的的查找与删除(C)感觉就是主函数有问题,请大家帮我看看应该怎么写??题目是:建立并输出单链表A,D,E,

单链表的的查找与删除(C)
感觉就是主函数有问题,请大家帮我看看应该怎么写??
题目是:
建立并输出单链表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;
}
}


[解决办法]
代码比较乱整理一下[code=C/C++][/code][code=C/C++][/code]
#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;

热点排行