Hassan Khosravi / Geoffrey Tien

Slides:



Advertisements
Similar presentations
Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Advertisements

Lecture 5 Stack Sandy Ardianto & Erick Pranata © Sekolah Tinggi Teknik Surabaya 1.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Topic 15 Implementing and Using Stacks
Data Structures & Algorithms
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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,
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,
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.
Stack: Linked List Implementation Push and pop at the head of the list New nodes should be inserted at the front of the list, so that they become the top.
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.
Topic 15 Implementing and Using Stacks
Objectives of these slides:
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Data Structures: CSCI 362 – Stack Implementation Data Structures: CSCI 362 – Stack Implementation lecture notes adapted from Data Structures with C++ using.
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,
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
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.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Chapter 7 Stacks. © 2004 Pearson Addison-Wesley. All rights reserved 7-2 The Abstract Data Type: Developing an ADT During the Design of a Solution Specifications.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Copyright © Curt Hill Stacks An Useful Abstract Data Type.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
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.
CSCE 3110 Data Structures & Algorithm Analysis
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Data Structures Using C++ 2E
Stacks.
Stacks Chapter 5.
Revised based on textbook author’s notes.
MEMORY REPRESENTATION OF STACKS
Cinda Heeren / Geoffrey Tien
Homework 4 questions???.
Stacks.
Cinda Heeren / Geoffrey Tien
Stacks.
Stacks Chapter 4.
Visit for more Learning Resources
Stack.
CSCE 3110 Data Structures & Algorithm Analysis
Stack Data Structure, Reverse Polish Notation, Homework 7
COMPUTER 2430 Object Oriented Programming and Data Structures I
CMSC 341 Lecture 5 Stacks, Queues
Abstract Data Types (ADTs)
CSCE 3110 Data Structures & Algorithm Analysis
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
STACK By:- Rajendra ShakyawalP.G.T. Computer Science KV-No.1, AFS, Tambaram, Chennai.
Stacks and Queues.
Hassan Khosravi / Geoffrey Tien
Stack A data structure in which elements are inserted and removed only at one end (called the top). Enforces Last-In-First-Out (LIFO) Uses of Stacks Evaluating.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks and Queues 1.
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Abstract Data Type Abstract Data Type as a design tool
Stacks Data structure Elements added, removed from one end only
Topic 15 Implementing and Using Stacks
Stack.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks and Queues.
Abstract Data Types Stacks CSCI 240
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Hassan Khosravi / Geoffrey Tien Stack Behaviour ADT October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Abstract data types Abstract data type (ADT) – a mathematical description of an object and a set of operations on the object Alternatively, a collection of data and the operations for accessing the data Stumpjumper The favourite baby of VanCity planners Z125 Pro Fun in the sun! GL1800 Quiet comfort Example: Dictionary ADT Stores pairs of strings: (word, definition) Operations: Insert(word, definition) Remove(word) Lookup(word) Insert Feet Useful for something, presumably Find(Z125 Pro) data storage implemented with a data structure Z125 Pro Fun in the sun! October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Implementing ADTs Using data structures Theoretically (in programming languages that support OOP) abstract base class describes ADT (operations etc.) inherited implementations apply data structures data structure can be changed transparently (to client code) Practice performance of a data structure may influence form of client code time vs space, one operation vs another October 16, 2017 Hassan Khosravi / Geoffrey Tien

ADT application – Postfix notation Reverse Polish Notation (RPN) Reverse Polish Notation (RPN) Also known as postfix notation A mathematical notation Where every operator follows its operands Example Infix: 5 + ((1 + 2) * 4) − 3 RPN: 5 1 2 + 4 * + 3 – 17 12 3 Infix: 5 + (( 1 + 2 ) * 4 ) – 3 = 14 October 16, 2017 Hassan Khosravi / Geoffrey Tien

