鱼C论坛

 找回密码
 立即注册
查看: 241|回复: 1

[已解决]非空双链表L插入一个值为x

[复制链接]
发表于 2023-12-5 17:05:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

struct Node{
        int data;
        struct Node* prev;
        struct Node* next;
};

void insertBefore(struct Node* head,int i,int x)
{
        if(i<=0)
        {
                printf("插入位置无效\n");
                return;
        }
       
        struct Node* current=head;
        int currentPosition=0;
       
        while(current!=NULL&&currentPosition<i)
        {
                current=current->next;
                currentPosition++;
        }
       
        if(currentPosition!=i||current==NULL)
        {
                printf("插入位置无效\n");
                return;
        }
       
        struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
        newNode->data=x;
        newNode->prev=current->prev;
        newNode->next=current;
        current->prev->next=newNode;
        current->prev=newNode;
       
}

int main()
{
        struct Node* head=(struct Node*)malloc(sizeof(struct Node));
        head->prev=NULL;
        head->next=NULL;
       
        int values[]={10,20,30};
        for(int i=3;i>=0;i--)
        {
                struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
                newNode->data=values[i];
                newNode->prev=head;
                newNode->next=head->next;
                if(head->next!=NULL)
                {
                        head->next->prev=newNode;
                }
                head->next=newNode;
        }
        insertBefore(head,2,15);
       
        struct Node* current=head->next;
        while(current!=NULL)
        {
                printf("%3d",current->data);
                current=current->next;
        }
       
        current=head->next;
        while(current!=NULL)
        {
                struct Node* temp=current;
                current=current->next;
                free(temp);
        }
        free(head);
       
        return 0;
}




为什么输出结果是10 15 20 30 0,0是从哪来的?
最佳答案
2023-12-5 17:05:18
问题出在循环中的`int values={10,20,30};`这行代码上。它应该被修改为`int values[] = {10, 20, 30};`,以便正确定义一个整数数组。

当您使用`values`数组来初始化新节点的数据时,您需要使用索引`i`来访问数组元素。也就是说, `newNode->data = values;`。

此外,在循环终止条件中的变量`i`应该大于等于0,即改为`for(int i=2;i>=0;i--)`。

当所有这些修改完成后,您应该得到正确的输出结果为10 15 20 30。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-12-5 17:05:18 | 显示全部楼层    本楼为最佳答案   
问题出在循环中的`int values={10,20,30};`这行代码上。它应该被修改为`int values[] = {10, 20, 30};`,以便正确定义一个整数数组。

当您使用`values`数组来初始化新节点的数据时,您需要使用索引`i`来访问数组元素。也就是说, `newNode->data = values;`。

此外,在循环终止条件中的变量`i`应该大于等于0,即改为`for(int i=2;i>=0;i--)`。

当所有这些修改完成后,您应该得到正确的输出结果为10 15 20 30。

以上回复来自 -- ChatGPT(FishC官方接口),如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 20:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表