C++列表功能不损坏

发布于 2025-01-22 14:50:04 字数 829 浏览 2 评论 0原文

我正在尝试创建一个程序,让我在列表的尾部添加元素,然后打印它们。这不会给我一个错误,但他什么也没做。我在做什么错?

#include<iostream>
using namespace std;
 
struct lista{
    int val;
    lista *next;
};

typedef lista* ptr_lista;

void tail_add(ptr_lista head, int valore){
    if(head=NULL){
         head=new lista;
         head->val=valore;
         head->next=NULL;
    } else {
        ptr_lista p=head;
        while(p->next!=NULL){
                p=p->next;
        }
        p->next=new lista;
        p->next->val=valore;
        p->next->next=NULL;
    }
}

void print(ptr_lista p){
     while(p!=NULL){
            cout<<p->val<< " ";
            p=p->next;
      }
}

int main(){
    ptr_lista m;
    tail_add(m,5);
    tail_add(m,6);
    print(m);
}

I'm tryin to create a program that let me add elements in tail of a list and then prints them. It doesn't give me an error but he doesn't do anything. What am I doing wrong ?

#include<iostream>
using namespace std;
 
struct lista{
    int val;
    lista *next;
};

typedef lista* ptr_lista;

void tail_add(ptr_lista head, int valore){
    if(head=NULL){
         head=new lista;
         head->val=valore;
         head->next=NULL;
    } else {
        ptr_lista p=head;
        while(p->next!=NULL){
                p=p->next;
        }
        p->next=new lista;
        p->next->val=valore;
        p->next->next=NULL;
    }
}

void print(ptr_lista p){
     while(p!=NULL){
            cout<<p->val<< " ";
            p=p->next;
      }
}

int main(){
    ptr_lista m;
    tail_add(m,5);
    tail_add(m,6);
    print(m);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦亿 2025-01-29 14:50:04

对于初学者,指针m尚未初始化,并且具有不确定的值,

ptr_lista m;

您需要初始化其

ptr_lista m = nullptr;

函数通过值接受指针,

void tail_add(ptr_lista head, int valore){

因此在功能中更改参数head

 head=new lista;

对原始指针m在MAIN中声明的效果。

您需要将参数声明为对指针的引用,

void tail_add(ptr_lista &head, int valore){

定义函数,以下方式

void tail_add( ptr_lista &head, int valore )
{
    ptr_lista new_lista_ptr = new lista { valore, nullptr };

    if ( head == nullptr )
    {
         head = new_lista_ptr;
    } 
    else 
    {
        ptr_lista p = head;

        while ( p->next ) p = p->next;

        p->next = new_lista_ptr;
    }
}

引入别名不是一个好

typedef lista* ptr_lista;

注意

const ptr_lista

主意

lista * const

可以通过以下方式

const lista *

,要为指针 声明函数参数的声明print,因为它不会更改列表的节点。

For starters the pointer m is not initialized and has an indeterminate value

ptr_lista m;

You need to initialize it

ptr_lista m = nullptr;

The function accepts the pointer by value

void tail_add(ptr_lista head, int valore){

So changing the parameter head within the function like

 head=new lista;

has no effect on the original pointer m declared in main.

You need to declare the parameter as a reference to the pointer

void tail_add(ptr_lista &head, int valore){

The function can be defined the following way

void tail_add( ptr_lista &head, int valore )
{
    ptr_lista new_lista_ptr = new lista { valore, nullptr };

    if ( head == nullptr )
    {
         head = new_lista_ptr;
    } 
    else 
    {
        ptr_lista p = head;

        while ( p->next ) p = p->next;

        p->next = new_lista_ptr;
    }
}

Pay attention to that it is not a good idea to introduce an alias for a pointer like

typedef lista* ptr_lista;

For example if you will write

const ptr_lista

then it means

lista * const

not

const lista *

that is required for the declaration of the parameter of the function print because it does not change nodes of the list.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文