此链接列表插入代码中是否存在任何逻辑错误。请帮助 [英] Is there any logical error in this linked list insertion code.please help

查看:72
本文介绍了此链接列表插入代码中是否存在任何逻辑错误。请帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

typedef struct Node
{
    int info;
    struct Node *next;
} node;

void insert(node *ptr,int val)
{
    if(ptr->next!=NULL)
    ptr=ptr->next;
    ptr->next=(node *)malloc(sizeof(node));
    ptr=ptr->next;
    ptr->info=val;
    ptr->next=NULL;
}

int main()
{
     node *start;
     start=(node*)malloc(sizeof( node));
     start->next=NULL;
     int i,elem;
     printf("Enter 1 if u want to insert data");
     scanf("%d",&i);
     if(i==1)
     {
            printf("\nEnter the element of linked list\n");
            scanf("%d",elem);
            insert(start,elem);
     }
}

推荐答案

嗯。

你看过代码了吗?什么?或者尝试在调试器中运行它?



假设你的插入操作应该在参数节点和列表中的下一个节点之间插入一个新节点



开始:

Um.
Have you looked at the code at all? Or tried running it in the debugger?

Assuming your insert operation should insert a new node between the parameter node and the next node in the list

Start:
A   ->   B  -> C

你按照预期传递B:

And you pass "B" in the you expect:

A   ->   B  -> NEW  -> C

之后。

那么,为什么你要做的第一件事就是扔掉你想要插入的节点的地址?

你需要将NEW节点的Next指针设置为要插入的节点的Next指针的当前值,然后将该节点的Next点设置为新节点。

所以,也许这可能会更好:

afterwards.
So, why is the first thing you do to throw away the address of the node you want to insert after?
You need to set the "Next" pointer of the NEW node to the current value of the "Next" pointer of the node you want to insert after, then set the "Next" point of that node to the new node.
So, perhaps this might work better:

node *newNode = (node *)malloc(sizeof(node));
newNode->info=val;
newNode->next = ptr->next;
ptr->next=newNode;

如果你检查null,那么你真的想检查 ptr 而不是下一个值。

If you check for null, then you really want to check ptr not the next value.


是的。并且不清楚你要做什么(你想在哪里插入新节点?)。这里有三个选项:

Yes there is. And it''s is unclear what you are trying to do (where are you trying to insert the new node?). Here are three options:
// If you are trying to insert a new node at the end of the linked list:
// e.g. you have [start][node a][node b]
// ptr = any node in the list; insert node after node b to give:
// [start][node a][node b][node c]
void insert(node* ptr, int val)
{
    while (ptr->next != NULL) // Loop to find last node in the list
    {
        ptr = ptr->next;
    }
    ptr->next = (node*)malloc(sizeof(node)); // create new node (node c) as next node in list
    ptr = ptr->next; // move our current pointer to point to the new node (node c)
    ptr->info = val; // Set new node's value
    ptr->next = NULL; // Set new node's next pointer to NULL (end of list)
}

// If you are trying to insert a new node after the current node pointed to by ptr
// e.g. you have [start][node a][node b]
// ptr = start; insert node where node a is, to give
// [start][node c][node a][node b]
void insert(node* ptr, int val)
{
    node* next = ptr->next;  // store value of next node (node a in this example)
    ptr->next = (node*)malloc(sizeof(node)); // create new node (node c)
    ptr = ptr->next; // move our current pointer to point to the new node (node c)
    ptr->info = val; // Set new node's value
    ptr->next = next; // Set new node to point to next node (node a)
}

// If want to insert a node "before" ptr:
// e.g. you have [start][node a][node b]
// ptr = start; insert node where 'start' is, to give
// [node c][start][node a][node b]
// you need to be slightly more careful, because as you can see,
// "start" is pointing to the same location, and the "start" pointer
// might not even be the value passed, so you can not assume to be able to
// reset it.  My suggestion here would be to copy the contents of "ptr"
// into the new node, and then sent the current ptr's values accordingly:
void insert(node* ptr,int val)
{
    node* newNode = (node *)malloc(sizeof(node)); // Create the new node
    newNode->next = ptr->next;  // Set current node's value and next pointer into new node
    newNode->info = ptr->val;
    ptr->val = val;  // Set current node's value as the one given
    ptr->next = newNode;  // Set current node's next node as the one we've just created.
}





您还应该检查传入的ptr是否为空,如果是,则可能抛出异常。



问候,

Ian。



You ought really to also check that "ptr" passed in is not null, and probably throw an exception if it is.

Regards,
Ian.


这篇关于此链接列表插入代码中是否存在任何逻辑错误。请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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