代码之家  ›  专栏  ›  技术社区  ›  Amarth Gûl

在C中,链表值仅在函数内部改变

  •  0
  • Amarth Gûl  · 技术社区  · 4 年前

    我试图用C语言实现一个链表:

    struct Node{
        struct Node *next;
        void *data;
    };
    

    具有插入功能:

    void insert(void *p2Node, void *data)
    {
        struct Node *newNode;
        struct Node **p2p2Node= (struct Node **)p2Node;
        
        if (newNode = malloc(sizeof(struct Node))) /* if successfully allocated */
        {
            newNode->data = data;
            
            if ((*p2p2Node) != NULL) /* if the list is not empty */
            {
                newNode->next =  (*p2p2Node)->next;
                (*p2p2Node)->next = newNode;
            }
            else
                (*p2p2Node) = newNode;
    
            p2Node = p2p2Node;
        }
        printf("Inside the insert: %s\n", (*p2p2Node)->data);
    }
    

    我打电话进来了 main() :

    int main()
    {
        char *setA = "liquid ";
        char *setB = " lgd";
        char *setC = "sample";  
        struct Node *nList = malloc(sizeof(struct Node));
    
        insert(nList, setC);
        printf("2Get %s\n", nList->data);
        
        return 0;
    }
    

    未报告任何错误或警告,但 该值仅在 insert .回到 main() 链接列表仍然为空。

    我不明白: nList 在里面 main() 是一个空指针。在…内 insert() , *p2Node 没有改变,我用 p2p2Node 要更改值 p2Node 指向,为什么它不起作用?我的指针错配了吗?有没有一种方法可以在不修改 插入() ?

    非常感谢。

    0 回复  |  直到 4 年前
        1
  •  1
  •   Malithya Fernando    4 年前

    使用此代码将值插入链表。

    struct node{
        int data;
        struct node* link;
    };
    
    struct node *root = NULL;
    int len;
    
    int main()
    {
        append();
        display();
    
        addatbegin();
        display();
    
        addatafter();
        display();
    }
    

    将值添加到列表的末尾。

    void append(){
        struct node* temp;
        temp = (struct node*)malloc(sizeof(struct node));
        printf("Enter the data: ");
        scanf("%d", &temp->data);
        temp->link = NULL;
        if(root == NULL) //list is empty
        {
            root=temp;
        }else
        {
            struct node* p;
            p=root;
            while(p->link != NULL)
            {
                p = p->link;
            }
            p->link = temp;
        }
    }
    

    将值添加到列表的开头。

    void addatbegin()
    {
        struct node* temp;
        temp = (struct node*)malloc(sizeof(struct node));
        printf("Enter the data : ");
        scanf("%d", &temp->data);
        temp->link = NULL;
        if(root == NULL)
        {
            temp = root;
        }
        else
        {
            temp->link = root;
            root = temp;
        }
    }
    

    在节点后添加值

    void addatafter()
    {
        struct node* temp, *p;
        int loc, i=1;
        printf("Enter the location : ");
        scanf("%d", &loc);
        if(loc > len)
        {
            printf("Invalid input.");
        }
        else
        {
            p = root;
            while(i > loc)
            {
                p = p->link;
                i++;
            }
            temp = (struct node*)malloc(sizeof(struct node));
            printf("Enter the data : ");
            scanf("%d", &temp->data);
            temp->link = NULL;
            temp->link = p->link;
            p->link = temp;
        }   
    }
    

    显示链接列表的步骤

    void display(){
        struct node* temp;
        temp = root;
        if(temp == NULL)
        {
            printf("List id empty.\n");
        }
        else
        {
            while (temp != NULL){
                printf("%d -> ", temp->data);
                temp = temp->link;
            }
            printf("\n\n");
        }
    }