CSE 373: Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 14
Advertisements

COMPSCI 105 S Principles of Computer Science 13 Stacks.
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
Chapter 3 Stacks.
Topic 15 Implementing and Using Stacks
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
TCSS 342, Winter 2005 Lecture Notes
Topic 15 Implementing and Using Stacks
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Building Java Programs
Building Java Programs
CSE 143 Lecture 7 Stacks and Queues reading: "Appendix Q" (see course website) slides created by Marty Stepp and Hélène Martin
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
© 2006 Pearson Addison-Wesley. All rights reserved7A-1 Chapter 7 Stacks (and a bit of generics for flavor)
Topic 3 The Stack ADT.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
Data Structures. The Stack: Definition A stack is an ordered collection of items into which new items may be inserted and from which items may be deleted.
CSE 143 Lecture 9 Interfaces; Stacks and Queues slides created by Marty Stepp
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
COMPSCI 105 SS 2015 Principles of Computer Science 09 ADT & Stacks.
CSE 373: Data Structures and Algorithms Lecture 2: Queues.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Click to edit Master text styles Stacks Data Structure.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
Stacks Stack Abstract Data Type (ADT) Stack ADT Interface
Stacks (and Queues).
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Building Java Programs
Stacks Chapter 5.
Stacks Chapter 6.
Stacks and Queues.
Revised based on textbook author’s notes.
CSE 373: Data Structures and Algorithms
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks and Queues.
Building Java Programs Chapter 14
Building Java Programs
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues.
Building Java Programs
Stacks and queues.
Building Java Programs Chapter 14
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks and Queues.
slides created by Marty Stepp
Building Java Programs
Building Java Programs
Building Java Programs Chapter 14
Welcome to CSE 143! Go to pollev.com/cse143.
Stacks and Queues CLRS, Section 10.1.
CSC 143 Stacks [Chapter 6].
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
slides created by Marty Stepp
slides created by Marty Stepp
Stacks.
Topic 15 Implementing and Using Stacks
Generics, Stack, Queue Based on slides by Alyssa Harding
slides created by Marty Stepp
CSE 373 Java Collection Framework reading: Weiss Ch. 3, 4.8
Data Structures and Algorithms 2/2561
Stacks and Queues.
© 2016 Pearson Education, Ltd. All rights reserved.
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks

Course objectives Learn basic data structures and algorithms data structures – how data is organized algorithms – unambiguous sequence of steps to compute something algorithm analysis – determining how long an algorithm will take to solve a problem Become a better software developer "Data Structures + Algorithms = Programs" -- Niklaus Wirth, author of Pascal language

Abstract Data Types abstract data type (ADT): A specification of a collection of data and the operations that can be performed on it. Describes what a collection does, not how it does it Described in Java with interfaces (e.g., List, Map, Set) Separate from implementation ADTs can be implemented in multiple ways by classes: ArrayList and LinkedList implement List HashSet and TreeSet implement Set LinkedList , ArrayDeque, etc. implement Queue They messed up on Stack; there's no Stack interface, just a class.

List ADT An ordered collection the form A0, A1, ..., AN-1, where N is the size of the list Operations described in Java's List interface (subset): add(el, index) inserts the element at the specified position in the list remove(index) removes the element at the specified position get(index) returns the element at the specified position set(index, el) replaces the element at the specified position with the specified element contains(el) returns true if the list contains the element size() returns the number of elements in the list ArrayList and LinkedList are implementations

Stack ADT stack: a list with the restriction that insertions/deletions can only be performed at the top/end of the list Last-In, First-Out ("LIFO") The elements are stored in order of insertion, but we do not think of them as having indexes. The client can only add/remove/examine the last element added (the "top"). basic stack operations: push: Add an element to the top. pop: Remove the top element. peek: Examine the top element. analogy: trays of food at the sizzler

Applications of Stacks Programming languages and compilers: method calls are placed onto a stack (call=push, return=pop) compilers use stacks to evaluate expressions Matching up related pairs of things: find out whether a string is a palindrome examine a file to see if its braces { } and other operators match convert "infix" expressions to "postfix" or "prefix" Sophisticated algorithms: searching through a maze with "backtracking" many programs use an "undo stack" of previous operations method3 return var local vars parameters method2 method1

Class Stack Stack<E>() constructs a new stack with elements of type E push(value) places given value on top of stack pop() removes top value from stack and returns it; throws EmptyStackException if stack is empty peek() returns top value from stack without removing it; size() returns number of elements in stack isEmpty() returns true if stack has no elements Stack<Integer> s = new Stack<Integer>(); s.push(42); s.push(-3); s.push(17); // bottom [42, -3, 17] top System.out.println(s.pop()); // 17

Stack limitations/idioms Remember: You cannot loop over a stack like you do a list. Stack<Integer> s = new Stack<Integer>(); ... for (int i = 0; i < s.size(); i++) { do something with s.get(i); } Instead, you pull contents out of the stack to view them. common idiom: Remove each element until the stack is empty. while (!s.isEmpty()) { do something with s.pop();

Exercise Write a method symbolsBalanced that accepts a String as a parameter and returns whether or not the parentheses and the curly brackets in that String are balanced as they would have to be in a valid Java program. Use a Stack to solve this problem.