310 likes | 523 Views
Linked Stack. Chapter 4. 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. Linked Stack.
E N D
Linked Stack Chapter 4
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.
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.
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 • http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/2011_02_07_Stacks/ • Add to project. • Project > Add Existing Item
Getting Started • Download final List Template project: • http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/2011_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 <iostream> #include <cassert> #include "DLList.h" template <class T> 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;
Stack.h private: DLList<T>* data; //int size; //int top; // Index of element at top of stack // // -1 when stack is empty };
Constructor and Destructor template <class T> Stack<T>::Stack(int capacity)Delete initialization list { data = new DLList<T>(); assert (data != 0); } template <class T> Stack<T>::~Stack() { delete data;Remove [] }
Push() template <class T> void Stack<T>::Push(const T& value) { data->addToHead(value); }
Pop() template <class T> T Stack<T>::Pop() { if (this->IsEmpty()) { throw "Pop called for empty stack"; } return data->deleteFromHead(); }
Top() template <class T> T Stack<T>::Top() const { if (this->IsEmpty()) { throw "Top of empty stack requested"; } T temp = data->deleteFromHead(); data->addToHead(temp); return temp; }
Display() template <class T> void Stack<T>::Display(std::ostream& out) const { data->printAll(); }
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
A Warning The isEmpty method in the List template should have been declared as bool. Make that change.
Try it on Linux • The Visual Studio compiler is more forgiving! • Add using namespace std; to printAll.
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
Modify printAll() • Modify DLList::printAll() to output all items on the same line. cout << p->info << " ";
Adding Arbitrarily Large Integers int main() { Stack<int> lhs; Stack<int> rhs; Stack<int> result; get_integer(lhs); get_integer(rhs); cout << "\nLHS = "; lhs.Display(cout); cout << "\nRHS = "; rhs.Display(cout); cin.get(); cin.get(); }
get_integer() void get_integer(Stack<int>& stack) { cout << "Enter an integer: "; char c = cin.get(); while (c != '\n') { assert (isdigit(c)); stack.Push(c - '0'); c = cin.get(); } }
Add to main() add(lhs, rhs, result); cout << "\nSum = "; result.Display(cout); cout << endl;
Function Add() void add(Stack<int>& lhs, Stack<int>& rhs, Stack<int>& 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(); }
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); } }