C++: linked list as an ADT(abstract data types)

来源:互联网 发布:兰州大学网络教育学校 编辑:程序博客网 时间:2024/05/27 09:44

我们可以将linked list 看成是数据结构(linked list), 我们也可以将其看成一个a black box that provides some functions to manage a collection of data(ADT的定义)。 即将linked list 看成是一个具有一些methods 的容器已处理一组数据集合的黑盒子。


下面给出ADT的定义(in a nut shell, 简而言之):

An ADT is a set of objects with a set of  operations。

这从某个角度看可以说这也是数据结构的定义。

但是数据结构和ADT是有区别的。  因为数据结构都定义好了如何去实现这些operations。 但是ADT并不去限制如何定义这些函数(operations)。

即:But how the operations are implemented  is not defined. (unlike a data structure)。


所以一旦我们用具体的方法实现了这些operations, 那么ADT就会变成数据结构(data structures)了。

如下图:



具有OOP编程背景的人会将ADT(抽象数据类型)视为抽象类(abstract class)。 这个abstract class 的所有operations 都是一些抽象的方法(abstract methods), 具体的定义由ADT的child class 去完成定义。

下面举几个ADTs 的例子。

EX1: sets of integers with operations for “union” and “intersection.”  

      Answer: 这是一个ADT。 我们并没有指定(specify)如何去实现并(union)和 交(“intersection”)这两个operations。 there could be slow and fast ways to do this。


EX2:lists of words with operations of “order”, “search.” 

   Answer: 这也是一个ADT。 我们已经知道binary search比 brute force search 快。 但是具体如何实现搜索, the implementation is up to the user。

EX3: • genealogical tree with operations “getFather” and “getFirstAncestor”

Answer: 也是一个ADT.


除了上述一眼就知道是ADT的, 还有一些更加抽象的(more abstact ADT), 不容易看的ADT:

Ex1:sets with operations for “union” and “intersection.” (Could be sets of integers, cars, people, 
whatever.)

EX2: lists with operations of “order”, “search.”  (Could be a list of words, endangered species, …)

EX3:tree with operations “getFather” and  “getFirstAncestor” (could be genealogical, or a pine tree, or a 
Java/C++ class hierarchy)


list 可以被看做一个ADT。 list ADT 的operations 如下:




既然ADT没有speciy 如何实现其operations, 那么我们该如何implement 这些operations 呢? 例如, 如何去implement List ADT 的那些operations.

答案是However you want。

例如, Find(53), 你可能用binary search 实现, 也可以用brute force search去查找, 这都okay。

insert(x, k) , 我们可能是一个array to store vaklues, and might move elements around to make room for new one。

等等。

尽管如何实现取决于你, 但是应该记住, 一些实现的方式总是比别的实现方式好(But some choices are better than others!)。

一旦我们实现了所有的ADT的operations, 那么我们的ADT就变成了一个data structures(数据结构)。


linked list 分为单链表(singly linked list)和 双链表(doubly linked list)。  linked list 是一个dynamic data structure(即: grows and 
shrinks as necessary at runtime)。 

单链表具体可如下表示:



singly linked list 的一个变体如下:



我们可以使用linked list 去实现其他的一些数据结构, 例如stack, queue 等等。


我们可以基于数组实现list , 我们也可以基于linked list 的实现list, 但是这两种选择有着不同的考量。

首先, 如果选择基于linked list 的方法去实现list, 那么some operations may be fast, because  array contents would not  have to be shifted, 

例如adding or removing the first item。 but other operations may be slower, because we cannot access items , other than the first and last, directly。 例如 retrieving or replacing these items requires a traversal  of the nodes。

而且linked llist 可能占用更多的内存。 每个节点会多占4 bytes(用于存储地址的指针)

但是, 并不是说linked list 的占用的内存就比数组的小, linked list 的内存利用率却比array大的多。 

(; the  number of nodes in a linked list is  always equal to the number of items 
currently being stored )

数组可能含有很多未使用的, 但是已经分配了的内存。



singly linked list 能够很好地实现 stacks 和 queues。 只需要限制插入和删除操作值发生在链表头后者尾。

linked list 作为ADT的时候, 可能的操作如下:





linked list的UML如下:



0 0
原创粉丝点击