COMPUTER 2430 Object Oriented Programming and Data Structures I

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

Templates in C++ Template function in C++ makes it easier to reuse classes and functions. A template can be viewed as a variable that can be instantiated.
Sample PMT online… Browse 1120/sumII05/PMT/2004_1/ 1120/sumII05/PMT/2004_1/
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Abstract Data Types (ADT) Collection –An object that can hold a list of other objects Homogeneous Collection –Contains elements all of the same type –Example:
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
Stacks CS-240 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed in reverse.
1 CSCD 326 Data Structures I Stacks. 2 Data Type Stack Most basic property: last item in (most recently inserted) is first item out LIFO - last in first.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5) Java.util.Stack class Java.util.Vector.
CHAPTER 6 Stacks Array Implementation. 2 Stacks A stack is a linear collection whose elements are added and removed from one end The last element to be.
CST Razdan et al Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing.
Stacks CS 308 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Stacks CS-240 & CS-341 Dick Steflik. Stacks Last In, First Out operation - LIFO As items are added they are chronologically ordered, items are removed.
Data Structures Lecture-12 : STL Azhar Maqsood NUST Institute of Information Technology (NIIT)
What is a Stack? n Logical (or ADT) level: A stack is an ordered group of homogeneous items in which the removal and addition of items can take place only.
1 Stacks – Chapter 3 A stack is a data structure in which all insertions and deletions of entries are made at one end, called the top of the stack. Alternatively,
Stack and Queue.
Week7 Stack Data Structures & Algorithms. Introduction to Stacks and Queues Widely used data structures Ordered List of element Easy to implement Easy.
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
Chapter 7 Stacks I CS Data Structures I COSC 2006 April 22, 2017
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.
CS 2430 Day 26. Announcements Exam #2: Wednesday, April 3 –Review in lab on Tuesday, April 2 –Sample problems sent via .
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
1 Stacks. 2 A stack has the property that the last item placed on the stack will be the first item removed Commonly referred to as last-in, first-out,
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Today’s Agenda  Generic  Iterators CS2336: Computer Science II.
CS 2430 Day 24. Announcements Quiz this Friday Program 5 posted on Monday Program 4 due date: Friday at 10pm Program 4 grace date: Wednesday at 10pm (don’t.
Access Modifiers Control which classes use a feature Only class-level variables may be controlled by access modifiers Modifiers 1. public 2. protected.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - Last In First Out The last (most recent) item.
3/3/20161 Stacks and Queues Introduction to Data Structures Ananda Gunawardena.
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.
Programming Abstractions
Sections 3.4 Formal Specification
Elementary Data Structures
Computing with C# and the .NET Framework
Revised based on textbook author’s notes.
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,
Week 3 - Friday CS221.
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,
Stack A stack is a linear, homogeneous, container that stores and dispenses its content in a LIFO manner. LIFO - The last (most recent) item inserted,
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
CSE 373: Data Structures and Algorithms
COMPUTER 2430 Object Oriented Programming and Data Structures I
Algorithms and Data Structures
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
Building Java Programs Chapter 14
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks.
Abstract Data Types (ADT)
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Introduction to Programming
slides created by Alyssa Harding
CSC 143 Stacks [Chapter 6].
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
COMPUTER 2430 Object Oriented Programming and Data Structures I
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks CS-240 Dick Steflik.
Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Presentation transcript:

COMPUTER 2430 Object Oriented Programming and Data Structures I

Quiz 3 1. Evaluate the following expressions a) Postfix Answer: -1 b) Prefix Answer: 29

Quiz 3 3. Convert the following to prefix and postfix a) Prefix + A - * B / C D E b) Postfix A B C D / * E - +

Quiz 3: As a User public Object removeAndReturnBottomElement( Stack s, int size ) { Stack temp = new Stack(size); // Hint: use another stack! if (s.isEmpty()) return null; while (!s.isEmpty()) temp.push( s.pop() ); Object obj = temp.pop(); while (!temp.isEmpty()) s.push) temp.pop() ); return obj; }

!s.isEmpty() public Object removeAndReturnBottomElement( Stack s, int size ) { . . . if (s.isEmpty()) return null; // while (!s.isEmpty()) while (s.isEmpty() == false) temp.push( s.pop() ); Object obj = temp.pop(); while (temp.isEmpty() == false) s.push) temp.pop() ); return obj; }

Quiz 3: As a User public Object removeAndReturnBottomElement( Stack s, int size ) { Stack temp = new Stack(size); // Hint: use another stack! if (s.isEmpty()) return null; while (!s.isEmpty()) temp.push( s.pop() ); Object obj = temp.pop(); s = temp; // will it work? // NO! return obj; }

Quiz 3: As an Implementer public class Stack { private Object[] items; private int top; public Object getBottomElement() if (top == 0) return null; Object obj = items[0]; top --; for (int i = 0; i < top; i ++) items[i] = items[i + 1]; return obj; }

Quiz 3: As an Implementer public class Stack { . . . public Object getBottomElement() if (top == 0) return null; Object obj = items[0]; for (int i = 0; i < top - 1; i ++) items[i] = items[i + 1]; top --; return obj; }

public class Stack { private Object[] items; private int top = 0; public Stack( int size ) ... public void push( Object obj ) public Object pop() public boolean isEmpty() public boolean isFull() }

Example Stack personList = new Stack(100); Person p = new Person(); personList.push( p ); // Valid? // Yes. // public void push( Object obj ) // Can use child when parent is expected. ... p = personList.pop(); // Valid? // NO! // public Object pop() // Cannot use parent when child is expected. p = (Person) personList.pop(); // Must cast!

Stack of Person public class PersonStack { private Person[] items; private int top = 0; public Stack( int size ) ... public void push( Person obj ) public Person pop() public boolean isEmpty() public boolean isFull() }

Stack of Person public class PersonStack { private Person[] items; ... } PersonStack personList = new PersonStack(100); Person p; p = personList.pop(); // Valid? // Yes! No cast needed. Object obj = new Object(); personList.push( obj ); // Valid? // NO! // Cannot use parent when child is expected.

Stack for Each Class public class PersonStack { private Person[] items; ... } public class DateStack private Date[] items; public class TVectorStack private TVector[] items; The code is almost the same!

Generic Stack public class Stack<E> { private E[] items; ... } E represents a class The class Stack can be instantiated with a particular class E is a parameter E must be a class and cannot be a primitive type

public class Stack<E> { private E[] items; private int top = 0; public Stack( int size ) items = (E[])new Object[size]; // Java Warning possible } public boolean isEmpty() return top == 0; public boolean isFull() return top >= items.length; ...

public class Stack<E> { private E[] items; private int top = 0; . . . public void push ( E x ) items[top ++] = x; } public E pop() return items[-- top];

Example Stack<Person> personStack = new Stack<Person>(100); Stack<Date> dateStack = new Stack<Date>(100); Person p; Date d; ... p = personStack.pop(); d = dateStack.pop(); // No cast needed! d = new Date(); dateStack.push( d ); // Valid? // Yes personStack.push( d ); // Valid? // NO!

Generic Stack RPN Evaluation Prog 4 Generic Stack RPN Evaluation

Prog 4 Class TVector Java class Vector 4-dimensional Example: (-7,21,5,-33) Java class Vector Generic class Methods: size() get(i) add(x)

Other Generic Classes Generic Queue More in CS 2630

Prog 3 & Prog 4