The data structure has always been the weakest place for me, so I should take a good look.
The first is stack.
Stack has five functions: Determine whether the stack is empty, determine whether the stack is full, exit the stack, go to the stack, and take the top elements of the stack. class-based implementation is five methods, five member functions.
For convenience, the maximum value that the stack can accommodate elements is set to a fixed value. The element is int type. Use C ++ to implement the following:
1 // implement a stack 2 # include <iostream> 3 4 using namespace STD; 5 6 const int max = 100; 7 8 class Stack 9 {10 private: 11 int data [Max]; // defines an array to store 12 INT top data; // points to the top 13 public: 14 stack (); // constructor 15 ~ Stack (); // destructor 16 bool empty () const; 17 bool full () const; 18 void push (INT); 19 void POP (); 20 int gettop (); 21}; 22 23 Stack: Stack () 24 {25 top =-1; 26} 27 28 Stack ::~ Stack () 29 {30} 31 32 bool Stack: Empty () const33 {34 return Top =-1; 35} 36 37 bool Stack: Full () const38 {39 return Top = Max-1; 40} 41 42 void Stack: Push (int x) 43 {44 If (full ()) 45 {46 cout <"the stack is full! "<Endl; 47 // exit (1); 48} 49 else50 {51 top ++; 52 data [Top] = x; 53} 54} 55 56 void Stack:: Pop () 57 {58 If (empty () 59 {60 cout <"Stack is empty! "<Endl; 61 // exit (1); 62} 63 else64 {65 top --; 66} 67} 68 69 int Stack: gettop () 70 {71 If (empty () 72 {73 cout <"Stack is empty! "<Endl; 74 // exit (1); 75} 76 else77 {78 return data [Top]; 79} 80} 81 82 int main () 83 {84 stack S1; 85 s1.pop (); 86 s1.push (1); 87 s1.push (2); 88 s1.push (3); 89 s1.push (4 ); 90 s1.push (5); 91 for (INT I = 0; I <5; I ++) 92 {93 cout <s1.gettop () <Endl; 94 s1.pop (); 95} 96 s1.pop (); 97 return 0; 98}
When writing this stack, I learned a small detail that I never knew, that is, the exit usage. In 60 rows, if the stack is empty, exit will directly exit the entire program, the subsequent code will not be executed.
The data stored in the stack written above is int type. If we want to store other types of data, it is troublesome. Therefore, I used a template to re-write the stack.
// Implement a stack # include <iostream> using namespace STD; const int max = 100; Template <class T> class Stack {PRIVATE: t data [Max]; // define an array to store int top data; // point to the stack top public: Stack (); // constructor ~ Stack (); // destructor bool empty () const; bool full () const; void push (t); void POP (); t gettop ();}; template <class T> stack <t >:: stack () {Top =-1 ;}template <class T> stack <t> ::~ Stack () {}template <class T> bool stack <t >:: empty () const {return Top =-1 ;} template <class T> bool stack <t>: Full () const {return Top = Max-1;} template <class T> void stack <t> :: push (t x) {If (full () {cout <"stack full! "<Endl; // exit (1) ;}else {top ++; data [Top] = x ;}} template <class T> void stack <t> :: pop () {If (empty () {cout <"Stack is empty! "<Endl; // exit (1) ;}else {top -- ;}}template <class T> T stack <t >:: gettop () {If (empty () {cout <"Stack is empty! "<Endl; // exit (1) ;}else {return data [Top] ;}} int main () {stack <char> S1; s1.pop (); s1.push ('A'); s1.push ('B'); s1.push ('C'); s1.push ('D'); s1.push ('E '); for (INT I = 0; I <5; I ++) {cout <s1.gettop () <Endl; s1.pop () ;}s1.pop (); Return 0 ;}
After the template is used, stack can store various types of data. This class does not need to be rewritten, which is very convenient.