Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle.

Slides:



Advertisements
Similar presentations
Queues Printer queues Several jobs submitted to printer Jobs form a queue Jobs processed in same order as they were received.
Advertisements

CS Data Structures I Chapter 6 Stacks I 2 Topics ADT Stack Stack Operations Using ADT Stack Line editor Bracket checking Special-Palindromes Implementation.
Senem Kumova Metin Spring2009 STACKS AND QUEUES Chapter 10 in A Book on C.
Data Structures Lecture 13: QUEUES Azhar Maqsood NUST Institute of Information Technology (NIIT)
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Stacks  a data structure which stores data in a Last-in First-out manner (LIFO)  has a pointer called TOP  can be implemented by either Array or Linked.
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 18 Stacks.
Elementary Data Structures CS 110: Data Structures and Algorithms First Semester,
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Data Structure Dr. Mohamed Khafagy.
Stacks  Standard operations: IsEmpty … return true iff stack is empty IsFull … return true iff stack has no remaining capacity Top … return top element.
Data Structures - Stacks. What are data structures? Different ways to organize data What data structures have we used before? lists / arrays Deck (AceyDeucey)
Stacks and Queues In this section of notes you will learn about two additional data structures as well as the consequences of different implementations.
Stacks, Queues, and Deques
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.
Data Structures: Lists i206 Fall 2010 John Chuang Some slides adapted from Glenn Brookshear, Brian Hayes, or Marti Hearst.
Stacks and Queues COMP171 Fall Stack and Queue / Slide 2 Stack Overview * Stack ADT * Basic operations of stack n Pushing, popping etc. * Implementations.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks.
More on Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lectures 2 & 3 Wanda M. Kunkle.
1 Lecture 24 Abstract Data Types (ADT) –I Overview  What is an Abstract Data type?  What is Stack ADT?  Stack ADT Specifications  Array Implementation.
Introduction to Arrays CSC 1401: Introduction to Programming with Java Week 10 – Lecture 1 Wanda M. Kunkle.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
More on Recursion Averting Program Crashes CSC 1401: Introduction to Programming with Java Week 9 – Lecture 3 Wanda M. Kunkle.
Data Structures Data structures permit the storage of related data for use in your program. –Arrays.
Microsoft® Small Basic
ADT Stacks and Queues. Stack: Logical Level “An ordered group of homogeneous items or elements in which items are added and removed from only one end.”
Topic 3 The Stack ADT.
Stack Data Structure By : Imam M Shofi. What is stack? A stack is a limited version of an array. A stack is a limited version of an array. New elements,
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,
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.
Stacks and Queues Introduction to Computing Science and Programming I.
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.
Stack. 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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
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.
CMSC 202 Stacks and Queues. What’s a Queue? A queue is a linear collection of homogeneous data in which items added to the queue must be placed at the.
Data Structures (part 2). Stacks An Everyday Example Your boss keeps bringing you important items to deal with and keeps saying: “Put that last ‘rush’
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
CP104 Introduction to Programming Array Lecture 25 __ 1 Josephus Problem In the Jewish revolt against Rome, Josephus and 39 of his comrades were holding.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
Stacks And Queues Chapter 18.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Data Structures & Algorithms
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.
Stacks Prepared By : Ramesh Kumar PGT CS(KV Chamera-II(NHPC)
STACK Data Structure
Lecture 21 Data Structures, Algorithms and Complexity Stacks and Queues GRIFFITH COLLEGE DUBLIN.
Chapter 18 Introduction to Custom Templates C++ How to Program, 9/e ©2016 by Pearson Education, Inc., Hoboken, NJ. All Rights Reserved. Instructor Note:
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Stacks. What is a Stack? A stack is a type of data structure (a way of organizing and sorting data so that it can be used efficiently). To be specific,
Click to edit Master text styles Stacks Data Structure.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Sections 3.4 Formal Specification
Stacks II David Lillis School of Computer Science and Informatics
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Stacks and Queues.
Stack and Queue APURBO DATTA.
Stacks A stack is a data structure that is similar in spirit to a pile of cafeteria trays. Think about the trays in the dining halls: when the dining staff.
CMSC 341 Lecture 5 Stacks, Queues
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Queues: Implemented using Arrays
Stacks: Implemented using Linked Lists
Presentation transcript:

Implementing Stacks Using Arrays CSC 1401: Introduction to Programming with Java Week 14 – Lecture 1 Wanda M. Kunkle

2 What is a stack? A stack is a collection of related data items based on the principle of last-in, first-out (LIFO): A stack is a collection of related data items based on the principle of last-in, first-out (LIFO): The last item placed on the stack is the first item removed from the stack. The last item placed on the stack is the first item removed from the stack. A stack is analogous to a pile of trays in a cafeteria: A stack is analogous to a pile of trays in a cafeteria: A person taking a tray removes the tray from the top of the pile. A person taking a tray removes the tray from the top of the pile. A person returning a tray places the tray on the top of the pile. A person returning a tray places the tray on the top of the pile.

3 Manipulating Stacks A stack differs from a pile of trays, however, in that a stack only allows us to see and access the item on the top; to access items farther down in the stack we must first remove those above it. A stack differs from a pile of trays, however, in that a stack only allows us to see and access the item on the top; to access items farther down in the stack we must first remove those above it. The two primary operations for manipulating items on a stack are: The two primary operations for manipulating items on a stack are: push push Places an item on the top of the stack Places an item on the top of the stack pop pop Removes an item from the top of the stack Removes an item from the top of the stack

4 How can we implement a stack using an array? Steps: Steps: 1. Determine the size of the stack 2. Determine the kind of data the stack will store 3. Implement (minimally) the push and pop operations

5 How can we implement a stack using an array? 1. Determine the size of the stack Example from today’s demo program, demoStack.java: static final int MAXSIZE = 10; ( Aside: Actually, the stack will be size 11, with the 0 th element reserved for a special purpose. Specifically, the 0 th slot will be used to keep track of the next available stack location. ) Example from today’s demo program, demoStack.java: static final int MAXSIZE = 10; ( Aside: Actually, the stack will be size 11, with the 0 th element reserved for a special purpose. Specifically, the 0 th slot will be used to keep track of the next available stack location. )

6 How can we implement a stack using an array? 2. Determine the kind of data the stack will store Example from today’s demo program, demoStack.java: // Define a stack that will store integers int intStack[] = new int[MAXSIZE+1]; Example from today’s demo program, demoStack.java: // Define a stack that will store integers int intStack[] = new int[MAXSIZE+1];

7 How can we implement a stack using an array? 3. Implement (minimally) the push and pop operations Example push and pop functions from today’s demo program, demoStack.java, appear on the next two slides. Example push and pop functions from today’s demo program, demoStack.java, appear on the next two slides.

8 Implementation of “push” // Pushes an element onto the stack static void push ( int[] a, int val ) { int topelementindex; input in = new input(); output out = new output(); if (!isfull(a)) { topelementindex = a[0]; a[topelementindex] = val; a[0]--; } else { out.writeln("Severe error!!!!!"); out.writeln("Try to push onto full stack"); out.writeln("Press ENTER to end the program"); String junk = in.readline(); System.exit(1); } } // end push // Pushes an element onto the stack static void push ( int[] a, int val ) { int topelementindex; input in = new input(); output out = new output(); if (!isfull(a)) { topelementindex = a[0]; a[topelementindex] = val; a[0]--; } else { out.writeln("Severe error!!!!!"); out.writeln("Try to push onto full stack"); out.writeln("Press ENTER to end the program"); String junk = in.readline(); System.exit(1); } } // end push

9 Implementation of “pop” // Removes an element from the top of the stack static int pop ( int[] a ) { int returnval = 0; int topelementindex; input in = new input(); output out = new output(); if (!isempty(a)) { a[0]++; topelementindex = a[0]; returnval = a[topelementindex]; } else { out.writeln("Severe error!!!!!"); out.writeln("Try to pop from empty stack"); out.writeln("Press ENTER to end the program"); String junk = in.readline(); System.exit(1); } return returnval; } // end pop // Removes an element from the top of the stack static int pop ( int[] a ) { int returnval = 0; int topelementindex; input in = new input(); output out = new output(); if (!isempty(a)) { a[0]++; topelementindex = a[0]; returnval = a[topelementindex]; } else { out.writeln("Severe error!!!!!"); out.writeln("Try to pop from empty stack"); out.writeln("Press ENTER to end the program"); String junk = in.readline(); System.exit(1); } return returnval; } // end pop

10 Additional Methods for Manipulating Stacks isempty isempty Returns true if the stack is empty, false otherwise Returns true if the stack is empty, false otherwise isfull isfull Returns true if the stack is full, false otherwise Returns true if the stack is full, false otherwise print print Prints the contents of the stack Prints the contents of the stack

11 Sample Program & Animation Now let’s take a look at a sample program that implements a stack: Now let’s take a look at a sample program that implements a stack: demoStack.java demoStack.java demoStack.java As well as a Java applet that demonstrates basic stack operations: As well as a Java applet that demonstrates basic stack operations: Stack Stack Stack