The use of chained storage stack as a chain stack (or simply a chain stack), the advantages of the chain stack is convenient for multiple stacks to share storage space and improve its efficiency, and there is no overflow situation (because the chain stack is linked to the pointer, as long as the memory is large enough, the chain can theoretically store the element is no limit);
Compared to the sequential stack, because the sequential stack is an array implementation, once the array fills up, you must re-request the memory, and all the elements "move", and the chain is omitted this "time-consuming" work, but need to pay the cost of attaching a pointer;
The chain stack is usually implemented by a single-linked list, and specifies that all operations must be a single-linked table header, and w our chain stack has no head node, m_top directly to the top of the stack element;
The chain stack is illustrated as follows:
Link Stack node Construction:
Template <typename type>class chainnode{ template <typename t> friend Ostream &operator< < (ostream &os, const linkstack<t> &stack); Friend class linkstack<type>;p rivate: chainnode (const Type &_data, Chainnode *_next = NULL) :d ata (_ Data), Next (_next) {} Type data; Chainnode *next;};
Chain Stack design :
Template <typename type>class linkstack{ template <typename t> friend Ostream &operator< < (ostream &os, const linkstack<t> &stack);p ublic: linkstack (): M_top (NULL) {} ~linkstack () { makeempty (); } BOOL IsEmpty () const { return m_top = = NULL ; } void Pop () throw (std::out_of_range); Const TYPE &top () const throw (std::out_of_range); void push (const Type &data); void Makeempty ();p rivate: chainnode<type> *m_top;};
The three main operations of the stack:
Template <typename type>const Type &linkstack<type>::top () Constthrow (std::out_of_range) { if ( IsEmpty ()) throw Std::out_of_range ("Stack is empty, can ' t get Data"); return m_top->data;}
Template <typename type>void Linkstack<type>::p op () throw (std::out_of_range) { if (IsEmpty ()) Throw Std::out_of_range ("Stack is empty, can ' t delete"); chainnode<type> *deletenode = m_top; M_top = m_top->next; Delete Deletenode;}
Template <typename type>void linkstack<type>::p ush (const Type &data) { //Here is the key point of the entire chain // The operation generates a node,// and automatically moves the m_top up one grid,/ /and M_top the node originally pointed to as the next node of the newly generated node //Note here //If push is run for the first time, the original m_top is null //New M_top points to the first element m_top = new chainnode<type> (data, m_top);}
Empty the entire stack :
Template <typename type>void linkstack<type>::makeempty () { while (!isempty ()) { pop (); }}
All content in the output stack :
Template <typename type>ostream &operator<< (ostream &os, const linkstack<type> &stack) { chainnode<type> *currentnode = stack.m_top; while (CurrentNode! = NULL) { cout << currentnode->data << '; CurrentNode = currentnode->next; } return OS;}
Test Code :
int main () { linkstack<int> test; for (int i = 0; i < ++i) { Test.push (rand ()%100); } cout << test << Endl; cout << "top =" << test.top () << Endl; Test.pop (); cout << "top =" << test.top () << Endl; Test.push (1); cout << "top =" << test.top () << Endl; while (!test.isempty ()) { test.pop (); } cout << test << Endl; Test.push (one); Test.push (a); Test.push (); cout << test << Endl; Test.makeempty (); Try { cout << "top =" << test.top () << Endl; } catch (const std::exception &e) { cerr << e.what () << Endl; } return 0;}
Data Structure Foundation (13)--the design and implementation of chain-stack