繁体   English   中英

如何使函数在C ++中返回结构指针

[英]how to make a function return a structure pointer in c++

我想让函数返回特定节点的地址。但是编译器未检测到我创建的节点数据类型结构。

struct node
{
int data;

node *link;
};

node *header,*current;
node traverse(int pos);


node *Linkedlist::traverse(int pos)
{
    int location = 0;  
    current->link = header->link;
    node *address = new node;
    address->data = NULL;
    address->link = NULL;


    while(current->link != NULL)
    {

        if(location == pos)
        {
            cout <<current->link->data <<" "<< endl; 
            address->link=current->link;
        }
        location ++;
        current->link = current->link->link;

    }


    return  address->link;
}

更改

return  *address;

return  address;

由于address是指向节点的指针变量,因此您只需简单地返回该变量。

指针变量之前的*是显式引用,表示获取指针变量address指向的值。 这与运算符&将要执行的操作相反&后者会获取变量的地址。

return  address;

因此,应该返回变量而不是返回变量所指向的值是合乎逻辑的。

注意, traverse的调用者需要注意通过调用delete显式地释放内存,否则会导致内存泄漏。 这是因为存在潜在的设计问题,您已在本地范围内分配了堆对象并返回了地址。

node * foo = Linkedlist::traverse(n);
...............
delete foo;

您可以简单地在堆中创建对象或将其添加为类成员,在前一种情况下,您可以轻松地将对象的所有权从一个范围转移到另一个范围,在第二种情况下,可以将对象的生存期转移将由对象Linkedlist控制

我尝试了这种“将指针返回指针的函数”。 请忽略main()中的其他内容。

返回指针的函数是takedata();

如果发现任何错误,或者可以帮助获得更好的代码,欢迎您。

# include<iostream> #
using namespace std;
##include<conio.h>##
###include<string.h>###
struct dll
{
int data;
dll *next,*prev;
}*start,*last,*trav;

class doublell
{
 private:
    //dll *start,*last,*trav;
    int info;
public:
    doublell()
    {
        start=NULL;
        last=NULL;
        trav=NULL;
    }

    void create();
    dll *takedata();

    void display(); 
};

//  code for data to take from user
dll *doublell :: takedata( )
{       
int info;
    cout<<"\n enter Data:\n";
    cin>>info;
    dll *newnode1;
    newnode1= new dll;
    newnode1->data=info;
    newnode1->next=NULL;
    newnode1->prev=NULL;
    return (newnode1);

}


//  code to create double link list
 void doublell:: create()
{
    /*string e;
    cout<<"\n press any time end to terminate\n";*/
    char ch;
   while(1)
  {
    cout<<"\n do you want to terminate, press Y/y to terminate\n\n\n";
    ch=getch();
    if((ch=='y')||(ch=='Y'))
        {
            display();
            cout<<"\n\n";
            break;
        }
    else
        {
            dll *newnode;
            newnode= new dll;
            newnode=takedata( );

            /*cout<<"\n enter Data:\n";
            cin>>info;
            newnode->data=info;
            newnode->next=NULL;
            newnode->prev=NULL;*/

            if (start==NULL )
            {
                /*newnode->prev=start;
                newnode->next=last;*/
                start=newnode;
                last=newnode;
            }

            else
            {   
                last->next=newnode;
                newnode->prev=last;
            //newnode->next=NULL;
                last=newnode;
            }
        }
}
}



 /* cout<<"\n enter Data:\n";
    cin>>info;
    dll *newnode;
    newnode->data=info;
    newnode->next=NULL;
    newnode->prev=NULL;

   do{

        takedata();
        if (start==NULL && last==NULL)
        {
            start=last=newnode;
        }
        else
        {   
            newnode->pre=last;
            last=newnode;
        }
        cin>>e;
    }while(strcmp(e,"end")!=0)
}*/

//  code to display
void doublell:: display()
{   
    if (start==NULL)
    {
    cout<<"\n double link list is empty./n";
    return;
}
    else
    {
        dll *trav=start;
        dll *arrow= trav;           // arrow pointer is used for coreect 
 usage of --> @ last

    cout<<"\n start is:-";
    cout<<"\n"<<start->data;
    cout<<trav->data;
    getch();
    cout<<"\n\n NULL <-- ";
    while(trav!=NULL)
    {
            /*if(arrow==NULL)
            {
                cout<<trav->data<<"<--";
            }
            else*/

                cout<<trav->data<<" <--> ";
                trav=trav->next;
                arrow=trav;
                if((arrow->next)->next==NULL)
                {   
                    arrow=trav->next;
                    cout<<trav->data<<" <--> ";
                    cout<<arrow->data<<" --> ";
                    trav=(trav->next)->next;
                }

    }
    cout<<" NULL ";
}

 }

main()
{
char ch;
int c;
class doublell ok;

do{

    cout<<"\n 1> create list"<<endl;
    cout<<"\n 4> display list"<<endl;
    cout<<"\n Choose your choice: ";
    cin>>c;
    switch(c)
    {

        case 1:  
                ok.create();
                ok.display();
                break;              
        case 4: exit;
                break;
    }
}while(c!=4);

getch();


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM