Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Stack Implementation in C++: Class Definition and Operations, Slides of Data Structures and Algorithms

The class definition and implementation of a stacktype in c++. The class interface, private data, and various operations such as push, pop, isempty, and isfull. The document also includes a class diagram and tracing client code examples.

Typology: Slides

2012/2013

Uploaded on 04/23/2013

saratey
saratey ๐Ÿ‡ฎ๐Ÿ‡ณ

4.3

(10)

87 documents

1 / 74

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 5
ADTs Stack and
Queue
Lecture 11
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a

Partial preview of the text

Download Stack Implementation in C++: Class Definition and Operations and more Slides Data Structures and Algorithms in PDF only on Docsity!

Chapter 5

ADTs Stack and Queue

Lecture 11

Stacks of Coins and Bills

Definition of Stack

โ€ข Logical (or ADT) level: A stack is an

ordered group of homogeneous items

(elements),

โ€ข in which the removal and addition of

stack items can take place only at the

top of the stack.

โ€ข A stack is a LIFO โ€œlast in, first outโ€

structure.

Stack ADT Operations

  • MakeEmpty -- Sets stack to an empty state.
  • IsEmpty -- Determines whether the stack is currently

empty.

  • IsFull -- Determines whether the stack is currently full.
  • Push (ItemType newItem) -- Throws exception if stack is full; otherwise adds newItem to the top of the stack.
  • Pop -- Throws exception if stack is empty; otherwise removes the item at the top of the stack.
  • ItemType Top -- Throws exception if stack is empty; otherwise returns a copy of the top item

class StackType { public:

StackType( );

bool IsFull () const;

bool IsEmpty() const;

void Push( ItemType item );

void Pop();

ItemType Top();

private: int top; ItemType items[MAX_ITEMS]; };

What are the

pre and post

conditions?

// File: StackType.cpp

#include "StackType.h" #include StackType::StackType( ) { top = -1; } bool StackType::IsEmpty() const { return(top == -1); }

bool StackType::IsFull() const { return (top == MAX_ITEMS-1); }

Class Interface Diagram

(Memory reversed to better illustrate concept)

StackType class

StackType

Pop

Push

IsFull

IsEmpty

Private data: top

[MAX_ITEMS-1]

.. . [ 2 ] [ 1 ] Top^ items^ [ 0 ]

char letter = โ€˜Vโ€™; StackType charStack; charStack.Push(letter); charStack.Push(โ€˜Cโ€™); charStack.Push(โ€˜Sโ€™); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(โ€˜Kโ€™); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code

Private data:

top

[ MAX_ITEMS-1 ]

. . . [ 2 ]

[ 1 ] items [ 0 ]

letter โ€˜Vโ€™

Tracing Client Code

letter โ€˜Vโ€™

Private data:

top 0

[ MAX_ITEMS-1 ]

. . . [ 2 ]

[ 1 ]

items [ 0 ] โ€˜ V โ€™

char letter = โ€˜Vโ€™; StackType charStack; charStack.Push(letter); charStack.Push(โ€˜Cโ€™); charStack.Push(โ€˜Sโ€™); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(โ€˜Kโ€™); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code

letter โ€˜Vโ€™

Private data:

top 1

[ MAX_ITEMS-1 ]

. . . [ 2 ]

[ 1 ] โ€˜ C โ€™

items [ 0 ] โ€˜ V โ€™

char letter = โ€˜Vโ€™; StackType charStack; charStack.Push(letter); charStack.Push(โ€˜Cโ€™); charStack.Push(โ€˜Sโ€™); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(โ€˜Kโ€™); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code

letter โ€˜Vโ€™

Private data:

top 2

[ MAX_ITEMS-1 ]

. . . [ 2 ] โ€˜ S โ€™

[ 1 ] โ€˜ C โ€™ items [ 0 ] โ€˜ V โ€™

char letter = โ€˜Vโ€™; StackType charStack; charStack.Push(letter); charStack.Push(โ€˜Cโ€™); charStack.Push(โ€˜Sโ€™); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(โ€˜Kโ€™); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code

letter โ€˜Vโ€™

Private data:

top 1

[ MAX_ITEMS-1 ]

. . . [ 2 ] โ€˜ S โ€™ [ 1 ] โ€˜ C โ€™ items [ 0 ] โ€˜ V โ€™

char letter = โ€˜Vโ€™; StackType charStack; charStack.Push(letter); charStack.Push(โ€˜Cโ€™); charStack.Push(โ€˜Sโ€™); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(โ€˜Kโ€™); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code

letter โ€˜Vโ€™

Private data:

top 2

[ MAX_ITEMS-1 ]

. . . [ 2 ] โ€˜ K โ€™ [ 1 ] โ€˜ C โ€™ items [ 0 ] โ€˜ V โ€™

char letter = โ€˜Vโ€™; StackType charStack; charStack.Push(letter); charStack.Push(โ€˜Cโ€™); charStack.Push(โ€˜Sโ€™); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(โ€˜Kโ€™); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code

letter โ€˜Kโ€™

Private data:

top 2

[ MAX_ITEMS-1 ]

. . . [ 2 ] โ€˜ K โ€™ [ 1 ] โ€˜ C โ€™ items [ 0 ] โ€˜ V โ€™

char letter = โ€˜Vโ€™; StackType charStack; charStack.Push(letter); charStack.Push(โ€˜Cโ€™); charStack.Push(โ€˜Sโ€™); if ( !charStack.IsEmpty( )) charStack.Pop( ); charStack.Push(โ€˜Kโ€™); while (!charStack.IsEmpty( )) { letter = charStack.Top(); charStack.Pop(0)}