|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 菜鸟小乔 于 2019-6-1 16:14 编辑
代码功能:创建循环链表,实现初始化赋值、插入元素的功能。(目前只写了插入函数、初始化函数)
问题描述:在插入函数和打印函数里,用new分配内存,在函数结尾delete释放——报错读取访问冲突。
如果把delete语句注释掉程序就可以运行,但感觉这样不严谨。
求助大神们帮忙看看,我的delete语句为什么释放内存出错?
完整代码如下:(出错的delete语句在.cpp文件69行处、94行处)
--------C_list.h文件---------数据类型定义、函数声明---------------------------
- #ifndef CircularLinkList_h_include
- #define CircularLinkList_h_include
- /* 《循环链表》
- */
- #include<iostream>
- #include<string>
- using namespace std;
- //#define MAX_SIZE 255
- //#define TRUE 1
- //#define FALSE 0
- //定义一种数据类型
- typedef struct {
- int id;
- string name ="初始名字";
- }EleType;
- //循环链表节点,
- typedef struct Node {
- EleType data;
- struct Node* next;
- }Node;
- //定义 循环链表的结构
- typedef struct C_LinkList {
- Node* next; //头指针(有头节点,)
- int length; //链表长度,初始值为0.
- }C_LinkList;
- //插入元素
- void Insert(C_LinkList* list_c, int pos, EleType element);
- //初始化
- void Init(C_LinkList* list_c, EleType* dataArray, int len);
- //打印
- void Print(C_LinkList* list_c);
- #endif
复制代码
--------.cpp文件------------函数实现-------------------------
- #include<iostream>
- #include<string>
- #include"C_list.h"
- using namespace std;
- //初始化
- void Init(C_LinkList* list_c, EleType* dataArray, int len)
- {
- for (int i = 0; i < len; i++)
- {
- Insert(list_c, i + 1, dataArray[i]);
- }
- }
- //插入元素
- void Insert(C_LinkList* list_c, int pos, EleType element)
- {
- Node* node = new Node;
- node->data = element;
- // element.id = 5;
- // element.name = "名字";
- node->next = NULL;
- if (pos == 1)
- {
- node->next = list_c->next;
- if (!node->next) //如果链表长度为零
- {
- //cout << "长度为零" << endl;
- node->next = node;
- }
- else//长度不为零,找到最后一个节点,并改变其指针域
- {
- //cout << "长度不为零" << endl;
- Node* lastnode = list_c->next;
- for (int i = 1; i < list_c->length; i++)
- {
- lastnode = lastnode->next;
- }
- lastnode->next = node;
- }
- list_c->next = node;
- list_c->length++;
- return;
- }//
- //cout << "pos不等于1" << endl; //插入pos不是第一个,从第一个开始遍历
- Node* currNode = new Node;
- currNode->next = NULL;
-
- currNode = list_c->next;
- for (int i = 1; currNode && i < pos - 1; i++)
- {
- currNode = currNode->next;
- }
- if (currNode)
- {
- //cout << "pos不等于1" << endl;
- node->next = currNode->next;
- currNode->next = node;
- list_c->length++;
- if (pos == list_c->length)//如果是最后一个结点
- {
- node->next = list_c->next;//改变最后一个节点的指针域为指向第一个结点
- }
- }
- //delete node; //这里4行释放内存的语句加进去,也报错读取访问冲突。
- //node = NULL;
- //delete currNode;
- //currNode->next = NULL;
- }
- ///////////////////////////////////
- void Print(C_LinkList *list_c)
- {
- if (list_c->length == 0 || list_c->next == NULL)
- {
- cout << "链表为空,无内容可打印!" << endl;
- list_c->length = 0;
- return;
- }
- Node* node = new Node;
- node->next = NULL;
-
- node = list_c->next; //创建临时结点第一个结点开始遍历
-
- for (int i = 0; i < list_c->length; i++)
- {
- cout << node->data.id << ":" << node->data.name << endl;
- node = node->next;
- }
- //delete node; //添加delete就会报错,访问冲突。
- //node = NULL;
- cout << "*****打印结束*******" << endl;
- }
复制代码
--------main.cpp文件----------------------------
- #include<iostream>
- #include<string>
- #include"C_list.h"
- using namespace std;
- //测试数据
- EleType dataArray[]{
- {1,"香蕉"},
- {2,"樱桃"},
- {3,"菠萝"},
- {4,"苹果"}
- };
- //测试函数
- void test();
- int main()
- {
- test();
- return 0;
- }
- ////////////////
- void test()
- {
- C_LinkList* clist = new C_LinkList;
- clist->next = NULL;
- clist->length = 0;
-
- Init(clist, dataArray, 4);//初始化赋值
- Print(clist);//打印
- Insert(clist, 1, dataArray[0]);//插入
- Print(clist);//打印
- Insert(clist, 2, dataArray[1]);
- Print(clist);//打印
- delete clist;
- clist = NULL;
- }
复制代码
你的变量是临时的,可是链表里面的空间并不是.你把new出来的节点直接接插入链表,即该地址以后还会用,所以不能delete
|
|