1. Trang chủ
  2. » Giáo Dục - Đào Tạo

7 1 stack tủ tài liệu bách khoa

37 71 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 37
Dung lượng 1,36 MB

Nội dung

Trần Thị Thanh Nga ngattt@hcmuaf.edu.vn Khoa Công nghệ thông tin, ĐH Nông Lâm HCM Stacks Stacks  A stack is a container of objects that are inserted and removed according to the Last-In-First-Out (LIFO) principle  Objects can be inserted at any time, but only the last (the most-recently inserted) object can be removed  Inserting an item is known as “pushing” onto the stack “Popping” off the stack is synonymous with removing an item Stacks Stacks examples  Example 5.1: Internet Web browsers store the addresses of recently visited sites on a stack  Each time a user visits a new site, that site's address is "pushed" onto the stack of addresses  The browser then allows the user to "pop" back to previously visited sites using the "back" button  Example 5.2: Text editors usually provide an "undo" mechanism that cancels recent editing operations and reverts to former states ofa document  This undo operation can be accomplished by keeping text changes in a stack Stacks The Stack Abstract Data Type  A stack is an abstract data type (ADT) that supports two main methods:  push(o): Inserts object o onto top of stack Input: Object; Output: none  pop(): Removes the top object of stack and returns it; if stack is empty an error occurs Input: none; Output: Object Stacks The Stack Abstract Data Type  There are other methods:  size(): Return the number of elements in the stack Input: none; Output: integer;  isEmpty(): Return a Boolean indicating if the stack is empty Input: none; Output: boolean;  top(): Return the top element in the stack, without removing it; an error occurs if the stack is empty Input: none; Output: Object;  clear(): Remove all items from the stack Stacks Stack example Stacks A Stack Interface in Java  Because of its importance, the stack data structure is included as a "built-in" class in the java.util package of Java But you can define it your own specific one  Implementing an abstract data type in Java involves two steps  the definition of a Java interface, which describes the names of the methods that the ADT supports  how they are to be declared and used Stacks A Stack Interface in Java  In addition, we must define exceptions for any error conditions that can arise  For example: the error condition that occurs when calling method pop() or top() on an empty stack  throwing an exception of type EmptyStackException public class EmptyStackException extends RuntimeException { public EmptyStackException(String err) { super(err); } } Stacks A Stack Interface in Java public interface StackInterface { public int size(); public boolean isEmpty(); public E top() throws StackException; public void push(E element); public E pop() throws StackException; public void clear(); } Stacks A Simple Array-Based Stack Implementation  The stack consists of:  an array A of a default size (≥ 1),  the variable top that refers to the top element in the stack, top changes from -1 to capacity –  the capacity that refers to the array size  The stack is empty when top = -1, and the stack is full when top = capacity-1 Stacks Postfix Expressions Calculator  The usual notation for writing arithmetic expressions (the notation we learned in elementary school) is called infix notation, in which the operator is written between the operands, for example: a + b  In the early 1920s, the Polish mathematician Jan Lukasiewicz discovered that if operators were written before the operands (prefix or Polish notation; for example, + a b), the parentheses can be omitted Stacks Postfix Expressions Calculator  In the late 1950s, the Australian philosopher and early computer scientist Charles L Hamblin proposed a scheme in which the operators follow the operands (postfix operators), resulting in the Reverse Polish notation  This has the advantage that the operators appear in the order required for computation  For example:  the expression: a + b * c  in a postfix expression is: a b c * + Stacks Postfix Expressions Calculator Stacks Postfix Expressions Calculator Stacks Converting from Infix to Postfix Read in the tokens one at a time If a token is an integer, write it into the output If a token is an operator, push it to the stack, if the stack is empty If the stack is not empty, you pop entries with higher or equal priority and only then you push that token to the stack If a token is a left parentheses '(', push it to the stack If a token is a right parentheses ')', you pop entries until you meet '(' When you finish reading the string, you pop up all tokens which are left there Arithmetic precedence is in increasing order: '+', '-', '*', '/'; Stacks Converting from Infix to Postfix  Infix: 2+(4+3*2+1)/3 Stacks Evaluating a Postfix Expression  Postfix expressions can be evaluated using the following algorithm:  Scan the expression from left to right  When an operator is found, back up to get the required number of operands, perform the operation, and continue Stacks Evaluating a Postfix Expression 593+42**7+* Stacks Evaluating a Postfix Expression Stacks 2*((3+4)-(5-6)) Example: + - - * Push Push Push Read 2 + Pop 4, Pop 3, + = Push Push Push Read Push Read Push Read Push Pop 6, Pop 5, - = -1 -1 Pop -1, Pop 7, - -1 = 8 * Pop 8, Pop 2, * = 16 16 Stacks -1 16 Converting a Number from Decimal to Binary 57 28 Ví dụ: 57 = ???2 14 Stacks 57 = 1110012 1 2 Converting a Number from Decimal to Binary while (n!= 0) { r= n % 2; push (s, r); // push so du vao stack n = n/2; } System.out.print("Kết quả: “); while (!isEmpty(s)) System.out.println(pop(s)); // pop so du khoi stack Stacks Quick Review  A stack is a data structure in which the items are added and deleted from one end only  A stack is a Last In First Out (LIFO) data structure  The basic operations on a stack are as follows:  Push an item onto the stack, pop an item from the stack, retrieve the top element of the stack, initialize the stack, check whether the stack is empty, and check whether the stack is full  A stack can be implemented as an array or a linked list  The middle elements of a stack should not be accessed directly Stacks Quick Review  Stacks are restricted versions of arrays and linked lists  Postfix notation does not require the use of parentheses to enforce operator precedence  In postfix notation, the operators are written after the operands  Postfix expressions are evaluated according to the following rules:  Scan the expression from left to right  If an operator is found, back up to get the required number of operands, evaluate the operator, and continue Stacks Question? Stacks ... occurs if the stack is empty Input: none; Output: Object;  clear(): Remove all items from the stack Stacks Stack example Stacks A Stack Interface in Java  Because of its importance, the stack data... changes from -1 to capacity –  the capacity that refers to the array size  The stack is empty when top = -1, and the stack is full when top = capacity -1 Stacks A Simple Array-Based Stack Implementation... empty stack  throwing an exception of type EmptyStackException public class EmptyStackException extends RuntimeException { public EmptyStackException(String err) { super(err); } } Stacks A Stack

Ngày đăng: 09/11/2019, 07:21

TỪ KHÓA LIÊN QUAN

w