Data Structure interview 3-common stack operations

Source: Internet
Author: User
Tags stack pop
Data Structure interview 3-common stack operations

Note: The interview book has related exercises, but the ideas are relatively unclear and the layout is incorrect. The author has rewritten the related books and opinions for your reference.

III. Basic stack operations

3.1 construct stack with Array

Note the following ]:

1. three elements of array-based Stack: 1) Maximum stack capacity maxsize; 2) current stack capacity = number of elements in the current stack = top stack-1; 3) type * list, an element of the Dynamic Array Storage stack;

2. for outbound and inbound stack operations, check whether the stack is empty or full. If the stack is empty or full, handle exceptions or error messages.

3. Pay attention to the push operation on the stack. First, the stack is first pushed to the top + 1; the pop operation on the stack is the opposite. First, the top-1 operation and then the stack is pushed. [Note: top points to the next position of the last element in the array ].

4. Pay attention to copying constructor and value assignment function. In particular, when assigning values, do not assign values by yourself. If the values are different in size, do not complete the value assignment operation.

Template <typenametype> class arrstack {public: arrstack (intnsize = 100 );~ Arrstack (); arrstack (constarrstack & copystack); arrstack & operator = (const arrstack & otherstack); voidinitializestack (); void destroystack (); bool isstackempty (); bool isstackfull (); void push (consttype & item); void POP (type & poppeditem); Private: int maxsize; int top; type * List ;}; template <typename type> arrstack <type >:: arrstack (INT nsize = 100) {If (nsize <0) {nsize = 100; List = newtype [nsize]; top = 0; maxsize = 100;} else {list = newtype [nsize]; Top = 0; maxsize = nsize ;}} template <typename type> arrstack <type> ::~ Arrstack () {If (! List) {Delete [] list; // Delete the array. Delete [] list; List = NULL; }}template <typename type> arrstack <type> :: arrstack (const arrstack & copystack) {maxsize = copystack. maxsize; Top = copystack. top; List = newtype [maxsize]; // note that the custom size is required, which is prone to errors. for (INT I = 0; I <top; I ++) {list [I] = copystack. list [I] ;}}template <typename type> arrstack <type> & arrstack <type >:: operator = (constarrstack & otherstack) {If (this = & oth Erstack) {cout <"can't copy oneself! "<Endl; return * This;} else {If (maxsize! = Otherstack. maxsize) {cout <"the size of two stack are not equal! "<Endl; return * This;} else {maxsize = otherstack. maxsize; Top = otherstack. top; For (INTI = 0; I <top; I ++) {list [I] = otherstack. list [I];} // endfor return * This;} // end else} template <typename type> void arrstack <type >:: initializestack () {destroystack () ;}template <typename type> void arrstack <type >:: destroystack () {Top = 0 ;}template <typename type> bool arrstack <type> :: isstackempty () {return (P = 0);} template <typename type> bool arrstack <type >:: isstackfull () {return (Top = maxsize );} template <typename type> void arrstack <type>: Push (const type & item) {If (! Isstackfull () {list [Top] = item; top ++;} else {cout <"the stack was already full! "<Endl ;}template <typename type> void arrstack <type >:: POP (type & poppeditem) {If (! Isstackempty () {top --; poppeditem = list [Top];} else {cout <"the stack was already empty! "<Endl ;}}

3.2 Create a stack with a linked list

Note:

1) the mark for determining the stack null is Top = NULL. Because the stack space can be dynamically allocated, it can be considered that the chain stack is not full.

2) The Stack's inbound push operation must first determine whether the stack is full or not. First import the stack, and then adjust the top pointer;

3) The stack pop operation must first determine whether the stack is empty and not empty before the stack can exit. First adjust the top pointer, then exit the stack, and delete the original node.

4) You can add count to determine the number of current nodes.

