在链接列表的开头插入节点

问题描述

因此,我一直在尝试使用函数在2个链接列表上编写各种操作的代码,并且在正确创建列表的同时,正确地在所有位置进行插入和删除,但是插入似乎是在头条件下进行的。不起作用。

我尝试使用此功能创建LL:

void Create(struct node **head) {
    if (No_of_List==2)
    {
        cout<<"\nCan't create more lists\n";
        Operations();
    }
    int n;
    cout<<"Create a linked list.\n\nEnter the number of elements in the Linked List: ";
    cin>>n;
    if (n!=0)
    {
        No_of_List++;
    }
    LIST_SIZE=n;
    while(n--)
    {
        cout<<"Enter the value to be inserted: ";
        node *new_node= new node;
        cin>>new_node->data;
        new_node->next=0;

        if(Empty_List(*head)==1)
            *head=new_node;
        else
        {
            temp=*head;
            while (temp->next!=NULL)
                temp=temp->next;
            temp->next=new_node;
        }
    }
    Operations();
}

我的插入代码是:

node *new_node = new node;
cout<<"Enter the value to be inserted: ";
cin>>new_node->data;
new_node->next=NULL;
int index;
cout<<"Enter the index: ";
cin>>index;
        
if (index==0)                               // CONDITION NOT WORKING
{
    new_node->next=head;
    head=new_node;
    cout<<"Value inserted successfully.\n";
    LIST_SIZE++;
    goto l;
    return;
}
        
if (index>LIST_SIZE)
{
    cout<<"Invalid index.";
    goto l;
    return;
}
        
temp=head;
temp1=NULL;
if (Empty_List(head)==1)
{
    cout<<"\nLinked List empty.\n";
}
    
for (int i=0;i<index;i++)
{
    temp1=temp;
    temp=temp->next;
}
temp1->next=new_node;
new_node->next=temp;
cout<<"Value inserted successfully.\n";
LIST_SIZE++;
}

但是我的index = 0条件没有在开头插入任何值,而另一个值插入在其他索引位置。请帮助

**注意:** 这只是一个代码段。您可以要求完整的代码来查找其他任何错误,但这是一个很长的错误,因此我没有在此处插入 https://onlinegdb.com/r1UzIfzPD这是完整的代码

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...