RPN example 5 1 2 + 4 * + 3 – To evaluate a postfix expression, read it from left to right Store ‘5’ Store ‘1’ Apply ‘+’ to last two operands Store ‘2’ Apply ‘*’ to last two operands Store ‘4’ Apply ‘+’ to last two operands Apply ‘–’ to last two operands Store ‘3’ 4 2 Note: the postfix string contains integers and characters, but the data collection contains only integers 12 1 3 14 17 5 Retrieve result October 16, 2017 Hassan Khosravi / Geoffrey Tien

Calculating a Postfix Expression for each input symbol if symbol is operand store(operand) if symbol is operator RHS = remove() LHS = remove() result = LHS operator RHS store(result) result = remove() October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Describing an ADT What are the storage properties of the data type that was used? Specifically how are items stored and removed? Note that items are never inserted between existing items The last item to be entered is the first item to be removed Known as LIFO (Last In First Out) This ADT is referred to as a stack October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien The Stack ADT A stack only allows items to be inserted and removed at one end We call this end the top of the stack The other end is called the bottom Access to other items in the stack is not allowed A stack can be used to naturally store data for postfix notation Operands are stored at the top of the stack And removed from the top of the stack Notice that we have not (yet) discussed how a stack should be implemented Just what it does An example of an Abstract Data Type October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Stack behaviour A stack ADT should support at least the first two of these operations: push – insert an item at the top of the stack pop – remove and return the top item peek – return the top item isEmpty – does the stack contain any items ADT operations should be performed efficiently The definition of efficiency varies from ADT to ADT The order of the items in a stack is based solely on the order in which they arrive October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Stacks in the wild Call Stack int square(int x) { return x*x; } int squareOfSum(int x, int y) { return square(x+y); int main() { int a = 4; int b = 8; int total = squareOfSum(a, b); printf("Total: %d\n", total); return 0; stack main a b total main a b squareOfSum x y square x October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Stacks in the wild Backtracking 1 4 5 8 2 3 6 7 9 October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Stack Implementation The stack ADT can be implemented using a variety of data structures, e.g. Arrays Linked Lists Both implementations must implement all the stack operations In constant time (time that is independent of the number of items in the stack) October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Stack implementation Using an array Suppose we use an array which does not contain any gaps between elements We can add and remove elements at the right side, as long as we know which element is the last item Treat the last element of the array as the "top" of the stack Information to track: index of top item maximum size of the array October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Stack implementation Using an array typedef struct { int top; int capacity; int* arr; } Stack; #define TRUE 1 #define FALSE 0 void initialize(Stack* st) { st->top = -1; st->capacity = 8; // or some other value st->arr = (int*) malloc(capacity * sizeof(int)); } int isEmpty(Stack* st) { if (st->top == -1) return TRUE; else return FALSE; } int isFull(Stack* st) { if (st->top == st->capacity - 1) return TRUE; else return FALSE; } October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Stack implementation Using an array push – the item is inserted and becomes the new stack top increment top before access However, we must ensure that the stack has space available before pushing int push(Stack* st, int value) { if (!isFull(st)) st->top++; st->arr[st->top] = value; return TRUE; else return FALSE; } October 16, 2017 Hassan Khosravi / Geoffrey Tien

Hassan Khosravi / Geoffrey Tien Stack implementation Using an array pop – the top item is removed and stack shrinks decrement top after access However, we must ensure that the stack is not already empty int pop(Stack* st) { if (!isEmpty(st)) st->arr[st->top] = -1; st->top--; return TRUE; else return FALSE; } int peek(Stack* st) { if (!isEmpty(st)) return st->arr[st->top]; else return FALSE; } October 16, 2017 Hassan Khosravi / Geoffrey Tien

Readings for this lesson Thareja Chapter 7.1 – 7.3 Stack demo: http://visualgo.net/en/list?slide=4 Next class Thareja chapter 7, 8.1 – 8.2 October 16, 2017 Hassan Khosravi / Geoffrey Tien