1 linked stack chapter 4. 2 linked stack we can implement a stack as a linked list. same operations....

30
1 Linked Stack Chapter 4

Upload: justin-evans

Post on 16-Jan-2016

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

1

Linked Stack

Chapter 4

Page 2: 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

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.

Page 3: 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

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.

Page 4: 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

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

http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/2011_02_07_Stacks/

Add to project. Project > Add Existing Item

Page 5: 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

5

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.

Page 6: 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

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;

Page 7: 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

7

Stack.h

private:

DLList<T>* data;

//int size;

//int top; // Index of element at top of stack

// // -1 when stack is empty

};

Page 8: 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

8

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 [] }

Page 9: 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

9

Push()

template <class T>

void Stack<T>::Push(const T& value)

{

data->addToHead(value);

}

Page 10: 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

10

Pop()

template <class T>

T Stack<T>::Pop()

{

if (this->IsEmpty())

{

throw "Pop called for empty stack";

}

return data->deleteFromHead();

}

Page 11: 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

11

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;

}

Page 12: 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

12

Display()

template <class T>

void Stack<T>::Display(std::ostream& out) const

{

data->printAll();

}

Page 13: 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

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

Page 14: 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

14

A Warning

The isEmpty method in the List template should have been declared as bool.

Make that change.

Page 15: 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

15

Program Running

Page 16: 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

16

Try it on Linux

Page 17: 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

17

Try it on Linux

The Visual Studio compiler is more forgiving!

Add using namespace std; to printAll.

Page 18: 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

18

OK This Time

Page 19: 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

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

Page 20: 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

20

Modify printAll()

Modify DLList::printAll() to output all items on the same line.

cout << p->info << " ";

Page 21: 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

21

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();

}

Page 22: 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

22

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();

}

}

Page 23: 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

23

Program in Action

Page 24: 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

24

Program in Action

Page 25: 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

25

Add to main()

add(lhs, rhs, result);

cout << "\nSum = ";

result.Display(cout);

cout << endl;

Page 26: 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

26

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();

}

Page 27: 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

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);

}

}

Page 28: 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

28

A Small Example

Page 29: 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

29

A Big Example

Page 30: 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

30

Another Big Example