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.

Slides:



Advertisements
Similar presentations
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Advertisements

Queues A waiting line that grows by adding elements to its end and shrinks by taking elements from its front Line at the grocery store Cars in traffic.
C++ Programming Languages
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
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.
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 18: Stacks And Queues.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
CS 240Chapter 6 - StacksPage 21 Chapter 6 Stacks The stack abstract data type is essentially a list using the LIFO (last-in-first-out) policy for adding.
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. What is a stack? Last-in first-out data structure (LIFO) New objects are placed on top Removal restricted to top object Examples?
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.
Lecture 7 Sept 16 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
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.
Lecture 6 Feb 12 Goals: stacks Implementation of stack applications Postfix expression evaluation Convert infix to postfix.
Stacks.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 18: Stacks and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
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
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 8 Stacks and Queues Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
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.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
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 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
Introduction to Stacks Chapter 2. Objectives Introduce abstract data types. Discuss implementation types. – Static – Dynamic – Contiguous Introduce the.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 6: Stacks Data Abstraction & Problem Solving with C++
11 Introduction to Object Oriented Programming (Continued) Cats.
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.
Stack Overview. Stack Stack ADT Basic operations of stack – Pushing, popping etc. Implementations of stacks using – array – linked list.
11-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
 140 Total Points ◦ 100 Points Writing Programs ◦ 24 Points Tracing Algorithms and determining results ◦ 16 Points Short Answer  Similar to quizzes.
CMSC 341 Deques, Stacks and Queues. 2/20/20062 The Double-Ended Queue ADT A Deque (rhymes with “check”) is a “Double Ended QUEue”. A Deque is a restricted.
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
11/07/141 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: –Data stored –Operations on the.
1 Introduction to Object Oriented Programming Chapter 10.
1 Queues Chapter 4. 2 Objectives You will be able to Describe a queue as an ADT. Build a dynamic array based implementation of a queue ADT.
1 The Standard Template Library Drozdek Section 3.7.
1 Linked Lists Chapter 3. 2 Objectives You will be able to: Describe an abstract data type for lists. Understand and use an implementation of a List ADT.
Computer Science Department Data Structure and Algorithms Lecture 3 Stacks.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
Chapter 4 ADTs Stack and Queue. 4-2 Formal ADT Specifications The Java interface construct lets us collect together method interfaces into a syntactic.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 18: Stacks and Queues.
Section 3.7 Linked-Based Implementations
Stack: a Linked Implementation
18 Chapter Stacks and Queues
Chapter 18: Stacks and Queues.
Homework 4 questions???.
Stacks.
Pointers and Linked Lists
Implementations of the ADT Stack
Chapter 16-2 Linked Structures
Stacks Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
CS 2308 Final Exam Review.
Chapter 19: Stacks and Queues.
Stacks CS-240 Dick Steflik.
Presentation transcript:

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 Subject only to the amount of memory available.

3 Linked Stack Rather than implementing a linked stack from scratch we will use an existing Link class and create an adapter. A wrapper that provides a different interface for an existing class. Using a linked list to implement a stack is especially simple. The operations provided by the Stack ADT are a subset of those provided by the List ADT.

4 Getting Started Create a new empty project Linked_Stack Copy stack.h and stack_test.cpp from last class into the new Linked_Stack project _02_07_Stacks/ _02_07_Stacks/ Add to project. Project > Add Existing Item

5 Getting Started Download final List Template project: _02_02_List_of_Objects/Completed_and_Corrected_Version/ _02_02_List_of_Objects/Completed_and_Corrected_Version/ Extract files. Copy DLList.h into new project. Add file to project. Note memory leak in destructor. Modify stack.h as shown on following slides. We will use a List to hold the stack data.

Stack.h #pragma once #include #include "DLList.h" template class Stack { public: Stack(int capacity = 1000); ~Stack(); bool IsEmpty() const {return data->isEmpty();}; bool IsFull() const {return false;}; // Add a value to the top of the stack. void Push(const T& value); // Remove and return value at top of stack. T Pop(); // Retrieve value at top of stack without removing it. T Top() const; // Display stack contents. void Display(std::ostream& out) const;

7 Stack.h private: DLList * data; //int size; //int top; // Index of element at top of stack // // -1 when stack is empty };

8 Constructor and Destructor template Stack ::Stack(int capacity) Delete initialization list { data = new DLList (); assert (data != 0); } template Stack ::~Stack() { delete data; Remove [] }

9 Push() template void Stack ::Push(const T& value) { data->addToHead(value); }

10 Pop() template T Stack ::Pop() { if (this->IsEmpty()) { throw "Pop called for empty stack"; } return data->deleteFromHead(); }

11 Top() template T Stack ::Top() const { if (this->IsEmpty()) { throw "Top of empty stack requested"; } T temp = data->deleteFromHead(); data->addToHead(temp); return temp; }

12 Display() template void Stack ::Display(std::ostream& out) const { data->printAll(); }

13 stack_test Our test should work unchanged Except: We can't force overflow now. Comment out the section that tests stack overflow. Lines 28 – 41 of stack_test.cpp Build

14 A Warning The isEmpty method in the List template should have been declared as bool. Make that change.

15 Program Running

16 Try it on Linux

17 Try it on Linux The Visual Studio compiler is more forgiving! Add using namespace std ; to printAll.

18 OK This Time

19 An Application Adding arbitrarily large numbers. Unlike pencil and paper arithmetic, computer arithmetic typically has a maximum size for numbers. We can use stacks to implement addition for arbitrarily large numbers. Drozdek, page 141

20 Modify printAll() Modify DLList::printAll() to output all items on the same line. cout info << " ";

21 Adding Arbitrarily Large Integers int main() { Stack lhs; Stack rhs; Stack result; get_integer(lhs); get_integer(rhs); cout << "\nLHS = "; lhs.Display(cout); cout << "\nRHS = "; rhs.Display(cout); cin.get(); }

22 get_integer() void get_integer(Stack & stack) { cout << "Enter an integer: "; char c = cin.get(); while (c != '\n') { assert (isdigit(c)); stack.Push(c - '0'); c = cin.get(); }

23 Program in Action

24 Program in Action

25 Add to main() add(lhs, rhs, result); cout << "\nSum = "; result.Display(cout); cout << endl;

26 Function Add() void add(Stack & lhs, Stack & rhs, Stack & result) { int carry = 0; while (!lhs.IsEmpty() || !rhs.IsEmpty() || carry > 0) { int next_lhs_digit = 0; int next_rhs_digit = 0; if (!lhs.IsEmpty()) { next_lhs_digit = lhs.Pop(); } if (!rhs.IsEmpty()) { next_rhs_digit = rhs.Pop(); }

27 Function Add() int next_sum_digit = next_lhs_digit + next_rhs_digit + carry; if (next_sum_digit > 9) { carry = 1; next_sum_digit -= 10; } else { carry = 0; } result.Push(next_sum_digit); }

28 A Small Example

29 A Big Example

30 Another Big Example