1
2
3 There Will Be Times That You Come To Class & I Dump A Whole Bunch Of New Stuff On You & You Leave Confused! TODAY MAY BE ONE OF THOSE DAYS! You Leave - Review Your Notes/Slides/etc. Do Your Homework. Study! And If You Do Nothing In Between Classes, You Will Probably Stay Confused - Get Behind - Fail
4
How many of you have ever written a program that had a stack? What do we ever use a stack for? LIFO is an acronym for __________________ Last In First Out FIFO is an acronym for __________________ First In First Out A Stack Is A {LIFO/FIFO} Structure ? __________________ LIFO Stack Applet 5
Stack Applet Required Primitive Operations For Stack Push – Add To Stack Pop – Remove From Stack Empty – Is Stack Empty Optional Primitive Operations For Stack StackTop – Peek To See What’s On Top [Peek] Resize – Resize A Dynamic Memory Stack Full – Is Stack Full 6
7
class Stack1 { public: // Constructor // Destructor // Push // Pop // Empty private: }; Class Design Should Often Start With The Data! What Type Of Info Goes Into The Container? Data Structures Are Often Data Driven long int What Is The Capacity Of The Container? 6 Need The Info Container? long int Info[MAX+1], Info Need Some Reference To Top? # define MAX 5 MAX Top; Top 8
# define MAX 5 class Stack1 { public: // Constructor // Destructor // Push // Pop // Empty private: long int Info[MAX + 1]. Top; }; Stack1 S1, S2; Design Really Poor Thus Far? Info MAX Top Info MAX Top S1 S2 All Stacks Have To Be The Same Exact Size! We Really Don’t Want To Have To Create One Class For 6 integers, another for 10 integers, another for 100 integers, etc. 9
StackInt::StackInt(void) { Max = MAX; Top = -1; // Empty }; StackInt S1; The StackInt Constructor Info Max Top S1 Not the best constructor, but it works! 5 & ? ? ? ? ? ? We Could Write Push, Pop, Empty, etc. 10
11
class StackInt { public: StackInt (long int NewMax = 10); // Destructor // Push // Pop // Empty private: long int *Info, Max, Top; }; StackInt S1, S2(5), S3(100); What If We Used Dynamic Memory & Passed The Size? Info Max Top S1 StackInt Design is Better Than Stack1 Design? 12 The Dynamic Stack Allows For Stacks To Be Of Different Size!
StackInt::StackInt(long int NewMax) { Max = NewMax; Info = new long int [Max + 1]; if (Info == NULL) { puts ("Out Of Memory In StackInt()"); Max = -1; } Top = -1; // Empty }; StackInt S1(5); The StackInt Constructor Info Max Top S1 Not the best constructor, but it works! 5 & ? ? ? ? ? ? 13 We Could Write Push, Pop, Empty, etc.
14
class StackInt { public: StackInt (long int NewMax = 10); ~StackInt (void); bool Empty (void); bool Full (void); bool Pop (long int & OldInfo); bool Push (long int NewInfo); bool StackTop (long int & OldInfo); # ifdef STACK_DIAGNOSTIC_LEVEL // void Display(char Message[]="", long int NoToDisplay = 0); # endif // STACK_DIAGNOSTIC_LEVEL private: long int *Info; long int Max, Top; }; # ifdef STACK_DIAGNOSTIC_LEVEL // void TestStackInt (void); # endif // STACK_DIAGNOSTIC_LEVEL # endif //STACK_CLASS ============================================== Putting It All Together! Good But Not Great! Why Not Great? What If We Needed A Stack For float, char, int, short int, double, Athlete, Part, Employee, etc.? 15
16
class StackFloat { public: StackFloat (long int NewMax = 10); ~StackFloat (void); bool Empty (void); bool Full (void); bool Pop ( float & OldInfo); bool Push ( float NewInfo); bool StackTop ( float & OldInfo); # ifdef STACK_DIAGNOSTIC_LEVEL // void Display(char Message[]="", long int NoToDisplay = 0); # endif // STACK_DIAGNOSTIC_LEVEL private: float *Info; long int Max, Top; }; # ifdef STACK_DIAGNOSTIC_LEVEL // void TestStackFloat (void); # endif // STACK_DIAGNOSTIC_LEVEL # endif //STACK_CLASS ============================================== Change long int to float Rename Class, Constructor, Destructor 17
18
class StackChar { public: StackChar (long int NewMax = 10); ~StackChar (void); bool Empty (void); bool Full (void); bool Pop ( char & OldInfo); bool Push ( char NewInfo); bool StackTop ( char & OldInfo); # ifdef STACK_DIAGNOSTIC_LEVEL // void Display(char Message[]="", long int NoToDisplay = 0); # endif // STACK_DIAGNOSTIC_LEVEL private: char *Info; long int Max, Top; }; # ifdef STACK_DIAGNOSTIC_LEVEL // void TestStackFloat (void); # endif // STACK_DIAGNOSTIC_LEVEL # endif //STACK_CLASS ============================================== Change long int to char Rename Class, Constructor, Destructor Could Do Likewise for Employee, short int, double, Athlete, etc. 19
20
21
22
typedef long int InfoType; class Stack2 { public: StackInt (long int NewMax = 10); ~StackInt (void); bool Empty (void); bool Full (void); bool Pop (InfoType & OldInfo); bool Push (InfoType NewInfo); bool StackTop (InfoType & OldInfo); # ifdef STACK_DIAGNOSTIC_LEVEL // void Display(char Message[]="", long int NoToDisplay = 0); # endif // STACK_DIAGNOSTIC_LEVEL private: InfoType *Info; long int Max, Top; }; # ifdef STACK_DIAGNOSTIC_LEVEL // void TestStackInt (void); # endif // STACK_DIAGNOSTIC_LEVEL # endif //STACK_CLASS ============================================== This is even better; only one change is required to transfer the stack from one datatype to another. Many Languages have some type of TypeDef Including C & C++ Problem? What if a single program has two stacks of different datatypes? 23
24
25
26
27
Extract The File Into C:\Temp Name The Folder TomH-Stack (Use Your Name) 28
Enter Your Name Twice Update The Rest Of The Documentation Later 29
The Program Should Compile 30
Comment Out The define STACK_DIAGNOSTIC_LEVEL Those Includes Necessary For Testing No Longer Need ToBe Included In The Project 31
Comment Out The define STACK_DIAGNOSTIC_LEVEL All Test Code Goes Away With One Comment! // When The Class Is Changed Later, All Testing Will Return By Deleting The Comment // Delete The Comment // 32
33
Info Max Top S1 5 & ? ? ? ? ? ? 34 Write The Code Necessary To Set Up The Template Stack With A Dynamic Array That Matches The Model To The Right template Stack :: Stack (long int NewMax) { }
When You Get The Constructor & Destructor Compiling, Uncomment Out The Test Code For Diagnostic Level 1 Execute Examine Output We Need Some Way To See That The Constructor Worked! 35
36
Display Function template void Stack ::Display(char Message[], long int NoToDisplay) { long int Postition;; if (NoToDisplay == -1) NoToDisplay = Max; else NoToDisplay = Top; if(strlen(Message) > 0) puts(Message); for (Postition = NoToDisplay; Postition >= 0; Postition--) { puts(" | |"); fflush(stdout); cout << setw(5) << right << Postition << " | "; cout << setw(50) << Info[Postition]; cout << " |\n"; cout.flush(); } if (Top == -1) { puts(" | |"); puts(" | Stack Is Empty |"); puts(" | |"); printf(" Max = %3ld Top = %3ld &=%ld &=%X\n\n", Max, Top, &Info[0], &Info[0]); } else { puts(" | |"); printf(" Max = %3ld Top = %3ld &=%ld &=%X\n\n", Max, Top, &Info[0], &Info[0]); } 37
Set The Level 2 Execute Examine Output Look At The Various Ways You Can Call The Function & What Each Does Display With Data 38
Stack IntStack1; IntStack1.Display("Contents Of IntStack1", -1); IntStack1 shall contain 11 Part elements – the default. Since the built in int class has no initialization in the constructor, the contents of this container is initialized to garbage in memory. 39
Stack FloatStack4(4); FloatStack5.Display("Contents Of FloatStack4", -1); FloatStack4 shall contain 5 float elements – the default. Since the built in Float class has no initialization in the constructor, the contents of this container is initialized to garbage in memory. 40
Stack CharStack5(5); CharStack5.Display("Contents Of CharStack5", -1); CharStack5 shall contain 6 char elements. Since the built in char class has no initialization in the constructor, the contents of this container is initialized to garbage in memory. 41
Stack ClassStack8(8); ClassStack8.Display("Contents Of ClassStack8", -1); ClassStack8 shall contain 9 Student elements. Since the Student class has a constructor to initialize the data, the contents of this container is initialized accordingly! 42
Stack GarageStack9(9); GarageStack9.Display("Contents Of GarageStack9", -1); GarageStack9 shall contain 10 Auto elements. Since the Auto class has a constructor to initialize the data, the contents of this container is initialized accordingly! 43
44
Info Max Top S1 5 & ? ? ? ? ? ? 45 Write The Code For Empty S1 is Empty! template bool Stack ::Empty (void) { } Empty=True! Info Max Top S2 0 5 & ? ? ? ? ? 45 Empty=False!
When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 3 Execute Examine Output 46
47
Write The Code For Push - 1 template bool Stack ::Push (InfoType NewInfo) { return (?); } if (IntStack1.Push (10)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); Watch Before You Begin To Code 48
Write The Code For Push - 2 template bool Stack ::Push (InfoType NewInfo) { return (?); } Watch Before You Begin To Code if (IntStack1.Push (20)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 49
Write The Code For Push - 3 Watch Before You Begin To Code if (IntStack1.Push (30)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 50
Write The Code For Push - 4 Watch Before You Begin To Code if (IntStack1.Push (40)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 51
Write The Code For Push - 5 Watch Before You Begin To Code if (IntStack1.Push (50)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 52
Write The Code For Push - 6 Watch Before You Begin To Code if (IntStack1.Push (50)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 53
Write The Code For Push - 7 if (IntStack1.Push (60)) IntStack1.Display("IntStack1"); else puts(" Attempted Stack Overflow"); 54
When You Get The PushCompiling, Uncomment Out The Test Code For Diagnostic Level 4 Execute Examine Output 55
56
Info Max Top S1 5 & ? ? ? ? ? ? 57 Write The Code For Empty S1 is Empty! template bool Stack ::Empty (void) { } Empty=True! Info Max Top S2 0 5 & ? ? ? ? ? 57 Empty=False!
When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 5 Execute Examine Output 58
59
Info Max Top S1 5 5 & Write The Code For Full S1 is Full! template bool Stack ::Full(void) { } Full=True! Info Max Top S2 0 5 & ? ? ? ? ? 60 Full=False!
When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 6 Execute Examine Output 61
62
Write The Code For Pop - 1 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); Watch Before You Begin To Code 50 OldInfo 63
rite The Code For Pop - 2 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); Watch Before You Begin To Code 40 OldInfo 64
Write The Code For Pop - 3 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); Watch Before You Begin To Code 30 OldInfo 65
Write The Code For Pop - 4 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); Watch Before You Begin To Code 20 OldInfo 66
Write The Code For Pop - 5 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); Watch Before You Begin To Code 10 OldInfo 67
Write The Code For Pop - 6 template bool Stack ::Pop (InfoType & OldInfo) { return (?); } if (IntStack1.Pop(OldInfo)) { printf("OldInfo = %ld\n\n", OldInfo); IntStack1.Display("IntStack1"); } else puts ("Attempted Stack Underflow!\n"); 68
When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 7 Execute Examine Output 69
70
Write The Code For StackTop - 1 template bool Stack ::StackTop (InfoType &OldInfo) { return (?); } if (IntStack1.StackTop(TopInfo)) printf("TopInfo = %ld\n\n", OldInfo); else puts ("Stack Must Be Empty!\n"); Watch Before You Begin To Code 50 TopInfo 71
Write The Code For StackTop - template bool Stack ::StackTop (InfoType &OldInfo) { return (?); } if (IntStack1.StackTop(TopInfo)) printf("TopInfo = %ld\n\n", OldInfo); else puts ("Stack Must Be Empty!\n"); Ben Guest Record TopInfo Watch Before You Begin To Code 72
Write The Code For StackTop - 3 template bool Stack ::StackTop (InfoType &OldInfo) { return (?); } if (IntStack1.StackTop(TopInfo)) printf("TopInfo = %ld\n\n", OldInfo); else puts ("Stack Must Be Empty!\n"); ? TopInfo Stack Must Be Empty 73
When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 8 Execute Examine Output 74
75
template bool Stack :: Resize (long int SizeChange) { return (?); } Write The Code For Resize - 1 if (CSCI_2320.Push (Hunter)) CSCI_2320.Display(); else puts(" Attempted Stack Overflow"); Change Push Watch Before You Begin To Code 76
bool Resize (long int SizeChange = 5) Write The Code For Resize - 2 Info = & Attempt To Create NewInfo With (Max + SizeChange) + 1 Records No Room For Hunter NewInfo = & if (NewInfo = NULL) return (UNSUCCESSFUL)! Copy Records 0 - Top From Info To NewInfo Watch Before You Begin To Code 77
bool Resize (long int SizeChange = 5) Write The Code For Resize - 3 Info = & Delete/Return The Memory Associated With Info Point Info To The Memory Associated With NewInfo NewInfo = & Info = & Watch Before You Begin To Code 78
bool Resize (long int SizeChange = 5) Write The Code For Resize - 4 Things Look Like This, But We Are Not Done Yet. What Is Wrong With This Picture? We Need To Update The Max! Max = Max + SizeChange 10 Watch Before You Begin To Code 79
Write The Code For Resize - 5 if (CSCI_2320.Push (Hunter)) CSCI_2320.Display(); else puts(" Attempted Stack Overflow"); 80
When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 9 Execute Examine Output 81
82
template void Stack :: operator = (const Stack & S) { } Write The Code For Operator = -1 If we do not overload the = operator, then we would have the following Shallow Copy 83
template void Stack :: operator = (const Stack & S) { } Write The Code For Operator = -2 CSCI3343 = CSCI2320; You Will Use Many Of The Commands & Concepts That We Used In Resize! Slightly Different Order! 84
When You Get The Empty Compiling, Uncomment Out The Test Code For Diagnostic Level 10 Execute Examine Output 85
86
Remember The Microsoft Office Example Structured Language Developed Windows Version First Mac Version Cost 88% of Windows Version C++ Developed Windows Version First Wrote Parallel Versions Of A Few Short Utilities To Open/Close File, Move Cursor To Row/Col, Minimize Window, etc. using the basic operating system calls. Mac Version Ran In One Day – had to tweek screen because of different pixel resolution – alter images on user manual. Function OverloadsOperator Overloads.hpp &.cpp SeparationStream overloads Templates 87
88
89
90