Ch5 - 1 C Plus Data Structures Nell Dale Chapter 5 Linked...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus 2 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) -- Adds newItem to the top of the stack. ● Pop (ItemType& item)-- Removes the item at the top of the stack and returns it in item. 3 ADT Stack Operations Transformers ■ MakeEmpty ■ Push ■ Pop Observers ■ IsEmpty ■ IsFull change state observe state 4 5 Another Stack Implementation ● One advantage of an ADT is that the kind of implementation used can be changed. ● The dynamic array implementation of the stack has a weakness -- the maximum size of the stack is passed to the constructor as parameter. ● Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack. 6 class StackType<char> StackType MakeEmpty Pop Push IsFull IsEmpty Private data: topPtr ~StackType ‘C’ ‘V’ 7 class StackType<float> StackType MakeEmpty Pop Push IsFull IsEmpty Private data: topPtr ~StackType 23.4 -7.9 8 class StackType<StrType> StackType MakeEmpty Pop Push IsFull IsEmpty Private data: topPtr ~StackType cat dog 9 Tracing Client Code letter ‘V’ char letter = ‘V’; StackType< char > myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); 10 Tracing Client Code letter ‘V’ char letter = ‘V’; StackType< char > myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); Private data: topPtr NULL 11 Tracing Client Code letter char letter = ‘V’; StackType< char > myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); Private data: topPtr ‘V’ ‘V’ 12 Tracing Client Code letter char letter = ‘V’; StackType< char > myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); Private data: topPtr ‘C’ ‘V’ ‘V’ 13 Tracing Client Code letter char letter = ‘V’; StackType< char > myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’); if ( !myStack.IsEmpty( ) ) myStack.Pop( letter ); myStack.Push(‘K’); Private data: topPtr ‘S’ ‘C’ ‘V’ ‘V’ 14 Tracing Client Code letter char letter = ‘V’; StackType< char > myStack; myStack.Push(letter); myStack.Push(‘C’); myStack.Push(‘S’);myStack....
View Full Document

This note was uploaded on 06/13/2011 for the course CSC 280 taught by Professor Lebre during the Spring '04 term at Moraine Valley Community College.

Page1 / 67

Ch5 - 1 C Plus Data Structures Nell Dale Chapter 5 Linked...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online