c - 访问冲突阅读链接列表

标签 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++;
}

}

最佳答案

修复示例

#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;,
}

关于c - 访问冲突阅读链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22575739/

相关文章:

使用 Inline/C 从 Perl 调用外部 C 函数(在共享库中)不起作用

c - 二叉树 - 取消引用指针

objective-c - Obj-C 对象未在 C 回调函数中释放

C 用链表数组模拟引用传递

java - 队列不删除元素(使用链表)[java]

c - C中如何为链表的头部分配空间?

c - 终端C程序输入文件

c - 数字的奇数频率和

c - 使用循环将 Char 值添加到链接列表 [C]

C-将子链表传递给函数