C++ 链接列表结构(将结构指针传递给函数)

C++ 链接列表结构(将结构指针传递给函数),c++,function,pointers,structure,C++,Function,Pointers,Structure,我为链接列表创建了一个结构,然后全局声明了它的指针,然后创建了两个函数来添加节点和显示列表。 当我在全局上声明结构指针时,一切正常 但后来我在主函数中声明了指针,并根据需要更改了代码。但现在当我编译并运行exe时。它给我xxxx.exe停止工作 #include<iostream> using namespace std; struct node { int data; node *adr; }; void insertate(int n, struct no

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

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

 #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;

 }  
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;

 }  
#包括
使用名称空间std;
结构节点{
int数据;
节点*adr;
};
void insertate(int n,结构节点*h)
{
结构节点*temp;
temp=新节点;
温度->数据=n;
temp->adr=NULL;
if(h==NULL)
{   
h=温度;
}
其他的
{
结构节点*q;
q=新节点;
q=h;
而(q->adr!=NULL)
{
q=q->adr;
}
q->adr=温度;
} 
}
无效打印(结构节点*h)
{
结构节点*c=h;
while(c!=NULL)
{
库塔德;

cout在main()中首先分配头部节点

然后尝试通过引用传递:

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;
}
您可以通过单击下面的链接进行测试,您可能希望更新打印功能,以便不打印当前为0的头值:


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

我尝试声明一个结构变量,然后传递“&structurevariable”在函数中,但仍然相同!@irfanadil“我们不能传递指针地址”是什么意思刚刚执行的更新代码现在只是它的Suffigg黑色控制台窗口,而不是打印任何东西!@ IrFaFADIL在这里,我再次改变它,以添加默认值到头,看看那个工作系统是使用DEV C++ 5.11 TDM GCC 4.92.我应该用什么?我就是不明白为什么它显示0作为第一个值
q = *h;