数据结构与算法分析 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 F(X)=i=0NAiXi. If most of the coefficients A i A_i Ai 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 P1(X)=10X1000+5X14+1 and P 2 ( X ) = 3 X 1990 − 2 X 1492 + 11 X + 5 P_2(X) = 3X^{1990} - 2X^{1492} + 11X + 5 P2(X)=3X19902X1492+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 语言描述第二版第三章——链表实现基数排序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值