我最近学数据结构顺序表时,插入与删除操作时出现问题,帮忙看看吧!代码如下:
#include "stdafx.h"
#include "stdlib.h"
void Error(char *str)
{
if(!str)
printf(str);
exit(0);
}
#define ListSize 100
typedef char DataType;
typedef struct 
{
DataType dt[ListSize];
int length;
}SeqList;
void InitList(SeqList *l)
{
l->length=0;
}
void GetListStatus(SeqList *l,int n)
{
if(n<0 || n>l->length-1)
Error("Position Error!");
if(l->length >=ListSize)
Error("Overflow!");

}bool ListEmpty(SeqList *l)
{
return (l->length==0);
}
bool ListFull(SeqList *l)
{
return (l->length +1==ListSize);
}void InsertList(SeqList *l ,DataType d,int n)
{
GetListStatus(l,n);
int i;
for(i=l->length-1;i>n-1;i--)
{
l->dt[i+1]=l->dt[i];
}
l->dt[i]=d;
l->length++;}
void DeleteList(SeqList *l ,int n)
{
GetListStatus(l,n);
int i;
for(i=n-1;i<l->length;i++)
{
l->dt[i-1]=l->dt[i];
}
l->length--;}
int main(int argc, char* argv[])
{
SeqList *l=NULL;
InitList(l);
InsertList(l,'a',0);
InsertList(l,'e',0);
InsertList(l,'r',0);
InsertList(l,'f',0);
InsertList(l,'g',0);
InsertList(l,'h',0);
InsertList(l,'w',0);
InsertList(l,'s',0);
return 0;
}
程序运行时提示“内存不能为written“请问是哪里出错了

解决方案 »

  1.   

    l没有分配内存空间,请用 SeqList *l= new SeqList;
    InitList(l);
    InsertList(l,'a',0);
    InsertList(l,'e',0);
    InsertList(l,'r',0);
    InsertList(l,'f',0);
    InsertList(l,'g',0);
    InsertList(l,'h',0);
    InsertList(l,'w',0);
    InsertList(l,'s',0);
             delete l;
    return 0;
      

  2.   

    int main(int argc, char* argv[])
    {

             SelList a; //添加
             SeqList *l=&a; //改
    InitList(l);
    InsertList(l,'a',0);
    InsertList(l,'e',0);
    InsertList(l,'r',0);
    InsertList(l,'f',0);
    InsertList(l,'g',0);
    InsertList(l,'h',0);
    InsertList(l,'w',0);
    InsertList(l,'s',0);
    return 0;
    }
      

  3.   

    西西老兄:你这个问题是好了可我打印时却没显示:
    void PrintList(SeqList *l)
    {
    if(ListEmpty(l))
    return;
    for(int i=0 ;i<l->length;i++)
    printf("%c\n",l->dt[i]);
    }
    请问如何是好?
      

  4.   

    程序运行到InitList后自动退出,进入Debug模式提示:
    Loaded 'E:\WINNT\System32\ntdll.dll', no matching symbolic information found.
    Loaded symbols for 'E:\WINNT\system32\MSVCRTD.DLL'
    Loaded 'E:\WINNT\system32\KERNEL32.DLL', no matching symbolic information found.
    Loaded 'E:\Program Files\rising\rav\ApiHook.dll', no matching symbolic information found.
    Loaded 'E:\WINNT\system32\ADVAPI32.DLL', no matching symbolic information found.
    Loaded 'E:\WINNT\system32\rpcrt4.dll', no matching symbolic information found.
    Loaded 'E:\Program Files\rising\rav\Memmon.dll', no matching symbolic information found.
    Loaded 'E:\WINNT\system32\USER32.DLL', no matching symbolic information found.
    Loaded 'E:\WINNT\system32\GDI32.DLL', no matching symbolic information found.
    Loaded 'E:\WINNT\system32\imm32.dll', no matching symbolic information found.
    The thread 0x25C has exited with code 0 (0x0).
    The thread 0x4D4 has exited with code 0 (0x0).
    The program 'K:\MFC\sjjg\Debug\sjjg.exe' has exited with code 0 (0x0).
    请问是怎么回事?