c语言关于建立链表的题目高手进
#include <stdio.h>#include <stdlib.h>
typedef struct node
{
char data;
struct node *next;
}ListNode;
ListNode *createlist()
{
char ch;
ListNode *head=(ListNode *)malloc(sizeof(ListNode));
ListNode *s,*r=head;
printf("shurujiedianzhi:");
ch=getchar();
while (ch!='\n')
{
s=(ListNode *)malloc(sizeof(ListNode));
s->data=ch;
r->next=s;r=s;
ch=getchar();
}
r->next=NULL;
return head;
}
void prin(ListNode *head)
{
ListNode *p=head->next;
if(p==NULL)
printf("kongbiao\n");
else
{
while(p!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
}
ListNode *getnode(ListNode *head,int i)
{
int j=0;
ListNode *p;
p=head;
while(p->next!=NULL&&j<i)
{
p=p->next;
j++;
}
if(p!=NULL)
return p;
else
return NULL;
;
}
void insertnode(ListNode *head,char x,int i)
{
ListNode *p,*s;
s=(ListNode *)malloc(sizeof(ListNode));
s->data=x;
if(i==1)
{
s->next=head->next;
head->next=s;
}
else
{
p=getnode(head,i-1);
if(p==NULL)
{
printf("iquzhiyoucuo\n");
exit(0);
}
s->next=p->next;
p->next=s;
}
}
ListNode *p;
main()
{
int i;
char a;
int b;
scanf("%d",&i);
switch(i)
{case 1:printf("n=");
p=createlist();
prin(p);
break;
case 2:printf("shurucharudeweizhi:")
scanf("%c,%d",&a,&b);
insertnode(p,a,b);
print(p);break;
default:printf("ERROR!Try again!\n\n");
}
}
哪个错了 编译通过但是的不到答案