C - 链表插入



我想创建 5 个节点并在第 3 个和第 4 个节点之间插入一个新的节点。这是代码..当我运行它时,我没有收到错误或输出。我做错了什么?提前谢谢。

#include <stdio.h>
#include <stdlib.h>
typedef struct node{
                int data;
                struct node *next;
}node;

int main(void) {
                struct node *root, *newElement, *temp;
                // first node is created.
                root = (node*)malloc(sizeof(node));
                root->data = 11;
                // to create 4 nodes more after the first one is created.
                int i;
                for (i=0; i <= 3; i++){
                               root->next = (node*)malloc(sizeof(node));
                               root = root->next;
                               root->data = (i * 10);
                }
                // new node to insert.
                newElement = (node*)malloc(sizeof(node));
                newElement-> data = 1;

                temp = (node*)malloc(sizeof(node));
                temp = root->next->next;
                root->next -> next = newElement;
                newElement->next = temp;

                // print the values 
                int j;
                for(j=0; j<=4; j++){
                    root = root->next;
                    printf("%dn", root->data);
                }

                return 0;
}
root = temp = (node*)malloc(sizeof(node));//no need cast from void*
temp->data = 11;
int i;
for (i=0; i <= 3; i++){
    //don't change root
    temp->next = (node*)malloc(sizeof(node));
    temp = temp->next;
    temp->data = (i * 10);
}
temp->next = NULL;//next of last node need set to NULL 
newElement = (node*)malloc(sizeof(node));
newElement-> data = 1;
//temp = (node*)malloc(sizeof(node));//no need (this make memory leak)
temp = root->next->next;
root->next->next = newElement;
newElement->next = temp;

for(temp = root; temp; temp = temp->next){
    printf("%dn", temp->data);
}

创建 4 个新节点时,在列表下游重新分配root指针,并松散对上游元素的访问权限。 Intead,在循环之前设置temp = root,然后在循环内使用 temp

            temp = root;
            int i;
            for (i=0; i <= 3; i++){
                           temp->next = (node*)malloc(sizeof(node));
                           temp = temp->next;
                           temp->data = (i * 10);
                           temp->next = NULL;
            }

你在创建节点时正在修改root,你应该创建某种从根开始并被修改的迭代器,这样你就不会丢失初始位置。

请尝试以下操作:

#include <stdio.h>
#include <stdlib.h>
typedef struct node{
    int data;
    struct node *next;
}node;

int main(void) {
    struct node *root, *newElement, *temp, *iterator;
    // first node is created.
    root = (node*)malloc(sizeof(node));
    iterator = root;
    iterator->data = 11;
    // to create 4 nodes more after the first one is created.
    int i;
    for (i=0; i <= 3; i++){
       iterator->next = (node*)malloc(sizeof(node));
       iterator = iterator->next;
       iterator->data = (i * 10);
    }
    // new node to insert.
    newElement = (node*)malloc(sizeof(node));
    newElement-> data = 1;
    temp = (node*)malloc(sizeof(node));
    iterator = root;
    temp = iterator->next->next;
    iterator->next->next = newElement;
    newElement->next = temp;
    iterator = root;
    // print the values 
    int j;
    for(j=0; j<=4; j++){
        printf("%dn", root->data);
        iterator = iterator->next;
    }
    return 0;
}

相关内容

  • 没有找到相关文章

最新更新