First Encounter with Generalized Lists and Multilinked Lists

发表于 2020-02-27 01:18 364 字 2 min read

cos avatar

cos

FE / ACG / 手工 / 深色模式强迫症 / INFP / 兴趣广泛养两只猫的老宅女 / remote

本文介绍了广义表和多链表两种数据结构。广义表允许元素本身也是广义表,而多链表通过多个指针字段实现节点间的多向连接,适用于复杂数据结构如树和图的存储。以稀疏矩阵为例,使用“正交链表”结构只存储非零元素,通过行指针和列指针实现行和列的连接,并用标签字段区分头节点和非零元素节点。

This article has been machine-translated from Chinese. The translation may contain inaccuracies or awkward phrasing. If in doubt, please refer to the original Chinese version.

I. Generalized Lists

  • A generalization of linear lists
  • In a linear list, all n elements are basic single elements
  • In a generalized list, these elements can be not only single elements but also another generalized list
> typedef struct GNode *GList; struct GNode {
>     int Tag;    //标志域:0表示结点是单元素,1表示结点是广义表
>     union {     //子表指针域SubList域单元素数据域Data复用,共用存储空间
>         ElementType Data;
>         GList SubList;
>     } URegion;
>     GList Next;     //指向后继结点 };

II. Multilinked Lists

  • Nodes in a list may belong to multiple chains

Nodes in a multilinked list have multiple pointer fields, as in the previous example which contains Next and SubList; However, a list with two pointer fields is not necessarily a multilinked list — for example, a doubly linked list is not a multilinked list

  • Multilinked lists have a wide range of applications:

Relatively complex data structures such as trees and graphs can generally be implemented using multilinked lists for storage

Example: Storing Matrices

Using a two-dimensional array has two drawbacks

  • First, the size of the array needs to be determined in advance
  • Second, for sparse matrices, it causes a large waste of storage space

Analysis: Use a typical multilinked list — orthogonal linked list to store sparse matrices Only store non-zero elements

  • Pointer fields of a node: Row coordinate Row, column coordinate Col, value Value
  • Each node is linked through two pointer fields to connect nodes in the same row and same column: Row pointer (or right pointer) Right Column pointer (or down pointer) Down
  • A tag field Tag is used to distinguish between head nodes and non-zero element nodes: The tag value for head nodes is “Head”, and the tag value for non-zero element nodes is “Term”

If you enjoyed this, leave a comment~

© 2020 - 2026 cos @cosine
Powered by theme astro-koharu · Inspired by Shoka