📜  自引用结构

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

自引用结构

自引用结构是那些具有一个或多个指针的结构,这些指针指向相同类型的结构,作为它们的成员。

换句话说,指向相同类型结构的结构本质上是自引用的。
例子:

CPP
struct node {
    int data1;
    char data2;
    struct node* link;
};
 
int main()
{
    struct node ob;
    return 0;
}


Python3
class node:
    def __init__(self):
        self.data1=0
        self.data2=''
        self.link=None
 
if __name__ == '__main__':
    ob=node()


CPP
#include 
 
struct node {
    int data1;
    char data2;
    struct node* link;
};
 
int main()
{
    struct node ob1; // Node1
 
    // Initialization
    ob1.link = NULL;
    ob1.data1 = 10;
    ob1.data2 = 20;
 
    struct node ob2; // Node2
 
    // Initialization
    ob2.link = NULL;
    ob2.data1 = 30;
    ob2.data2 = 40;
 
    // Linking ob1 and ob2
    ob1.link = &ob2;
 
    // Accessing data members of  ob2 using ob1
    printf("%d", ob1.link->data1);
    printf("\n%d", ob1.link->data2);
    return 0;
}


Python3
class node:
    def __init__(self):
        self.data1=0
        self.data2=0
        self.link=None
 
if __name__ == '__main__':
    ob1=node() # Node1
 
    # Initialization
    ob1.link = None
    ob1.data1 = 10
    ob1.data2 = 20
 
    ob2=node() # Node2
 
    # Initialization
    ob2.link = None
    ob2.data1 = 30
    ob2.data2 = 40
 
    # Linking ob1 and ob2
    ob1.link = ob2
 
    # Accessing data members of  ob2 using ob1
    print(ob1.link.data1)
    print(ob1.link.data2)


CPP
#include 
 
struct node {
    int data;
    struct node* prev_link;
    struct node* next_link;
};
 
int main()
{
    struct node ob1; // Node1
 
    // Initialization
    ob1.prev_link = NULL;
    ob1.next_link = NULL;
    ob1.data = 10;
 
    struct node ob2; // Node2
 
    // Initialization
    ob2.prev_link = NULL;
    ob2.next_link = NULL;
    ob2.data = 20;
 
    struct node ob3; // Node3
 
    // Initialization
    ob3.prev_link = NULL;
    ob3.next_link = NULL;
    ob3.data = 30;
 
    // Forward links
    ob1.next_link = &ob2;
    ob2.next_link = &ob3;
 
    // Backward links
    ob2.prev_link = &ob1;
    ob3.prev_link = &ob2;
 
    // Accessing  data of ob1, ob2 and ob3 by ob1
    printf("%d\t", ob1.data);
    printf("%d\t", ob1.next_link->data);
    printf("%d\n", ob1.next_link->next_link->data);
 
    // Accessing data of ob1, ob2 and ob3 by ob2
    printf("%d\t", ob2.prev_link->data);
    printf("%d\t", ob2.data);
    printf("%d\n", ob2.next_link->data);
 
    // Accessing data of ob1, ob2 and ob3 by ob3
    printf("%d\t", ob3.prev_link->prev_link->data);
    printf("%d\t", ob3.prev_link->data);
    printf("%d", ob3.data);
    return 0;
}


Python3
class node:
    def __init__(self):
        self.data=0
        self.prev_link=None
        self.next_link=None
 
if __name__ == '__main__':
    ob1=node() #Node1
 
    # Initialization
    ob1.prev_link = None
    ob1.next_link = None
    ob1.data = 10
 
    ob2=node() #Node2
 
    # Initialization
    ob2.prev_link = None
    ob2.next_link = None
    ob2.data = 20
 
    ob3=node() # Node3
 
    # Initialization
    ob3.prev_link = None
    ob3.next_link = None
    ob3.data = 30
 
    # Forward links
    ob1.next_link = ob2
    ob2.next_link = ob3
 
    # Backward links
    ob2.prev_link = ob1
    ob3.prev_link = ob2
 
    # Accessing  data of ob1, ob2 and ob3 by ob1
    print(ob1.data,end='\t')
    print(ob1.next_link.data,end='\t')
    print(ob1.next_link.next_link.data)
 
    # Accessing data of ob1, ob2 and ob3 by ob2
    print(ob2.prev_link.data,end='\t')
    print(ob2.data,end='\t')
    print(ob2.next_link.data)
 
    # Accessing data of ob1, ob2 and ob3 by ob3
    print(ob3.prev_link.prev_link.data,end='\t')
    print(ob3.prev_link.data,end='\t')
    print(ob3.data)


在上面的示例中,“链接”是指向“节点”类型结构的指针。因此,结构“节点”是一个以“链接”作为引用指针的自引用结构。
需要考虑的重要一点是指针应该在访问之前正确初始化,因为默认情况下它包含垃圾值。
自参照结构的类型

  1. 单链接自引用结构
  2. 具有多个链接的自引用结构

