Object-Oriented Programming Simple Stack Implementation.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
COSC 1P03 Data Structures and Abstraction 9.1 The Queue Whenever you are asked if you can do a job, tell 'em, "Certainly, I can!" Then get busy and find.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
COP3538 – Data Structures Using OOP Chapter 4 – Stacks and Queues.
Encapsulation. Encapsulation Encapsulation means the bringing together of a set of attributes and methods into an object definition and hiding their implementational.
Stacks, Queues, and Deques
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.
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.
Generics OOP Tirgul What is it good for ? Stack myStack = new Stack() ; // old version (1.4.2) myStack.push(new Integer(0)) ; int x = ((Integer)
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.
Queues CS-240 & CS-341 Dick Steflik. Queues First In, First Out operation - FIFO As items are added they are chronologically ordered, items are removed.
Abstract data types & object-oriented paradigm. Abstraction Abstraction: a view of an entity that includes only the attributes of significance in a particular.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
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 Chapter 2 Stacks Andreas Savva. 2 Stacks A stack is a data structure in which all insertions and deletions of entries are made at one.
Ali Abdul Karem Habib Kufa University / mathematics & Science Of Computer.
CS 1031 C++: Object-Oriented Programming Classes and Objects Template classes Operator Overloading Inheritance Polymorphism.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
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,
Ics202 Data Structures. U n i v e r s i t y o f H a i l 1. Stacks top push (8)push (2)
Review 1 Introduction Representation of Linear Array In Memory Operations on linear Arrays Traverse Insert Delete Example.
Creating Simple Classes. Outline of Class Account Class Account Account # Balance Holder name phone# Overdrawn (true/false) Data Members Open Credit Debit.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Sieve public class sieve { public static void main(String args[]) {
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,
Stacks. A stack is a data structure that holds a sequence of elements and stores and retrieves items in a last-in first- out manner (LIFO). This means.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
AITI Lecture 18 Introduction to Data Structure, Stack, and Queue Adapted from MIT Course 1.00 Spring 2003 Lecture 23 and Tutorial Note 8 (Teachers: Please.
Introduction to Data Structures and Algorithms
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Lecture 20 Stacks and Queues. Stacks, Queues and Priority Queues Stacks – first-in-last-out structure – used for function evaluation (run-time stack)
ITI Introduction to Computing II Lab-5 Dewan Tanvir Ahmed University of Ottawa.
Lab 3. Why Compressed Row Storage –A sparse matrix has a lot of elements of value zero. –Using a two dimensional array to store a sparse matrix wastes.
Copyright © Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by Tony.
“The desire for safety stands against every great and noble enterprise.” – Tacitus Thought for the Day.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
IndexedListWithIteratorsViaLinear1Ind
Linked Data Structures
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Stacks and Queues Chapter 4.
Stack and Queue APURBO DATTA.
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Decision statements. - They can use logic to arrive at desired results
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
CSE 214 – Computer Science II Stack Applications
Teknik Informatika - Universitas Muhammadiyah Malang (UMM)
COMPUTER 2430 Object Oriented Programming and Data Structures I
class PrintOnetoTen { public static void main(String args[]) {
F II 3. Classes and Objects Objectives
UNIT-II.
Stacks CS-240 Dick Steflik.
LAB#3 Stacks Nora Albabtin nora albabtin.
Stacks.
Chapter 5 Stack (part 1).
22C:21 Discrete Structures
A type is a collection of values
Presentation transcript:

Object-Oriented Programming Simple Stack Implementation

Class Diagram (Stack of int) CIntStack -int mStack[]; -int mSize; -int mTop; CIntStack(int size); +boolean push(int item); +int pop(); +void resetStack(); +void stackInfo();

Variable Members int mStack[]; –Stack storage using array of integer int mSize; –Size of stack storage int mTop; –Stack pointer used to keep the stack position

Methods CIntStack(int size); –Constructor performs stack initialization –size is the size of stack storage boolean push(int item); –push() adds new item to the top of stack –mTop will be incremented if push() is successful –Method returns true if successful, false otherwise

Method (cont.) int pop(); –pop() retrieves the item from the top of stack –If stack is empty, pop() returns -1 void resetStack(); –Clear stack storage (by simply setting mTop to zero) void stackInfo(); –Print stack status

Class Attributes public class CIntStack { private int mStack[]; private int mSize; private int mTop; //all methods go here }

Constructor() public CIntStack(int size) { this.mStack = new int[size]; this.mSize = size; this.mTop = 0; System.out.println("Initialize Stack size to: " + this.mSize); }

push() public boolean push(int item) { if(this.mTop < this.mSize){ this.mStack[this.mTop++] = item; return true; } else return false; //Stack Overflow }

pop() public int pop() { if(this.mTop==0) // stack is empty, return -1 { System.out.println("ERR: Stack Underflow"); return -1; } else return this.mStack[--this.mTop]; // OK to pop }

stackInfo() public void stackInfo() { System.out.println(" "); System.out.println(“Size of Stack: “ + this.mSize); System.out.println(“Number of items: “ + this.mTop); System.out.println(" "); for(int i=0; i<this.mTop; i++) System.out.println(“: “ + this.mStack[i]); System.out.println(" "); }

resetStack() public void resetStack() { this.mTop = 0; }

Example of How to Use It public class myStack { public static void main(String[] args) { CIntStack st = new CIntStack(3); if(!st.push(1)) System.out.println("Overflow"); if(!st.push(2)) System.out.println("Overflow"); if(!st.push(3)) System.out.println("Overflow"); if(!st.push(4)) System.out.println("Overflow"); st.stackInfo(); System.out.println(st.pop()); //st.resetStack(); System.out.println(st.pop()); }