主题:帮我看看顺序表操作,看看哪儿出了毛病???解答送分
#include <stdio.h>
#include <malloc.h>
#define OK 1
#define ERROR 0
typedef int Status;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct{
int *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem) return(ERROR);
L.length=0;
L.listsize=LIST_INIT_SIZE;
}
Status ListInsert_Sq(SqList &L,int i,int e)
{ int *newbase,*p,*q;
if(i<1||i>L.length+1) return(ERROR);
if(L.length>=L.listsize)
{newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!newbase) return(ERROR);
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;
++L.length;
return(OK);
}
void un(SqList &La,SqList Lb)
{int La_len,Lb_len,i,j,e,*p;
La_len=La.length; Lb_len=Lb.length;
for(i=0;i<Lb_len;i++)
{e=Lb.elem[i];
j=1; p=La.elem;
while(j<=La_len && *p++!=e) ++j;
if(j>La_len) ListInsert_Sq(La,++La_len,e);
}
}
main()
{SqList La,Lb;
int a[4]={3,5,8,11},b[7]={2,6,8,9,11,15,20},i;
InitList_Sq(La);
La.length=4; La.elem=a;
printf("\n线性表La为:");
for(i=0;i<La.length;i++)
printf("%3d",La.elem[i]);
InitList_Sq(Lb);
Lb.length=7; Lb.elem=b;
printf("\n线性表Lb为:");
for(i=0;i<Lb.length;i++)
printf("%3d",Lb.elem[i]);
un(La,Lb);
printf("\n线性表La为:");
for(i=0;i<La.length;i++)
printf("%3d",La.elem[i]);
printf("\n\n");
}
以上程序不知哪儿出了毛病,运行不了,显示不能读内存????, 劳高手指点迷津
#include <malloc.h>
#define OK 1
#define ERROR 0
typedef int Status;
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
typedef struct{
int *elem;
int length;
int listsize;
}SqList;
Status InitList_Sq(SqList &L)
{L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem) return(ERROR);
L.length=0;
L.listsize=LIST_INIT_SIZE;
}
Status ListInsert_Sq(SqList &L,int i,int e)
{ int *newbase,*p,*q;
if(i<1||i>L.length+1) return(ERROR);
if(L.length>=L.listsize)
{newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!newbase) return(ERROR);
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;
++L.length;
return(OK);
}
void un(SqList &La,SqList Lb)
{int La_len,Lb_len,i,j,e,*p;
La_len=La.length; Lb_len=Lb.length;
for(i=0;i<Lb_len;i++)
{e=Lb.elem[i];
j=1; p=La.elem;
while(j<=La_len && *p++!=e) ++j;
if(j>La_len) ListInsert_Sq(La,++La_len,e);
}
}
main()
{SqList La,Lb;
int a[4]={3,5,8,11},b[7]={2,6,8,9,11,15,20},i;
InitList_Sq(La);
La.length=4; La.elem=a;
printf("\n线性表La为:");
for(i=0;i<La.length;i++)
printf("%3d",La.elem[i]);
InitList_Sq(Lb);
Lb.length=7; Lb.elem=b;
printf("\n线性表Lb为:");
for(i=0;i<Lb.length;i++)
printf("%3d",Lb.elem[i]);
un(La,Lb);
printf("\n线性表La为:");
for(i=0;i<La.length;i++)
printf("%3d",La.elem[i]);
printf("\n\n");
}
以上程序不知哪儿出了毛病,运行不了,显示不能读内存????, 劳高手指点迷津