具有单链接的自引用结构:这些结构只能有一个自指针作为其成员。以下示例将向我们展示如何将自引用结构的对象与单个链接连接并访问相应的数据成员。形成的连接如下图所示。

CPP

#include 
 
struct node {
    int data1;
    char data2;
    struct node* link;
};
 
int main()
{
    struct node ob1; // Node1
 
    // Initialization
    ob1.link = NULL;
    ob1.data1 = 10;
    ob1.data2 = 20;
 
    struct node ob2; // Node2
 
    // Initialization
    ob2.link = NULL;
    ob2.data1 = 30;
    ob2.data2 = 40;
 
    // Linking ob1 and ob2
    ob1.link = &ob2;
 
    // Accessing data members of  ob2 using ob1
    printf("%d", ob1.link->data1);
    printf("\n%d", ob1.link->data2);
    return 0;
}

Python3

class node:
    def __init__(self):
        self.data1=0
        self.data2=0
        self.link=None
 
if __name__ == '__main__':
    ob1=node() # Node1
 
    # Initialization
    ob1.link = None
    ob1.data1 = 10
    ob1.data2 = 20
 
    ob2=node() # Node2
 
    # Initialization
    ob2.link = None
    ob2.data1 = 30
    ob2.data2 = 40
 
    # Linking ob1 and ob2
    ob1.link = ob2
 
    # Accessing data members of  ob2 using ob1
    print(ob1.link.data1)
    print(ob1.link.data2)
输出:
30
40

具有多个链接的自引用结构:具有多个链接的自引用结构可以有多个自指针。使用这些结构可以轻松构建许多复杂的数据结构。这样的结构可以很容易地一次连接到多个节点。以下示例显示了一种具有多个链接的此类结构。
使用下图可以理解上述示例中的连接。

CPP

#include 
 
struct node {
    int data;
    struct node* prev_link;
    struct node* next_link;
};
 
int main()
{
    struct node ob1; // Node1
 
    // Initialization
    ob1.prev_link = NULL;
    ob1.next_link = NULL;
    ob1.data = 10;
 
    struct node ob2; // Node2
 
    // Initialization
    ob2.prev_link = NULL;
    ob2.next_link = NULL;
    ob2.data = 20;
 
    struct node ob3; // Node3
 
    // Initialization
    ob3.prev_link = NULL;
    ob3.next_link = NULL;
    ob3.data = 30;
 
    // Forward links
    ob1.next_link = &ob2;
    ob2.next_link = &ob3;
 
    // Backward links
    ob2.prev_link = &ob1;
    ob3.prev_link = &ob2;
 
    // Accessing  data of ob1, ob2 and ob3 by ob1
    printf("%d\t", ob1.data);
    printf("%d\t", ob1.next_link->data);
    printf("%d\n", ob1.next_link->next_link->data);
 
    // Accessing data of ob1, ob2 and ob3 by ob2
    printf("%d\t", ob2.prev_link->data);
    printf("%d\t", ob2.data);
    printf("%d\n", ob2.next_link->data);
 
    // Accessing data of ob1, ob2 and ob3 by ob3
    printf("%d\t", ob3.prev_link->prev_link->data);
    printf("%d\t", ob3.prev_link->data);
    printf("%d", ob3.data);
    return 0;
}

Python3

class node:
    def __init__(self):
        self.data=0
        self.prev_link=None
        self.next_link=None
 
if __name__ == '__main__':
    ob1=node() #Node1
 
    # Initialization
    ob1.prev_link = None
    ob1.next_link = None
    ob1.data = 10
 
    ob2=node() #Node2
 
    # Initialization
    ob2.prev_link = None
    ob2.next_link = None
    ob2.data = 20
 
    ob3=node() # Node3
 
    # Initialization
    ob3.prev_link = None
    ob3.next_link = None
    ob3.data = 30
 
    # Forward links
    ob1.next_link = ob2
    ob2.next_link = ob3
 
    # Backward links
    ob2.prev_link = ob1
    ob3.prev_link = ob2
 
    # Accessing  data of ob1, ob2 and ob3 by ob1
    print(ob1.data,end='\t')
    print(ob1.next_link.data,end='\t')
    print(ob1.next_link.next_link.data)
 
    # Accessing data of ob1, ob2 and ob3 by ob2
    print(ob2.prev_link.data,end='\t')
    print(ob2.data,end='\t')
    print(ob2.next_link.data)
 
    # Accessing data of ob1, ob2 and ob3 by ob3
    print(ob3.prev_link.prev_link.data,end='\t')
    print(ob3.prev_link.data,end='\t')
    print(ob3.data)
输出:
10    20    30
10    20    30
10    20    30

在上面的例子中我们可以看到'ob1'、'ob2'和'ob3'是自引用结构'node'的三个对象。并且它们使用它们的链接以这样的方式连接,使得它们中的任何一个都可以轻松地访问彼此的数据。这就是自指结构的美妙之处。连接可以根据程序员的要求进行操作。
应用:
自引用结构在创建其他复杂数据结构时非常有用,例如:

  • 链表
  • 堆栈
  • 队列
  • 树木
  • 图表等