Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在链表中插入开始中间和结束_C++_List_Reference_Undefined_Singly Linked List - Fatal编程技术网

C++ 在链表中插入开始中间和结束

C++ 在链表中插入开始中间和结束,c++,list,reference,undefined,singly-linked-list,C++,List,Reference,Undefined,Singly Linked List,我今天在练习链表,尽我最大的努力去理解它,所以我试着制作一个单链表,我可以在开始、中间和结尾添加,如果列表为空,它也会初始化为添加一个,然后打印结果 我已经使用函数来插入和显示输入或输出,但输出结果仍然为空,在打印列表时,我尝试更改 node*head=NULL; 但还是什么都没发生 void insert(node* head, int numb, int size, int pos) { node* temp = new node(); int counter; te

我今天在练习链表,尽我最大的努力去理解它,所以我试着制作一个单链表,我可以在开始、中间和结尾添加,如果列表为空,它也会初始化为添加一个,然后打印结果

我已经使用函数来插入和显示输入或输出,但输出结果仍然为空,在打印列表时,我尝试更改 node*head=NULL; 但还是什么都没发生

void insert(node* head, int numb, int size, int pos)
{
    node* temp = new node();
    int counter;
    temp->number = numb;
    if (head == NULL) {
        head = temp;
    }
    else {
        int counter = 0;
        node* current = head;
        node* trail = NULL;
        while (counter++) {
            if (counter == pos) {
                temp->next = current;
                trail->next = temp;
                break;
            }
            else {
                trail = current;
                current = current->next;
                continue;
            }
        }
    }
    size++;
}
void printlist(node* head)
{
    while (head != NULL) {
        cout << " " << head->number;
        head = head->next;
    }
}
int main()
{
    node* head = NULL;
    int numb, size = 0, pos;
    numb = 5;
    pos = 0;
    insert(head, numb, size, pos);
    printlist(head);
    numb = 6;
    pos = 2;
    insert(head, numb, size, pos);
    printlist(head);
}
void插入(节点*头部、整数编号、整数大小、整数位置)
{
node*temp=新节点();
整数计数器;
温度->数字=麻木;
if(head==NULL){
压头=温度;
}
否则{
int计数器=0;
节点*电流=头部;
节点*trail=NULL;
while(计数器++){
如果(计数器==位置){
温度->下一步=当前;
trail->next=temp;
打破
}
否则{
trail=电流;
当前=当前->下一步;
继续;
}
}
}
大小++;
}
无效打印列表(节点*头)
{
while(head!=NULL){
下一步;
}
}
int main()
{
node*head=NULL;
int numb,大小=0,位置;
麻木=5;
pos=0;
插入(头部、麻木、大小、位置);
印刷品清单(标题);
麻木=6;
pos=2;
插入(头部、麻木、大小、位置);
印刷品清单(标题);
}

我希望第一个的输出是5,第二个是5 6。

insert(node*head
中传递的指针只是
main
中指针的副本。对该指针的任何修改(例如
head=temp
)都不会反映在
main

您需要传递指向指针的指针或指向指针的引用,例如:

void insert(node*& head, int numb, int size, int pos)

函数无效,通常具有未定义的行为。例如,函数中的此语句

    size++;
没有意义,因为参数
size
没有引用类型。即函数处理其参数的副本。作为参数传递给函数的对象
size
将保持不变。因此,main中的变量
size
将始终等于0

还是在循环中

    node* trail = NULL;
    while (counter++) {
        if (counter == pos) {
            temp->next = current;
            trail->next = temp;
        //...
对于等于1的位置,节点轨迹等于NULL,因此此语句

            trail->next = temp;
具有未定义的行为

此外,main中的head节点不会被函数更改,因为它是通过值传递给函数的。也就是说,函数处理head节点的副本

将变量
size
pos
定义为有符号整数类型是个坏主意。在这种情况下,必须在函数中检查参数
pos
的值是否大于或等于0

该功能可定义为如下演示程序所示

#include <iostream>

struct node
{
    int number;
    node *next;
};

void insert( node * &head, int number, size_t &size, size_t pos )
{
    node **current = &head;

    while ( pos != 0 && *current != nullptr )
    {
        --pos;
        current = &( *current )->next;
    }

    *current = new node { number, *current };

    ++size;
}

std::ostream & printlist( const node* head, std::ostream &os = std::cout )
{
    for ( const node *current = head; current != nullptr; current = current->next )
    {
        os << current->number << ' ';
    }

    return os;
}

int main() 
{
    node *head = nullptr;
    size_t size = 0;
    size_t pos;
    int numb;

    numb = 5;
    pos = 0;

    insert( head, numb, size, pos );

    printlist(head) << '\n';

    numb = 6;
    pos = 2;

    insert( head, numb, size, pos );

    printlist(head) << '\n';    

    numb = 4;
    pos = 0;

    insert( head, numb, size, pos );

    printlist(head) << '\n';    

    numb = 10;
    pos = 2;

    insert( head, numb, size, pos );

    printlist(head) << '\n';    

    std::cout << "There are " << size << " nodes in the list.\n";

    return 0;
}
请注意,您还需要编写一个函数来释放列表的所有分配内存

5 
5 6 
4 5 6 
4 5 10 6 
There are 4 nodes in the list.