Data Structures G5029

Similar documents
Associate Professor Dr. Raed Ibraheem Hamed

Outline. Example stack Version 1 -- int stack with fixed array Version 2 -- int stack with flexible array Version 3 -- with interface

ITEC2620 Introduction to Data Structures

What can go wrong in a Java program while running?

Lecture Data Structure Stack

1.00 Lecture 26. Data Structures: Introduction Stacks. Reading for next time: Big Java: Data Structures

COMP250: Stacks. Jérôme Waldispühl School of Computer Science McGill University. Based on slides from (Goodrich & Tamassia, 2004)

Interfaces & Generics

Data Abstraction and Specification of ADTs

Stacks. Lecture6: Stacks. Stack Operations. Stack Interface in Java

CMPSCI 187: Programming With Data Structures. Lecture #11: Implementing Stacks With Arrays David Mix Barrington 28 September 2012

Stacks (5.1) Abstract Data Types (ADTs) CSE 2011 Winter 2011

Stacks and Queues. David Greenstein Monta Vista

Queues. CITS2200 Data Structures and Algorithms. Topic 5

What can we do with Coin dispenser?

Write a program to implement stack or any other data structure in Java ASSIGNMENT NO 15

Chapter 2. Stack & Queues. M hiwa ahmad aziz

CSCD 326 Data Structures I Stacks

Stack Implementation

Stack ADT. ! push(x) puts the element x on top of the stack! pop removes the topmost element from the stack.

Lists are great, but. Stacks 2

CMSC 132, Object-Oriented Programming II Summer Lecture 9:

Stacks. Gaddis 18.1, Molly A. O'Neil CS 2308 :: Spring 2016

CT 229 Object-Oriented Programming Continued

Computer Science CS221 Test 2 Name. 1. Give a definition of the following terms, and include a brief example. a) Big Oh

Derive From A Linear List Class. Stacks. Derive From ArrayLinearList. Derive From ArrayLinearList. Derive From ArrayLinearList.

ADTs Stack and Queue. Outline

No Aids Allowed. Do not turn this page until you have received the signal to start.

The Stack ADT. Stacks. The Stack ADT. The Stack ADT. Set of objects in which the location an item is inserted and deleted is prespecified.

CSE 143 SAMPLE MIDTERM

CSE Data Structures and Algorithms... In Java! Stacks. CSE2100 DS & Algorithms 1

Stacks. Stacks. Main stack operations. The ADT Stack stores arbitrary objects. Insertions and deletions follow the last-in first-out (LIFO) principle.

CS 151. Linked Lists, Recursively Implemented. Wednesday, October 3, 12

CPSC 221: Algorithms and Data Structures Lecture #1: Stacks and Queues

Stacks and Queues. Gregory D. Weber. CSCI C243 Data Structures

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

! A data type for which: ! In fact, an ADT may be implemented by various. ! Examples:

More Group HW. #ifndef Stackh #define Stackh. #include <cstdlib> using namespace std;

Ch. 18: ADTs: Stacks and Queues. Abstract Data Type

1. Stack Implementation Using 1D Array

n Data structures that reflect a temporal relationship q order of removal based on order of insertion n We will consider:

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

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.

Stacks have many uses Arithmetic Language parsing Keeping track of recursion (more in this in a week or so)

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.

8/28/12. Outline. Part 2. Stacks. Linear, time ordered structures. What can we do with Coin dispenser? Stacks

CS110D: PROGRAMMING LANGUAGE I

CS350: Data Structures Stacks

UNIT-2 Stack & Queue

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Basic Data Structures

Collections Chapter 12. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Lecture 3: Stacks & Queues

CPSC 221: Algorithms and Data Structures ADTs, Stacks, and Queues

Stack. 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

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

LIFO : Last In First Out

Basic Data Structures 1 / 24

CSE 143 SAMPLE MIDTERM SOLUTION

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

More Data Structures (Part 1) Stacks

Collections and Iterators. Collections

Introduction to Computer Science II (ITI 1121) Final Examination

CIS 120 Final Exam December 17, Name (printed): Pennkey (login id):

ITI Introduction to Computing II

Stacks and Queues. Chapter Stacks

Linked List. April 2, 2007 Programming and Data Structure 1

No Aids Allowed. Do not turn this page until you have received the signal to start.

Keeping Order:! Stacks, Queues, & Deques. Travis W. Peters Dartmouth College - CS 10

1.00/ Introduction to Computers and Engineering Problem Solving. Quiz 2 / November 10, 2005

ITI Introduction to Computing II

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Control flow in Eiffel

Stacks and Their Applications

Stacks. Common data structures - useful for organizing data for specific tasks Lists Stacks - an Abstract Data Type

CSC 1052 Algorithms & Data Structures II: Queues

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Linear Data Structure

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Introduction to Computing II (ITI 1121) Final Examination

CSC 1052 Algorithms & Data Structures II: Stacks

CS/ENGRD 2110 SPRING 2018

Computer Science 210 Data Structures Siena College Fall Topic Notes: Linear Structures

