收藏 分享(赏)

《计算机专业英语》电子教案-第4章.ppt

上传人:天天快乐 文档编号:1232711 上传时间:2018-06-19 格式:PPT 页数:43 大小:512.50KB
下载 相关 举报
《计算机专业英语》电子教案-第4章.ppt_第1页
第1页 / 共43页
《计算机专业英语》电子教案-第4章.ppt_第2页
第2页 / 共43页
《计算机专业英语》电子教案-第4章.ppt_第3页
第3页 / 共43页
《计算机专业英语》电子教案-第4章.ppt_第4页
第4页 / 共43页
《计算机专业英语》电子教案-第4章.ppt_第5页
第5页 / 共43页
点击查看更多>>
资源描述

1、Computer English,Chapter 4 Data Structure,计算机专业英语,4-2,Key points: useful terms and definitions of data structureDifficult points: Stack, queue, tree,计算机专业英语,4-3,Requirements:,1. Three reasons for using data structures are efficiency, abstraction, and reusability.,2. The properties of Stack, Queue, a

2、nd Tree,3. 掌握常用英汉互译技巧,计算机专业英语,4-4,New Words & Expressions:harsh table 杂凑(哈希)表priority queues 优先队列reusability n.复用性binary tree 二叉树traversing 遍历,走过context-free 与上下文无关,4.1 An Introduction to Data Structures,计算机专业英语,4-5,Data comes in all shapes and sizes, but often it can be organized in the same way. F

3、or example, consider a list of things to do, a list of ingredients in a recipe, or a reading list for a class. Although each contains a different type of data, they all contain data organized in a similar way: a list. A list is one simple example of a data structure. Of course, there are many other

4、common ways to organize data as well. In computing, some of the most common organizations are linked lists, stacks, queues, sets, hash tables, trees, heaps, priority queues, and graphs. Three reasons for using data structures are efficiency, abstraction, and reusability.,数据以各种形状和大小出现,但是它常常可以用同样的方式来组

5、织。例如,考虑要做事情的列表、处方成份的清单或一个班级的阅读目录。虽然它们包含不同类型的数据,但他们都包含以一种相似方式组织的数据:一个列表。列表是数据结构的一个简单例子。当然,还有许多其他组织数据通用方法。在计算机技术中,一些最常用的组织方式是链接表、堆栈、队列、集合、哈希表、树、堆、优先队列和图。使用数据结构的三个原因是效率、抽象性和复用性。,4.1 An Introduction to Data Structures,计算机专业英语,4-6,Efficiency Data structures organize data in ways that make algorithms more

6、 efficient. For example, consider some of the ways we can organize data for searching it. One simplistic approach is to place the data in an array and search the data by traversing element by element until the desired element is found. However, this method is inefficient because in many cases we end

7、 up traversing every element. By using another type of data structure, such as a hash table or a binary tree we can search the data considerably faster.,效率数据结构使用令算法更有效率的方法组织数据。例如,考虑一些我们用来查找数据的组织方式。一种过分简单的方式是将数据放置到数组中,并用遍历的方法找到需要的元素。然而,这种方法是低效率的,因为在许多情况下,我们需要遍历所有元素才能完成。使用其他类型的数据结构,如哈希表和二叉数,我们能够相当快速地搜

8、寻数据。,4.1 An Introduction to Data Structures,计算机专业英语,4-7,Abstraction Data structures provide a more understandable way to look at data; thus, they offer a level of abstraction in solving problems. For example, by storing data in a stack, we can focus on things that we do with stacks, such as pushing

9、and popping elements, rather than the details of how to implement each operation. In other words, data structures let us talk about programs in a less programmatic way.,抽象化数据结构提供一个更好理解的方法查看数据;因此,它们在解决问题中提供一定的抽象化水平。例如,通过把数据储存在堆栈中,我们可以将重点集中在对堆栈的操作上,如使元素进栈和出栈,而不是集中在实现操作的细节上。换句话说,数据结构使我们以较少的编程方式谈论程序。,4.

10、1 An Introduction to Data Structures,计算机专业英语,4-8,Reusability Data structures are reusable because they tend to be modular and context-free. They are modular because each has a prescribed interface through which access to data stored in the data structure is restricted. That is, we access the data us

11、ing only those operations the interface defines. Data structures are context-free because they can be used with any type of data and in a variety of situations or contexts. In C, we make a data structure store data of any type by using void pointers to the data rather than by maintaining private cop

12、ies of the data in the data structure itself.,复用性:因为数据结构趋向于模块化并和环境无关,所以数据结构是可以复用的。因为每种结构有一个预定的接口,通过该接口限制访问存储在数据结构中的数据,所以它们是模块化的。也就是说,我们只能使用接口定义的那些操作来访问数据。因为数据结构能用于任何类型的数据,并用于多种环境中,所以数据结构与使用环境无关。在C语言中,我们通过使用空指针,而不是通过维护非公开的数据备份,使数据结构存储任何类型的数据。,4.1 An Introduction to Data Structures,计算机专业英语,4-9,New Wor

13、ds & Expressionsinviting adj.引人动心的contiguous adj.邻近的, 接近的stack n. 堆栈insertion n.插入deletion n.删除, 删除部分pop 退栈push 进栈backtrack v.回溯pseudocode n.计伪代码retrieve v.重新得到;n.找回pointer n.指针pertinent adj.有关的, 相干的, 中肯的extract vt. 取,引 back out 返回entail vt. 使承担, 带来traverse v.遍历shrink v.收缩allot vt.分配,充当,依靠predecesso

14、r n.前辈, 前任back and forth adv.来来往往地, 来回地vacancy n.空, 空白, 空缺stuff vt.填充, 塞满AbbreviationsLIFO (last-in, first-out) 后进先出FIFO (first-in, first-out) 先进先出,4.2 Stacks,计算机专业英语,4-10,One of the properties of a list that makes a linked structure more inviting than a contiguous one is the need to insert and dele

15、te entries inside the list. Recall that it was such operations that had the potential of forcing the massive movement of names to fill or create holes in the case of a contiguous list. If we restrict such operations to the ends of the structure, we find that the use of a contiguous structure becomes

16、 a more convenient system. An example of this phenomenon is a stack, which is a list in which all insertions and deletions are performed at the same end of the structure. A consequence of this restriction is that the last entry entered will always be the first entry removed-an observation that leads

17、 to stacks being known as last-in, first-out (LIFO) structures.,插入和删除记录的需求是使链接表结构比邻接表结构更诱人的原因之一。让我们回想一下在邻接表中具有填补和创建存储空缺能力的操作。如果我们限制这种操作只可以在结构的尾部进行,则邻接表就是一种比较方便的系统。这种现象的一个例子就是堆栈。在堆栈中,插入和删除操作都在结构的相同末端进行。如此限制的结果就是最后一个进入表的记录也就是第一个从表中删除的记录。这种结构称为后进先出结构。,4.2 Stacks,计算机专业英语,4-11,The end of a stack at which

18、 entries are inserted and deleted is called the top of the stack. The other end is sometimes called the stacks base. To reflect the fact that access to a stack is restricted to the topmost entry, we use special terminology when referring to the insertion and deletion operations. The process of inser

19、ting an object on the stack is called a push operation, and the process of deleting an object is called a pop operation. Thus we speak of pushing an entry onto a stack and popping an entry off a stack.,堆栈尾部可以进行插入和删除操作的记录称为堆栈的栈顶,另一端叫做栈底。为了表示如何限制堆栈只能从栈顶访问,我们用一种特殊的术语来表示插入和删除操作。把一个对象插入堆栈的操作称为进栈操作,而从堆栈中删

20、除一个对象的操作称为出栈操作,所以我们常说将一个条目进栈或者将其出栈。,4.2 Stacks,计算机专业英语,4-12,BacktrackingA classic application of a stack involves the execution of a program involving procedures as found in our pseudocode. When the execution of a procedure is requested, the machine must transfer its attention to the procedure; yet

21、later, when the procedure is completed, the machine must return to the original location before continuing. This means that, when the initial transfer is made, there must be a mechanism for remembering the location to which execution ultimately returns.,回溯堆栈的一个典型应用发生在一个程序单元调用一个过程的操作中。为了完成这个调用,机器必须将它

22、的注意力转移到这个过程上;当过程调用结束后,机器必须返回到程序块进行调用时所处的位置。这就意味着必须有一种用来记录操作结束后返回的位置的机制。,4.2 Stacks,计算机专业英语,4-13,The situation is complicated by the fact that the procedure may itself request the execution of another procedure, which may request still another, and so on (Figure 7.9). Consequently, the return locatio

23、ns being remembered begin to pile up. Later, as each of these procedures is completed, execution must be returned to the proper place within the program unit that called the completed procedure. A system is therefore needed to save the return locations and later retrieve them in the proper order.,如果

24、一个被调用的过程本身还要调用其他过程,而那些过程同样也需要调用另外的过程,这样一来整个情形就会很复杂。因此,返回地址的记忆就开始堆积。然后,当每一个过程都结束后,操作必须返回到被称为完成过程的程序块中的合适位置。因此,系统需要按照适当的顺序存储和找回返回地址。,4.2 Stacks,计算机专业英语,4-14,A stack is an ideal structure for such a system. As each procedure is called, a pointer to the pertinent return Location is pushed on a stack, an

25、d as each procedure is completed, the top entry from the stack is extracted with the assurance of obtaining a pointer to the proper return location. This example is representative of stack applications in general in that it demonstrates the relationship between stacks and the process of backtracking

26、. Indeed, the concept of a stack is inherent in any process that entails backing out of a system in the opposite order from which it was entered.,堆栈是满足这种需要的理想结构。当一个过程被调用时,将指向返回地址的指针进栈。然后,当一个过程完成时,将栈顶条目出栈,程序就可以准确得到返回地址。这是应用栈的一个典型例子,它表明了栈和回溯过程的关系。在任何可以从进入端反向返回系统的过程中,堆栈的概念是与生俱来的。,4.2 Stacks,计算机专业英语,4-1

27、5,As another example of backtracking, suppose we want to print the names in a linked list in reverse order-that is, last name first. Our problem is that the only way we can access the names is by following the linked structure. Thus we need a way of holding each name retrieved until all of the names

28、 that follow have been retrieved and printed. Our solution is to traverse the list from its beginning to its end while pushing the names we find onto a stack. After reaching the end of the list, we print the names as we pop them off the stack.,我们在来举另一个例子,假设反向输出一张链接表中的姓名,也就是把最后一个名字第一个输出。问题是我们只能跟着链接结构

29、访问姓名。因此,我们需要一种方式,通过这种方式,我们可以保持每一个姓名能被检索,直到排列在这个姓名之后的姓名被得到并输出。我们的方案是从链接表的开始顺序遍历到结尾,与此同时把每一个姓名按照遍历顺序进栈。当到达链接表的末尾后,我们通过出栈操作来输出姓名。,4.2 Stacks,计算机专业英语,4-16,Stack ImplementationTo implement a stack structure in a computers memory, it is customary to reserve a block of contiguous memory cells large enough

30、to accommodate the stack as it grows and shrinks. (Determining the size of this block can often be a critical decision. If too little room is reserved, the stack ultimately exceeds the allotted storage space; if too much room is reserved, memory space will be wasted.) One end of this block is design

31、ated as the stacks base. It is here that the first entry pushed on the stack is stored, with each additional entry being placed next to its predecessor as the stack grows toward the other end of the reserved block.,栈的实现为了在计算机存储中实现栈结构,一般采取的方法是保留一块足够容纳栈大小变化的内存单元。(通常来说,确定块的大小是一个很重要的任务。如果保留的空间过小,那么栈最后可能

32、从所分配的存储空间中溢出;而如果保留的空间过大,又是一种浪费。)块的一端作为栈底,栈的第一条数据会被存储在这里,以后的条目被依次放置在它之后的存储单元中,也就是堆栈向另外一端增加。,4.2 Stacks,计算机专业英语,4-17,Thus, as entries are pushed and popped, the top of the stack moves back and forth within the reserved block of memory cells. A means is therefore needed to maintain a record of the loca

33、tion of the top entry. For this purpose, the address of the top entry is stored in an additional memory cell known as the stack pointer. That is, the stack pointer points to the top of the stack.,因此,在条目进栈和出栈的时候,栈顶的位置就在存储单元块中前后移动。为了保存这个位置的轨迹,栈顶条目的地址被存储在一个附加的存储单元中,这个附加的存储单元被被称为堆栈指针。也就是说,堆栈指针就是一个指向栈顶的指

34、针。,4.2 Stacks,计算机专业英语,4-18,The complete system, as illustrated in Figure 4-1, works as follows: To push a new entry on the stack, we first adjust the stack pointer to point to the vacancy just beyond the top of the stack and then place the new entry at this location. To pop an entry from the stack,

35、we read the data pointed to by the stack pointer and then adjust the pointer to point to the next entry down on the stack.,一个完整的系统(如图4-1所示)是这样工作的:为了把一条新的数据压入堆栈,我们首先调整堆栈指针,使之指向当前栈顶之前的空白。然后将新的条目置于此处。为了将条目从堆栈中弹出,我们首先读出堆栈指针所指向的数据,然后调整此指针指向堆栈中的下一条数据所在的存储单元。,4.2 Stacks,计算机专业英语,4-19,As we observed in the c

36、ase of lists, a programmer would probably find it advantageous to write procedures that perform these push and pop operations so that the stack could be used as an abstract tool. Note that these procedures should handle such special cases as attempts to pop entries from an empty stack and to push en

37、tries onto a full stack. In particular, a complete stack system would probably contain procedures for pushing entries, popping entries, testing for an empty stack, and testing for a full stack.,同我们观察到表中的情况一样,程序员也可以将堆栈编写成一个可以进行进栈和出栈操作的抽象工具。注意,这些过程应该可以处理诸如试图从空栈中弹出数据,或者将数据压入一个已经填满的堆栈等特殊情况。所以一个完整的堆栈系统应该

38、包括进栈、出栈、测试堆栈是否空或满的功能。,4.2 Stacks,计算机专业英语,4-20,A stack organized in a contiguous block of memory cells exhibits little difference between the conceptual structure and the actual structure in main memory. Suppose, however, that we cannot reserve a fixed block of memory and be assured that the stack wi

39、ll always fit. A solution is to implement the stack as a linked structure similar to that of a list. This avoids the limitations of restricting the stack to a fixed-size block, since it allows the entries in the stack to be stuffed into small pieces of available space anywhere in memory. In such a s

40、ituation, the conceptual stack structure will be quite different from the actual arrangement of the data in memory.,存储在存储单元连续块中的栈展现出主存储器中概念结构与实际之间的一定差异。假设我们不能预测栈的大小,我们就不能保留一个总能满足堆栈的固定大小的存储块。一种解决方法就是实现一种与表结构相似的链接结构栈。这种方法避免了将堆栈限定在一块固定块中的局限性,因为它允许将新的条目插入存储器中任何一块足够大的空闲空间中。在这种情况下,概念上的堆栈结构与其在存储器中实际的数据组织方式

41、就大不相同了。,4.2 Stacks,计算机专业英语,4-21,New Words vt.使移居, 使移植circular queue 循环队列envision vt.想象, 预想bridge vt.跨接,接通,4.3 Queues,计算机专业英语,4-22,In contrast to a stack in which both insertions and deletions are performed at the same end, a queue is a list in which all insertions are performed at one end while all

42、deletions are made at the other. We have already met this structure in relation to waiting lines, where we recognized it as being a first-in, first-out (FIFO) storage system. Actually, the concept of a queue is inherent in any system in which objects are served in the same order in which they arrive

43、.,4.3 Queues,栈的插入与删除操作都是在表的相同端进行的。而与此不同,队列是插入和删除操作分别在两端进行的表。我们已经遇到过这种与等待队列相关的结构,在此种情况中,我们把它当作是一种先进先出的存储系统。实际上,在那些对象输入与输出顺序相同的系统中,队列的概念是与生俱来的。队列的结尾从等待队列的关联中得到名字。,计算机专业英语,4-23,The ends of a queue get their names from this waiting-line relationship. The end at which entries are removed is called the he

44、ad (or sometimes the front) of the queue just as we say that the next person to be served in a cafeteria is at the head (or front) of the line. Similarly, the end of the queue at which new entries are added is called the tail (or rear).,4.3 Queues,结尾处,也就是条目被移出队列的地方,被称为队列的队首(有时候也称为队前),这就好像我们在快餐厅中下一个将

45、点餐的顾客为一队的队首一样。同样,队列的尾部,也就是条目被添加的地方,被称为队尾(或者队后)。,计算机专业英语,4-24,We can implement a queue in a computers memory within a block of contiguous cells in a way similar to our storage of a stack. Since we need to perform operations at both ends of the structure, we set aside two memory cells to use as pointe

46、rs instead of just one, as we did for a stack. One of these pointers, called the head pointers keeps track of the head of the queue; the other, called the tail pointer, keeps track of the tail.,4.3 Queues,我们可以像存储栈那样通过连续单元组成的存储块在计算机主存储器中实现队列。因为我们需要在此结构的两端都进行操作,我们分配出两个存储单元用来当作指针,而非栈中那样仅仅需要一个单元来存储指针。其中

47、的一个指针被称为头指针,用来保持队列头的轨迹;另一个指针被称为尾指针,用来保持队尾的轨迹。,计算机专业英语,4-25,When the queue is empty, both of these pointers point to the same location. Each time an entry is inserted, it is placed in the location pointed to by the tail pointer and then the tail pointer is adjusted to point toward the next unused loc

48、ation. In this manner, the tail pointer is always pointing to the first vacancy at the tail of the queue. Removing an entry from the queue involves extracting the entry pointed to by the head pointer and then adjusting the head pointer to point toward the entry that followed the removed entry.,4.3 Q

49、ueues,如果一个队列为空,那么两个指针应该指向相同的位置。每当新的条目被插入时,均会被置于尾指针所指向的位置,同时修改尾指针,使之指向下一个未使用的位置。这样,尾指针总是指向队尾后的第一空闲存储单元。将一条数据移出队列的操作包括将头指针指向的条目取出,同时调整头指针使之指向排在被移出条目之后的位置 。,计算机专业英语,4-26,A problem remains with the storage system as described thus far. If left unchecked, the queue crawls slowly through memory like a gla

50、cier, destroying any other data in its path (Figure 4.2). This movement is the result of the rather egocentric policy of inserting each new entry by merely placing it next to the previous one and repositioning the tail pointer accordingly. If we add enough entries, the tail of the queue ultimately extends all the way to the end of the machines memory.,

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

当前位置:首页 > 中等教育 > 中考课件

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


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

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

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