Data Structures and Algorithms in Java 4th phần 4 ppsx

92 491 0
Data Structures and Algorithms in Java 4th phần 4 ppsx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

and then fill the array back up again by popping the elements off of the stack. In Code Fragment 5.8, we give a Java implementation of this algorithm. Incidentally, this method also illustrates how we can use generic types in a simple application that uses a generic stack. In particular, when the elements are popped off the stack in this example, they are automatically returned as elements of the E type; hence, they can be immediately returned to the input array. We show an example use of this method in Code Fragment 5.9. Code Fragment 5.8: A generic method that reverses the elements in an array of type E objects, using a stack declared using the Stack<E> interface. Code Fragment 5.9: A test of the reverse method using two arrays. 278 5.1.5 Matching Parentheses and HTML Tags In this subsection, we explore two related applications of stacks, the first of which is for matching parentheses and grouping symbols in arithmetic expressions. Arithmetic expressions can contain various pairs of grouping symbols, such as • Parentheses: "(" and ")" • Braces: "{" and "}" • Brackets: "[" and "]" • Floor function symbols: " " and "" • Ceiling function symbols: "" and "," and each opening symbol must match with its corresponding closing symbol. For example, a left bracket, "[," must match with a corresponding right bracket, "]," as in the following expression: [(5 + x) − (y + z)]. 279 The following examples further illustrate this concept: • Correct: ( )(( )){([( )])} • Correct: ((( )(( )){([( )])})) • Incorrect: )(( )){([( )])} • Incorrect: ({[])} • Incorrect: (. We leave the precise definition of matching of grouping symbols to Exercise R-5.5. An Algorithm for Parentheses Matching An important problem in processing arithmetic expressions is to make sure their grouping symbols match up correctly. We can use a stack S to perform the matching of grouping symbols in an arithmetic expression with a single left-to- right scan. The algorithm tests that left and right symbols match up and also that the left and right symbols are both of the same type. Suppose we are given a sequence X = x 0 x 1 x 2 …x n−1 , where each x i is a token that can be a grouping symbol, a variable name, an arithmetic operator, or a number. The basic idea behind checking that the grouping symbols in S match correctly, is to process the tokens in X in order. Each time we encounter an opening symbol, we push that symbol onto S, and each time we encounter a closing symbol, we pop the top symbol from the stack S (assuming S is not empty) and we check that these two symbols are of the same type. If the stack is empty after we have processed the whole sequence, then the symbols in X match. Assuming that the push and pop operations are implemented to run in constant time, this algorithm runs in O(n), that is linear, time. We give a pseudo-code description of this algorithm in Code Fragment 5.10. Code Fragment 5.10: Algorithm for matching grouping symbols in an arithmetic expression. 280 Matching Tags in an HTML Document Another application in which matching is important is in the validation of HTML documents. HTML is the standard format for hyperlinked documents on the Internet. In an HTML document, portions of text are delimited by HTML tags. A simple opening HTML tag has the form "<name>" and the corresponding closing tag has the form "</name>." Commonly used HTML tags include • body: document body • h1: section header • center: center justify • p: paragraph • ol: numbered (ordered) list • li: list item. Ideally, an HTML document should have matching tags, although most browsers tolerate a certain number of mismatching tags. We show a sample HTML document and a possible rendering in Figure 5.3 . 281 Figure 5.3: Illustrating HTML tags. (a) An HTML document; (b) its rendering. Fortunately, more or less the same algorithm as in Code Fragment 5.10 can be used to match the tags in an HTML document. In Code Fragments 5.11 and 5.12, we give a Java program for matching tags in an HTML document read from standard input. For simplicity, we assume that all tags are the simple opening or closing tags defined above and that no tags are formed incorrectly. Code Fragment 5.11: A complete Java program for testing if an HTML document has fully matching tags. (Continues in Code Fragment 5.12 .) 282 Code Fragment 5.12: Java program for testing for matching tags in an HTML document. (Continued from 5.11.) Method isHTMLMatched uses a stack to store the names of the opening tags seen so far, similar to how the stack was used in Code Fragment 5.10 . Method parseHTML uses a Scanner s to extract the tags from the HTML document, using the pattern "<[^>]*>," which denotes a string that starts with '<', followed by zero or more characters that are not '>', followed by a '>'. 283 5.2 Queues 284 Another fundamental data structure is the queue. It is a close "cousin" of the stack, as a queue is a collection of objects that are inserted and removed according to the first- in first-out (FIFO) principle. That is, elements can be inserted at any time, but only the element that has been in the queue the longest can be removed at any time. We usually say that elements enter a queue at the rear and are removed from the front. The metaphor for this terminology is a line of people waiting to get on an amusement park ride. People waiting for such a ride enter at the rear of the line and get on the ride from the front of the line. 5.2.1 The Queue Abstract Data Type Formally, the queue abstract data type defines a collection that keeps objects in a sequence, where element access and deletion are restricted to the first element in the sequence, which is called the front of the queue, and element insertion is restricted to the end of the sequence, which is called the rear of the queue. This restriction enforces the rule that items are inserted and deleted in a queue according to the first-in first-out (FIFO) principle. The queue abstract data type (ADT) supports the following two fundamental methods: enqueue(e): Insert element e at the rear of the queue. dequeue(): Remove and return from the queue the object at the front; an error occurs if the queue is empty. Additionally, similar to the case with the Stack ADT, the queue ADT includes the following supporting methods: size(): Return the number of objects in the queue. isEmpty(): Return a Boolean value that indicates whether the queue is empty. front(): Return, but do not remove, the front object in the queue; an error occurs if the queue is empty. Example 5.4: The following table shows a series of queue operations and their effects on an initially empty queue Q of integer objects. For simplicity, we use integers instead of integer objects as arguments of the operations. Operation Output front ← Q ← rear 285 enqueue(5) - (5) enqueue(3) - (5, 3) dequeue( ) 5 (3) enqueue(7) - (3, 7) dequeue( ) 3 (7) front( ) 7 (7) dequeue( ) 7 ( ) dequeue( ) "error" ( ) isEmpty( ) 286 true ( ) enqueue(9) - (9) enqueue(7) - (9, 7) size() 2 (9, 7) enqueue(3) - (9, 7, 3) enqueue(5) - (9, 7, 3, 5) dequeue( ) 9 (7, 3, 5) Example Applications There are several possible applications for queues. Stores, theaters, reservation centers, and other similar services typically process customer requests according to the FIFO principle. A queue would therefore be a logical choice for a data structure to handle transaction processing for such applications. For example, it would be a natural choice for handling calls to the reservation center of an airline or to the box office of a theater. 287 [...]... Abstract Data Type 232 6.2 .4 Doubly Linked List Implementation 236 311 6.3 Iterators 242 6.3.1 The Iterator and Iterable Abstract Data Types 242 6.3.2 The Java For-Each Loop 244 6.3.3 Implementing Iterators 245 6.3 .4 List Iterators in Java 247 6 .4 List ADTs and the Collections Framework 249 6 .4. 1 The Java Collections Framework 249 6 .4. 2 The java. util.LinkedList Class 250 6 .4. 3 312... queues, S and T, which can store integers Bob gives Alice 50 odd integers and 50 even integers and insists that she stores all 100 integers in S and T They then play a game where Bob picks S or T at random and then applies the round-robin scheduler, described in the chapter, to the chosen queue a random number of times If the number left out of the queue at the end of this game is odd, Bob wins Otherwise,... the bridge in 2 minutes, Daisy can cross it in 4 minutes, Crazy can cross it in 10 minutes, and Lazy can cross it in 20 minutes Of course, when two cows are tied to the yoke, they must go at the speed of the slower cow Describe how Bob can get all his cows across the bridge in 34 minutes Projects P-5.1 Implement the stack ADT with a doubly linked list P-5.2 Implement the stack ADT using the Java ArrayList... that uses only D and Q (and no other variables or objects) and results in D storing the elements (1,2,3,5 ,4, 6,7,8), in this order R-5.11 Repeat the previous problem using the deque D and an initially empty stack S Creativity C-5.1 Suppose you have a stack S containing n elements and a queue Q that is initially empty Describe how you can use Q to scan S to see if it contains a certain element x, with... in S using an integer in the range [0,n − 1] that is equal to the number of elements of S that precede e in S The index of an element e in S is the number of elements that are before e in S Hence, the first element in S has index 0 and the last element has index n − 1 Also, if an element of S has index i, its previous element (if it exists) has index i − 1, and its next element (if it exists) has index... above, are included in the java. util.LinkedList class So, if we need to use a deque and would rather not implement one from scratch, we can simply use the built -in java. util.LinkedList class In any case, we show a Deque interface in Code Fragment 5.17 and an implementation of this interface in Code Fragment 5.18 Code Fragment 5.17: Interface Deque documented with comments in Javadoc style (Section... itera-tively applying the private pop and push methods) elements from stack S to stack T until either S becomes empty or T becomes full So, for example, starting from our initial configuration and performing transfer(A, C) results in A now holding 97 elements and C holding 3 Describe a sequence of transfer operations that starts from the initial configuration and results in B holding 4 elements at the... sequence, using the FIFO protocol to identify shares Chapter Notes We were introduced to the approach of defining data structures first in terms of their ADTs and then in terms of concrete implementations by the classic books by Aho, Hopcroft, and Ullman [4, 5], which incidentally is where we first saw aproblem similar to Exercise C-5.6 Exercises C-5.10, C-5.11, and C-5.12 are similar to interview questions... children continue passing the potato until a leader rings a bell, at which point the child holding the potato must leave the game after handing the potato to the next child in the circle After the selected child leaves, the other children close up the circle This process is then continued until there is only one child remaining, who is declared the winner If the leader always uses the strategy of ringing the...A Queue Interface in Java A Java interface for the queue ADT is given in Code Fragment 5.13 This generic interface specifies that objects of arbitrary object types can be inserted into the queue Thus, we don't have to use explicit casting when removing elements Note that the size and isEmpty methods have the same meaning as their counterparts in the Stack ADT These two methods, . begins with a starting child in the circle, and the children continue passing the potato until a leader rings a bell, at which point the child holding the potato must leave the game after handing. complete Java program for testing if an HTML document has fully matching tags. (Continues in Code Fragment 5.12 .) 282 Code Fragment 5.12: Java program for testing for matching tags in an. are inserted and deleted in a queue according to the first -in first-out (FIFO) principle. The queue abstract data type (ADT) supports the following two fundamental methods: enqueue(e): Insert

Ngày đăng: 14/08/2014, 01:21

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan