1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.

Slides:



Advertisements
Similar presentations
The unorganized person’s data structure
Advertisements

1 Stacks Chapter 4. 2 Objectives You will be able to: Describe a stack as an ADT. Build a dynamic-array-based implementation of stacks. Build a linked-list.
Pseudocode for Bracket matching Stack mystack; // declare & initialize stack Loop through all characters in program { if (symbol is an ‘opener’) // (,
Chapter 2 INTRODUCTION TO STACKS. Outline 1. Stack Specifications 2. Implementation of Stacks 3. Application: A Desk Calculator 4. Application: Bracket.
1 Data Structures CSCI 132, Spring 2014 Lecture 8 Implementing Queues.
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.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Lecture 5 Sept 15 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks.
Stacks  Standard operations: IsEmpty … return true iff stack is empty Top … return top element of stack Push … add an element to the top of the stack.
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.
Object Oriented Data Structures
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,
1 Stacks Stack Examples Stack API More Examples/Uses Base Conversion Activation Records RPN Implementing a Stack Stacks.
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 STRUCTURES AND ALGORITHMS Lecture Notes 4 Prepared by İnanç TAHRALI.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
 STACK STACK  BASIC STACK OPERATIONS BASIC STACK OPERATIONS  PUSH ALGORITHM PUSH ALGORITHM  POP ALGORITHM POP ALGORITHM  EVALUATING A POSTFIX EXPRESSION.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
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.
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 2 Overloaded Operators, Class Templates, and Abstraction Jeffrey S. Childs Clarion University.
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Chapter 2 INTRODUCTION TO STACKS 1. Stack Specifications 2. Implementation of Stacks 3. Application: A Desk Calculator 4. Application: Bracket Matching.
1 Data Structures CSCI 132, Spring 2014 Lecture 19 Lists.
Data Structures & Algorithms
Kruse/Ryba ch041 Linked Stacks and Queues Pointers and Linked Structures Linked Stacks Linked Stacks with Safeguards Linked Queues Application: Polynomials.
1 Data Structures CSCI 132, Spring 2014 Lecture 6 Applications using Stacks.
1 CSC 222: Computer Programming II Spring 2004 Stacks and recursion  stack ADT  push, pop, top, empty, size  vector-based implementation, library 
1 Data Structures CSCI 132, Spring 2016 Notes 6 Applications using Stacks.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Data Structures CSCI 132, Spring 2014 Lecture 7 Queues.
1 C++ Classes and Data Structures Course link…..
Chapter 3 Lists, Stacks, Queues. Abstract Data Types A set of items – Just items, not data types, nothing related to programming code A set of operations.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Mark Redekopp David Kempe
Stacks The content for these slides was originally created by Gerard Harrison. Ported to C# by Mike Panitz.
Stacks Stacks.
CC 215 Data Structures Stack ADT
Homework 4 questions???.
Queues Queues Queues.
Stack.
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Stacks, Queues, and Deques
Popping Items Off a Stack Lesson xx
Stack and Queues Stack implementation using Array
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
Stacks as Linked lists top_node typedef Stack_entry Node_entry;
CSC 143 Stacks [Chapter 6].
Mutable Data (define mylist (list 1 2 3)) (bind ((new (list 4)))
Stacks Data structure Elements added, removed from one end only
Visit for more Learning Resources
Using a Queue Chapter 8 introduces the queue data type.
Using a Queue Chapter 8 introduces the queue data type.
Stacks, Queues, and Deques
Abstract Data Types Stacks CSCI 240
5.3 Implementing a Stack Chapter 5 – The Stack.
Data Structures & Programming
Presentation transcript:

1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks

2 Lists A list is an abstract data type consisting of an ordered group of items. A list is dynamic. Its length can vary. Items can be added to or deleted from the list There are many ways of implementing a list. For example: An array Linked lists

3 Arrays Arrays in C++ have fixed length. Arrays can be used to implement lists. A separate variable must keep track of the length of the list in the array.

4 Example /*Write a program to read in positive integers and store them in an array until a negative number is encountered.*/ int main(void) { int myList[30]; int count = 0; cin >> myList[0];//read first number while (myList[count] >= 0 ) { count++; cin >> myList[count];//read in next number } }

5 Stacks A stack is a list that has addition and deletion of items only from one end. It is like a stack of plates: Plates can be added to the top of the stack. Plates can be removed from the top of the stack. This is an example of “Last in, First out” (LIFO). Adding an item is called “pushing” onto the stack. Deleting an item is called “popping” off of the stack.

6 Using Stacks A Stack class should have the following methods: push(item)//Push an item onto the stack pop( )//Pop an item off the stack top( )//Return value of top item empty()//Return true if stack is empty.

7 Reversing a List with a Stack #include "stack.h"//stack class for a stack of integers int main( ) { int number; Stack numberStack; cout << "Enter positive integers. End with -1." << endl; cin >> number; while (number >= 0) { numberStack.push( number ); cin >> number; } cout << endl; while ( !numberStack.empty( ) ) { numberStack.top( number ); cout << number; numberStack.pop( ); } cout << endl; }

8 The Standard Template Library The C++ Standard Template Library (STL) has many useful classes and ADTs. To use the STL effectively, you need to understand the underlying data types. In this course we will learn how to implement the data structures from scratch.

9 Error Checking Error codes provide a way to signal that a particular error has occurred. Error codes allow the client program to deal with errors in an application-specific way. One way to specify error codes is with an enumeration: enum Error_code {success, fail, range_error, underflow, overflow, …};

10 The Stack Specification typedef int Stack_entry; const int maxstack =10 ;//small value for testing class Stack { public: Stack(); bool empty() const; Error_code pop(); Error_code top(Stack_entry &item) const; Error_code push(const Stack_entry &item); private: int count ; Stack_entry entry [maxstack ]; };

11 Implementing push( ) Error_code Stack ::push(const Stack_entry &item) { //Implement push() here }

12 Implementing push( ) Error_code Stack ::push(const Stack_entry &item) { Error_code outcome =success ; if (count >=maxstack) { outcome =overflow ; } else { entry [count ]=item ; count++; } return outcome ; }

13 Implementing pop( ) Error_code Stack ::pop() { //implement pop() }

14 Implementing pop( ) Error_code Stack ::pop() { Error_code outcome =success ; if (count ==0) { outcome =underflow ; } else { count-- ; } return outcome ; }

15 Implementing top( ) Error_code Stack ::top(Stack_entry &item) const { //implement top( ) }

16 Implementing top( ) Error_code Stack ::top(Stack_entry &item) const { Error_code outcome =success ; if (count ==0){ outcome =underflow ; } else { item =entry [count - 1]; } return outcome ; }

17 Implementing empty( ) bool Stack ::empty( )const { //implement empty( ) }

18 Implementing empty( ) bool Stack ::empty( )const { bool outcome =true; if (count >0) { outcome =false; } return outcome ; }

19 Implementing Stack( ) Stack ::Stack() {//implement Stack( ) }

20 Implementing Stack( ) Stack ::Stack() { count =0 ; }