鱼C论坛

 找回密码
 立即注册
查看: 3419|回复: 3

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

[复制链接]
发表于 2013-12-4 23:46:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
    懂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[5]={2,3,1,4,6};
        for(j=1;j<=5;j++)
        {
        //        i=listInsert(&L,1,a[j-1]);
                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");
}


链式表

链式表


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2013-12-5 23:29:32 | 显示全部楼层
鱼币 全都给出去了!!有木有人会解压
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2013-12-7 14:31:08 | 显示全部楼层

回帖奖励 +30 鱼币

#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[5]={2,3,1,4,6};
        for(j=1;j<=5;j++)
        {
        //        i=listInsert(&L,1,a[j-1]);
                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()函数中用的确是一级指针。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2013-12-8 17:26:12 | 显示全部楼层

谢谢,解答!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-6 03:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表