Lesson 4 Data Structure: Stacks, Queues and Linked Lists.

15
Lesson 4 Data Structure: Stacks, Queues and Linked Lists

Transcript of Lesson 4 Data Structure: Stacks, Queues and Linked Lists.

Lesson 4

Data Structure: Stacks, Queues and Linked Lists

2 长沙通信职业技术学院英语教研室

This lesson is designed to present the fundamentals of data structures. The study of data structures is core to a computer science curriculum. It provides a rich context for the study of problem-solving techniques and program design and utilizes powerful programming constructs and algorithms.

3 长沙通信职业技术学院英语教研室

Data abstraction is a central concept in program design. The abstraction defines the domain and structure of the data, along with a collection of operation that access the data. The abstraction, called an abstract data type (ADT), creates a user-defined data type whose operations specify how a client may manipulate the data. The ADT is implementation independent and enables the programmer to focus on idealized models of the data and its operations.

Data structure and Data types(1)

4 长沙通信职业技术学院英语教研室

Data structure and Data types(2)

Basic data types includes numbers, characters, user-defined enumerated types, and pointers. These types are native to most programming languages. Each basic data type includes data and operations, the components of an ADT. Programming languages implement ADTs on a computer by using different representations for data, including binary numbers and the ASCII character set

5 长沙通信职业技术学院英语教研室

Data structure and Data types(3)

The number, character, and pointer types describe simple data because objects of these types cannot be divided into simpler parts. In contrast, structured data types have components that are built from simple types with rules to define relationships among the components. The structured types include arrays, string, records, files, lists, stacks, queues, trees, graphs, and tables. Most programming languages provide language constructs or library functions to handle arrays, strings, records, and file structures. As such, we identify these as built-in structured data types. In this lesson, we discuss in more detail the classical stack, queue and linked list.

6 长沙通信职业技术学院英语教研室

A stack is one of the most frequently used and most important data structures. Applications of stacks are vast. For instance, syntax recognition in a compiler is stack based, as is expression evaluation. At a lower level, stacks are used to pass parameters to functions and to make the actual function call to and return from a function.

A stack is a list of items that are accessible at only one end of the list. Items are added or deleted from the list only at the top of the stack. Food trays in a dining hall or a pile of boxes are good models for a stack.

Stacks(1)

7 长沙通信职业技术学院英语教研室

Stacks(2)

A stack is designed to hold items that are naturally accessed from the top of a list. A stack structure features operations that add and delete items. A push operation adds an item to the top of the stack. The operation of removing an element from the stack is said to pop the stack. Figure 4. 2 illustrates a sequence of push and pop operations. The last item added to the stack is the first one removed. For this reason, a stack is said to have LIFO (last-in/first-out) ordering.

8 长沙通信职业技术学院英语教研室

Stacks(3)

We always use an array to hold the stack elements. As a result, the maximum number of elements in the stack may not exceed the number of the elements in the array and the stack full condition is relevant. We can remove the restriction if we develop a stack with a linked list.

9 长沙通信职业技术学院英语教研室

Queues(1)

A queue is a data structure that stores elements in a list and permits data access only at the two ends of the list (figure 4. 3). An element is inserted at the rear of the list and is deleted from the front of the list. Applications use a queue to store items in their order of occurrence.

10 长沙通信职业技术学院英语教研室

Queues(2) Elements are removed from the queue in the same

order in which they are stored and hence a queue provides FIFO (first-in/first-out) or FCFS (first-come/first-served) ordering. The orderly serving of store customers and the buffering of printer jobs in a print spooler are classic examples of queues.

A queue includes a list and specific references to the front and rear positions (figure 4. 4). These positions are used to insert and delete items in the queue. Like a stack, a queue stores items of generic data type. Like a stack, an abstract queue does not limit the number of entries. However, if an array is used to implement the list, a "queue full" condition can occur.

11 长沙通信职业技术学院英语教研室

Linked lists(1)

We need to develop a new structure that frees us from a contiguous data storage arrangement It is linked list, which allow for dynamic list handling. Linked lists provide efficient list handling methods and overcome many of the limitations with arrays.

The independent items in a linked list are called nodes. A node consists of a data field and a pointer indicating the "next" item in the list. The pointer is the connector that ties together the individual nodes of the list. (figure 4. 5)

12 长沙通信职业技术学院英语教研室

Linked lists(2)

A linked list consists of a set of nodes whose first element, or front, is a node pointed to by head The list chains nodes together from the front of the list to the end or the rear of the list The rear is identified as the node whose pointer field has the value NULL = 0. We can think of a linked list as a freeway that has an "on-ramp" at the head and an "off-ramp" at NULL. List applications traverse the nodes by following each pointer to the next node. At any point in the scan, the current location is referenced by the pointer currPtr. In a case in which the list does not contain a node, the head pointer is NULL.

13 长沙通信职业技术学院英语教研室

Linked lists(3)

The length of the list can grow without limits by adding new links. Furthermore, new items can be inserted into the list by merely breaking a connection, adding a new link, and restoring the connection.

Items are removed from the list by breaking two connections, removing a link, and then reconnecting the chain.

14 长沙通信职业技术学院英语教研室

Words & Expressions(1)

Abstract Data Type (ADT) access 抽象数据类型 array v. 存取 buffer n. 数组 built-in n. 缓冲器 v. 缓冲 a. 嵌入式的,内置的 compiler n. 编译程序,编译器 contiguous a. 相邻的 data abstraction 数据抽象 data structure 数据结构 data type 数据类型 domain n. 域,定义域 dynamic 动态的 enumeration type 枚举类型 expression evaluation 表达式计算 field n. 域,定义域 file n. 文件 first-come/first-served (FCFS) 先来先服务 first-in/first-out (FIFO) 先进先出 function n. 函数

15 长沙通信职业技术学院英语教研室

Words & Expressions(2) graph n. 图表,图 item n. 项目,项,元素 last-in/first-out (LIFO) 后进先出 list n. 表 manipulate v. 操作,使用 node n. 结点 ordering n. 排序,定序 parameter n. 参数 pointer n. 指针 print spooler 打印假脱机 queue n. 队列 record n. 记录 reference n. 引用 stack n. 堆栈 string n. 字符串 structured a. 结构化的 syntax recognition 语法识别 table n. 制表,表格 traverse v. 遍历 tree n. 树,树状目录结构