stacks
DESCRIPTION
Stacks. The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz. Overview. Definition of Abstract Data Type Stack Introduction Example: Reverse Polish Notation Stack Specification Implementation Of Stacks. C# Data Types. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/1.jpg)
Stacks
The content for these slides was originally created by Gerard Harrison.Ported to C# by Mike Panitz
![Page 2: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/2.jpg)
![Page 3: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/3.jpg)
Overview
Definition of Abstract Data Type Stack Introduction Example: Reverse Polish Notation Stack Specification Implementation Of Stacks
![Page 4: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/4.jpg)
C# Data Types
C# provides simple types such as int, float, bool
C# also provides classes (and structs, which we’ll see later) which we use to build new types
![Page 5: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/5.jpg)
Abstract Data Types (ADTs)
An Abstract Data Type is a language-independent view of a more complicated data type ‘pattern’ Consists of data, and a set of actions
that can be done on the type
We’ll use the idea of a Stack in many languages, and contexts
![Page 6: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/6.jpg)
Goals of ADTs
Clarification Reusability Decoupling Encapsulation & Information Hiding
![Page 7: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/7.jpg)
Stack Introduction
A stack is an abstract data type in which all the insertions and deletions of entries are made at one end, called the top of the stack. The most recently added entry is the
first entry that will be removed Sometimes referred to as Last-In
First Out (LIFO)
![Page 8: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/8.jpg)
![Page 9: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/9.jpg)
![Page 10: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/10.jpg)
![Page 11: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/11.jpg)
Examples
Call stack (of function/method calls)
“Undo”/ “Redo” feature of Word, etc
Finding one’s way through a maze
Depth-First Search of a tree structure(ex: BinarySearchTree.Print)
We’ll see these later in the term
Also: Reverse Polish Notation
![Page 12: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/12.jpg)
![Page 13: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/13.jpg)
![Page 14: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/14.jpg)
![Page 15: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/15.jpg)
![Page 16: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/16.jpg)
![Page 17: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/17.jpg)
![Page 18: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/18.jpg)
![Page 19: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/19.jpg)
![Page 20: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/20.jpg)
Detecting Palindromes
This is a good place for the ‘detecting palindromes’ exercise
![Page 21: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/21.jpg)
Stack Class Specification (API)
API:Application Programming Interface
Methods, properties, fields, events, etc, that can be called from a C# program that you write
The API used here is loosely based on the .Net FCL Stack class.
![Page 22: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/22.jpg)
Stack.Push
If the stack is not full, add item to the top of the stack.
If the stack is full, an overflow error has occurred, and throw an OverflowException
void Push(int item);// throws OverflowException
![Page 23: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/23.jpg)
Stack.Pop
If the stack is not empty, then the top item is removed & returned via the out parameter.
If the stack is empty, then an underflow error has occurred, and an error value is returned.
int Pop();// throws UnderflowException
![Page 24: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/24.jpg)
Stack.Peek
If the stack is not empty, then the top item is returned via the out parameter. The stack itself is unchanged
If the stack is empty, then an UnderflowException is thrown.
int Peek();// throws UnderflowException
![Page 25: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/25.jpg)
Stack.IsEmpty
If the stack is empty, then true is returned. Otherwise, returns false.
bool Stack.IsEmpty();
![Page 26: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/26.jpg)
![Page 27: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/27.jpg)
Stack: Implementation
Each instance of the class will use per-instance variables to keep track of An array of integers
These represent the contents of the stack An integer to keep track of the index of
the ‘top’ of the stack If there are no items in the stack, we’ll set
this to -1.
![Page 28: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/28.jpg)
Stack: Implementation: Ctor
public class Stack {private int []items;private int iTop;
public Stack(){
items = new int[10];iTop = -1;
}
Note: We should also provide at least one other constructor, so that a person could choose a different size for the stack.
Question: From a testing perspective, why would a small stack be advantageous?
![Page 29: Stacks](https://reader035.vdocument.in/reader035/viewer/2022062519/56815447550346895dc257a4/html5/thumbnails/29.jpg)
Summary
Used where reversal of data is needed
Simple to use, and simple to implement