我需要編寫一個返回鏈表大小的函數。這似乎很簡單,但是當我嘗試推進我的指針時,我得到了一個異常。 這是代碼:在鏈表中拋出異常
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;
}
這是定義鏈表代碼:
這是創建列表的代碼:
List *create_list(int n)
{
List *list = (List*) calloc(n,sizeof(List));
list->head = NULL;
return list;
}
最後,我已經使用這兩個函數將數據(數字)插入到列表中:(所以不要認爲列表是空的)。
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;
}
List_node *mk_node(int data, int n)
{
List_node *node = (List_node *)calloc(n, sizeof(List_node));
node->data = data;
return node;
}
使用斷點我發現問題出在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();
}
正如你所看到的,我使用插入功能插入()的數字。我希望它是好的。
功能相反的是:(我真的希望它的作品,我把它翻譯從僞代碼)
List reverse(List *list)
{
List_node *x, *y, *z;
if (list->head == NULL || list->head->next == NULL)
{
return *list;
}
x = list->head;
y = list->head->next;
z = list->head->next->next;
x->next = NULL;
while (z != NULL)
{
y->next = x;
x = y;
y = z;
z = z->next;
}
y->next = x;
list->head = y;
}
基本上,這是說,我有一個分配兩個2定向分類鏈表,我應該把它們合併成一個大的排序鏈表。
至於例外情況 - Lists.exe中0x00E21766處未處理的異常:0xC0000005:訪問衝突讀取位置0xCCCCCCD0。
編輯 - 在print_list功能:
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");
}
似乎你沒有設置'x-> next = NULL',因爲它需要設置爲NULL,以便列表在某個點結束。看看[LinkedListBasics](http://cslibrary.stanford.edu/103/LinkedListBasics.pdf)以供參考。 – Abhinav 2014-12-27 14:56:23
你究竟是如何在列表中插入值的? – 2014-12-27 15:04:08
「異常」 - 如果您指出哪一個可能會有所幫助。 – usr2564301 2014-12-27 16:01:13