免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3036 | 回复: 4
打印 上一主题 下一主题

[算法] 关于链式链表的问题,求解 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-12-04 23:57 |只看该作者 |倒序浏览
    懂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");
}


论坛徽章:
1
天蝎座
日期:2013-10-23 21:11:03
2 [报告]
发表于 2013-12-05 08:41 |只看该作者
把lz的代码编译了一下
LZ可以看看解决了下面的警告后会不会有问题
t.c: In function ‘listTraverse’:
t.c:77:3: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘struct LNode **’ [-Wformat=]
   printf("&L->next=%d\n",&L->next);
   ^
t.c: In function ‘main’:
t.c:118:14: warning: passing argument 1 of ‘initList’ from incompatible pointer type [enabled by default]
              initList(&L);
              ^
t.c:25:6: note: expected ‘struct LNode *’ but argument is of type ‘struct LNode **’
void initList(struct LNode *L)
      ^
t.c:121:15: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘void *’ [-Wformat=]
               printf("NULL= %d\n",NULL);
               ^
t.c:127:28: warning: passing argument 1 of ‘listInsert’ from incompatible pointer type [enabled by default]
                            i=listInsert(&L,1,j);
                            ^
t.c:48:5: note: expected ‘struct LNode *’ but argument is of type ‘struct LNode **’
int listInsert(struct LNode *L,int i,ElemType e)
     ^
t.c:134:17: warning: passing argument 1 of ‘listTraverse’ from incompatible pointer type [enabled by default]
                 listTraverse(&L);
                 ^
t.c:74:5: note: expected ‘struct LNode *’ but argument is of type ‘struct LNode **’
int listTraverse(struct LNode *L)
     ^

论坛徽章:
0
3 [报告]
发表于 2013-12-05 23:59 |只看该作者
回复 2# openspace



程序没问题呀~~~  传参就是这样传的、、、,有几句是我为了调序而写的......   
拍拖帮我修正........

#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;


        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;

        if(p ==NULL)
                return -1;
        for(p = L->next;p!=NULL;p=p->next)
        {
                printf("data %d\n",p->data);
        }


        #if 0
        if(p!=NULL)
        {
                do{
                        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)
{
        struct LNode *L;
        ElemType e,e0;
        int i;
        int j,k;
       
        initList(&L);


//        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");
}



   

论坛徽章:
0
4 [报告]
发表于 2013-12-06 00:27 |只看该作者
在linux下的程序一样的!!!!
这里没警告!!!
帮忙解解!!!

//----------------------------------------------------------------------------------
listHead.h


#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;


//-----------------------------------------------------------------------------

function2_1.c


#include "listHead.h"

//构建一个空的链表
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;
}


//------------------------------------------------------------------------------------------------


main2_1.c



#include "listHead.h"

void print(ElemType c)
{
        printf("%d ",c);
}

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");
}



//--------------------------------------------------------
Makefile


AIM:=main2_1
$(AIM):main2_1.c function2_1.c listHead.h
        gcc $^ -o $@

.PHONY:clean
clean:
        -rm $(AIM) *~



//-------------------------------------------------------------------------






























论坛徽章:
12
巳蛇
日期:2013-09-16 15:32:242015年辞旧岁徽章
日期:2015-03-03 16:54:152015年亚洲杯之约旦
日期:2015-02-11 14:38:37双鱼座
日期:2015-01-05 11:05:47戌狗
日期:2014-12-08 09:41:18戌狗
日期:2014-08-15 09:29:29双子座
日期:2014-08-05 09:17:17卯兔
日期:2014-06-08 15:32:18巳蛇
日期:2014-01-27 08:47:08白羊座
日期:2013-11-28 21:04:15巨蟹座
日期:2013-11-13 21:58:012015年亚洲杯之科威特
日期:2015-04-17 16:51:51
5 [报告]
发表于 2013-12-06 09:25 |只看该作者
这么多警告你视而不见。
  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(truct 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. {
  19.         *L=(struct LNode *)malloc(sizeof(struct LNode)); //产生头结点,并使L指向此头结点
  20.         if(!*L)
  21.         {
  22.                 exit(OVERFLOW);
  23.         }
  24.         (*L)->next=NULL;//指针域为空
  25. }

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

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


  43.         while(p && j<i-1)//p是否会为0?? j++直到为i-1为止
  44.         {
  45.                 p=p->next;
  46.                 j++;
  47.         }
  48.         if(!p||j>i-1)//i小于1或者大于表长// ??大于表长??
  49.         {
  50.                 printf("insert fail !!!\n");
  51.                 return ERROR;
  52.         }
  53.         s=(struct LNode *)malloc(sizeof(struct LNode));
  54.         s->data=e;
  55.         s->next=p->next;
  56.         p->next=s;

  57.         return OK;
  58. }

  59. int listTraverse(struct LNode *L)
  60. {
  61.         struct LNode  *p;

  62.         if(p ==NULL)
  63.                 return -1;
  64.         for(p = L->next;p!=NULL;p=p->next)
  65.         {
  66.                 printf("data %d\n",p->data);
  67.         }


  68.         #if 0
  69.         if(p!=NULL)
  70.         {
  71.                 do{
  72.                         printf("%d  ",p->data);
  73.                         tmp = p;                       
  74.                         p=p->next;
  75.                 }while(p!=tmp);
  76.         }
  77.         else
  78.                 printf("List is NULL!!\n");
  79.         #endif



  80.         printf("List !!!\n");
  81.         printf("\n");
  82.         return 0;
  83. }

  84. int main(void)
  85. {
  86.         struct LNode *L;
  87.         ElemType e,e0;
  88.         int i;
  89.         int j,k;
  90.       
  91.         initList(&L);


  92. //        int a[5]={2,3,1,4,6};
  93.         for(j=1;j<=5;j++)
  94.         {
  95.                 i=listInsert(L,1,j);
  96.                 if(i==0)
  97.                         printf("j=%d success Insert !!\n",j);
  98.                 else
  99.                         printf("error  !!!\n");
  100.         }

  101.         listTraverse(L);
  102.         printf("List !!!\n");
  103.         printf(" h3-----herer\n");
  104. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP