问题遇到的现象和发生背景 引发了异常: 读取访问权限冲突
问题相关代码,请勿粘贴截图
运行结果及报错内容 运行起来就崩了
我的解答思路和尝试过的方法
我想要达到的结果 有无捞我一下
-
-
结构体元素 TSeqListNode * Node是一级指针,Node中的元素是TSeqListNode 类型,也就是unsigned int 类型,
所以在insert函数中,传入的是SeqListNode * node指针,在往Node里放的时候,放入的应该是node指向内存的值,而不应该直接用node
同理,Get函数也是一个道理。你的代码写的复杂了,很容易绕晕了。先按你的思路给你改出来,稍后再给你写个精简版。
另外,unsigned int转int可能会越界,这个注意下。
代码修改如下:
#include<stdio.h>
#include<stdlib.h>
typedef void SeqList;
typedef void SeqListNode;
typedef unsigned int TSeqListNode;//对数据元素的定义
typedef struct _tag_SeqList
{
int capacity;//最大容量
int length;//当前长度
TSeqListNode* Node;//指向数组的指针
}TSeqList;
SeqList* Create(int capacity)
{
TSeqList* ret = NULL;//创建一个线性表的指针
if ( capacity >= 0)
{
ret = (TSeqList*)malloc(sizeof(TSeqList));//申请线性表和数组的空间
ret->Node = (TSeqListNode*)malloc(sizeof(TSeqListNode) * capacity);
ret->capacity = capacity;
ret->length = 0;
}
return ret;
}
int Insert(SeqList* list,SeqListNode* node, int pos)
{
TSeqList* slist = (TSeqList*)list;
int ret = (list != NULL);
int i = 0;
ret = ret && (slist->length + 1 <= slist->capacity);
ret = ret && (pos >= 0);
if (ret)
{
if (pos >= slist->length)
{
pos = slist->length;
}
for (i = slist->length; i > pos; i--)
{
slist->Node[i] = slist->Node[i - 1];
}
slist->Node[i] = *((TSeqListNode*)node); //修改
slist->length++;
}
return ret;
}
SeqListNode* Get(SeqList* list, int pos)
{
TSeqList* slist = (TSeqList*)list;
SeqListNode* ret = NULL;
if (slist != NULL && (pos >= 0) && (pos < slist->length))//修改 pos < slist->length,pos从0开始,所以不能取slist->length
{
ret = (SeqListNode*)(&(slist->Node[pos])); //修改
}
return ret;
}
int main()
{
SeqList *list = Create(5);
int a = 0; int b = 1; int c = 2; int d = 3; int e = 4; int f = 5;
int indext = 0;
Insert(list, &a, 0);
Insert(list, &b, 0);
Insert(list, &c, 0);
Insert(list, &d, 0);
Insert(list, &e, 0);
int* p = (int*)Get(list, 0);
printf("%d", *p);
return 0;
}
下面是精减版代码:
#include<stdio.h>
#include<stdlib.h>
typedef unsigned int TSeqListNode;//对数据元素的定义
typedef struct _tag_SeqList
{
int capacity;//最大容量
int length;//当前长度
TSeqListNode* Node;//指向数组的指针
}TSeqList;
TSeqList* Create(int capacity)
{
TSeqList* ret = NULL;//创建一个线性表的指针
if ( capacity >= 0)
{
ret = (TSeqList*)malloc(sizeof(TSeqList));//申请线性表和数组的空间
ret->Node = (TSeqListNode*)malloc(sizeof(TSeqListNode) * capacity);
ret->capacity = capacity;
ret->length = 0;
}
return ret;
}
int Insert(TSeqList* list,TSeqListNode node, int pos)
{
int ret = (list != NULL);
int i = 0;
ret = ret && (list->length + 1 <= list->capacity);
ret = ret && (pos >= 0);
if (ret)
{
if (pos >= list->length)
{
pos = list->length;
}
for (i = list->length; i > pos; i--)
{
list->Node[i] = list->Node[i - 1];
}
list->Node[i] = node;
list->length++;
}
return ret;
}
TSeqListNode Get(TSeqList* list, int pos)
{
TSeqListNode ret = 0;
if (list != NULL && (pos >= 0) && (pos < list->length))
{
ret = list->Node[pos];
}
return ret;
}
int main()
{
TSeqList *list = Create(5);
int a = 0; int b = 1; int c = 2; int d = 3; int e = 4; int f = 5;
int indext = 0;
Insert(list, a, 0);
Insert(list, b, 0);
Insert(list, c, 0);
Insert(list, d, 0);
Insert(list, e, 0);
int p = (int)Get(list, 0);
printf("%d", p);
return 0;
}