bibingyan 发表于 2008-3-25 14:41

线性表的问题

请帮我看看,哪里出错呢.    程序可以运行出来.但最后会弹出对话框,提示出错.
先谢谢了..
#include<iostream.h>
#include<stdlib.h>
#include<malloc.h>
#include<time.h>
#define Status int
#define ElemType int
#define OK 1
#define ERROR -1
#define OVERFLOW        -1

#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
typedef struct
{
        ElemType *elem;
        int Length;
        int ListSize;
}SqList;

Status InitList(SqList &L)
{
        L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
        if(!L.elem)        exit(OVERFLOW);
        L.Length=0;
        L.ListSize=LIST_INIT_SIZE;
        return OK;
}

Status List_Insert(SqList &L,int i,ElemType e)
{
        if(i<1||i>L.Length+1) return ERROR;        ///////为什么要加1
        ElemType *newbase;
        ElemType *q,*p;
        if(L.Length>=L.ListSize)
        {
                newbase=(ElemType*)realloc(L.elem,L.ListSize+LISTINCREMENT*sizeof(ElemType));
                if(!newbase) exit(OVERFLOW);
                L.elem=newbase;
                L.ListSize+=LISTINCREMENT;
        }
        q=&(L.elem[i-1]);
        for(p=&(L.elem[L.Length-1]);p>=q;--p)        *(p+1)=*p;
        *q=e;
        //cout<<*q<<endl;
        L.Length++;
        //cout<<L.Length<<"qqqqqqqqqqq"<<endl;
        return OK;
}

Status ListDelete(SqList &L,int i,ElemType &e)
{
        if((i<1)||(i>L.Length)) return ERROR;
        ElemType *q,*p;
        p=&(L.elem[i-1]);
        e=*p;
        q=L.elem+L.Length-1;
        for(++p;p<=q;p++)        *(p-1)=*p;
        L.Length--;
        return OK;
}
void main()
{
        SqList L;
        InitList(L);
        for(int i=1;i<=20;i++)       
        List_Insert(L,1,i);
        //cout<<L.Length<<"asdafa"<<endl;
        for(int j=1;j<5;j++)
        cout<<L.elem[j-1]<<endl;
        //ElemType *t;
        //for(t=L.elem;t<=&(L.elem[L.Length-1]);t++) cout<<*(t)<<endl;
}

happyboe 发表于 2008-3-25 17:56

我用visual2005可以正常编译,正常运行,结果也正确

bibingyan 发表于 2008-3-25 18:04

啊?    我是用VC 6.0的编译器运行的,结果没错啊.就是运行完毕这后会弹出一个出错的对话框.

bibingyan 发表于 2008-3-25 21:18

#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
当我把这两个值都改成 1时,输出还会出错.

happyboe 发表于 2008-3-26 10:10

你会不会跟踪调试啊,如果会的话你可以跟踪调试看看最后是那里出错.visual
stuio2005因为是商业版的软件,所以有些地方的检查不是很严格,vc6.0可能对某些东西检查比较严格。但是可以肯定你的程序整体上没有大的问题,你可以跟踪调试看看出错的地方在那里。

bibingyan 发表于 2008-3-26 12:20

就是那里的动态会配内存那出错呢。。realloc 。。

论坛元老 发表于 2008-4-2 18:30

我用visual2005可以正常编译,正常运行,结果也正确

页: [1]

编程论坛