天之骄子 发表于 2013-12-4 23:46:34

关于链式链表的问题,求解

    懂C数据结构的帮忙解决一下!!!!
   很诡异的问题!!!!!

   这道题困扰我很久了,现在很急着要解决!!!,求二位帮忙一下
问题:我名插入5个数为什么打印的时候会那么不正常!!!,为什么对多打印几个数!!


我的鱼币就那么多了!!就帮忙者回答!!!

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define OVERFLOW -1
#define ERROR -1

#define OK 0
//#define NULL 0
#define LEN sizeof(truct LNode);

typedef int ElemType;

struct LNode //结点结构
{
        ElemType data; //数据域
        struct LNode *next;   //指针域
};

typedef struct LNode *linkList;




//构建一个空的链表
void initList(struct LNode *L)
{
        L=(struct LNode *)malloc(sizeof(struct LNode)); //产生头结点,并使L指向此头结点
        if(!L)
        {
                exit(OVERFLOW);
        }
        L->next=NULL;//指针域为空
}

//销毁链表
void destroyList(struct LNode *L)
{
        struct LNode *q;
        while(L) //如果链表不为空
        {
                q=L->next; //将当前结点的下一个结点赋值给q
                free(L); //释放地一个结点
                L=q;
        }
}

//向链表插入数据
int listInsert(struct LNode *L,int i,ElemType e)
{//在带头结点的单链表L中的第i个位置之前插入元素e
        int j=0;
        struct LNode *p=L;
        struct LNode *s;

        printf("i=%d ------ e=%d \n",i,e);

        while(p && j<i-1)//p是否会为0?? j++直到为i-1为止
        {
                p=p->next;
                j++;
        }
        if(!p||j>i-1)//i小于1或者大于表长// ??大于表长??
        {
                printf("insert fail !!!\n");
                return ERROR;
        }
        s=(struct LNode *)malloc(sizeof(struct LNode));
        s->data=e;
        s->next=p->next;
        p->next=s;

        return OK;
}

int listTraverse(struct LNode *L)
{
        struct LNode*p = L->next;
printf("&L->next=%d\n",&L->next);
        if(p ==NULL)
                return -1;
        printf("scan!\n");
        for(p = L->next;p!=NULL;p=p->next)
        {
                printf("data %d\n",p->data);
                //printf("*p %d",*p);
        }


        #if 0
        if(p!=NULL)
        {
                do{
                        printf("\n*p %d\n",*p);
                        printf("%d",p->data);
                        tmp = p;                       
                        p=p->next;
                }while(p!=tmp);
        }
        else
                printf("List is NULL!!\n");
        #endif



        printf("List !!!\n");
        printf("\n");
        return 0;
}




int main(void)
{
        linkList L;
        ElemType e,e0;
        int i;
        int j,k;
        initList(&L);


        printf("NULL= %d\n",NULL);

//        int a={2,3,1,4,6};
        for(j=1;j<=5;j++)
        {
        //        i=listInsert(&L,1,a);
                i=listInsert(&L,1,j);
                if(i==0)
                        printf("j=%d success Insert !!\n",j);
                else
                        printf("error!!!\n");
        }

        listTraverse(&L);
        printf("List !!!\n");
        printf(" h3-----herer\n");
}




天之骄子 发表于 2013-12-5 23:29:32

鱼币 全都给出去了!!有木有人会解压

小亮1201 发表于 2013-12-7 14:31:08

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>


#define OVERFLOW -1
#define ERROR -1

#define OK 0
//#define NULL 0

#define LEN sizeof(struct LNode);

typedef int ElemType;

struct LNode //结点结构
{
        ElemType data; //数据域
        struct LNode *next;   //指针域
};

typedef struct LNode *linkList;



//构建一个空的链表
//void initList(struct LNode *L)
void initList(linkList *L)   
{
        (*L) = (struct LNode *)malloc(sizeof(struct LNode)); //产生头结点,并使L指向此头结点
        if(!(*L))
        {
                exit(OVERFLOW);
        }
        (*L)->next=NULL;//指针域为空
}

//销毁链表
void destroyList(struct LNode *L)
{
        struct LNode *q;
        while(L) //如果链表不为空
        {
                q=L->next; //将当前结点的下一个结点赋值给q
                free(L); //释放地一个结点
                L=q;
        }
}

//向链表插入数据
int listInsert(struct LNode *L,int i,ElemType e)
{//在带头结点的单链表L中的第i个位置之前插入元素e
        int j=0;
        struct LNode *p=L;
        struct LNode *s;
       
        printf("i=%d ------ e=%d \n",i,e);
       
        while(p && j<i-1)//p是否会为0?? j++直到为i-1为止
        {
                p=p->next;
                j++;
        }
        if(!p||j>i-1)//i小于1或者大于表长// ??大于表长??
        {
                printf("insert fail !!!\n");
                return ERROR;
        }
        s=(struct LNode *)malloc(sizeof(struct LNode));
        s->data=e;
        s->next=p->next;
        p->next=s;
       
        return OK;
}

int listTraverse(struct LNode *L)
{
        struct LNode*p = L->next;
        //printf("&L->next=%d\n",&L->next);
        if(p ==NULL)
                return -1;
        printf("scan!\n");
        for(p = L->next;p!=NULL;p=p->next)
        {
                printf("data %d\n",p->data);
                //printf("*p %d",*p);
        }
       
       
#if 0
        if(p!=NULL)
        {
                do{
                        printf("\n*p %d\n",*p);
                        printf("%d",p->data);
                        tmp = p;                        
                        p=p->next;
                }while(p!=tmp);
        }
        else
                printf("List is NULL!!\n");
#endif
       
       
       
        printf("List !!!\n");
        printf("\n");
        return 0;
}




int main(void)
{
        linkList L;
//        ElemType e,e0;
        int i;
        int j;
        initList(&L);
       
       
        printf("NULL= %d\n",NULL);
       
        //      int a={2,3,1,4,6};
        for(j=1;j<=5;j++)
        {
      //      i=listInsert(&L,1,a);
                i=listInsert(L,1,j);
                if(i==0)
                        printf("j=%d success Insert !!\n",j);
                else
                        printf("error!!!\n");
        }
       
        listTraverse(L);
        printf("List !!!\n");
        printf(" h3-----herer\n");

        return 0;
}我不晓得楼主那程序为什么能运行出来,我用VC++6.0是会报错的,在楼主的基础上改的程序可以运行正常结果,代码仅供参考。还有一点要提醒楼主,函数调用的时候参数的传递要统一类型,即initList(&L);和void initList(linkList *L)
{
..............
}的类型要一致,楼主程序中,主函数调用initList函数用二级指针,而initList()函数中用的确是一级指针。

天之骄子 发表于 2013-12-8 17:26:12

小亮1201 发表于 2013-12-7 14:31 static/image/common/back.gif
我不晓得楼主那程序为什么能运行出来,我用VC++6.0是会报错的,在楼主的基础上改的程序可以运行正常结果,代 ...

谢谢,解答!!!
页: [1]
查看完整版本: 关于链式链表的问题,求解