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

Slides:



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

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.
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.
Abstract Data Types Data abstraction, or abstract data types, is a programming methodology where one defines not only the data structure to be used, but.
Stacks Chapter 5. Chapter Objectives  To learn about the stack data type and how to use its four methods: push, pop, peek, and empty  To understand.
Data Structures & Algorithms
Stacks. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
Tirgul 3 Subjects of this Tirgul: Linked Lists Doubly-Linked Lists Sparse Matrices Stack Queue.
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 Chapter 5. Chapter 5: Stacks2 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty.
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 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.
Stacks.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
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.
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.
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.
CHAPTER 6 Stacks. 2 A stack is a linear collection whose elements are added and removed from one end The last element to be put on the stack is the first.
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.
CS Winter 2011 Abstract Data Types. Container Classes Over the years, programmers have identified a small number of different ways of organizing.
1 Lecture 26 Abstract Data Types – IV Overview  The List ADT  Implementing Stacks as Linked List  Linked List Implementation of Queues .  Preview:
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.
Stacks, Queues, and Deques
CS 171: Introduction to Computer Science II Stacks Ymir Vigfusson.
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.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
© 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.
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’
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
1 The Stack Class Final Review Fall 2005 CS 101 Aaron Bloomfield.
Stacks and Queues Based on D.S. Malik, Java Programming: Program Design Including Data Structures.
Object-Oriented Programming Simple Stack Implementation.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite 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.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
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.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
CSCI 3333 Data Structures 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.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks.
Stacks CS-240 Dick Steflik.
Stacks, Queues, and Deques
CS210- Lecture 3 Jun 6, 2005 Announcements
Stacks.
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.
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’ll see later) 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  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 out parameter.  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