Two C ++ implementations of stack

Source: Internet
Author: User

 

Stack is one of the most widely used data structures and it is necessary to summarize them.

 

Stack is a table that can only be inserted or deleted in one position. It is the end of the table, called the top of the stack. It is a post-in-first-out (LIFO) table). There are only two basic stack operations: push (in stack) and pop (Out stack). The former is equivalent to insert, and the latter is equivalent to deleting the last element.

 

Stack is essentially a restricted table, so you can use any form of table to implement it. The most common use is linked list and array.

 

Features of using a linked list: you do not need to specify the size of the list, which does not waste space. Applying for release of dynamic memory is involved in stack loading and output, and the time consumption is high;

 

Array features: You need to specify the size of the array, which may waste space or be insufficient. The stack and stack do not involve dynamic memory application release, therefore, there is almost no cost in time; random access is also supported.

 

 

 

 

Conclusion: The use of linked lists is generally the first choice, unless two conditions are met: 1. High requirements on running efficiency; 2. The size of space required by the stack can be predicted.

 

 

 

The stack implemented by using a single-chain table is as follows:

 

/* Stack. h (slist. h is the heaher file of single list class SList )*/

# Include "slist. h"

 

Template <typename T>

Class Stack

{

Public:

Stack ();

Stack (const T & initdata );

~ Stack ();

Public:

Int IsEmpty () const;

Void MakeEmpty (); // empty.

Int GetCount () const;

// Void DisposeStack (); // clear. For the Stack implemented by using the linked list, the two clears have the same meaning. For the Stack implemented by using arrays, they have different meanings.

Int Push (T data );

Int Pop (T * data = NULL );

Int Top (T * data) const;

 

Private:

SList <T> slist;

};

 

 

Template <typename T>

Inline Stack <T >:: Stack (): slist ()

{

}

 

Template <typename T>

Inline Stack <T >:: Stack (const T & initdata): slist (initdata)

{

}

 

Template <typename T>

Inline Stack <T> ::~ Stack ()

{

}

 

Template <typename T>

Inline int Stack <T >:: IsEmpty () const

{

Return slist. IsEmpty ();

}

 

Template <typename T>

Inline void Stack <T >:: MakeEmpty ()

{

Slist. RemoveAll ();

}

 

Template <typename T>

Inline int Stack <T >:: GetCount () const

{

Return slist. GetCount ();

}

/* Template <typename T>

Inline void Stack <T >:: DisposeStack ()

{

Slist. RemoveAll ();

}*/

 

Template <typename T>

Inline int Stack <T>: Push (T data)

{

Return slist. AddHead (data );

}

 

Template <typename T>

Inline int Stack <T >:: Pop (T * data)

{

If (IsEmpty ())

Return 0;

 

If (data)

Top (data );

 

Slist. RemoveHead ();

Return 1;

}

 

Template <typename T>

Inline int Stack <T>: Top (T * data) const

{

ASSERT (data );

 

If (IsEmpty ())

Return 0;

 

* Data = slist. GetHead ();

Return 1;

}

 

 

 

 

The stack implemented by array is as follows:

 

/* Stackarray. h */

 

# Include <assert. h>

 

Const int EmptyTOS =-1;

Const int MinStackSize = 5;

Const int MaxStackSize = 500;

 

 

Template <typename T>

Class StackArray

{

Public:

StackArray (int maxsize = MaxStackSize );

~ StackArray ();

Public:

Int IsEmpty () const;

Void MakeEmpty ();

Int GetCount () const;

Int IsFull ();

Int Resize (int newmaxsize); // change the capacity.

Int Push (const T & data );

Int Pop (T * data = NULL );

Int Top (T * data) const;

Private:

Void DisposeStack (); // release the memory occupied by the array, that is, the stack is destroyed.

Private:

Int capacity;

Int tos; // Top of stack for now.

T * array;

};

 

Template <typename T>

Inline StackArray <T >:: StackArray (int maxsize): capacity (maxsize), tos (EmptyTOS), array (NULL)

{

ASSERT (capacity> = MinStackSize );

Try

{

Array = new T [capacity];

}

Catch (std: bad_alloc &)

{

}

}

 

Template <typename T>

Inline StackArray <T> ::~ StackArray ()

{

 

DisposeStack ();

}

 

Template <typename T>

Inline void StackArray <T>: DisposeStack ()

{

Capacity = 0;

Tos = EmptyTOS;

If (array)

{

Delete [] array;

}

}

 

Template <typename T>

Inline int StackArray <T >:: IsEmpty () const

{

Return EmptyTOS = tos;

}

 

 

Template <typename T>

Inline void StackArray <T>: MakeEmpty ()

{

Tos = EmptyTOS;

}

 

Template <typename T>

Inline int StackArray <T>: GetCount () const

{

Return tos + 1;

}

 

Template <typename T>

Inline int StackArray <T >:: IsFull ()

{

Return tos> = capacity-1;

}

 

Template <typename T>

Inline int StackArray <T >:: Resize (int newmaxsize)

{

DisposeStack ();

Capacity = newmaxsize;

Tos = EmptyTOS;

Try

{

Array = new T [newmaxsize];

}

Catch (std: bad_alloc &)

{

Return 0;

}

Return 1;

}

 

Template <typename T>

Inline int StackArray <T>: Push (const T & data)

{

If (IsFull ())

{

Return 0;

}

Else

{

Array [++ tos] = data;

Return 1;

}

}

 

Template <typename T>

Inline int StackArray <T>: Pop (T * data = NULL)

{

If (IsEmpty ())

{

Return 0;

}

Else

{

If (data)

{

* Data = array [tos];

}

-- Tos;

Return 1;

}

}

 

 

Template <typename T>

Inline int StackArray <T>: Top (T * data) const

{

If (IsEmpty ())

{

Return 0;

}

Else

{

* Data = array [tos];

Return 1;

}

}

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.