CS212:Data Structure
|
|
- Rodger Lester
- 6 years ago
- Views:
Transcription
1 C212:Data tructure
2 Direct applications Page-visited history in a Web browser Undo sequence in a text editor Chain of method calls in the Java Virtual Machine Indirect applications Auxiliary data structure for algorithms Component of other data structures Goodrich, Tamassia
3 The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion main() { int i = 5; foo(i); foo(int j) { int k; k = j+1; bar(k); bar(int m) { Goodrich, Tamassia
4 The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion main() { int i = 5; foo(i); foo(int j) { int k; k = j+1; bar(k); bar(int m) { main PC = 2 i = Goodrich, Tamassia
5 The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion main() { int i = 5; foo(i); foo(int j) { int k; k = j+1; bar(k); bar(int m) { foo PC = 3 j = 5 k = 6 main PC = 2 i = Goodrich, Tamassia
6 The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion main() { int i = 5; foo(i); foo(int j) { int k; k = j+1; bar(k); bar(int m) { bar PC = 1 m = 6 foo PC = 3 j = 5 k = 6 main PC = 2 i = Goodrich, Tamassia
7 The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion main() { int i = 5; foo(i); foo(int j) { int k; k = j+1; bar(k); bar(int m) { foo PC = 3 j = 5 k = 6 main PC = 2 i = Goodrich, Tamassia
8 The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion main() { int i = 5; foo(i); foo(int j) { int k; k = j+1; bar(k); bar(int m) { main PC = 2 i = Goodrich, Tamassia
9 The Java Virtual Machine (JVM) keeps track of the chain of active methods with a stack When a method is called, the JVM pushes on the stack a frame containing Local variables and return value Program counter, keeping track of the statement being executed When a method ends, its frame is popped from the stack and control is passed to the method on top of the stack Allows for recursion main() { int i = 5; foo(i); foo(int j) { int k; k = j+1; bar(k); bar(int m) { Goodrich, Tamassia
10 public class Tester { // other methods here public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); while(!s.empty()) l.insert(s.pop()); 10
11 C public class Tester { // other methods here L public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); while(!s.empty()) l.insert(s.pop()); 11
12 public class Tester { // other methods here L C public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); while(!s.empty()) l.insert(s.pop()); T 12
13 public class Tester { // other methods here L C public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); while(!s.empty()) l.insert(s.pop()); T 13
14 public class Tester { // other methods here L C public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); while(!s.empty()) l.insert(s.pop()); 1 T 14
15 public class Tester { // other methods here L C 2 3 public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); while(!s.empty()) l.insert(s.pop()); 1 T 15
16 public class Tester { // other methods here L C 2 3 public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); 2 T while(!s.empty()) l.insert(s.pop()); 1 16
17 public class Tester { // other methods here L C 3 public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); 2 T while(!s.empty()) l.insert(s.pop()); 1 17
18 public class Tester { // other methods here L C 3 public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); 3 T 2 while(!s.empty()) l.insert(s.pop()); 1 18
19 C public class Tester { // other methods here L public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); 3 T 2 while(!s.empty()) l.insert(s.pop()); 1 19
20 C public class Tester { // other methods here L public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); 3 T 2 while(!s.empty()) l.insert(s.pop()); 1 20
21 C public class Tester { L 3 // other methods here public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); 3 T 2 while(!s.empty()) l.insert(s.pop()); 1 21
22 C public class Tester { L 3 2 // other methods here public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); 2 T while(!s.empty()) l.insert(s.pop()); 1 22
23 C public class Tester { L // other methods here public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); while(!s.empty()) l.insert(s.pop()); 1 T 23
24 C public class Tester { L // other methods here public void intreverse(list<integer> l) { tack<integer> s = new tack<integer>(); l.findfirst(); while(!l.empty()) { s.push(l.retrieve()); l.remove(); while(!s.empty()) l.insert(s.pop()); T 24
25 Each (, {, or [ must be paired with a matching ),, or [ correct: ( )(( )){([( )]) correct: ((( )(( )){([( )]))) incorrect: )(( )){([( )]) incorrect: ({[ ]) incorrect: ( 2010 Goodrich, Tamassia 25
26 Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched 2010 Goodrich, Tamassia 26
27 (2+[3-5]*2) Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched 2010 Goodrich, Tamassia 27
28 (2+[3-5]*2) Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched ( 2010 Goodrich, Tamassia 28
29 (2+[3-5]*2) Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched [ ( 2010 Goodrich, Tamassia 29
30 (2+[3-5]*2) Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched [ ( 2010 Goodrich, Tamassia 30
31 (2+[3-5]*2) Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched ( 2010 Goodrich, Tamassia 31
32 (2+[3-5]*2) true Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched 2010 Goodrich, Tamassia 32
33 (6+[8+2)*1) Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched 2010 Goodrich, Tamassia 33
34 (6+[8+2)*1) Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched ( 2010 Goodrich, Tamassia 34
35 (6+[8+2)*1) Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched [ ( 2010 Goodrich, Tamassia 35
36 (6+[8+2)*1) false Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched [ ( 2010 Goodrich, Tamassia 36
37 (6+[8+2]*1 Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched 2010 Goodrich, Tamassia 37
38 (6+[8+2]*1 Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched ( 2010 Goodrich, Tamassia 38
39 (6+[8+2]*1 Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched [ ( 2010 Goodrich, Tamassia 39
40 (6+[8+2]*1 Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched [ ( 2010 Goodrich, Tamassia 40
41 (6+[8+2]*1 false Algorithm ParenMatch(X, n): Input: An array X of n tokens, each of which is either a grouping symbol, a variable, an arithmetic operator, or a number Output: true if and only if all the grouping symbols in X match Let be an empty stack for i=0 to n-1 do if X[i] is an opening grouping symbol then.push(x[i]) else if X[i] is a closing grouping symbol then return false {nothing to match with if.pop() does not match the type of X[i] then return false {wrong type return true {every symbol matched else return false {some symbols were never matched ( 2010 Goodrich, Tamassia 41
42 For fully-correct HTML, each <name> should pair with a matching </name> <body> <center> <h1> The Little Boat </h1> </center> <p> The storm tossed the little boat like a cheap sneaker in an old washing machine. The three drunken fishermen were used to such treatment, of course, but not the tree salesman, who even as a stowaway now felt that he had overpaid for the voyage.</p> <ol> <li> Will the salesman die? </li> <li> What color is the boat? </li> <li> And what about Naomi? </li> </ol> </body> The Little Boat The storm tossed the little boat like a cheap sneaker in an old washing machine. The three drunken fishermen were used to such treatment, of course, but not the tree salesman, who even as a stowaway now felt that he had overpaid for the voyage. 1. Will the salesman die? 2. What color is the boat? 3. And what about Naomi? 2010 Goodrich, Tamassia 42
43 import java.io.*; import java.util.canner; import net.datastructures.*; /** implified test of matching tags in an HTML document. */ public class HTML { /** trip the first and last characters off a <tag> string. */ public static tring stripends(tring t) { if (t.length() <= 2) return null; // this is a degenerate tag return t.substring(1, t.length() - 1); /** Test if a stripped tag string is empty or a true opening tag. */ public static boolean isopeningtag(tring tag) { return (tag.length() == 0) (tag.charat(0)!= '/'); /** Test if stripped tag1 matches closing tag2 (first character is '/'). */ public static boolean arematchingtags(tring tag1, tring tag2) { return tag1.equals(tag2.substring(1)); // test against name after '/' 2010 Goodrich, Tamassia 43
44 /** Test if every opening tag has a matching closing tag. */ public static boolean ishtmlmatched(tring[] tag) { tack<tring> = new Nodetack<tring>(); // tack for matching tags for (int i = 0; (i < tag.length) && (tag[i]!= null); i++) { if (isopeningtag(tag[i])).push(tag[i]); // opening tag; push it on the stack else { if (.isempty()) return false;// nothing to match if (!arematchingtags(.pop(), tag[i])) return false; // wrong match if(.isempty()) return true; // we matched everything return false; // we have some tags that never were matched 2010 Goodrich, Tamassia 44
45 public final static int CAPACITY = 1000; // Tag array size /* Parse an HTML document into an array of html tags */ public static tring[] parsehtml(canner s) { tring[] tag = new tring[capacity]; // our tag array (initially all null) int count = 0; // tag counter tring token; // token returned by the scanner s while (s.hasnextline()) { while ((token = s.findinline("<[^>]*>"))!= null) // find the next tag tag[count++] = stripends(token); // strip the ends off this tag s.nextline(); // go to the next line return tag; // our array of (stripped) tags public static void main(tring[] args) throws IOException { if (ishtmlmatched(parsehtml(new canner(ystem.in)))) ystem.out.println("the input file is a matched HTML document."); else ystem.out.println("the input file is not a matched HTML document."); 2010 Goodrich, Tamassia 45
46 14 3 * = (14 (3 * 2) ) + 7 Operator precedence * has precedence over +/ Associativity operators of the same precedence group evaluated from left to right Example: (x y) + z rather than x (y + z) Idea: push each operator on the stack, but first pop and perform higher and equal precedence operations tallmann 46
47 Two stacks: optk holds operators valtk holds values Use $ as special end of input token with lowest precedence Algorithm doop() x valtk.pop(); y valtk.pop(); op optk.pop(); valtk.push( y op x ) Algorithm repeatops( refop ): while ( valtk.size() > 1 prec(refop) prec(optk.top() ) doop() Algorithm EvalExp() Input: a stream of tokens representing an arithmetic expression (with numbers) Output: the value of the expression while there s another token z if isnumber(z) then else repeatops($); return valtk.top() valtk.push(z) repeatops(z); optk.push(z) 47
48 * Operator has lower precedence than +/ * * * $ $ 5 14 $ F 2010 tallmann 48
Stacks Goodrich, Tamassia Stacks
Stacks Stacks 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations
More informationADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S
Stacks Stacks & Queues A linear sequence, or list, is an ordered collection of elements: S = (s 1, s 2,..., s n ) Stacks and queues are finite linear sequences. A Stack is a LIFO (Last In First Out) list.
More informationStacks Goodrich, Tamassia
Stacks Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations Example:
More informationData Structures Lecture 5
Fall 2017 Fang Yu Software Security Lab. Dept. Management Information Systems, National Chengchi University Data Structures Lecture 5 Announcement Project Proposal due on Nov. 9 Remember to bring a hardcopy
More informationStacks Goodrich, Tamassia, Goldwasser Stacks
Presentation for use with the textbook Data Structures and Algorithms in Java, 6 th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Stacks Stacks 1 The Stack ADT The Stack ADT
More informationCOMP9024: Data Structures and Algorithms
COMP9024: Data Structures and Algorithms Week Four: Stacks and Queues Hui Wu Session 2, 2015 http://www.cse.unsw.edu.au/~cs9024 1 Outline Stacks Queues 2 Stacks 3 Abstract Data Types (ADTs) An abstract
More informationCSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1
CSE 2100 Data Structures and Algorithms... In Java Stacks 1 What is Stack A stack is a collection of objects that are inserted and removed according to the last-in, first-out (LIFO) principle. Internet
More informationStacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java
Stacks (01F) Lecture6: Stacks What is stack? An abstract data type storing arbitrary objects. An ordered list in which all insertions and deletions are made at one end, called top Push Pop Top Bohyung
More informationCOMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)
COMP250: Stacks Jérôme Waldispühl School of Computer Science McGill University Based on slides from (Goodrich & Tamassia, 2004) 2004 Goodrich, Tamassia The Stack ADT A Stack ADT is a list that allows only
More informationOutline and Reading. The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack.
Stacks Outline and Reading The Stack ADT ( 4.2.1) Applications of Stacks ( 4.2.3) Array-based implementation ( 4.2.2) Growable array-based stack Stacks 2 Abstract Data Types (ADTs) An abstract data type
More informationStacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011
Stacks (5.1) CSE 2011 Winter 2011 26 January 2011 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error
More informationOutline and Reading. The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.
Stacks Outline and Reading The Stack ADT ( 2.1.1) Applications of Stacks ( 2.1.1) Array-based implementation ( 2.1.1) Growable array-based stack ( 1.5) Stacks 2 Abstract Data Types (ADTs) An abstract data
More informationAbstract Data Types (ADTs) Stacks. The Stack ADT ( 4.2) Stack Interface in Java
Abstract Data Types (ADTs) tacks A abstract data type (ADT) is a abstractio of a data structure A ADT specifies: Data stored Operatios o the data Error coditios associated with operatios Example: ADT modelig
More informationCH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES
CH4.2-4.3. ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN JAVA, GOODRICH, TAMASSIA AND GOLDWASSER
More informationThe Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.
The Stack ADT Stacks Set of objects in which the location an item is inserted and deleted is prespecified Stacks! Insert in order! Delete most recent item inserted! LIFO - last in, first out Stacks 2 The
More informationDataStruct 5. Stacks, Queues, and Deques
2013-2 DataStruct 5. Stacks, Queues, and Deques Michael T. Goodrich, et. al, Data Structures and Algorithms in C++, 2 nd Ed., John Wiley & Sons, Inc., 2011. October 30, 2013 Advanced Networking Technology
More information1/18/12. Chapter 5: Stacks, Queues and Deques. Stacks. Outline and Reading. Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University
Chapter 5: Stacks, ueues and Deques Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich,
More informationHOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS
HOWDY! WELCOME TO CSCE 221 DATA STRUCTURES AND ALGORITHMS SYLLABUS ABSTRACT DATA TYPES (ADTS) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations
More informationdata structures and algorithms lecture 6
data structures and algorithms 2017 09 21 lecture 6 overview lower bound counting sort radix sort stacks queues material question we have seen for example insertion sort: worst-case in O(n 2 ) merge sort:
More informationLecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists
Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists Chapters 3.1-3.3, 5.1-5.2, 6.1-1 - Core Collection Interfaces - 2 - The Java Collections Framework Interface Abstract
More informationLecture 3 Linear Data Structures
Lecture 3 Linear Data Structures - 1 - Learning Outcomes Based on this lecture, you should: Know the basic linear data structures Be able to express each as an Abstract Data Type (ADT) Be able to specify
More informationCOSC This week. List ADT. Abstract Data Type (ADT) Lis t ADT Implementations. Data Struc tures and Algorithms
This week COSC 1030.03 Week 9, March 8, 2004 Read chapter 19 Work on assignment #3 Discussed in Tutorial this week Must have a partial solution shown to your TA next week. Data structures (continued) S
More informationCSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013
CSCI 136 Written Exam #1 Fundamentals of Computer Science II Spring 2013 Name: This exam consists of 5 problems on the following 6 pages. You may use your double-sided hand-written 8 ½ x 11 note sheet
More informationCIS Fall Data Structures Midterm exam 10/16/2012
CIS 2168 2012 Fall Data Structures Midterm exam 10/16/2012 Name: Problem 1 (30 points) 1. Suppose we have an array implementation of the stack class, with ten items in the stack stored at data[0] through
More informationFoundations of Data Structures
Foundations of Data Structures Lecture 4 Elementary Abstract Data Types (ADT): Stack Queue 1 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies:
More informationComputer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh
Computer Science CS221 Test 2 Name 1. Give a definition of the following terms, and include a brief example. a) Big Oh b) abstract class c) overriding d) implementing an interface 10/21/1999 Page 1 of
More information-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.
Topic 15 Implementing and Using "stack n. The set of things a person has to do in the future. "I haven't done it yet because every time I pop my stack something new gets pushed." If you are interrupted
More informationDesign Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8
Subroutines and Control Abstraction Textbook, Chapter 8 1 Subroutines and Control Abstraction Mechanisms for process abstraction Single entry (except FORTRAN, PL/I) Caller is suspended Control returns
More informationStack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.
STACK Stack ADT 2 A stack is an abstract data type based on the list data model All operations are performed at one end of the list called the top of the stack (TOS) LIFO (for last-in first-out) list is
More informationCS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12
CS 151 Linked Lists, Recursively Implemented 1 2 Linked Lists, Revisited Recall that a linked list is a structure that represents a sequence of elements that are stored non-contiguously in memory. We can
More informationLecture 3: Stacks & Queues
Lecture 3: Stacks & Queues Prakash Gautam https://prakashgautam.com.np/dipit02/ info@prakashgautam.com.np 22 March, 2018 Objectives Definition: Stacks & Queues Operations of Stack & Queues Implementation
More informationApplication of Stack (Backtracking)
Application of Stack (Backtracking) Think of a labyrinth or maze How do you find a way from an entrance to an exit? Once you reach a dead end, you must backtrack. But backtrack to where? to the previous
More informationCh02 Data Structures
Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Ch02 Data Structures xkcd Seven http://xkcd.com/1417/ Used with permission under
More informationLecture 3 Linear Data Structures
Lecture 3 Linear Data Structures Chapters 3.1-3.3, 5.1-5.2, 6.1-1 - Outline Our goal in this lecture is to Review the basic linear data structures Demonstrate how each can be defined as an Abstract Data
More informationCh02 Data Structures
Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Ch02 Data Structures xkcd Seven http://xkcd.com/1417/ Used with permission under
More informationLecture 3 Linear Data Structures
Lecture 3 Linear Data Structures - 1 - Learning Outcomes Based on this lecture, you should: Know the basic linear data structures Be able to express each as an Abstract Data Type (ADT) Be able to specify
More informationCOMP 250 Winter stacks Feb. 2, 2016
Stack ADT You are familiar with stacks in your everyday life. You can have a stack of books on a table. You can have a stack of plates on a shelf. In computer science, a stack is an abstract data type
More informationCOMP 250. Lecture 8. stack. Sept. 25, 2017
COMP 250 Lecture 8 stack Sept. 25, 2017 1 get(i) set(i,e) add(i,e) remove(i) remove(e) clear() isempty() size() What is a List (abstract)? // Returns the i-th element (but doesn't remove it) // Replaces
More informationJava Primer 1: Types, Classes and Operators
Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,
More informationDo not turn to the next page until the start of the exam.
Principles of Java Language with Applications, PIC20a E. Ryu Winter 2017 Final Exam Monday, March 20, 2017 3 hours, 8 questions, 100 points, 11 pages While we don t expect you will need more space than
More informationCS 211 Programming Practicum Spring 2017
Due: Tuesday, 3/28/17 at 11:59 pm Infix Expression Evaluator Programming Project 5 For this lab, write a JAVA program that will evaluate an infix expression. The algorithm REQUIRED for this program will
More informationCMSC 132: Object-Oriented Programming II. Stack and Queue
CMSC 132: Object-Oriented Programming II Stack and Queue 1 Stack Allows access to only the last item inserted. An item is inserted or removed from the stack from one end called the top of the stack. This
More informationGRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013
1 GRAMMARS & PARSING Lecture 7 CS2110 Fall 2013 Pointers to the textbook 2 Parse trees: Text page 592 (23.34), Figure 23-31 Definition of Java Language, sometimes useful: http://docs.oracle.com/javase/specs/jls/se7/html/index.html
More informationLinear Data Structure
Linear Data Structure Definition A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array Linked List Stacks Queues Operations on linear Data Structures Traversal
More informationITEC2620 Introduction to Data Structures
9//07 ITEC60 Introduction to Data Structures Lecture 7a ADTs and Stacks Abstract Data Types A way to specify the functionality of an entity without worrying about its implementation Similar to a JAVA interface
More information16. Dynamic Data Structures
Data Structures 6. Dynamic Data Structures A data structure is a particular way of organizing data in a computer so that it can be used efficiently Linked lists, Abstract data types stack, queue, Sorted
More informationComputational Expression
Computational Expression Variables, Primitive Data Types, Expressions Janyl Jumadinova 28-30 January, 2019 Janyl Jumadinova Computational Expression 28-30 January, 2019 1 / 17 Variables Variable is a name
More informationLinked List Implementation of Queues
Outline queue implementation: linked queue application of queues and stacks: data structure traversal double-ended queues application of queues: simulation of an airline counter random numbers recursion
More informationOutline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring
Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,
More informationStacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 5 Copyright 2012 by Pearson Education, Inc. All rights reserved Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions A Problem Solved: Checking for Balanced
More informationAlgorithms & Datastructures Laboratory Exercise Sheet 1
Algorithms & Datastructures Laboratory Exercise Sheet 1 Wolfgang Pausch Heiko Studt René Thiemann Tomas Vitvar
More informationStacks Fall 2018 Margaret Reid-Miller
Stacks 15-121 Fall 2018 Margaret Reid-Miller Today Exam 2 is next Tuesday, October 30 Today: Quiz 5 solutions Recursive add from last week (see SinglyLinkedListR.java) Stacks ADT (Queues on Thursday) ArrayStack
More informationQuestion 1a) Trace of program
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = 4, 8, 2, -9, 6; s = 1; r = 0; i = x.length - 1; while (i > 0) s = s * -1; i = i - 1; r = r + s * x[i]; System.out.println(r);
More informationCS159. Nathan Sprague
CS159 Nathan Sprague What s wrong with the following code? 1 /* ************************************************** 2 * Return the mean, or -1 if the array has length 0. 3 ***************************************************
More informationMore Data Structures (Part 1) Stacks
More Data Structures (Part 1) Stacks 1 Stack examples of stacks 2 Top of Stack top of the stack 3 Stack Operations classically, stacks only support two operations 1. push 2. pop add to the top of the stack
More informationCS 211 Programming Practicum Spring 2018
Due: Thursday, 4/5/18 at 11:59 pm Infix Expression Evaluator Programming Project 5 For this lab, write a C++ program that will evaluate an infix expression. The algorithm REQUIRED for this program will
More informationCS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University
CS 11 Introduction to Computing II Wayne Snyder Department Boston University Today Object-Oriented Programming Concluded Stacks, Queues, and Priority Queues as Abstract Data Types Reference types: Basic
More informationLecture 4: Stack Applications CS2504/CS4092 Algorithms and Linear Data Structures. Parentheses and Mathematical Expressions
Lecture 4: Applications CS2504/CS4092 Algorithms and Linear Data Structures Dr Kieran T. Herley Department of Computer Science University College Cork Summary. Postfix notation for arithmetic expressions.
More informationinfix expressions (review)
Outline infix, prefix, and postfix expressions queues queue interface queue applications queue implementation: array queue queue implementation: linked queue application of queues and stacks: data structure
More informationSPIM Procedure Calls
SPIM Procedure Calls 22C:60 Jonathan Hall March 29, 2008 1 Motivation We would like to create procedures that are easy to use and easy to read. To this end we will discuss standard conventions as it relates
More information1. Stack Implementation Using 1D Array
Lecture 5 Stacks 1 Lecture Content 1. Stack Implementation Using 1D Array 2. Stack Implementation Using Singly Linked List 3. Applications of Stack 3.1 Infix and Postfix Arithmetic Expressions 3.2 Evaluate
More informationData Abstraction and Specification of ADTs
CITS2200 Data Structures and Algorithms Topic 4 Data Abstraction and Specification of ADTs Example The Reversal Problem and a non-adt solution Data abstraction Specifying ADTs Interfaces javadoc documentation
More informationAssignment 5. Introduction
Assignment 5 Introduction The objectives of this assignment are to exercise a few advanced object oriented programming and basic data structures concepts. The first mini-goal is to understand that objects
More informationCS350: Data Structures Stacks
Stacks James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola Stacks Stacks are a very common data structure that can be used for a variety of data storage
More informationCS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5
CS115 INTRODUCTION TO COMPUTER SCIENCE 1 Additional Notes Module 5 Example my-length (Slide 17) 2 (define (my-length alos) [(empty? alos) 0] [else (+ 1 (my-length (rest alos)))])) (my-length empty) alos
More information1. Short circuit AND (&&) = if first condition is false then the second is never evaluated
1. What are the 2 types of short circuit operators. 1. Short circuit AND (&&) = if first condition is false then the second is never evaluated because true and false gives you false only. 2. Short circuit
More informationKeeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10
Keeping Order:! Stacks, Queues, & Deques 1 Stacks 2 Stacks A stack is a last in, first out (LIFO) data structure Primary Operations: push() add item to top pop() return the top item and remove it peek()
More informationAn Introduction to Trees
An Introduction to Trees Alice E. Fischer Spring 2017 Alice E. Fischer An Introduction to Trees... 1/34 Spring 2017 1 / 34 Outline 1 Trees the Abstraction Definitions 2 Expression Trees 3 Binary Search
More informationStack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5
What is Stack? Stack 1. Stack is LIFO Structure [ Last in First Out ] 2. Stack is Ordered List of Elements of Same Type. 3. Stack is Linear List 4. In Stack all Operations such as Insertion and Deletion
More informationCosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs
1 Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs Michael Albert michael.albert@cs.otago.ac.nz Keywords: abstract data type, collection, generic class type, stack 2 Collections
More information15. Stacks and Queues
COMP1917 15s2 15. Stacks and Queues 1 COMP1917: Computing 1 15. Stacks and Queues Reading: Moffat, Section 10.1-10.2 Overview Stacks Queues Adding to the Tail of a List Efficiency Issues Queue Structure
More informationStacks, Queues (cont d)
Stacks, Queues (cont d) CSE 2011 Winter 2007 February 1, 2007 1 The Adapter Pattern Using methods of one class to implement methods of another class Example: using List to implement Stack and Queue 2 1
More informationInput-Output and Exception Handling
Software and Programming I Input-Output and Exception Handling Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Outline Reading and writing text files Exceptions The try block catch and finally
More informationCSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige
CSC 308 2.0 System Development with Java Exception Handling Department of Statistics and Computer Science 1 2 Errors Errors can be categorized as several ways; Syntax Errors Logical Errors Runtime Errors
More informationIntroduction to Programming Using Java (98-388)
Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;
More informationSystem Software Assignment 1 Runtime Support for Procedures
System Software Assignment 1 Runtime Support for Procedures Exercise 1: Nested procedures Some programming languages like Oberon and Pascal support nested procedures. 1. Find a run-time structure for such
More informationcsci 210: Data Structures Stacks and Queues
csci 210: Data Structures Stacks and Queues 1 Summary Topics Stacks and Queues as abstract data types ( ADT) Implementations arrays linked lists Analysis and comparison Applications: searching with stacks
More informationCS 211. Project 5 Infix Expression Evaluation
CS 211 Project 5 Infix Expression Evaluation Part 1 Create you own Stack Class Need two stacks one stack of integers for the operands one stack of characters for the operators Do we need 2 Stack Classes?
More informationPostfix Notation is a notation in which the operator follows its operands in the expression (e.g ).
Assignment 5 Introduction For this assignment, you will write classes to evaluate arithmetic expressions represented as text. For example, the string "1 2 ( * 4)" would evaluate to 15. This process will
More informationWhat is software testing? Software testing is designing, executing and evaluating test cases in order to detect faults.
ϖοιδ τεσταδδανδχουντ() { ασσερ τεθυαλσ(1, ο.αδδανδχουντ(νεω ΑρραψΛιστ()); ϖοιδ τεσταδδανδχουντ() { ασσερ τεθυαλσ(1, ο.αδδανδχουντ(νεω ΑρραψΛιστ()); ιντ αδδανδχουντ(λιστ λιστ) { ρετυρν λιστ.σιζε(); ιντ
More informationCSC 222: Computer Programming II. Spring 2005
CSC 222: Computer Programming II Spring 2005 Stacks and recursion stack ADT push, pop, peek, empty, size ArrayList-based implementation, java.util.stack application: parenthesis/delimiter matching postfix
More informationExceptions and Design
Exceptions and Exceptions and Table of contents 1 Error Handling Overview Exceptions RuntimeExceptions 2 Exceptions and Overview Exceptions RuntimeExceptions Exceptions Exceptions and Overview Exceptions
More informationEXAMINATIONS 2011 Trimester 2, MID-TERM TEST. COMP103 Introduction to Data Structures and Algorithms SOLUTIONS
T E W H A R E W Ā N A N G A O T E Ū P O K O O T E I K A A M Ā U I VUW V I C T O R I A UNIVERSITY OF WELLINGTON Student ID:....................... EXAMINATIONS 2011 Trimester 2, MID-TERM TEST COMP103 Introduction
More informationCS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find
CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS
More informationStacks and their Applications
Stacks and their Applications Lecture 23 Sections 18.1-18.2 Robb T. Koether Hampden-Sydney College Fri, Mar 16, 2018 Robb T. Koether Hampden-Sydney College) Stacks and their Applications Fri, Mar 16, 2018
More informationfrom inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions
Exam information in lab Tue, 18 Apr 2017, 9:00-noon programming part from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions
More informationBasic Data Structures
Basic Data Structures Some Java Preliminaries Generics (aka parametrized types) is a Java mechanism that enables the implementation of collection ADTs that can store any type of data Stack s1
More informationITI Introduction to Computing II
index.pdf March 17, 2013 1 ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Version of March 17, 2013 Definitions A List is a linear abstract
More informationCollections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Collections Chapter 12 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 2 Scope Introduction to Collections: Collection terminology The Java Collections API Abstract nature of collections
More informationStacks have many uses Arithmetic Language parsing Keeping track of recursion (more in this in a week or so)
Implementing Stacks Basics of Exceptions OK, so stacks are useful Stacks have many uses Arithmetic Language parsing Keeping track of recursion (more in this in a week or so) How can stacks be implemented?
More informationCMPS 12A - Winter 2002 Final Exam A March 16, Name: ID:
CMPS 12A - Winter 2002 Final Exam A March 16, 2002 Name: ID: This is a closed note, closed book exam. Any place where you are asked to write code, you must declare all variables that you use. However,
More informationCMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011
CMPSCI 187: Programming With Data Structures Lecture 12: Implementing Stacks With Linked Lists 5 October 2011 Implementing Stacks With Linked Lists Overview: The LinkedStack Class from L&C The Fields and
More informationAssociate Professor Dr. Raed Ibraheem Hamed
Associate Professor Dr. Raed Ibraheem Hamed University of Human Development, College of Science and Technology Computer Science Department 2015 2016 1 What this Lecture is about: Stack Structure Stack
More informationWhat can go wrong in a Java program while running?
Exception Handling See https://docs.oracle.com/javase/tutorial/ essential/exceptions/runtime.html See also other resources available on the module webpage This lecture Summary on polymorphism, multiple
More informationCOMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others
COMP-202 Recursion Recursion Recursive Definitions Run-time Stacks Recursive Programming Recursion vs. Iteration Indirect Recursion Lecture Outline 2 Recursive Definitions (1) A recursive definition is
More informationProject 1: Implementation of the Stack ADT and Its Application
Project 1: Implementation of the Stack ADT and Its Application Dr. Hasmik Gharibyan Deadlines: submit your files via handin by midnight (end of the day) on Thursday, 10/08/15. Late submission: submit your
More informationBasic Data Structures 1 / 24
Basic Data Structures 1 / 24 Outline 1 Some Java Preliminaries 2 Linked Lists 3 Bags 4 Queues 5 Stacks 6 Performance Characteristics 2 / 24 Some Java Preliminaries Generics (aka parametrized types) is
More informationPace University. Fundamental Concepts of CS121 1
Pace University Fundamental Concepts of CS121 1 Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University October 12, 2005 This document complements my tutorial Introduction
More informationCPSC 211 Data Structures & Implementations (c) Texas A&M University [ 165] Postfix Expressions
CPSC 211 Data Structures & Implementations (c) Texas A&M University [ 165] Postfix Expressions We normally write arithmetic expressions using infix notation: the operator (such as +) goes in between the
More informationCS 211 Programming Practicum Fall 2018
Due: Wednesday, 11/7/18 at 11:59 pm Infix Expression Evaluator Programming Project 5 For this lab, write a C++ program that will evaluate an infix expression. The algorithm REQUIRED for this program will
More information