| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 515 人关注过本帖
标题:为什么线性表老是出现错误,高手请忙看看~~谢谢
只看楼主 加入收藏
大虫归来
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-11-14
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:3 
为什么线性表老是出现错误,高手请忙看看~~谢谢
#include <stdio.h>
#include <stdlib.h>

#define LIST_INIT_SIZE  10
#define LISTINCREMENT   10

#define OVERFLOW    0
#define OK          1
#define Empty       2

typedef int Elemtype;
typedef int Status;

typedef struct{
    Elemtype *elem;
    int length;
    int listsize;
}Sqlist;

Status InitList(Sqlist List)
{
    List.elem = (Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));
    if(!List.elem) exit(OVERFLOW);
    List.length = 0;
    List.listsize = LIST_INIT_SIZE;
    return OK;
}

Status ListLength(Sqlist List)
{
   if(List.length == 0) return Empty;
   else return List.length;
}

void ResizeList(Sqlist List)
{
    if(List.length >= List.listsize)
    {
        Elemtype *p = (Elemtype *)realloc(List.elem,(LIST_INIT_SIZE + LISTINCREMENT)*sizeof(Elemtype));
        List.elem = p;
        List.length = List.length;
        List.listsize = LIST_INIT_SIZE + LISTINCREMENT;
    }
}

void InsertLastElem(Sqlist List,Elemtype data)
{
    if(List.length >= List.listsize)
    {
        ResizeList(List);
    }
    printf("List.length = %d,data = %d\r\n",List.length,data);
    List.length++;
    List.elem[List.length-1] = data;//有问题
    printf("List.elem[%d] = %d\r\n",List.length,List.elem[List.length]);
}

void main()
{
    int m,a[10]={1,2,3,4,5,6,7,8,9,10};
    Sqlist List;

    InitList(List);
    for(m=0;m<10;m++)
    {
        InsertLastElem(List,a[m]);
    }
    printf("m = %d\r\n",ListLength(List));
}
搜索更多相关主题的帖子: include return 线性表 
2011-11-14 21:22
cnfarer
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:179
帖 子:3330
专家分:21157
注 册:2010-1-19
收藏
得分:20 
没有全改,只改了部分,但能运行了,剩下的自己处理吧!
#include <stdio.h>
#include <stdlib.h>

#define LIST_INIT_SIZE  10
#define LISTINCREMENT   10

#define OVERFLOW    0
#define OK          1
#define Empty       2

typedef int Elemtype;
typedef int Status;

typedef struct{
    Elemtype *elem;
    int length;
    int listsize;
}Sqlist;

Status InitList(Sqlist *List)
{
    List->elem = (Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));
    if(!List->elem) exit(OVERFLOW);
    List->length = 0;
    List->listsize = LIST_INIT_SIZE;
    return OK;
}

Status ListLength(Sqlist List)
{
   if(List.length == 0) return Empty;
   else return List.length;
}

void ResizeList(Sqlist List)
{
    if(List.length >= List.listsize)
    {
        Elemtype *p = (Elemtype *)realloc(List.elem,(LIST_INIT_SIZE + LISTINCREMENT)*sizeof(Elemtype));
        List.elem = p;
        List.length = List.length;
        List.listsize = LIST_INIT_SIZE + LISTINCREMENT;
    }
}

void InsertLastElem(Sqlist *List,Elemtype data)
{
    if(List->length >= List->listsize)
    {
       // ResizeList(List);
    }
    printf("List.length = %d,data = %d\r\n",List->length,data);
   
    List->elem[List->length] = data;//有问题
    List->length++;
    printf("List.elem[%d] = %d\r\n",List->length,List->elem[List->length-1]);
}

void main()
{
    int m,a[10]={1,2,3,4,5,6,7,8,9,10};
    Sqlist List;

    InitList(&List);
    for(m=0;m<10;m++)
    {
        InsertLastElem(&List,a[m]);
    }
    printf("m = %d\r\n",ListLength(List));
}

★★★★★为人民服务★★★★★
2011-11-14 22:08
大虫归来
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2011-11-14
收藏
得分:0 
回复 2楼 cnfarer
    谢谢版主!原来的程序对IintList(Sqlist List)与InitList(Sqlist *List)理解有误,List在没有声明为全局变量时,IintList(Sqlist List)中的List为局部变量,同InsertList中的类似,List的值只在局部内改变,不能在全局中改变。故没有声明List为全局变量时,需要使用InitList(Sqlist *List)形式;如果声明List为全局变量,需要使用IintList(Sqlist List)的这种形式。注意局部变量和全局变量,注意指针和变量与区别。
    下面列出本人的两种修改方法,与各位交流:
    方法1:
 #include <stdio.h>
#include <stdlib.h>

#define LIST_INIT_SIZE  10
#define LISTINCREMENT   10

#define OVERFLOW    0
#define OK          1
#define Empty       2

typedef int Elemtype;
typedef int Status;

typedef struct {
    Elemtype *elem;
    int length;
    int listsize;
}Sqlist;

Sqlist List;

Status InitList()
{
    List.elem = (Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));
    if(!List.elem) exit(OVERFLOW);
    List.length = 0;
    List.listsize = LIST_INIT_SIZE;
    return OK;
}

Status ListLength()
{
   if(List.length == 0) return Empty;
   else return List.length;
}

void ResizeList()
{
    if(List.length >= List.listsize)
    {
        List.elem = (Elemtype *)realloc(List.elem,(LIST_INIT_SIZE + LISTINCREMENT)*sizeof(Elemtype));
        List.length = List.length;
        List.listsize = LIST_INIT_SIZE + LISTINCREMENT;
    }
}

void InsertLastElem(Elemtype data)
{
    if(List.length >= List.listsize)
    {
        ResizeList(List);
    }
    printf("List.length = %d,data = %d\r\n",List.length,data);
    List.elem[List.length] = data;
    List.length++;
    printf("List.elem[%d] = %d\r\n",List.length-1,List.elem[List.length-1]);
}

void main()
{
    int m,a[10]={1,2,3,4,5,6,7,8,9,10};

    InitList(List);
    for(m=0;m<10;m++)
    {
        InsertLastElem(a[m]);
    }
    printf("m = %d\r\n",ListLength(List));
}
方法2:
#include <stdio.h>
#include <stdlib.h>

#define LIST_INIT_SIZE  10
#define LISTINCREMENT   10

#define OVERFLOW    0
#define OK          1
#define Empty       2

typedef int Elemtype;
typedef int Status;

typedef struct{
    Elemtype *elem;
    int length;
    int listsize;
}Sqlist;

Status InitList(Sqlist *List)
{
    List->elem = (Elemtype *)malloc(LIST_INIT_SIZE*sizeof(Elemtype));
    if(!List->elem) exit(OVERFLOW);
    List->length = 0;
    List->listsize = LIST_INIT_SIZE;
    return OK;
}

Status ListLength(Sqlist *List)
{
   if(List->length == 0) return Empty;
   else return List->length;
}

void ResizeList(Sqlist *List)
{
    if(List->length >= List->listsize)
    {
        Elemtype *p = (Elemtype *)realloc(List->elem,(LIST_INIT_SIZE + LISTINCREMENT)*sizeof(Elemtype));
        List->elem = p;
        List->length = LIST_INIT_SIZE;
        List->listsize = LIST_INIT_SIZE + LISTINCREMENT;
    }
}

void InsertLastElem(Sqlist *List,Elemtype data)
{
    if(List->length >= List->listsize)
    {
       // ResizeList(List);
    }
    printf("List.length = %d,data = %d\r\n",List->length,data);

    List->elem[List->length] = data;
    List->length++;
    printf("List.elem[%d] = %d\r\n",List->length,List->elem[List->length-1]);
}

void main()
{
    int m,a[10]={1,2,3,4,5,6,7,8,9,10};
    Sqlist List;

    InitList(&List);
    for(m=0;m<10;m++)
    {
        InsertLastElem(&List,a[m]);
    }
    printf("m = %d\r\n",ListLength(&List));
}   
2011-11-17 22:14
zwklxt
Rank: 1
来 自:北京
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-11-18
收藏
得分:0 
看了楼主的分析,困扰我很多天的问题忽然想明白了,感谢楼主。
2011-11-18 19:46
快速回复:为什么线性表老是出现错误,高手请忙看看~~谢谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017333 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved