《Data Structure And Algorithm Analysis In C++》读书笔记三

Chapter 3 Lists, Stacks, and Queues

 

Topics:

*Introduce the concept of Abstract Data Types(ADTs).

*Show how to efficiently perform operations on lists.

*Introduce the stack ADT and its use in implementing recursion.

*Introduce the queue ADT and its use in operating systems and algorithm design.

 

3.1 Abstract Data Types(ADTs)

An abstract data type(ADT) is a set of objects together with a set of operations. Abstract data types are mathematical abstractions; nowhere in a ADT's definition is there any mention of how the set of operations is implemented. Objects such as lists, sets, and graphs, along with there operations , can be viewed as ADTs, just as integers, reals, and booleans are data types. Integers, reals, and booleans have operations associated with them, an so do ADTs. For the set ADT, we might have such operations as add, remove, size, and contains. Alternatively, we might only want the two operations union and find, which would define a different ADT on the set.

  The C++ class allows the implementation of ADTs, and hiding the implementations details.

 

3.2 The List ADT

3.2.1 Simple Array Implementation of Lists

vector class internally stores an array, and allow to grow by doubling its capacity when needed.

An array implementations allows printList to be carried out in linear time, and the find Kth operation takes constant time,

But the insertion and remove operations are potentially expensive, depending on where the insertions and deletions occur.(worst case is O(N) such insert element to first position, or delete element from first position) On everage ,half of the list needs to be moved for either operation. So linear time is required. 

If all of the operation occurs at the end of the array ,then the time is O(1).

If the insert and delete only occur at the end of the array, it should be OK to use array. But if the insertion and deletion must occur throughout the whole array, choose linklist.

 

3.2.2 Simple Linked Lists

To avoid the linear cost of insertion and deletion, we need to ensure that the list is not stored contiguously, otherwise the entire parts of the list will need to be moved. refer Figure3.1

    The linked list consists of a series of nodes(which is not required adjacent in memory). Each nodes contain the element and  a link to a node containing its successor.(next link). The last cell's next link points to nullptr.

    To execute printList() or find(x), start at the first node and traverse the list by following the next links. it taks linear-time. But some tims is O(i) i is the position of the node.

    The remove method can be executed in one next pointer change. Refer Figure 3.2

    The insert method requires obtaining a new node from system by using a new call and then executing two next pointer manuevers. Refer figure 3.3

    Assume we known where a change is to be made, inserting or removing an item from a linked list involves only a const number of changes to node links.

    The special case of adding to the front or removing the first item is thus a constant-time operation.

    The special case of adding at the end can be constant-time. find the next-to-last item, change its next link to nullptr, and then update the link that maintains the last node.(a link to its previous node double linked list ref figure 3.4)

3.4 vector and list in STL

C++ provide a Standard Template Library(STL), provide some implementations of ADT. this data structures are called collections or containers.

vector provides a growable array implementation of the List ADT. it is indexable in constant time. the disadvantage is the insertion and remove operations is expensive.

list provides a doubly linked list implementation of the List ADT. the advantage is the insertion and remove operations required constant-time, the disadvantage is that the list is not easily indexable.

 

three common methods for STL containers:

int size() const; // return the number of elements in the container.

void clear(); // remove all elements from the container.

bool empty() const;  //return true if the container contains no elements, and the false otherwise.

 

both vector and list support adding and removing from the end of the list ADT in constant time.

and both vector and list support accessing the front item in the list in constant time.

 

void push_back(const Object&x); // add x to the end of the list.

void pop_back(); // removes the object at the end of the list.

const Object & back() const:  //return the object at the end of the list. (support mutator overload)

const Object & front() const;  // return the object at the front of the list. (support mutator overload)

 

 

because double linked list support efficient ch

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值