鱼C论坛

 找回密码
 立即注册
查看: 3042|回复: 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 鱼币

  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>


  4. #define OVERFLOW -1
  5. #define ERROR -1

  6. #define OK 0
  7. //#define NULL 0

  8. #define LEN sizeof(struct LNode);

  9. typedef int ElemType;

  10. struct LNode //结点结构
  11. {
  12.         ElemType data; //数据域
  13.         struct LNode *next;   //指针域
  14. };

  15. typedef struct LNode *linkList;



  16. //构建一个空的链表
  17. //void initList(struct LNode *L)
  18. void initList(linkList *L)   
  19. {
  20.         (*L) = (struct LNode *)malloc(sizeof(struct LNode)); //产生头结点,并使L指向此头结点
  21.         if(!(*L))
  22.         {
  23.                 exit(OVERFLOW);
  24.         }
  25.         (*L)->next=NULL;//指针域为空
  26. }

  27. //销毁链表
  28. void destroyList(struct LNode *L)
  29. {
  30.         struct LNode *q;
  31.         while(L) //如果链表不为空
  32.         {
  33.                 q=L->next; //将当前结点的下一个结点赋值给q
  34.                 free(L); //释放地一个结点
  35.                 L=q;
  36.         }
  37. }

  38. //向链表插入数据
  39. int listInsert(struct LNode *L,int i,ElemType e)
  40. {//在带头结点的单链表L中的第i个位置之前插入元素e
  41.         int j=0;
  42.         struct LNode *p=L;
  43.         struct LNode *s;
  44.        
  45.         printf("i=%d ------ e=%d \n",i,e);
  46.        
  47.         while(p && j<i-1)//p是否会为0?? j++直到为i-1为止
  48.         {
  49.                 p=p->next;
  50.                 j++;
  51.         }
  52.         if(!p||j>i-1)//i小于1或者大于表长// ??大于表长??
  53.         {
  54.                 printf("insert fail !!!\n");
  55.                 return ERROR;
  56.         }
  57.         s=(struct LNode *)malloc(sizeof(struct LNode));
  58.         s->data=e;
  59.         s->next=p->next;
  60.         p->next=s;
  61.        
  62.         return OK;
  63. }

  64. int listTraverse(struct LNode *L)
  65. {
  66.         struct LNode  *p = L->next;
  67.         //printf("&L->next=%d\n",&L->next);
  68.         if(p ==NULL)
  69.                 return -1;
  70.         printf("scan!\n");
  71.         for(p = L->next;p!=NULL;p=p->next)
  72.         {
  73.                 printf("data %d\n",p->data);
  74.                 //printf("*p %d",*p);
  75.         }
  76.        
  77.        
  78. #if 0
  79.         if(p!=NULL)
  80.         {
  81.                 do{
  82.                         printf("\n*p %d\n",*p);
  83.                         printf("%d  ",p->data);
  84.                         tmp = p;                        
  85.                         p=p->next;
  86.                 }while(p!=tmp);
  87.         }
  88.         else
  89.                 printf("List is NULL!!\n");
  90. #endif
  91.        
  92.        
  93.        
  94.         printf("List !!!\n");
  95.         printf("\n");
  96.         return 0;
  97. }




  98. int main(void)
  99. {
  100.         linkList L;
  101. //        ElemType e,e0;
  102.         int i;
  103.         int j;
  104.         initList(&L);
  105.        
  106.        
  107.         printf("NULL= %d\n",NULL);
  108.        
  109.         //        int a[5]={2,3,1,4,6};
  110.         for(j=1;j<=5;j++)
  111.         {
  112.         //        i=listInsert(&L,1,a[j-1]);
  113.                 i=listInsert(L,1,j);
  114.                 if(i==0)
  115.                         printf("j=%d success Insert !!\n",j);
  116.                 else
  117.                         printf("error  !!!\n");
  118.         }
  119.        
  120.         listTraverse(L);
  121.         printf("List !!!\n");
  122.         printf(" h3-----herer\n");

  123.         return 0;
  124. }
复制代码
我不晓得楼主那程序为什么能运行出来,我用VC++6.0是会报错的,在楼主的基础上改的程序可以运行正常结果,代码仅供参考。还有一点要提醒楼主,函数调用的时候参数的传递要统一类型,即
  1. initList(&L);
复制代码
  1. void initList(linkList *L)  
  2. {
  3. ..............
  4. }
复制代码
的类型要一致,楼主程序中,主函数调用initList函数用二级指针,而initList()函数中用的确是一级指针。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 06:26

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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