Programming Language Concepts: Lecture 1

Topic 6: Inner Classes

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

IT 4043 Data Structures and Algorithms. Budditha Hettige Department of Computer Science

CMPSCI 187: Programming With Data Structures. Lecture 12: Implementing Stacks With Linked Lists 5 October 2011

CSCD 326 Data Structures I Queues

Motivating Example: Observations (1) Generics in Java. Motivating Example: A Book of Objects. Motivating Example: Observations (2)

Generics in Java. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Stacks. Access to other items in the stack is not allowed A LIFO (Last In First Out) data structure

Algorithms & Datastructures Laboratory Exercise Sheet 1

Lecture 22: Java. Overall Structure. Classes & Objects. Every statement must end with ';' Carl Kingsford, , Fall 2015

1 P age DS & OOPS / UNIT II

Lecture 19 Programming Exceptions CSE11 Fall 2013

Cosc 241 Programming and Problem Solving Lecture 9 (26/3/18) Collections and ADTs

Exceptions and Design

CMSC 132: Object-Oriented Programming II. Stack and Queue

Transcription:

Data Structures G5029 Lecture 2 Kingsley Sage Room 5C16, Pevensey III khs20@sussex.ac.uk University of Sussex 2006 Lecture 2 Stacks The usual analogy is the stack of plates. A way of buffering a stream of objects, in which the the Last In is the First Out (LIFO). What might we use a stack for? Functional requirements of a stack. Two basic methods, push and pop. The first plate begins the pile, the next is placed on top of the first and so on. A plate may be removed from the pile at any time, but only from the top. Pushing a plate onto the pile increases the number on the pile. Popping a plate from the pile decreases the number on the pile. 1

Stacks Say we want to build a calculator to evaluate (3 + (2 * 5)) / 2 Work your way from the outside of the expression to the inside putting the operands (numbers and mathematical operators) onto the stack Stacks calculator example Evaluating (3+(2*5)) / 2 2 * 5 + 3 / 2 The stack has 7 elements on it and 2 is at the top of the stack. To evaluate, put the item at the head into a variable and pop each element in turn performing the specified calculation on the variable. Finished when the stack is empty. 2

Simple implementation Stack is represented by a private array of objects private Object[] stack; private int top; // index for the top of the stack // Constructor public SimpleStack(int capacity) { stack = new Objectt[capacity]; top = 0; // To declare a stack SimpleStack = new SimpleStack(20); public class SimpleStack { private Object[] stack; private int top; public SimpleStack(int capacity) { stack = new Object[capacity]; top=0; public boolean isempty() { return (top==0); public boolean isfull() { return (top==stack.length); public void push(object item) throws Exception { if (isfull()) { throw New Exception( stack overflow ); else { stack[top++] = item; public Object pop() throws Exception { if (isempty()) { throw new Exception( stack underflow ); else { return stack[--top]; 3

Interfaces Notice that clients of the SimpleStack class only have access to the stack through the public methods isempty(), isfull(), push() and pop(). The actual stack array and the pointer top are private and cannot be directly manipulated by the client. Arrays are not the only way to implement a stack we can also use linked lists (later in the course). the Java construct of the interface: public interface Stack { public boolean isempty(); public void push(object items); public Object pop(); Implementation Now we shall see how we can use the stack interface to create a new stack class StackArray. StackArray will be able to deal with the situation when a stack needs to be re-sized because it is full. This will make use of a System Java method to copy one stack to another new one. System.arraycopy(stack, 0, newstack, 0, stack.length); // This is equivalent to for (int i=0; i<stack.length;i++) { newstack[i] = stack[i]; 4

import java.util.nosuchelementexception; public class StackArray implements Stack { private Object[] stack; private int top; public StackArray() { stack = new Object[1]; top = 0; public boolean isempty() { return (top==0); public void push(object item) { if (top==stack.length) { // expand the stack Object[] newstack = new Object[2*stack.length]; System.arraycopy(stack,0,newStack,0,stack.length); stack = newstack; stack[top++] = item; public Object pop() { if (top==0) { throw new NoSuchElementException(); else { return stack[--top]; Demonstration Now we can see StackArray in use:. Can t instantiate Stack directly, as it s only an interface. Need to object -ify items to put them on the stack. import DataStructures.*; public class StackDemo { public static void main(string[] args) { Stack s = new StackArray(); for (int i = 0; i < 8; i++) { a.push(new Integer(i)); while (!s.isempty()) { System.out.println(s.pop()); 5

Note on Exceptions For simplicity, the present DataStructures package re-uses existing Java exceptions, rather than defining its own. Specifically, the exceptions used are: ArithmeticException IllegalArgumentExcpetion IllegalStateException NoSuchElementException UnsupportedOperationException These are all sub-class of the RuntimeException class. It is good programming practice to require the programmer to catch any exception which can arise for reasons beyond the programmer s control, for example when interacting with an external device, such as a keyboard, mouse, server, whatever. Next time OK, we ve seen the LIFO stack, next time we shall look at the First In, First Out data structure the queue. 6