📜  C++ 中的自引用类

📅  最后修改于: 2022-05-13 01:55:29.798000             🧑  作者: Mango

C++ 中的自引用类

类是导致面向对象编程的 C++ 中的构建块。它是一种用户定义的类型,拥有自己的数据成员和成员函数。这些可以通过创建类型类的实例来访问。

自引用类是一种特殊类型的类,专门为 C++ 中的链表和基于树的实现创建。要创建自引用类,请将数据成员声明为指向同一类对象的指针。

句法:

下面是实现自引用类的 C++ 程序。

C++
// C++ program to implement 
// self-referential class
#include
using namespace std;
  
// Class definition
class Self
{
    public:
        int x;
        Self *srefer;
        Self(int a):srefer(nullptr),x(a){}
              
};
  
// Function to print 
// values
void print(Self *b)
{
    if(b == nullptr)
    return;
    do
    {
        cout << b->x << endl;
    }while((b = b->srefer));
}
  
// Driver code
int main()
{
    Self x(5), y(7), z(9);
    x.srefer = &y;
    y.srefer = &z;
    print(&x);
    return 0;
}


C++
// C++ program to implement 
// self-referential class
#include
using namespace std;
  
// Class Linked list
class list
{
    private:
    
        // data of a node
        int data;    
    
        // pointer to next node
        list *next;  
    public:
        list()
        {
            data = 0;
            next = NULL;
        }
        list(int dat)
        {
            data = dat;
            next = NULL;
        }
        ~list();
        int get()
        {
            return data;
        }
    
        // Function to insert node 
        // in linked list
        void insert(list *node);     
    
        // Function to display list
        friend void display(list *); 
};
     
// Inserts node function
// If the list is empty the first node 
// is created else the new node is 
// inserted at the end of the list.
void list::insert(list *node)
{
    // this node pointer to catch 
    // last node 
    list *last = this; 
    
    // if node->next!=NULL, it is not 
    // the last node
    while(last->next)  
        last = last->next;
    
    // make last node point to new node
    last->next = node;   
}
  
// Displays the doubly linked list 
// in both forward and reverse order 
// by making use of the series of 
// next and prev pointers.
void display(list *first)
{
    list *traverse;
    cout << "Elements of List are:";
    cout << endl;
    for(traverse = first; traverse;
        traverse = traverse->next)
        cout << traverse->data << " ";
    cout << endl;
}
  
// Driver code
int main()
{
    list *first = NULL;
    list *node;
    
    for (int i = 1; i < 5; i++)
    {
      node = new list(i);
      if(first == NULL)
        first = node;
      else
        first->insert(node);
    }
    
    // Display the elements of list
    display(first);
}


输出
5
7
9

关键点:

  • 许多常用的动态数据结构,如堆栈、队列、链表等。使用自引用成员。
  • 类可以包含一个或多个成员,这些成员是指向同一类的其他对象的指针。
  • 此指针保存数据结构中下一个对象的地址。

具有指向同一类的下一个对象的指针的链表的图形表示如下所示 -

具有自引用类的链表

示例:下面是使用类实现链表的 C++ 程序。该类必须包含一个指向列表中下一个节点的指针成员。

C++

// C++ program to implement 
// self-referential class
#include
using namespace std;
  
// Class Linked list
class list
{
    private:
    
        // data of a node
        int data;    
    
        // pointer to next node
        list *next;  
    public:
        list()
        {
            data = 0;
            next = NULL;
        }
        list(int dat)
        {
            data = dat;
            next = NULL;
        }
        ~list();
        int get()
        {
            return data;
        }
    
        // Function to insert node 
        // in linked list
        void insert(list *node);     
    
        // Function to display list
        friend void display(list *); 
};
     
// Inserts node function
// If the list is empty the first node 
// is created else the new node is 
// inserted at the end of the list.
void list::insert(list *node)
{
    // this node pointer to catch 
    // last node 
    list *last = this; 
    
    // if node->next!=NULL, it is not 
    // the last node
    while(last->next)  
        last = last->next;
    
    // make last node point to new node
    last->next = node;   
}
  
// Displays the doubly linked list 
// in both forward and reverse order 
// by making use of the series of 
// next and prev pointers.
void display(list *first)
{
    list *traverse;
    cout << "Elements of List are:";
    cout << endl;
    for(traverse = first; traverse;
        traverse = traverse->next)
        cout << traverse->data << " ";
    cout << endl;
}
  
// Driver code
int main()
{
    list *first = NULL;
    list *node;
    
    for (int i = 1; i < 5; i++)
    {
      node = new list(i);
      if(first == NULL)
        first = node;
      else
        first->insert(node);
    }
    
    // Display the elements of list
    display(first);
}
输出
Elements of List are:
1 2 3 4