Data structure and Algorithm analysis: C Language Description _ Original book second Edition CH3 tables, stacks and queues _reading notes

Source: Internet
Author: User

tables, stacks, and queues are the simplest and most basic three kinds of data structures. Basically, every meaningful program will clearly use at least one such data structure, such as a stack that is always used indirectly in a program, regardless of whether you have done life in the program.

This chapter focuses on learning:

    • Understanding the concept of abstract data types (ADT)
    • Learn how to perform effective operations on a table
    • Familiar with the stack ADT and its application in the realization of recursion
    • Familiarity with queue ADT and its application in the design of operating system and algorithms
ADT

Abstract data type is a collection of operations, a mathematical abstraction, and does not involve the collection of how to implement operations in ADT, which can be seen as an extension of modular design.

There is no rule for each ADT to tell us what to do, which is a design decision, error handling, and relationship reorganization, which generally depends on the programmer.

Table ADT

Tables can be implemented in arrays, where lookup operations are performed in linear time, and the worst case for insert and delete operations is O (N). Because the inefficiencies of insertions and deletions and the size of the table must be known beforehand, simple arrays generally do not have to implement the data structure of the table.

In order to avoid the time overhead of insertions and deletions, we need to allow tables to be stored in a discontinuous form, otherwise some or all of the tables need to be moved overall.

The design often leaves a flag node called the header (header) or the dummy node (dummy node), which solves several tricky problems: inserting elements from the table header, deleting header elements, and generally deleting the problem, remembering the elements in front of the deleted element.

Three use cases:

    1. The calculation of a polynomial polynomial, for most of the items have a dense polynomial can be implemented by the array, in turn, the runtime is not accepted, this time can be quickly realized with a linked list.
    2. Implements the cardinality sort.
    3. Multiple tables. Multiple students choose multiple courses (40,000 students and 2500 courses) more efficiently than two-dimensional arrays with multi-table implementations.
Stack ADT

A stack is a table that restricts insertions and deletions to only one location, which is the end of the table, called the top of the stack, and only the top elements of the stack are accessible. Stacks are sometimes called LIFO (LIFO) tables. As with tables, stacks can be implemented either as arrays or as linked lists.

Since createstack requires a parameter to initialize the length of the array in the array implementation of the stack, and no parameters are required in the implementation of the list, the implementation of the latter does not add dummy elements, then the use of the stack's routines needs to know which method is being used. Unfortunately, efficiency and software idealism often collide.

Three applications:

    1. A balanced symbol that can be used for compiler checker syntax errors.

Algorithm Description: Make an empty stack, read the characters into the end of the file. If the character is an open symbol, it is pushed into the stack. If the character is a closed symbol, then the stack empty times is wrong. Otherwise, the stack element pops up. If the pop-up symbol is not the corresponding open symbol, an error is displayed. At the end of the file, if the stack is not empty, the error.

As you can see, the algorithm is linear and online (on-line).

2. Suffix expression. such as Operation 4.99 * 1.06 + 5.99 + 6.99 * 1.06 with suffix (postfix) or inverse Polish (reverse Polish) notation: 4.99 1.06 * 5.99 + 6.99 1.06 * +

Algorithm Description: When you see a number to push it into the stack, encountered an operator on the stack from the pop up two number, the results are pushed into the stack.

Time spent O (N).

In addition, we can also use a stack to convert a standard form (also known as infix (infix)) to a suffix.

3. Function call. Not much to say, Csapp is much more detailed than here.

Queue ADT

Like stacks, queues are also tables. However, when a queue is used, insertions are made at one end and deleted at the other end. Queues can be implemented using arrays or linked lists.

Three applications:

    1. When the job is sent to a line printer, they are arranged in the order in which they arrive. Therefore, the job being sent to a line printer is basically placed in a queue.
    2. This example is about the computer network. Use many of the PC's network settings, where the disk is placed on a machine called a file server. A user using a different computer accesses a file on a first-come-first-served basis, so its data structure is a queue.
    3. Further examples are:

A) When all the operators are busy, paging to large companies is usually placed in a queue.

b) in large-scale universities, if all terminals are occupied, students must sign on a waiting list due to limited resources. The student who spends the longest time on the terminal will be forced to leave first, while the student who waits the longest will be the next user to be allowed to use the terminal.

Like stacks, queues have other rich uses, and it's amazing how simple data structures can be so important.

Summary

  This chapter describes some of the ADT concepts and illustrates this concept using three of the most common abstract data types (ADT). The main purpose is to separate the specific implementations of the abstract data types from their functions. The program must know what the operation is doing, but it's better if you don't know how to do it.

tables, stacks, and queues may be three basic data structures in all computer science, and a large number of examples attest to their wide range of uses. In particular. We see how the stack is used to record procedures and function calls, and how recursion is actually implemented. It is important to understand these processes, not only because they make process sounds possible, but also because the realization of recursion eliminates the multitude of mysteries surrounding its use. Although recursion is very powerful, it is not entirely arbitrary, and recursive misuse and abuse can cause a program to crash.

Data structure and Algorithm analysis: C Language Description _ Original book second Edition CH3 tables, stacks and queues _reading notes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.