收藏 分享(赏)

数据结构概念.ppt

上传人:myw993772 文档编号:8798363 上传时间:2019-07-12 格式:PPT 页数:39 大小:1.05MB
下载 相关 举报
数据结构概念.ppt_第1页
第1页 / 共39页
数据结构概念.ppt_第2页
第2页 / 共39页
数据结构概念.ppt_第3页
第3页 / 共39页
数据结构概念.ppt_第4页
第4页 / 共39页
数据结构概念.ppt_第5页
第5页 / 共39页
点击查看更多>>
资源描述

1、数据结构概念,用计算机解决问题时的主要步骤: 分析问题,抽象出一个数据模型:基本数据元素以及它们之间的逻辑关系; 解决问题需要在上述集合上实现的运算; 实现数据元素集合的存储和运算。,抽象数据类型,在实际软件开发过程中把数据模型与运算“打包”起来形成一个“抽象数据类型”抽象数据类型的实现成为一个工具或者构件,称为数据结构;抽象数据类型也是这个工具的实现者(制造者)和使用者的一个协议,使得使用者和实现者可以独立工作,互不影响,只要他们遵守这个协议。,抽象数据类型,在C+中,一个ADT与其实现构成一个class,或者说抽象数据类型可以由C+的类来描述, ADT的每个运算对应一个成员函数。例如,表示

2、圆的class class Circle private: float r, x, y;public: Circle(float v1,float v2, float v3);float area ()= return pi*r*r;,算法,算法的概念与特点; 算法设计的一般要求; 复杂度的估算与表示;,Chapter 2 栈(Stacks),栈的规格说明(Stack Specifications) 栈的实现(Implementation of Stacks) 栈的应用(Application: Bracket matching) 抽象数据类型及其实现(Abstract Data Types

3、and Their Implementations),You take one on the top,You put one on the top,Image that you want to have a program that reads a list of integers and then output them in the reverse order.How do you program it?,Stacks (栈),一个栈是一些元素的线形列表,其中元素的插入和删除均在表的同一端进行。插入和删除发生的一端称为栈顶(the top of the stack)。,栈的两个基本操作插入

4、和删除分别称为push (入栈) and pop(出栈) 。栈的特点是“后进栈的元素先出栈”(last in, first out),故栈又称为后进先出表(LIFO)。,Stacks in STL,The Standard Template Library (STL) a C+ library. a generic library: its components are heavily parameterized, i.e. almost every component (classes, functions) is a template. The STL stack implementati

5、on It is a class template. Its first parameter specifies what sort of items the stack contains. It has a second parameter that has a default value. The second parameter controls what sort of stack implementation will be used.,Methods in Stacks, void stack:push(value): this member places value at the

6、 top of the stack. void stack:pop(): this member removes the element at the top of the stack. Note that the popped element is not returned by this member. Nothing happens if pop() is used with an empty stack.,Stack methods,- bool stack:empty(): this member returns true if the stack contains no eleme

7、nts. unsigned stack:size(): this member returns the number of elements in the stack. Type &stack:top(): this member returns a reference to the stacks top (and only visible) element. It is the responsibility of the programmer to use this member only if the stack is not empty.,Using Stacks,Example: Re

8、versing a list of numbers,Using STL stack implementation,Always specify preconditons, postconditions of the program!,Reversing a list,Chapter 2 栈(Stacks),栈的规格说明(Stack Specifications) 栈的实现(Implementation of Stacks) 栈的应用(Application: Bracket matching) 抽象数据类型及其实现(Abstract Data Types and Their Implement

9、ations),Stacks as an ADT,一个栈是同类型元素构成的线性序列,并且定义了下列运算: 建立一个空栈; 在栈顶添加一个新元素,如果栈未满; 在栈顶删除一个元素,如果栈不空; 检查栈是否空; 取得栈顶元素,如果栈不空。,Specification of stack methods,用一个类Stack表示ADT栈。 用Stack_entry表示栈的元素的类型(如用typedef 说明它所指的类型); 使用枚举类型Error_code表示错误类型enum Error_code success, underflow, overflow; Error handling: methods

