C 链接列表中引发异常
我需要编写一个函数,返回链表的大小。这看起来很简单,但当我试图推进指针时,我遇到了一个例外。 代码如下:C 链接列表中引发异常,c,C,我需要编写一个函数,返回链表的大小。这看起来很简单,但当我试图推进指针时,我遇到了一个例外。 代码如下: int List_size(List *list) { List_node *p; int counter = 0; p = list->head; while (p != NULL) { p = p->next; counter = counter + 1; } return counte
int List_size(List *list)
{
List_node *p;
int counter = 0;
p = list->head;
while (p != NULL)
{
p = p->next;
counter = counter + 1;
}
return counter;
}
这是定义链接列表的代码:
typedef struct List_node
{
int data;
struct List_node *next;
struct List_node *prev;
}List_node;
typedef struct List
{
List_node *head;
}List;
这是创建列表的代码:
List *create_list(int n)
{
List *list = (List*) calloc(n,sizeof(List));
list->head = NULL;
return list;
}
最后,我使用这两个函数将数据(数字)插入到列表中:(因此不要认为列表是空的)
使用断点,我发现问题在于p=p->next,但我不明白为什么
多谢各位
编辑-我不认为完整的代码是必要的,但这里是:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
void merge(List *small_list, List *big_list)
{
List_node *x, *y;
y = small_list->head;
x = big_list->head;
t = x->prev;
// while (y < x) //If there are smaller numbers on the small list, insert them first
// {
// insert(y,t)
// t = t->next;
// y = y->next;
// }
while (y != NULL && x->next != NULL)
{
if (x <= y && x->next >= y)
{
insert(y, x);
y = y->next;
}
else
x = x->next;
}
while (y != NULL)
{
insert(y, x);
y = y->next;
}
}
void main()
{
List *L1, *L2, *big_list, *small_list;
int num1, num2;
int i;
int a, b;
create_list(5,&L1);
create_list(3,&L2);
printf("Insert first number into L1\n");
scanf("%d", &num1);
node = mk_node(0,5);
insert_first(node, &L1);
for (i = 0; i<5; i++)
{
printf("Now insert the rest of the numbers\n");
scanf("%d", &num1);
next = mk_node(&num1,5);
insert(next, node);
node = next;
}
printf("Insert first number into L2\n");
scanf("%d", &num2);
node = mk_node(0,3);
insert_first(node, &L2);
for (i = 0; i<3; i++)
{
printf("Now insert the rest of the numbers\n");
scanf("%d", &num2);
next = mk_node(&num2,3);
insert(next, node);
node = next;
}
//If lists are in descending order, reverse them to ascending order
//if (L1->head > L1->head->next)
// reverse(L1);
// if (L2->head > L2->head->next)
// reverse(L2);
int size_L1 = List_size(L1);
int size_L2 = List_size(L2);
if (size_L1 <= size_L2)
{
big_list = L2;
small_list = L1;
}
else
{
big_list = L1;
small_list = L2;
}
merge(small_list, big_list);
print_list(L3);
getchar(); getchar();
}
基本上,这是一个作业,它说我有两个双向排序的链表,我应该把它们合并成一个大的排序链表
对于Lists.exe中0x00E21766处的异常-未处理的异常:0xC0000005:访问冲突读取位置0xCCD0
编辑-打印列表功能:
void print_list(List *list)
{
List_node *p;
p = list->head;
printf("The linked list consists of: ");
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
问题在于您没有显示的代码,因为您给出的所有代码都很好(*) 我可以使用您的结构和函数:
- 创建一个列表
- 添加第一个节点(先插入_)
- 添加其他节点(带插入)
- 获取大小(使用列表大小)
- 显示每个节点的数据值
- 释放节点和列表
#include <stdio.h>
#include <malloc.h>
typedef struct List_node
{
int data;
struct List_node *next;
struct List_node *prev;
}List_node;
typedef struct List
{
List_node *head;
}List;
List *create_list()
{
List *list = (List*) malloc(sizeof(List));
list->head = NULL;
return list;
}
int List_size(List *list)
{
List_node *p;
int counter = 0;
p = list->head;
while (p != NULL)
{
p = p->next;
counter = counter + 1;
}
return counter;
}
void insert_first(List_node *x, List *list)
{
x->next = list->head;
list->head = x;
}
void insert(List_node *x, List_node *y)
{
x->next = y->next;
y->next = x;
}
/* my own additions */
List_node *mk_node(int data) {
List_node *node = (List_node *) malloc(sizeof(List_node));
node->data = data;
return node;
}
void delete_list(List *list) {
List_node *node = list->head;
while (node != NULL) {
List_node *next = node ->next;
free(node);
node = next;
}
free(list);
}
int main() {
List* list = create_list();
List_node *node, *next;
int i;
node = mk_node(0);
insert_first(node, list);
for (i=1; i<5; i++) {
next = mk_node(i);
insert(next, node);
node = next;
}
int n = List_size(list);
printf("size : %d\n", n);
node = list->head;
while (node != NULL) {
printf("node val : %d\n", node->data);
node = node->next;
}
delete_list(list);
return 0;
}
#包括
#包括
类型定义结构列表\u节点
{
int数据;
结构列表\节点*下一步;
结构列表\u节点*prev;
}列表节点;
类型定义结构列表
{
列表_节点*头;
}名单;
列表*创建_列表()
{
List*List=(List*)malloc(sizeof(List));
list->head=NULL;
退货清单;
}
整数列表大小(列表*列表)
{
列表_节点*p;
int计数器=0;
p=列表->标题;
while(p!=NULL)
{
p=p->next;
计数器=计数器+1;
}
返回计数器;
}
先无效插入(列表节点*x,列表*List)
{
x->next=列表->标题;
列表->头=x;
}
无效插入(列表节点*x,列表节点*y)
{
x->next=y->next;
y->next=x;
}
/*我自己的补充*/
列表节点*mk节点(整数数据){
列表节点*节点=(列表节点*)malloc(sizeof(列表节点));
节点->数据=数据;
返回节点;
}
作废删除列表(列表*列表){
列表\节点*节点=列表->标题;
while(节点!=NULL){
列表\节点*下一步=节点->下一步;
自由(节点);
节点=下一个;
}
免费(名单);
}
int main(){
List*List=create_List();
列出_节点*节点,*下一步;
int i;
节点=mk_节点(0);
首先插入_(节点、列表);
对于(i=1;i头;
while(节点!=NULL){
printf(“节点值:%d\n”,节点->数据);
节点=节点->下一步;
}
删除列表(列表);
返回0;
}
(*)您的代码并不完美,因为
List\u node
struct包含一个从未使用过的指针prev
,因此您以每三分之一未使用值的单个链接列表结束。问题在于未显示的代码,因为您给出的所有代码都很好(*)
我可以使用您的结构和函数:
- 创建一个列表
- 添加第一个节点(先插入_)
- 添加其他节点(带插入)
- 获取大小(使用列表大小)
- 显示每个节点的数据值
- 释放节点和列表
#include <stdio.h>
#include <malloc.h>
typedef struct List_node
{
int data;
struct List_node *next;
struct List_node *prev;
}List_node;
typedef struct List
{
List_node *head;
}List;
List *create_list()
{
List *list = (List*) malloc(sizeof(List));
list->head = NULL;
return list;
}
int List_size(List *list)
{
List_node *p;
int counter = 0;
p = list->head;
while (p != NULL)
{
p = p->next;
counter = counter + 1;
}
return counter;
}
void insert_first(List_node *x, List *list)
{
x->next = list->head;
list->head = x;
}
void insert(List_node *x, List_node *y)
{
x->next = y->next;
y->next = x;
}
/* my own additions */
List_node *mk_node(int data) {
List_node *node = (List_node *) malloc(sizeof(List_node));
node->data = data;
return node;
}
void delete_list(List *list) {
List_node *node = list->head;
while (node != NULL) {
List_node *next = node ->next;
free(node);
node = next;
}
free(list);
}
int main() {
List* list = create_list();
List_node *node, *next;
int i;
node = mk_node(0);
insert_first(node, list);
for (i=1; i<5; i++) {
next = mk_node(i);
insert(next, node);
node = next;
}
int n = List_size(list);
printf("size : %d\n", n);
node = list->head;
while (node != NULL) {
printf("node val : %d\n", node->data);
node = node->next;
}
delete_list(list);
return 0;
}
#包括
#包括
类型定义结构列表\u节点
{
int数据;
结构列表\节点*下一步;
结构列表\u节点*prev;
}列表节点;
类型定义结构列表
{
列表_节点*头;
}名单;
列表*创建_列表()
{
List*List=(List*)malloc(sizeof(List));
list->head=NULL;
退货清单;
}
整数列表大小(列表*列表)
{
列表_节点*p;
int计数器=0;
p=列表->标题;
while(p!=NULL)
{
p=p->next;
计数器=计数器+1;
}
返回计数器;
}
先无效插入(列表节点*x,列表*List)
{
x->next=列表->标题;
列表->头=x;
}
无效插入(列表节点*x,列表节点*y)
{
x->next=y->next;
y->next=x;
}
/*我自己的补充*/
列表节点*mk节点(整数数据){
列表节点*节点=(列表节点*)malloc(sizeof(列表节点));
节点->数据=数据;
返回节点;
}
作废删除列表(列表*列表){
列表\节点*节点=列表->标题;
while(节点!=NULL){
列表\节点*下一步=节点->下一步;
自由(节点);
节点=下一个;
}
免费(名单);
}
int main(){
List*List=create_List();
列出_节点*节点,*下一步;
int i;
节点=mk_节点(0);
首先插入_(节点、列表);
对于(i=1;i头;
while(节点!=NULL){
printf(“节点值:%d\n”,节点->数据);
节点=节点->下一步;
}
删除列表(列表);
返回0;
}
(*)您的代码并不完美,因为
List\u node
struct包含一个从未使用过的指针prev
,因此您以每三分之一未使用值的单个链表结束。所以问题是您分配列表头,但从不为列表元素分配内存。Serge添加了进行分配的mk\u node(所以Serge解决了它)。所以问题是您分配列表头,但从不为列表元素分配内存。Serge添加了执行分配的mk_节点(所以Serge解决了它).我在上面添加了一条评论,说我把你的代码复制到了我的编译器中,但它没有编译,所以它不会运行:对create\u list的调用没有编译:它们返回一个值作为函数返回,而不是在一个参数中;存在未声明的变量。此外,给create\u list和mk\u节点一个参数是没有用的:它们将分配一个数组,不创建单个列表元素。调用calloc(1,sizeof(your_thing))就足够了。修复这些EROR后,几个错误变得明显:
- 在insert_first(节点,&L2)中,您给它列表的地址,它已经是一个指针。删除“&”(编译器应该已经抱怨了!)
- 与列表大小(&L1)相同;L1已经是指针。请删除“&”
- 输入:如果(x数据…)
#include <stdio.h> #include <malloc.h> typedef struct List_node { int data; struct List_node *next; struct List_node *prev; }List_node; typedef struct List { List_node *head; }List; List *create_list() { List *list = (List*) malloc(sizeof(List)); list->head = NULL; return list; } int List_size(List *list) { List_node *p; int counter = 0; p = list->head; while (p != NULL) { p = p->next; counter = counter + 1; } return counter; } void insert_first(List_node *x, List *list) { x->next = list->head; list->head = x; } void insert(List_node *x, List_node *y) { x->next = y->next; y->next = x; } /* my own additions */ List_node *mk_node(int data) { List_node *node = (List_node *) malloc(sizeof(List_node)); node->data = data; return node; } void delete_list(List *list) { List_node *node = list->head; while (node != NULL) { List_node *next = node ->next; free(node); node = next; } free(list); } int main() { List* list = create_list(); List_node *node, *next; int i; node = mk_node(0); insert_first(node, list); for (i=1; i<5; i++) { next = mk_node(i); insert(next, node); node = next; } int n = List_size(list); printf("size : %d\n", n); node = list->head; while (node != NULL) { printf("node val : %d\n", node->data); node = node->next; } delete_list(list); return 0; }
List *merge(List *small_list, List *big_list) { List_node *x, *y, *r; List *result= create_list(); y = small_list->head; x = big_list->head; if (x->data < y->data) {result->head= x; x= x->next;} else {result->head= y; y= y->next;} r= result->head; while (y != NULL && x != NULL) { if (x->data < y->data) { r->next= x; x = x->next; } else { r->next= y; y = y->next; } r= r->next; } while (y != NULL) { r->next= y; y = y->next; r= r->next; } while (x != NULL) { r->next= x; x = x->next; r= r->next; } r->next= 0; return (result); }