Presentation is loading. Please wait.

Presentation is loading. Please wait.

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 Stacks.

Similar presentations


Presentation on theme: "Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 Stacks."— Presentation transcript:

1 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 Stacks Chapter 7

2 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 2 Introduction to Stacks A stack is a last-in-first-out (LIFO) data structure Adding an item –Referred to as pushing it onto the stack Removing an item –Referred to as popping it from the stack

3 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 3 A Stack Definition: –An ordered collection of data items –Can be accessed at only one end (the top) Operations: –construct a stack (usually empty) –check if it is empty –Push: add an element to the top –Top: retrieve the top element –Pop:remove the top element

4 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 4 Stack Specifications Stack(); //Construct a Stack object. //Precondition: None. //Postcondition: An empty Stack object has been constructed bool empty() const; //Check if stack is empty. //Precondition: None //Postcondition: Returns true if stack is empty and false otherwise. void push(const StackElement & value); //Add a value to a stack. //Precondition: value is to be added to this stack //Postcondition: value is added at top of stack provided there is space; //otherwise, a stack-full message is displayed //and execution is terminated. void pop(); //Remove value at top of stack (if any). //Precondition: Stack is nonempty. //Postcondition: Value at top of stack has been removed, unless the stack is empty; //in that case, an error message is displayed and execution allowed to proceed. StackElement top() const; //Retrieve value at top of stack (if any). //Precondition: Stack is nonempty //Postcondition: Value at top of stack is returned, unless the stack is empty; //in that case, an error message is displayed and a "garbage value" is returned.

5 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 5 Dynamic Array to Store Stack Elements Same issues regarding static arrays for stacks as for lists –Can run out of space if stack set too small –Can waste space if stack set too large As before, we demonstrate a dynamic array implementation to solve the problems Note additional data members required

6 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 6 Stack Header File #include #ifndef STACK #define STACK typedef int StackElement; class Stack { public: Stack(int numElements = 128); Stack(const & Stack original); bool empty() const; void push(const StackElement & value); void display(ostream & out) const; StackElement top() const; void pop(); ~Stack(); const Stack & operator=(const &Stack rightHandSide); private: StackElement *myArray; int myTop, myCapacity; }; // end of class declaration #endif Fig 7.6A DStack.h, Fig. 7.6B DStack.cppFig 7.6A DStack.hFig. 7.6B DStack.cpp Which methods needs to be changed to accommodate new implementation?

7 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 7 Dynamic Array to Store Stack Elements Constructor must –Check that specified numElements > 0 –Set capacity to numElements –Allocate an array pointed to by myArray with capacity = myCapacity –Set myTop to -1 if allocation goes OK Stack::Stack(int numElements) { //We will fill this in class } Running Time?

8 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 8 Dynamic Array to Store Stack Elements Class Destructor needed –Avoids memory leak –Deallocates array allocated by constructor Stack::~Stack( ) { //We will fill this in class } Running Time?

9 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 9 Dynamic Array to Store Stack Elements Copy Constructor needed for –Initializations –Passing value parameter –Returning a function value –Creating a temporary storage value Provides for deep copy Code the copy constructor!! Running Time?

10 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 10 Dynamic Array to Store Stack Elements Assignment operator –Again, deep copy needed –copies member-by-member, not just address Note implementation of algorithm in operator= definitiondefinition View driver program to test DStack class, Fig. 7.10 Fig. 7.10

11 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 11 Further Considerations What if dynamic array initially allocated for stack is too small? –Terminate execution? –Replace with larger array! Creating a larger array –Allocate larger array –Use loop to copy elements into new array –Delete old array –Point myArray variable at this new array

12 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 12 Linked Stacks Another alternative to allowing stacks to grow as needed Linked list stack needs only one data member –Pointer myTop –Nodes allocated (but not part of stack class) Note declaration, Fig. 7-11Fig. 7-11

13 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 13 Implementing Linked Stack Operations Constructor –Simply assign null pointer to myTop Empty –Check for myTop == null Push –Insertion at beginning of list Top –Return data to which myTop points View definitions in Fig. 7.12 Fig. 7.12 View definitions in Fig. 7.12 Fig. 7.12

14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 14 Implementing Linked Stack Operations Pop –Delete first node in the linked list ptr = myTop; myTop = myTop->next; delete ptr; Output –Traverse the list for (ptr = myTop; ptr != 0; ptr = ptr->next) out data << endl; View definitions in Fig. 7.12 Fig. 7.12 View definitions in Fig. 7.12 Fig. 7.12

15 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 15 Implementing Linked Stack Operations Destructor –Must traverse list and deallocate nodes –Note need to keep track of ptr->next before calling delete ptr; Copy Constructor –Traverse linked list, copying each into new node –Attach new node to copy View definitions in Fig. 7.12 Fig. 7.12 View definitions in Fig. 7.12 Fig. 7.12

16 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 16 Implementing Linked Stack Operations Assignment operator –Similar to copy constructor –Must first rule out self assignment –Must destroy list in stack being assigned a new value View completed linked list version of stack class, Fig 7.12Fig 7.12 Note driver program, Fig. 7.12CFig. 7.12C


Download ppt "Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved. 0-13-140909-3 1 Stacks."

Similar presentations


Ads by Google