Download presentation
Presentation is loading. Please wait.
Published byMartina Carpenter Modified over 8 years ago
1
1 Data Structures CSCI 132, Spring 2016 Notes_ 5 Stacks
2
2 Lists A list is an abstract data type consisting of an ordered group of items. A list is dynamic. Its length can vary. Items can be added to or deleted from the list There are many ways of implementing a list. For example: An array Linked lists
3
3 Arrays Arrays in C++ have fixed length. Arrays can be used to implement lists. A separate variable must keep track of the length of the list in the array.
4
4 Example /*Write a program to read in positive integers and store them in an array until a negative number is encountered.*/ int main(void) { int myList[30]; int count = 0; cin >> myList[0];//read first number while (myList[count] >= 0 ) { count++; cin >> myList[count];//read in next number } }
5
5 Stacks A stack is a list that has addition and deletion of items only from one end. It is like a stack of plates: Plates can be added to the top of the stack. Plates can be removed from the top of the stack. This is an example of “Last in, First out” (LIFO). Adding an item is called “pushing” onto the stack. Deleting an item is called “popping” off of the stack.
6
6 Using Stacks A Stack class should have the following methods: push(item)//Push an item onto the stack pop( )//Pop an item off the stack top( )//Return value of top item empty()//Return true if stack is empty.
7
7 Reversing a List with a Stack #include "stack.h"//stack class for a stack of integers int main( ) { int number; Stack numberStack; cout << "Enter positive integers. End with -1." << endl; cin >> number; while (number >= 0) { numberStack.push( number ); cin >> number; } cout << endl; while ( !numberStack.empty( ) ) { numberStack.top( number ); cout << number; numberStack.pop( ); } cout << endl; }
8
8 The Standard Template Library The C++ Standard Template Library (STL) has many useful classes and ADTs. To use the STL effectively, you need to understand the underlying data types. In this course we will learn how to implement the data structures from scratch.
9
9 Error Checking Error codes provide a way to signal that a particular error has occurred. Error codes allow the client program to deal with errors in an application-specific way. One way to specify error codes is with an enumeration: enum Error_code {success, fail, range_error, underflow, overflow, …};
10
10 The Stack Specification typedef int Stack_entry; const int maxstack =10 ;//small value for testing class Stack { public: Stack(); bool empty() const; Error_code pop(); Error_code top(Stack_entry &item) const; Error_code push(const Stack_entry &item); private: int count ; Stack_entry entry [maxstack ]; };
11
11 Implementing push( ) Error_code Stack ::push(const Stack_entry &item) { //Implement push() here }
12
12 Implementing push( ) Error_code Stack ::push(const Stack_entry &item) { Error_code outcome =success ; if (count >=maxstack) { outcome =overflow ; } else { entry [count ]=item ; count++; } return outcome ; }
13
13 Implementing pop( ) Error_code Stack ::pop() { //implement pop() }
14
14 Implementing pop( ) Error_code Stack ::pop() { Error_code outcome =success ; if (count ==0) { outcome =underflow ; } else { count-- ; } return outcome ; }
15
15 Implementing top( ) Error_code Stack ::top(Stack_entry &item) const { //implement top( ) }
16
16 Implementing top( ) Error_code Stack ::top(Stack_entry &item) const { Error_code outcome =success ; if (count ==0){ outcome =underflow ; } else { item =entry [count - 1]; } return outcome ; }
17
17 Implementing empty( ) bool Stack ::empty( )const { //implement empty( ) }
18
18 Implementing empty( ) bool Stack ::empty( )const { bool outcome =true; if (count >0) { outcome =false; } return outcome ; }
19
19 Implementing Stack( ) Stack ::Stack() {//implement Stack( ) }
20
20 Implementing Stack( ) Stack ::Stack() { count =0 ; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.