读书人

关于段访问异常 线性表初步

发布时间: 2012-04-12 15:46:35 作者: rapoo

求助 关于段访问错误 线性表初步
出错的部分后面用注释标出了 耽误大家的时间了 帮我这个小菜鸟入入门吧

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2

#define LIST_INIT_SIZE 100 //初始分配量
#define LISTINCREMENT 10 //分配增量

typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem; //存储空间基址
int length; //当前长度
int listsize; //当前分配的存储容量
}SqList;

Status InitList_Sq(SqList *L);
Status DestroyList_Sq(SqList *L);

int ListEmpty_Sq(SqList *L);
int ListLength_Sq(SqList *L);
Status GetElem_Sq(SqList *L, int i, ElemType *e);
int Compare(ElemType a, ElemType b);
int LocateElem_Sq(SqList *L, ElemType e);
Status PriorElem_Sq(SqList *L, ElemType cur_e, ElemType *pre_e);

Status ListInsert_Sq(SqList *L, int i, ElemType e);

Status ListTraverse_Sq(SqList *L);

int main(int argc, char *argv[])
{
SqList *list, _list;
int e, i, *pre_e, *next_e;
list = &_list;
list-> elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(list-> elem == NULL)
exit(OVERFLOW); //存储分配失败
list-> length = 0; //空表长度为0
list-> listsize = LIST_INIT_SIZE;//初始存储容量

for(i = 1;i <= 5;i++)
{
scanf( "%d ",&e);
ListInsert_Sq(list, i, e);
}
if(NextElem_Sq(list, 45, pre_e))
printf( "%d\n ", pre_e);


ListTraverse_Sq(list);
DestroyList_Sq(list);
system( "PAUSE ");
return 0;
}

Status InitList_Sq(SqList *L)
{
//构造一个空的线性表
L-> elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
if(L-> elem == NULL)
exit(OVERFLOW); //存储分配失败
L-> length = 0; //空表长度为0
L-> listsize = LIST_INIT_SIZE;//初始存储容量
return OK;
}//IntiList_Sq

Status DestroyList_Sq(SqList *L)
{
if(L-> elem != NULL)
{
free(L-> elem);
L-> elem = NULL;
return OK;
}
else
return ERROR;
}


int LocateElem_Sq(SqList *L, ElemType e)
{
//在顺序线形表L中查找第1个值与e满足compare()的元素的位序
//若找到,则返回其在L中的位序,否则返回0
int i;
ElemType *p;
i = 1; //i的处置为第1个元素的位序
p = L-> elem; //p的初值为第1个元素的存储位置
while(i <= L-> length && ! Compare(*p++, e))
++i;
if(i <= L-> length)
return i;
else
return 0;
}//LocateElem_Sq

Status PriorElem_Sq(SqList *L, ElemType cur_e, ElemType *pre_e)
{
int position;
position = LocateElem_Sq(L, cur_e);
if(position > 1 && position <= L-> length)
{
*pre_e = *(L-> elem + position - 2); //这步出现问题,结构体里的指针
//老是访问错误


return OK;
}
else
return ERROR;
}

int Compare(ElemType a, ElemType b)
{
if(a == b)
return TRUE;
else
return FALSE;
}

Status ListInsert_Sq(SqList *L, int i, ElemType e)
{
//在顺现行表L中第i个位置之前插入新的元素e
//i的合法值为1 <=i <=ListLength_Sq(L) + 1
ElemType *newbase, *p, *q;
if(i < 1 ||i > L-> length + 1)
return ERROR;
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; //插入e
++ L-> length; //表长增1
return OK;
}//ListInsert_Sq

Status ListTraverse_Sq(SqList *L)
{
int i;
if(L-> elem != NULL)
{
for(i = 0; i < L-> length; i++)
printf( "%d ", L-> elem[i]);


printf( "\n ");
return OK;
}
else
return ERROR;
}

[解决办法]
NextElem_Sq函数未声明定义
[解决办法]
关于段访问错误 
--------------------
你查下什么地方对于内存地址取操作&的应用错误,代码太多 要不我就给你看了
[解决办法]
SqList *list, _list;
int e, i, *pre_e, *next_e;
list = &_list;

很明显这样写是有问题的,_list变量本身还都没有初始化,就把其地址给了list。

L-> elem + position - 2 //链表的地址不是连续的,所以不能这样用,如果这样用,windows会
提示你指向了非法的内存地址
[解决办法]
非法指针操作数据了

读书人网 >C语言

热点推荐