数据结构与算法分析 C 语言描述第二版第三章——链表(linked list)

数据结构与算法分析 C 语言描述第二版第三章——链表(linked list)

1. General idea

The linked list consists a series of structures, which are not necessarily adjacent in memory. Each structure contains the element and a pointer to a structure containing its successor. We will call this the Next pointer.

The last cell’s Next pointer points to NULL; this value is defined by C and cannot be confused with another pointer.

In order to access the list, we need to know when the first cell can be found. We will keep a sentinel node, which is sometimes referred to as a header or dummy node.
在这里插入图片描述

2. Doubly linked lists

Sometimes it is convenient to traverse lists backwards.

The solution is to add an extra field to the data structure, containing a pointer to the previous cell.

The cost of this is an extra link, which adds to the space requirement and also doubles the cost of insertions and deletions because there are more pointers to fix.

On the other hand, it simplifies deletion, because you no longer have to refer to a key by using a pointer to the previous cell.
在这里插入图片描述

3. Circularly linked lists

A popular conversion is to have the last cell keep a pointer back to the first.

This can be done with or without a header (if the header is present, the last cell points to it).

The following is a double circularly linked list:
在这里插入图片描述

4. Examples

4.1 The Polynomial ADT

We can define an abstract data type for single-variable polynomials (with nonnegative exponents) by using a list.

Let F ( X ) = i = 0 N A i X i F(X) = \sum_{i = 0}^NA_iX^i . If most of the coefficients A i A_i are nonzero, we can use a simple array to store the coefficients.

We could then write routines to perform addition, subtraction, multiplication, differentiation, and other operations on these polynomials.

But if P 1 ( X ) = 10 X 1000 + 5 X 14 + 1 P_1(X) = 10X^{1000} + 5X^{14} + 1 and P 2 ( X ) = 3 X 1990 2 X 1492 + 11 X + 5 P_2(X) = 3X^{1990} - 2X^{1492} + 11X + 5 , then the running time is likely to be unacceptable. One can see that most of the time is spent multiplying zeros and stepping through what amounts to nonexistent parts of the input polynomials.

An alternative is to use a linked list.
数据结构与算法分析 C 语言描述第二版第三章——链表实现多项式相加
数据结构与算法分析 C 语言描述第二版第三章——链表实现多项式相乘

4.2 Radix Sort

数据结构与算法分析 C 语言描述第二版第三章——链表实现基数排序

发布了120 篇原创文章 · 获赞 2 · 访问量 5795

猜你喜欢

转载自blog.csdn.net/Lee567/article/details/103297474