C中的简单链接列表:内存访问错误 [英] Simple Linked List in C : memory Access Error

查看:64
本文介绍了C中的简单链接列表:内存访问错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "List.h"

typedef int element;                                
typedef struct _ListNode //form of node
{
    element data;                                       
    struct ListNode *link;
}   ListNode;


ListNode *header = NULL; //make header in global variable
int num_node = 0; //counting number of nodes

int AppendNode(const ListNode item);
void DisplayItem(void); 
int InsertNode(const int pos, const ListNode item);


int AppendNode(const ListNode item)                     
{
    ListNode *current, *new_item = NULL;
    new_item = (ListNode *)malloc(sizeof(ListNode));

    if (new_item == NULL)   return 0;

    new_item->data = item.data;                     
    new_item->link = NULL;                              

    if (header == NULL)                         
        header = new_item;                      
    else{                                   
        current = header;                       
        while (current->link != NULL)                   
        {
            current = current->link;                    
            current->link = new_item;                   
        }
    }
    num_node++;                                         
    return 1;                                           
}


void DisplayItem(void) //print all the nodes
{
    ListNode *current = NULL;
    current = header;   
    while (current != NULL)
    {
        printf("%d\n", current->data); //MEMORY ACCESS PROBLEM
        current = current->link;
    }
}

int InsertNode(const int position, const ListNode item)
{
    ListNode *current = NULL;
    ListNode *new_item = NULL;
    new_item = (ListNode *)malloc(sizeof(ListNode));

    if (new_item == NULL)   return 0;

    if (position == 1)
    {
        new_item->link = header;
        header = new_item;
        num_node++;
    }
    else if((1 < position) && (position < num_node))
    {
        int current_position = 0;       
        current = header;           
        while (current_position != (position - 1))
        {
            current = current->link;
            current_position++;
        }
        new_item->link = current->link;
        current->link = new_item;
        num_node++;
    }
    else return 0;                                  
}

int main(void)
{
    ListNode node1; node1.data = 10;    
    DisplayItem();

    getchar();
    return 0;
}

我想制作一个简单的链表程序。但由于内存访问问题而无法使用。
另外,我又增加了两个节点,但是除了节点1之外没有追加其他节点。
我想使用引用呼叫。并且所有函数都应使结果为0、1。

I wanted to make simple linked list program. but it doesn't work because of Memory Access Problem. Also, I make two more nodes, but it doesn't be appended except node 1. I wanted to use Call-by-Reference. and all function should be make result 0, 1.

0 - fail.
1 - success.

看起来不舒服,但是...我该怎么做?

It looks like uncomfortable but... How could I make this work?

推荐答案

请在下面用我的注释查看您的简单错误,并在下面尝试进行更正:-

Please see your simple mistake below with my comment and try with my correction below:-

typedef struct _ListNode //form of node
{
    element data;                                       
    struct ListNode *link;/* here it should be _ListNode */
}   ListNode;

现在看看您的以下方法在哪里出错了

now see your below method where you did the mistake

int AppendNode(const ListNode item)                     
{
    ListNode *current, *new_item = NULL;
    new_item = (ListNode *)malloc(sizeof(ListNode));

    if (new_item == NULL)   return 0;

    new_item->data = item.data;                     
    new_item->link = NULL;                              

    if (header == NULL)                         
        header = new_item;                      
    else{                                   
        current = header;                       
        while (current->link != NULL) 
        /* here in the above while statement current->link always NULL because it is pointing to the first node and first->next == NULL. Hence this while loop will never execute and link list will not be created because you have assign the new node inside this while loop. Move the code out side of this loop */               
        {
            current = current->link;                    
            current->link = new_item;                   
        }
    }

正确的while循环应该是

correct while loop should be

       while (current->link != NULL) 
        {
            current = current->link;                                  
        }
          current->link = new_item; /* this line of code should be out side of while loop */
    num_node++;                                         
    return 1;                                           
 }

Just correct the while loop like above in function AppendNode as well.
Now it will work fine

这篇关于C中的简单链接列表:内存访问错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