【问题标题】:Linklist Structure (passing strucure pointer to a function )链表结构(将结构指针传递给函数)
【发布时间】:2017-10-24 20:14:29
【问题描述】:

我为链接列表创建了一个结构,然后全局声明了它的指针,然后创建了两个函数来添加节点和显示列表。 当我全局声明结构指针时,一切正常,

但后来我在主函数中声明了指针,并根据我的需要更改了代码。但是现在当我编译并运行exe时。它让我 xxxx.exe 停止工作!

Tldr : 将结构指针传递给函数但它不起作用!

 #include<iostream>

 using namespace std;

 struct node {

 int data;
 node *adr;
 };


 void insertate(int n, struct node *h)
 {

    struct node *temp;
    temp = new node;
    temp->data=n;
    temp->adr=NULL;


    if(h==NULL)
    {   
        h=temp;    
    }

    else
    {
        struct node *q;
        q= new node;
        q=h;
        while(q->adr!=NULL)
        {
            q=q->adr;      
        }
        q->adr=temp;   

    } 

 }


 void  print(struct node *h)
 {

    struct node *c=h;
    while(c!=NULL)
    {
        cout<<c->data;
        c=c->adr;   
        cout<<endl;     
    }          
 }      


 int main()
 {   
    struct node *a;

    insertate(5,a);
    insertate(4,a);
    insertate(31,a);
    insertate(32,a);
    insertate(34,a);
    insertate(36,a);

    print(a);

    return 0;

 }  

【问题讨论】:

    标签: c++ function pointers structure


    【解决方案1】:

    首先在main()中分配你的头节点

    struct node *a = new node;
    

    然后尝试通过引用传递:

    insertate(5, &a);
    

    你还需要更新你的方法定义,而不是 *h 它将是 **h 像这样:

    #include <iostream>
    using namespace std;
    
    struct node {
      int data;
      node *adr;
    };
    
    void insertate(int n, struct node **h) {
      struct node *temp;
      temp = new node;
      temp->data=n;
      temp->adr=NULL;
    
      if(h==NULL) {
        *h=temp;
      }
      else
      {
        struct node *q;
        q= new node;
        q=*h;
        while(q->adr!=NULL)
        {
          q=q->adr;
        }
        q->adr=temp;
      }
    }
    
    void print(struct node **h)
    {
      struct node *c=*h;
      while(c!=NULL)
      {
        cout<<c->data;
        c=c->adr;
        cout<<endl;
      }
    }
    
    int main() {
      struct node *a = new node;
      a->data = 0;      // Set this to initial head value
      a->adr = NULL;
      insertate(5, &a);
      insertate(4, &a);
      insertate(31, &a);
      insertate(32, &a);
      insertate(34, &a);
      insertate(36, &a);
      print(&a);
      return 0;
    }
    

    当你在方法中访问 h 时,因为它是一个指向你需要取消引用的指针的指针,所以请注意:

    q = *h;
    

    您可以通过点击下面的链接来测试它,您可能想要更新您的打印功能以不打印出现在为 0 的头部值:

    Try it online!

    通过这种方式,我们在主函数中使用 new 运算符在堆上分配头指针。有些人给头指针一个虚拟变量,但在这种情况下,我认为在主函数中初始化更容易。现在我们有了一个指向堆中内存的指针,我们可以使用它的地址(又名 & 运算符)将它传递给我们的函数。这将创建一个指向指针 (**h) 的指针,该指针是其类型。就像普通指针一样,我们需要取消引用它才能访问它的值,这就是为什么我们要访问头指针时使用 *h。

    【讨论】:

    • 我尝试声明一个结构变量,然后在函数中传递“&structurevariable”,但它仍然一样!
    • @irfanadil “我们不能传递指针地址”是什么意思,是要求还是什么?
    • 刚刚执行更新的代码现在只是显示黑色控制台窗口,不打印任何东西!
    • @irfanadil 在这里我再次更改它为头部添加一个默认值,看看是否有效
    • 我正在使用 dev c++ 5.11 TDM-GCC 4.9.2..我应该使用哪个?而且我只是不明白为什么它显示 0 作为第一个值
    猜你喜欢
    • 1970-01-01
    • 2020-05-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-02
    • 2019-05-03
    • 2021-07-17
    • 1970-01-01
    相关资源
    最近更新 更多