Chain represents the linear form

The definition of a single list

While it may be implemented sequence table random access, but during initialization need to apply a large contiguous memory space, and it is inserted in the implementation example, also requires a lot of moving elements delete operation, the time complexity is high. A new storage representation today about linear table, which is a linear chain table of representation.

First of all, we would first look at the definition of a single list. The book says, the linear form of chain store, it referred to as a singly linked list . Look at a small example, today is the weekend, Xiao Ming and his little friends want to eat hot pot. Today different from the last, they are not come together, but each to the hot pot restaurants, so they arranged to wait in the waiting area of the position is not necessarily continuous. This linear chain stored table, which is the same as a single chain, in which the memory cells are not necessarily contiguous. The storage unit through a set of arbitrary single chain to a data element stored in a linear form, which is the location of stored data elements are not necessarily consecutive.

Next, if the children know that there are number one position message, he will tell the next junior partner by phone he waited until the position, then the children will notice the following fourth junior partner using a mobile phone, then the children will still VI final notice a small partner, tell him to wait until the position, and thus maintain such a linear logic of using a mobile phone.

Store in the chain, such as mobile phones things for maintaining the linear relationship is called logical pointer , therefore, in a single linked list, is achieved by a linear relationship pointer logic, sequential storage it is different. Sequential storage logical linear relationship can be achieved by a storage unit connected, in a single linked list, the data elements are stored in any location, it can only be achieved by a linear relationship between the logical pointer.

Achieve a single linked list

首先给出一个线性表,存放数据元素,a1 到 an 有这样的线性逻辑关系。单链表在存储空间上是这样存放的,它是存放在任意位置的,这个任意位置表示它们不一定要连续。如图所示的 a2、a3 与 a3、a4

第二个很明显的差别是单链表在存放数据元素同时,不仅仅存放了数据本身,还存放了下一个数据元素地址,通过这样的地址来维持了线性的逻辑关系。表示 a2 是 a1 的后继,a3 是 a2 的后继。这样地址像一条链子一样,把所有的数据元素串了起来,形成了一个单链表,我们管这样的数据加地址的组合叫做单链表的一个结点。该结点是由存放数据元素的数据 data 和存放下一个数据元素的地址的指针域 next 共同来组成。

typedef struct LNode {
    ElemType data;
    struct LNode *next;
} LNode, *LinkList;

在程序设计语言上,我们会通过一个结构体来实现这样一个结点,它的名字叫做 LNode ,其中包含保存数据元素的变量 data 和存放下一个数据元素的指针 LNode *next 。它这个结构体的名字不仅仅叫做 LNode,还叫做 LinkList。通过名字可以发现一个结点也可以表示为一个单链表。因为只要给出第一个结点的地址,就可以依次找到属于这个单链表的所有结点。所以通过一个指针,还可以表示该单链表。

单链表虽然有一些优点,但是它也存在一些弊端。比如说单链表在存放数据元素的同时,还要存放指向下一个结点位置的指针域,那么这些指针域会造成空间的浪费。单链表在存放数据元素时,没有像顺序表那样存储空间连续相邻这样一个特点,所以不能实现随机存取,只能实现顺序存取。也就是说,想要找到其中的一个数据元素,只能从第一个数据元素的位置依次查找,遍历单链表。这就是单链表的具体实现方式。

这是所画出的一个单链表 L。其中保存的数据元素是 a1 到 an ,并且 a2 是 a1 后继 ,a3 是 a2 后继这样的线性逻辑关系。在线性表的表头也就是 a1 结点的位置有一个指针 head 指向了第一个数据结点,这样指针管它叫做头指针。如果知道了头指针,也就知道了整个线性表所有的结点。因为可以通过头指针,按照遍历的方法找到线性表当中的每一个结点。

但是在平时利用单链表去解决问题时,往往会利用这样一种引入头结点的单链表。

此时头指针指向的是头结点,而头节点的 next 域指向的是线性表当中的第一个数据元素的位置。头结点中的数据域,往往是不存放数据元素的,但是可能有时会利用它来存放一些关键的信息,比如说单链表的表长等。

在引入头结点之后,很多东西都发生变化了,其中一个就是如何判断单链表是一个空表。观察第一种单链表,发现判断他为空的方法为头指针是否为空,如果头指针为空时,那么单链表就为空。第二种判断单链表为空的方法是头结点的 next 域,也就是 head -> next 为 null 时,单链表 L 为空。

要记住引入头结点之后,第一个数据元素依旧是头结点的 next 域指向的那个结点,而绝非是头结点。

在平常我们利用单链表解决问题时,为什么会用这种引入头结点的单链表呢?它有两个优点,第一个优点是链表的第一个位置和其他位置的操作进行统一了。为什么可以统一操作,比如说在单链表没有头结点时,想要实现插入操作,在表中进行插入操作时,两边都是有结点的,而在表头进行插入操作时,前面是没有结点的,所以在具体实现上,这里会有所不同。接着来看带有头结点的单链表,在表头进行插入时,它两边依旧是都有结点的,所以它就达到了一个统一操作的目的。

第二个优点是空表和非空表的操作统一。如何统一?我们知道,无论是有没有数据结点,带有头结点的,单链表始终 head 指针都不为空,它始终指向了头结点,所以说空表和非空表的操作也统一了。这就是为什么在利用单链表时要引入一个头结点。

Guess you like

Origin www.cnblogs.com/qiuxirufeng/p/12079387.html