Data Structure BASICS (6): Data Structure Basics
Stack is a linear table that only allows insert or delete operations at one end. It features: first-in-first-out (FILO)/Second-in-first-out (LIFO );
Stack VS. Queue
The stack and queue are both dynamic sets, but in the stack, you can remove the recently inserted one: the stack implements a kind of "last-in, first-out) similarly, in the queue, the elements that can be removed always have the longest time in the set: the queue implements first-in, first-out) in the next article, we will focus on reviewing queues.
STACK:
// Implementation and parsing of the ordered stack template <typename Type> class MyStack {template <typename T> friend ostream & operator <(std: ostream & OS, const MyStack <T> & stack); public: MyStack (int stackCapacity = 16 );~ MyStack (); bool isEmpty () const; void push (const Type & item); void pop () throw (std: range_error); const Type & top () const throw (std: range_error); private: Type * m_stack; int m_top; int m_capacity ;};
Template <typename Type> MyStack <Type>: MyStack (int stackCapacity): m_capacity (stackCapacity) {if (m_capacity <1) throw std :: range_error ("new size must> = 1"); // apply for memory and construct the object m_stack = new Type [m_capacity]; if (m_stack = NULL) throw std :: bad_alloc (); m_top =-1 ;}
Template <typename Type> MyStack <Type> ::~ MyStack () {// destructor object and release memory delete [] m_stack; m_stack = NULL; m_top =-1; // point the top pointer to invalid m_capacity = 0 ;}
// Global function: enlarge or zoom in the array template <typename Type> static void changeSize1D (Type * & array, int oldSize, int newSize) throw (std: range_error, std :: bad_alloc) {if (newSize <0) throw std: range_error ("new size must> = 0"); Type * tmp = new Type [newSize]; if (tmp = NULL) throw std: bad_alloc (); int minSize = std: min (oldSize, newSize); std: copy (array, array + minSize, tmp); delete [] array; // release the original array = tmp; // direct the original pointer to the newly applied array} template <typename Type> void MyStack <Type>: push (const Type & item) {// The maximum array capacity is m_capacity, therefore, the maximum subscript of the array is m_capacity-1 if (m_top> = m_capacity-1) {changeSize1D (m_stack, m_capacity, m_capacity * 2); // One expansion 2 times m_capacity * = 2 ;} // Insert the element into the top of the stack m_stack [++ m_top] = item ;}
// Whether the stack is empty template <typename Type> inline bool MyStack <Type >:: isEmpty () const {return-1 = m_top ;}
Template <typename Type> inline const Type & MyStack <Type >:: top () constthrow (std: range_error) {if (isEmpty () throw std :: range_error ("stack is empty"); return m_stack [m_top]; // return the top element of the stack}
Template <typename Type> inline void MyStack <Type>: pop () throw (std: range_error) {if (isEmpty () throw std :: range_error ("stack is empty"); // Note: if the elements of the object type stored in the stack need to be displayed, the Destructor is called, // At the same time, you also need to move the top pointer of the stack down m_stack [m_top --]. ~ Type ();}
// Output all the stack content to test the template <typename Type> ostream & operator <(ostream & OS, const MyStack <Type> & stack) {OS <stack. m_stack [0]; for (int I = 1; I <= stack. m_top; ++ I) OS <<'' <stack. m_stack [I]; return OS ;}
Appendix-test code
int main(){ MyStack<int> iStack; iStack.push(10); iStack.push(22); iStack.push(15); cout << iStack << endl; try { cout << "Top = " << iStack.top() << endl; iStack.pop(); cout << "Top = " << iStack.top() << endl; iStack.pop(); cout << "Top = " << iStack.top() << endl; iStack.pop(); cout << "Top = " << iStack.top() << endl; } catch (const std::exception &e) { cout << e.what() << endl; }}