|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 菜鸟小乔 于 2019-5-31 21:53 编辑
循环链表,目前只写了插入函数,调用时出问题。
初始化一个空循环环链表,调用插入函数,插入第一个结点。
编译可以通过,运行时异常中断。
异常中断报错:
0x00007FFF74A8135A (vcruntime140d.dll)处(位于 ch2_3.exe 中)引发的异常: 0xC0000005: 读取位置 0xFFFFFFFFFFFFFFFF 时发生访问冲突。
中断同时弹出一个xstring文件。
.h文件--------------------------------
- #ifndef CircularLinkList_h_include
- #define CircularLinkList_h_include
- /* 《循环链表》
- */
- #include<iostream>
- using namespace std;
- //定义一种数据类型
- 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 Print(C_LinkList* list_c);
- #endif
复制代码
------.cpp文件----------------
- #include<iostream>
- #include"C_list.h"
- using namespace std;
- //初始化
- void Init(C_LinkList* list_c, EleType* dataArray, int len)
- {
-
- }
- //插入元素
- void Insert(C_LinkList* list_c, int pos, EleType element)
- {
- //定义空节点
- Node* node = (Node*)malloc(sizeof(struct Node));
- node->data = element; //如果注释掉这一行就可以运行,但是就没办法给data赋值了!!!
- 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 = 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;//改变最后一个节点的指针域为指向第一个结点
- }
- }
- }
- ///////////////////////////////////
- void Print(C_LinkList *list_c)
- {
- if (list_c->length == 0 || list_c->next == NULL)
- {
- cout << "链表为空,无内容可打印!" << endl;
- list_c->length = 0;
- return;
- }
- //链表不为空时打印:
- Node* node = list_c->next; //创建临时结点为第一个结点开始遍历
-
- for (int i = 0; i < list_c->length; i++)
- {
- cout << node->data.id << ":" << node->data.name << endl;
- node = node->next;
- }
-
- }
复制代码
--------main函数--------
- #include<iostream>
- #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 = (C_LinkList*)malloc(sizeof(C_LinkList));//初始化空的循环链表
- clist->next = NULL;
- clist->length = 0;
- Insert(clist, 1, dataArray[2]);//在第一个位置插入元素
- Print(clist);//打印
- free(clist);
- }
复制代码
malloc没有调用string的构造函数,EleType中的name这个string对象不存在,不能调用这个name对象的拷贝构造函数
有两个修改方案
1.用new替换malloc
2.在指定地址构造对象
我对于当前这个问题,不推荐第二种解决方案
- /* 《循环链表》
- */
- #include<iostream>
- #include <string>
- using namespace std;
- //定义一种数据类型
- 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 Print(C_LinkList* list_c);
- //初始化
- void Init(C_LinkList* list_c, EleType* dataArray, int len)
- {
- }
- //插入元素
- void Insert(C_LinkList* list_c, int pos, EleType element)
- {
- //定义空节点
- //Node* node = (Node*)malloc(sizeof(struct Node));
- //node->data = element; //如果注释掉这一行就可以运行,但是就没办法给data赋值了!!!
- //node->next = NULL;
-
-
- Node* node = new Node;
- node->data = element; //如果注释掉这一行就可以运行,但是就没办法给data赋值了!!!
- 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 = 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;//改变最后一个节点的指针域为指向第一个结点
- }
- }
- }
- ///////////////////////////////////
- void Print(C_LinkList *list_c)
- {
- if(list_c->length == 0 || list_c->next == NULL)
- {
- cout << "链表为空,无内容可打印!" << endl;
- list_c->length = 0;
- return;
- }
- //链表不为空时打印:
- Node* node = list_c->next; //创建临时结点为第一个结点开始遍历
- for(int i = 0; i < list_c->length; i++)
- {
- cout << node->data.id << ":" << node->data.name << endl;
- node = node->next;
- }
- }
- //测试数据
- EleType dataArray[]{
- {1, "香蕉"},
- {2, "樱桃"},
- {3, "菠萝"},
- {4, "苹果"}
- };
- //测试函数
- void test();
- int main()
- {
- test();
- return 0;
- }
- ////////////////
- void test()
- {
- C_LinkList* clist = (C_LinkList*)malloc(sizeof(C_LinkList));//初始化空的循环链表
- clist->next = NULL;
- clist->length = 0;
- Insert(clist, 1, dataArray[2]);//在第一个位置插入元素
- Print(clist);//打印
- free(clist);
- }
复制代码
|
|