一个连链表的程序(帮忙改改)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct lnode
{
char *str;
struct lnode *next;
}lnode,*listnode;
int createlist(listnode *H,int cnt)
{
listnode tail,s;
int i;
char *strg;
(*H)=(listnode)malloc(sizeof(lnode));
if(!(*H))
{
exit(-1);
}
(*H)->next=NULL;
tail=(*H);
for(i=0;i<cnt;i++)
{
s=(listnode)malloc(sizeof(lnode));
if(!s)
{
exit(-1);
}
printf("please input a word: ");
scanf("%s\n",strg);
s->str=strg;
s->next=tail->next;
tail->next=s;
tail=s;
}
return 1;
}
int listlength(listnode h)
{
int cnt=0;
listnode p=h->next;
if(p)
{
++cnt;
p=p->next;
}
return cnt;
}
int listemty(listnode h)
{
if(h->next)
{
return 1;
}
else
{
return 0;
}
}
int listinsert(listnode h,int i,char *strg)
{
listnode p=h,s;int j=0;
while((p->next)&&(j<i-1))
{
p=p->next;
++j;
}
if(!(p->next)||(j>i-1))
{
return 0;
}
s=(listnode)malloc(sizeof(lnode));
printf("input a word:");
scanf("%s\n",strg);
s->str=strg;
s->next=p->next;
p->next=s;
return 1;
}
int listdelete(listnode h,int i,char *strg)
{
listnode p=h,q;
int j=0;
while(p->next&&j<i-1)
{
p=p->next;
++j;
}
if(!(p->next)||j>i-1)
{
return 0;
}
q=p->next;
strg=q->str;
p->next=q->next;
free(q);
q=NULL;
return 1;
}
int printlist(listnode h)
{
listnode p=h->next;
while(p->next)
{
printf("the word is this:%s\n",p->str);
p=p->next;
}
printf("\n");
return 1;
}
int main()
{
listnode h;
int cnt;
int i;
char *strg;
printf("请输入你要创建的链表的长度\n");
scanf("%d\n",cnt);
createlist(&h,cnt);
printlist(h);
}
[解决办法]
没时间改,给你一个自己比着改吧。
http://blog.csdn.net/wfwd/archive/2007/08/23/1756265.aspx
或者模板实现的:
http://blog.csdn.net/wfwd/archive/2007/08/28/1762246.aspx
[解决办法]
1. scanf("%d\n",cnt);有问题, 改成scanf("%d\n",&cnt);
2. scanf("%s\n",strg); strg没有分配空间