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

对链表的基本操作,该怎么解决

2012-02-11 
对链表的基本操作#includestdio.h#includemalloc.h#includestring.htypedefstructnode{char numcha

对链表的基本操作
#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct node
{
char num;
char name;
struct node *next;
}L;
void listcreat(L *structlist)
{
L *structtemp,*structlist1;
structlist1=structtemp=(struct node*)malloc(sizeof(struct node));
while(1)
{ structlist1=structtemp;
structtemp=(struct node*)malloc(sizeof(struct node));
printf("输入一个号:当(0000)时结束!\n");
scanf("%s",&structtemp->num);
if(strcmp(structtemp->num,"0000")==0)
return;
printf("输入该号对应的name:");
scanf("%s",&structtemp->name);
structlist1->next=structtemp;
}
structlist1->next=NULL;
}
void listprint(L *structlist)
{
while(structlist!=NULL)
{
printf("%s%s\n",structlist->num,structlist->name);
structlist=structlist->next;
}
}
void choose()
{
printf("1.yes\n");
printf("2.no\n");
}
void listdelete(L *structlist)
{
int intamend;//amend exit!
int intnum;
int i=0;
while(1)
{
printf("需要删除的位置:\n");
scanf("%d",&intnum); //需要添加判断删除链表结点的位置!whliekaol!
  while(i!=intnum){
structlist->next=structlist->next->next;
printf("当前被删除的号:%d",i);
i++;
}
printf("还要继续?\n");
choose();
scanf("%d",&intamend);
if(intamend==2)
break;
}
}
 
void main()
{
L *structlist;
listcreat(structlist);
listdelete(structlist);
listprint(structlist);
}
语法上没错误,编译也能通过,就是运行有问题XP下是内存错误,windows7下稍微输入一个数字就错误!

[解决办法]
有点问题改啦一下.加啦些注释.
另外不知道是否符合你的设计的本意义的.or 请跟帖说明.

C/C++ code
#include<stdio.h>#include<malloc.h>#include<string.h>typedef struct node{char num[100]; //´æ´¢ÔªËØÒªÃ´ÓÃÊý×é,ҪôÓÃÖ¸Õë.char name[100];struct node *next;}L;void listprint(L *structlist){    structlist = structlist->next;    while(structlist!=NULL)    {        printf("%s %s\n",structlist->num,structlist->name);        structlist=structlist->next;    }}void listcreat(L **structlist)//½¨Á¢µÄÊÇÒ»¸ö´øÄ¬ÈÏÍ·½áµãµÄÁ´±í.ÁíÍâÒÀÕÕÄãµÄ´ó²¿·Ö´úÂë.Ó¦¸ÃÊÇÓÃβ²å·¨.structlist1ÊÇβ°ÍµÄ×÷ÓÃ.{    L *structtemp,*structlist1;    structtemp = structlist1 =NULL;    structlist1=(struct node*)malloc(sizeof(struct node));        (*structlist) = structlist1;//*structlist²»±ä.*structlistΪÕâÌõÁ´±íµÄĬÈÏÍ·½áµã.        while(1)    {         structtemp=(struct node*)malloc(sizeof(struct node));        printf("ÊäÈëÒ»¸öºÅ:µ±(0000)ʱ½áÊø!\n");        scanf("%s",structtemp->num);        if(strcmp(structtemp->num,"0000")==0)        {            free(structtemp);            structtemp = NULL;            return;        }        printf("ÊäÈë¸ÃºÅ¶ÔÓ¦µÄname:");            scanf("%s",structtemp->name);        structtemp->next = NULL;                structlist1->next=structtemp;//½«structtemp¼ÓÈëÁ´±í.        structlist1 = structtemp;//ÉèÖÃеÄβ½áµã.    }    }void choose(){    printf("1.yes\n");    printf("2.no\n");}void listdelete(L *structlist){    int intamend;//amend exit!    int intnum;    int i=1;    struct node* pre_deletenode = NULL;//ɾ³ýʱÐèÏÈÕÒµ½´ýɾ³ý½áµãµÄǰ½áµã    struct node* tmp = NULL;    while(1)//¼Ù¶¨Ä¬ÈÏÍ·½áµãºóµÚÒ»¸öÔªËØÎª1.    {        printf("ÐèҪɾ³ýµÄλÖÃ:\n");        scanf("%d",&intnum); //ÐèÒªÌí¼ÓÅжÏɾ³ýÁ´±í½áµãµÄλÖÃ!whliekaol        if(intnum<=0)        {            printf("ÊäÈëÐè´óÓÚ0.\n");            continue;        }        pre_deletenode = structlist;        i = 1;//×÷Ï´Îɾ³ýǰÐèÖØÖÃpre_deletenode,i.        while((i<intnum) && (pre_deletenode->next != NULL))        {            //structlist->next=structlist->next->next;            //printf("µ±Ç°±»É¾³ýµÄºÅ:%d",i);            pre_deletenode = pre_deletenode->next;            i++;        }                if(pre_deletenode->next == NULL)        {            printf("ÔªËØ¸öÊýСÓÚÊäÈëµÄ¸öÊý.²»Ö´ÐÐɾ³ý.\n");        }        else        {            if(intnum>0)            {                tmp  =     pre_deletenode->next; //Ò»°ãɾ³ý½áµãÊÇÏÈÕÒµ½Ðèɾ³ý½áµãµÄÉÏÒ»¸ö½áµã.                pre_deletenode->next =     pre_deletenode->next->next;                free(tmp);            }        }                printf("»¹Òª¼ÌÐø?\n");        choose();        scanf("%d",&intamend);        if(intamend==2)            break;    }} void main(){    L *structlist;    listcreat(&structlist);//ÐèÒª¸Ä±äÍ·½áµãµÄÖµ,ÄǾʹ«Ö¸ÕëµÄÖ¸Õë.»òÕßÓÃreturnµÄ·½Ê½.    listprint(structlist);    listdelete(structlist);    listprint(structlist);} 


[解决办法]
每日一mark!
[解决办法]
要分啊!哈哈

热点排行