Template <typename type> struct nodetype {type Info; nodetype * link ;}; template <typename type> class partial stack {public: Partial stack ();~ Consumer stack (); consumer stack (constlinkedstack <type> &); consumer stack & operator = (const consumer stack <type> &); voidinitializestack (); void destroystack (); bool isstackempty () const; bool isstackfull () const; void push (consttype & item); void POP (type & poppeditem); void nodecount (); voidreverseprint (); // non-Recursive Implementation of reverse printing... PRIVATE: nodetype <type> * Top; int count; // count node count}; Template <typename type> slave stack <type >:: Li Nkedstack () {COUNT = 0; Top = NULL;} template <typename type> consumer stack <type> ::~ Consumer stack () {While (top! = NULL) {nodetype <type> * tempnode = new nodetype <type>; tempnode = top; Top = Top-> link; deletetempnode ;} // end if} template <typename type> consumer stack <type >:: consumer stack (constlinkedstack <type> & copystack) {If (copystack. top! = NULL) {nodetype <type> * Current; nodetype <type> * First; nodetype <type> * newnode; Top = newnodetype <type>; top-> info = copystack. top-> Info; // the top here cannot be used directly. Memory Error! Top-> link = copystack. top-> link; first = top; // first Follow up the current linked list... current = copystack. top-> link; // current follow-up copy linked list... while (current! = NULL) {newnode = new nodetype <type>; newnode-> link = Current-> link; newnode-> info = Current-> Info; first-> link = newnode; first = newnode; current = Current-> link;} // end while COUNT = copystack. count ;}// end if else {Top = NULL; Count = 0 ;}}template <typename type> consumer stack <type> & Consumer stack <type> :: operator = (const consumer stack <type> & otherstack) {// 1 avoid assigning values if (this = & otherstack) {cout <"can't copy Oneself! "<Endl; return * This;} // 2 other else {If (top! = NULL) {destroystack ();} If (otherstack. Top! = NULL) {nodetype <type> * Current; nodetype <type> * First; nodetype <type> * newnode; Top = new nodetype <type>; top-> info = otherstack. top-> Info; top-> link = otherstack. top-> link; first = top; // first Follow up the current linked list... current = otherstack. top-> link; // current follow-up copy linked list... while (current! = NULL) {newnode = new nodetype <type>; newnode-> link = Current-> link; newnode-> info = Current-> Info; first-> link = newnode; first = newnode; current = Current-> link;} // endwhile COUNT = otherstack. count ;}// end if else {Top = NULL; Count = 0;} return * This ;}} template <typename type> void consumer stack <type >:: initializestack () {destroystack () ;}template <typename type> void consumer stack <type >:: destroystack (){ Count = 0; Top = NULL;} template <typename type> bool consumer stack <type >:: isstackempty () const {return (COUNT = 0 );} template <typename type> bool custom stack <type>: isstackfull () const // The space is not fixed. Dynamic Application! {Return false;} template <typename type> void consumer stack <type >:: push (const type & item) {If (! Isstackfull () {nodetype <type> * newnode = new nodetype <type>; newnode-> info = item; newnode-> link = NULL; If (Top = NULL) {Top = newnode;} else {newnode-> link = top; Top = newnode;} count ++; cout <item <"was pushed! "<Endl ;}template <typename type> void consumer stack <type >:: POP (type & poppeditem) {If (! Isstackempty () {nodetype <type> * temp = new nodetype <type>; temp = top; poppeditem = Top-> Info; Top = Top-> link; count --; cout <poppeditem <"was popped! "<Endl; Delete temp ;}template <typename type> void consumer stack <type >:: nodecount () {cout <"nodecount =" <count <Endl;} // print the reverse linked list using recursion in the previous section, which is non-recursive. Template <typename type> void preview stack <type>: reverseprint () // non-Recursive Implementation of reverse printing... {// The annotation part can be used as a non-Recursive Implementation of the linked list call stack. // Nodetype <type> * Current = new nodetype <type>; // current = top; // while (current! = NULL) // {// POP (current-> info); // current = Current-> link; //} int poppeditem = 0; while (! Isstackempty () {Pop (poppeditem );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.