访问违规阅读链表

时间:2014-03-22 09:24:23

标签: c linked-list

大家好,我在这个功能中遇到一个小错误,但是我无法弄清楚这是什么问题,我得到了访问违规读取位置。我已经实现了一个使用此方法的功能,但它正在工作。我能知道为什么会这样吗?提前谢谢。

这是我的int main()代码:

typedef struct node{
int item;
struct node *next;
} ListNode;

void spilt(ListNode *head, ListNode **ptrEvenList, ListNode **ptrOddList);
int main()
{
int i = 0, j = 0;
ListNode *head = NULL, *temp = NULL, *even = NULL, *odd = NULL;

printf("Enter a value:");
scanf("%d", &i);
while (i != -1)
{
    if (head == NULL)
    {
        head = malloc(sizeof(ListNode));
        temp = head;
    }
    else
    {
        temp->next = malloc(sizeof(ListNode));
        temp = temp->next;

    }
    temp->item = i;
    printf("Enter a value:");
    scanf("%d", &i);
}

spilt(head, even, odd);




    getch();
    return 0;


}



void spilt(ListNode *head, ListNode **ptrEvenList, ListNode **ptrOddList)
{
int i = 0;
ListNode *even=NULL, *odd=NULL,*temp1=NULL,*temp2=NULL;
ListNode *test = head;

odd = malloc(sizeof(ListNode));
even = malloc(sizeof(ListNode));
temp1 = even;
temp2 = odd;


while (test != NULL)
{


    if (i % 2 == 0)
    {
        temp1->next = malloc(sizeof(ListNode));
        temp1 = temp1->next;
        temp1->item = head->item;

    }

    else if (i%2==1)
    {
        temp2->next = malloc(sizeof(ListNode));
        temp2 = temp2->next;
        temp2->item = head->item;
    }

    test = test->next; // error (access violation reading location)
    i++;



}

}

以下工作职能:

int search(ListNode *head, int value)
{
int i = 0;
ListNode *node = head;
if (head == NULL)
    return -1;

while (node != NULL)
{

    if (node->item == value)
        return i;
    node = node->next;
    i++;
}

}

1 个答案:

答案 0 :(得分:1)

修复示例

#include <stdio.h>
#include <stdlib.h>

typedef struct node{
    int item;
    struct node *next;
} ListNode;

void spilt(ListNode *head, ListNode **ptrEvenList, ListNode **ptrOddList);

void print(ListNode *np){
    while(np){
        printf("%d ", np->item);
        np = np->next;
    }
    printf("\n");
}

int main(void){
    int i = 0, j = 0;
    ListNode *head = NULL, *temp = NULL, *even = NULL, *odd = NULL;

    printf("Enter a value:");
    scanf("%d", &i);
    while (i != -1){
        if (head == NULL){
            head = malloc(sizeof(ListNode));
            temp = head;
        } else {
            temp->next = malloc(sizeof(ListNode));
            temp = temp->next;
        }
        temp->item = i;
        temp->next = NULL;
        printf("Enter a value:");
        scanf("%d", &i);
    }
    printf("head:");print(head);
    spilt(head, &even, &odd);
    printf("even:");print(even);
    printf("odd:");print(odd);
    getch();
    return 0;
}

void spilt(ListNode *head, ListNode **ptrEvenList, ListNode **ptrOddList){
    int i = 0;
    ListNode *even=NULL, *odd=NULL, *temp1=NULL, *temp2=NULL;
    ListNode *test = head;

    while (test != NULL){
        if (i % 2 == 0){
            if (even == NULL){
                even = malloc(sizeof(ListNode));
                temp1 = even;
            } else {
                temp1->next = malloc(sizeof(ListNode));
                temp1 = temp1->next;
            }
            temp1->item = test->item;
            temp1->next = NULL;
        } else {//if (i%2==1)
            if (odd == NULL){
                odd = malloc(sizeof(ListNode));
                temp2 = odd;
            } else {
                temp2->next = malloc(sizeof(ListNode));
                temp2 = temp2->next;
            }
            temp2->item = test->item;
            temp2->next = NULL;
        }
        test = test->next;
        ++i;
    }
    *ptrEvenList = even;
    *ptrOddList = odd;,
}