22
Data Structures Data Structures Chapter 2 Chapter 2 Stacks Stacks Andreas Savva

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

Embed Size (px)

Citation preview

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

Data StructuresData Structures

Chapter 2Chapter 2StacksStacks

Andreas Savva

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

22

StacksStacks A stack is a data structure in which all insertions and A stack is a data structure in which all insertions and

deletions of entries are made at one end, called the deletions of entries are made at one end, called the top of the stack.top of the stack.

The last entry which is inserted is the first one that The last entry which is inserted is the first one that will be removed. (will be removed. (Last In First Out : LIFOLast In First Out : LIFO))

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

33

Application of StacksApplication of Stacks

Page visited history in a Web-Page visited history in a Web-browserbrowser

Undo sequence in a text editorUndo sequence in a text editor Program run-time environmentProgram run-time environment

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

44

ABCDEF

The StackThe Stack

Pushing and popping a stack

Push box A onto the stackPush box A onto the stack

Push box B onto the stackPush box B onto the stack

Pop a box from the stackPop a box from the stack

Pop a box from the stackPop a box from the stack

Push box C onto the stackPush box C onto the stack

Push box D onto the stackPush box D onto the stack

Push box E onto the stackPush box E onto the stack

Push box F onto the stackPush box F onto the stack

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

55

Implementation of StacksImplementation of Stacks

Some Basic Operations for StacksSome Basic Operations for Stacks CreateCreate a stack, leaving it empty. a stack, leaving it empty. Test whether the stack is Test whether the stack is EmptyEmpty.. Retrieve the Retrieve the TopTop entry from the stack, entry from the stack,

provided the stack is not empty.provided the stack is not empty. PopPop the entry off the top of the stack, the entry off the top of the stack,

provided the stack is not empty.provided the stack is not empty. PushPush a new entry onto the top of the a new entry onto the top of the

stack, provided that the stack is not stack, provided that the stack is not full.full.

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

66

Basic OperationsBasic Operations

falsefalse

Create() =Create() =

Empty =Empty =

315 Empty =Empty =truetrue

55Top =Top =

315 Top =Top = ErrorError

underflowunderflow

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

77

Basic OperationsBasic Operations

Push 2, =Push 2, =

315

Pop =Pop =

315 Pop =Pop = ErrorError

underflowunderflow

2

315

31

Push 7, =Push 7, =2

315 ErrorError

overflowoverflow

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

88

Standard Template Library (STL)Standard Template Library (STL)

The STL provides convenient implementations for The STL provides convenient implementations for many common data structures.many common data structures.

We can include the STL stack implementation into We can include the STL stack implementation into our programs with the directive:our programs with the directive:

#include <stack>#include <stack>

And then we can define initially empty stack And then we can define initially empty stack objects and apply push, pop, top and empty.objects and apply push, pop, top and empty.

In C++, a In C++, a templatetemplate constructor allows us to constructor allows us to create data structures whose entities have create data structures whose entities have different types:different types:

stack <stack <doubledouble> numbers;> numbers;

stack <stack <intint> numbers;> numbers;

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

99

Example using the STL stackExample using the STL stack

#include <stack>int main( )/* Pre: The user supplies an integer n and n real numbers Post: The numbers are printed in reverse order Uses: The STL class stack and its methods */{ int n; double item; stack<double> numbers; // Declares and initializes a stack of numbers cout << ”Enter an integer n followed by n real numbers” << endl; cin >> n; for (int i = 0; i < n; i++) { cin >> item; numbers.push(item); } cout << endl << endl << ”Numbers in reverse: ”; while (!numbers.empty( )) { cout << numbers.top( ) << ” ”; numbers.pop( ); }}

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

1010

Information HidingInformation Hiding

The code in a client program should not depend The code in a client program should not depend on a particular choice of implementation.on a particular choice of implementation.

We may first decide to represent a stacks one We may first decide to represent a stacks one way and then we may decide that another is way and then we may decide that another is better.better.

If we use If we use information hidinginformation hiding by writing separate by writing separate functions for manipulating stacks, then only the functions for manipulating stacks, then only the declarations will need to be changed.declarations will need to be changed.

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

1111

Basic Operations for StacksBasic Operations for Stacks CreateCreate a stack, leaving it empty. a stack, leaving it empty. Test whether the stack is Test whether the stack is EmptyEmpty.. Test whether the stack is Test whether the stack is FullFull.. Return the Return the SizeSize (number of entries) of the (number of entries) of the

stack.stack. Retrieve the Retrieve the TopTop entry from the stack, entry from the stack,

provided the stack is not empty.provided the stack is not empty. PopPop the entry off the top of the stack, the entry off the top of the stack,

provided the stack is not empty.provided the stack is not empty. PushPush a new entry onto the top of the stack, a new entry onto the top of the stack,

provided that the stack is not full.provided that the stack is not full. PrintPrint all the entries of the stack. all the entries of the stack.

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

1212

The Stack ClassThe Stack Class

methods: Stack (constructor) empty full size top pop push print

Data members

class Stack

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

1313

Stack implementation - Stack implementation - ArrayArray

typedef double Stack_entry;enum Error_code {success,overflow,underflow};const int max = 100;class Stack {public: Stack(); bool empty() const; bool full() const; int size() const; Error_code top(Stack_entry &) const; Error_code pop(); Error_code push(const Stack_entry &); void print() const;private: int count; Stack_entry entry[max];};

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

1414

Data Structure - Data Structure - ArrayArray

0 . . .[0] [1] [2] [max-1]

Empty Stack:

1 * . . .[0] [1] [2] [max-1]

Push the first entry:

n items in the stack:

n * * * * * . . .[0] [1] [2] [max-1][n-1] [n]

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

1515

Create StackCreate Stack

Stack::Stack()// Pre: None.// Post: Initialize Stack to be

empty

{

}

We use a constructor to initialize a new created stack as empty.

0 . . .[0] [1] [2] [max-1][n-1] [n]

countcount entryentry

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

1616

EmptyEmpty

bool Stack::empty() const// Pre: None.// Post:A result of true is returned if the Stack// is empty, otherwise false is returned.

{

}

0 . . .[0] [1] [2] [max-1][n-1] [n]

countcount entryentry

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

1717

FullFull

bool Stack::full() const// Pre: None.// Post: A result of true is returned if the Stack// is full, otherwise false is returned.

{

}

max * * * * * * *. . .[0] [1] [2] [max-1][n-1] [n]

countcount entryentry

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

1818

SizeSize

int Stack::size() const// Pre: None.// Post: Return the number of entries in the stack.

{

}

n * * * * * . . .[0] [1] [2] [max-1][n-1] [n]

countcount entryentry

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

1919

TopTopError_code Stack::top(Stack_entry &item) const// Pre: None.// Post:The top of a nonempty Stack is copied to item.// A code of underflow is returned if the Stack is empty.{

}

n * * * * * . . .[0] [1] [2] [max-1][n-1] [n]

countcount

ToToppentryentry

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

2020

PopPopError_code Stack::pop()// Pre: None.// Post:If the Stack is not empty, the top of the Stack is// removed. If the Stack is empty, an Error code of// underflow is returned and the Stack is left unchanged.

{

}

n * * * * * . . .[0] [1] [2] [max-1][n-1] [n]

countcount

n-1

entryentry

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

2121

PushPushError_code Stack::push(const Stack_entry &item)// Pre: None.// Post:If the Stack is not full, item is added to the top of// the Stack. If the Stack is full, an Error code of// overflow is returned and the Stack is left unchanged.

{

}n * * * * * . . .[0] [1] [2] [max-1][n-1] [n]

countcount

*n+1

entryentry

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

2222

PrintPrint

void Stack::print() const// Pre: None.// Post:Display the entries of the Stack.

{

}