Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CS Data Structures II Review COSC 2006 April 14, 2017
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Data Structures & Algorithms
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.
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
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 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.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
30-Jun-15 Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
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.
Abstract Data Types (ADTs) and data structures: terminology and definitions A type is a collection of values. For example, the boolean type consists of.
Topic 3 The Stack ADT.
Chapter 6 Stacks CS Data Structures Mehmet H Gunes Modified from authors’ slides.
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. 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.
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.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Stack Data Structure By Marwa M. A. Elfattah. Stack - What A stack is one of the most important non- primitive linear data structure in computer science.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Stack. ADS2 Lecture 1010 The Stack ADT (GoTa §5.1) The Stack ADT stores arbitrary objects Insertions and deletions follow the last-in.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures and Algorithms Stack. 2 The Stack ADT Introduction to the Stack data structure Designing a Stack class using dynamic arrays Linked Stacks.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Chapter 6 A Stacks. © 2004 Pearson Addison-Wesley. All rights reserved6 A-2 The Abstract Data Type: Developing an ADT During the Design of a Solution.
Comprehensive Introduction to OOP with Java, C. Thomas Wu Stack ADT
Stacks Chapter 5.
CSCI 3333 Data Structures Stacks.
Chapter 6: The Stack Abstract Data Type
Stacks.
The Stack ADT. 3-2 Objectives Define a stack collection Use a stack to solve a problem Examine an array implementation of a stack.
Cinda Heeren / Geoffrey Tien
Stacks.
Stack and Queue APURBO DATTA.
CMSC 341 Lecture 5 Stacks, Queues
Abstract Data Types (ADTs)
Stack ADT & Modularity 2 implementations of the Stack abstract data type: Array Linked List Program design: modularity, abstraction and information hiding.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Stacks, Queues, and Deques
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
Stacks and Queues.
Stacks, Queues, and Deques
Stacks Chapter 6 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks and Queues CLRS, Section 10.1.
CSC 143 Stacks [Chapter 6].
Stacks Abstract Data Types (ADTs) Stacks
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Introduction to Data Structure
Stacks CS-240 Dick Steflik.
Stacks, Queues, and Deques
CS210- Lecture 3 Jun 6, 2005 Announcements
Stacks.
Stacks and Queues.
Abstract Data Types Stacks CSCI 240
A type is a collection of values
Queues The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Abstract Data Types (ADTs)
CMPT 225 Lecture 7 – Stack.
Data Structures & Programming
Presentation transcript:

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 C# provides simple types such as int, float, bool C# also provides classes (and structs) which we use to build new types

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

Goals of ADTs Clarification Reusability Decoupling Encapsulation & Information Hiding

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)

Examples Back button on a web browser 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

Detecting Palindromes This is a good place for the ‘detecting palindromes’ exercise

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.

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

Stack.Pop If the stack is not empty, then the top item is removed & returned via the return value. If the stack is empty, then an underflow error has occurred, and an error value is returned. int Pop(); // throws UnderflowException

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

Stack.IsEmpty If the stack is empty, then true is returned. Otherwise, returns false. bool Stack.IsEmpty();

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.

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?

Summary Used where reversal of data is needed Simple to use, and simple to implement