10、 should detect errors and let the client to decide what to do.,Specification for Methods,const: item cannot be changed,Reference: no need to copy a possible big data.,Specification for Methods,const: method cannot change anything.,栈的存储结构的选择,如何在计算机内表示栈的元素或者栈的存储结构呢? 栈的存储表示应该 表现元素之间的关系; 利于栈的运算的实现。 高级语言

11、提供两种基本表示方法: 使用数组的连续结构; 使用指针的链式结构。,连续栈(Contiguous Stack)的实现,使用一段连续的单元存储栈的元素:,The Class Specification,Implement empty and push methods.,Pushing,Remember that count is changed correctly.,Poping,Other methods,Nothing is changed, including count.,Constructor,Stack:Stack() /* Post: The stack is initialzed

12、 to be empty. */ count = 0; ,The empty method,How about the implementation using linked lists?,Making Stack generic,使用template可以使得上述栈的实现generic:template class Stack / no changes in the declarations of data and methods ; template Error_code Stack:push(const Stack_entry &item) /no changes in the imple

13、mentation ,Chapter 2 栈(Stacks),栈的规格说明(Stack Specifications) 栈的实现(Implementation of Stacks) 栈的应用(Application: Bracket matching) 抽象数据类型及其实现(Abstract Data Types and Their Implementations),When to use stacks?,In a situation last in first out.,Application:bracket matching,问题:检查程序文件中的括号是否匹配。 括号包括:,(,),. 算

14、法思想: 逐个读入字符,忽略非括号字符; 如果是左括号,记下来; 如果是右括号,检查是否和最近读入的左括号匹配;如果不匹配,则匹配失败; 重复上述过程,如果读到文件尾部时,没有未匹配的左括号留下,则匹配成功,否则,匹配失败。,Bracket matching (2),数据结构的选择:需要记录未匹配的左括号;未匹配的左括号是按照后记录下的先匹配,故应该使用栈。 Implementation:Use stack to store unmatched left brackets.If a left bracket is read, push in stack. If a right bracket

15、is read, check if it matches the top of the stack. Pop off the top if it matches. Report bad match if not. In the end the stack should be empty if matching is correct.,Abstract Data Types,In the bracket matching example we could use array and a counter instead of stack. Drawbacks: repeat work, you w

16、ill be doing in your own way what Stack has provided. Stacks is an Abstract Data Type (ADT) that people have abstracted from many situations: in different situations, we need to store a collection of items and retrieve them in the reverse order in which the items are stored. When solving a problem,

17、we should use ADTs.,Abstract Data Types (2),A type is a set whose elements are called the values of the type. Atomic types: int, float, char (their values are something we will not divide) Structured types: types constructed on atomic types; array, classes (whose values are structured objects). An a

18、bstract data type is a type with operations that can be performed on the type.,ADT (3),Stack (on some type T) is an ADT: The type is the set of finite sequences of elements of T; The operations:1. Create the stack, leaving it empty;2. Test whether it is empty;3. Push a new entry onto the top of the

19、stack, provided it is not full;4. Pop 5. Retrieve the top entry ,Implementations of ADTs,Conceptual level(抽象层): define ADT, how the data are related and what operations are needed. Algorithmic level(算法层): decide how the data will be structured and operations be carried out; Programming level(程序层):Implementation is carried out. An ADT is implemented as a class, data members corresponds to data parts of the ADT and methods to the operations.,Pointers and Pitfalls,Exercises 2.1,P56: E1, E4,本章内容,ADT栈的概念:栈的特点,如何使用class表示栈和栈的运算; 栈的连续实现; 栈的应用:在处理的数据有“先进后出”特点时应该使用数据结构栈。,

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报