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

本章介绍了抽象数据类型(ADT)、列表ADT的实现(包括数组和链表),以及C++ STL中的vector和list容器。栈ADT用于实现递归,队列ADT在操作系统和算法设计中有应用。数组实现的列表插入和删除操作可能昂贵,而链表可以提供更高效的插入和删除。STL中的vector适合索引但插入和删除慢,而list反之。迭代器提供了对容器中元素的访问,并有const_iterators支持常量容器。此外,展示了如何使用STL的erase方法以及const_iterator的重要性。
摘要由CSDN通过智能技术生成

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.</

The fourth edition of Data Structures and Algorithm Analysis in C++ describes data structures, methods of organizing large amounts of data, and algorithm analysis, the estimation of the running time of algorithms. As computers become faster and faster, the need for programs that can handle large amounts of input becomes more acute. Paradoxically, this requires more careful attention to efficiency, since inefficiencies in programs become most obvious when input sizes are large. By analyzing an algorithm before it is actually coded, students can decide if a particular solution will be feasible. For example, in this text students look at specific problems and see how careful implementations can reduce the time constraint for large amounts of data from centuries to less than a second. Therefore, no algorithm or data structure is presented without an explanation of its running time. In some cases, minute details that affect the running time of the implementation are explored. Once a solution method is determined, a program must still be written. As computers have become more powerful, the problems they must solve have become larger and more complex, requiring development of more intricate programs. The goal of this text is to teach students good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amount of efficiency. This book is suitable for either an advanced data structures course or a first-year graduate course in algorithm analysis. Students should have some knowledge of intermediate programming, including such topics as pointers, recursion, and object-based programming, as well as some background in discrete math.
### 回答1: data structure and algorithm analysis in c是一本C语言数据结构和算法分析的经典教材,由Mark Allen Weiss编写。该书的主要特点是对数据结构和算法的讲解非常详细且透彻,采用了很多实例进行讲解,使得读者可以很快地掌握这些内容。此外,该书还特别强调了算法分析和设计的重要性,帮助读者理解复杂算法的实现方式,提高算法的优化能力。 而data structure and algorithm analysis in c的下载方式也非常简单,可以在网上找到相关的资源下载,并且也可以通过购买实体书的方式获得。对于想要深入学习C语言数据结构和算法分析的人来说,这本书是非常值得推荐的一本入门教材。 ### 回答2: data structure and algorithm analysis in c是一本关于C语言数据结构和算法分析的经典教科书。该书涵盖了广泛的数据结构和算法,包括数组、链表、树、图、排序、查找等。本书不仅涵盖了基本概念和技术,而且提供了深入的分析和高级应用。书中有丰富的例子和习题,方便读者深入理解和应用。该书是学习数据结构和算法的好材料,对于提高程序员的编程能力和解决问题的能力有很大的帮助。 ### 回答3: Data Structure and Algorithm Analysis in C是一本面向C++程序设计开发人员的算法和数据结构分析书籍。它涵盖了许多重要的算法和数据结构,如排序和搜索算法,二叉树,平衡树和图论等方面。本书旨在帮助读者深入了解算法和数据结构的基本知识,并提供了许多实用的示例和演练题来帮助读者巩固自己的知识。 本书包含了许多实际的示例和演练题,这些题目涵盖了从简单到复杂的各种情况,有助于读者更好地理解和应用所学知识。与此同时,这本书也提供了大量的编码和调试技巧,帮助读者编写出高效和可维护的代码。 总之,Data Structure and Algorithm Analysis in C是一本优秀的参考书,无论你是刚接触算法和数据结构或是已经具备一定的基础,它都可以提供丰富的知识和实践经验。如果你正在寻找一本深入了解算法和数据结构的书籍,那么它一定是首选之一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值