Click here to Skip to main content
16,016,088 members
Articles / Programming Languages / C++/CLI
Article

Dynamic Stack

Rate me:
Please Sign up or sign in to vote.
2.07/5 (9 votes)
9 Jul 20063 min read 31.6K   268   7   2
Dynamic Stack by using Linked list concept

Introduction<o:p>

Before starting this article I want to mention this article is not for professionals this is only for kids who are new in programming of C++ .NET.<o:p>

Now let see first what is stack? Stack is a datastructure which based on LIFO methodology means (LAST IN FIRST OUT) the item which is inserted first in the structure must be take out last from the structure. Stack has two main functions PUSH() and POP(). <o:p>

PUSH() means to insert the item in the structure.<o:p>

POP() means to pull out the last inserted item from the structure.<o:p>

<o:p> 

Stack is very useful in number of occasions for example if we consider simple arithmetic expression such as (3 + 2 * 2) then we normally traverse the arithmetic expression in POST ORDER to calculate which can easily be done through Stack.<o:p>

 <o:p>

Mostly Stack is build on Array type structure which is static in nature you can’t dynamically change the length of array.<o:p>

<o:p> 

Description<o:p>

Just before coming to view the code we have to know what is linked list? Here is just short brief of linked list.<o:p>

<o:p> 

The simplest kind of linked list is a singly-linked list (or slist for short), which has one link per node. This link points to the next node in the list, or to a null value or empty list if it is the final node.<o:p>

<o:p> 

<v:shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><v:stroke joinstyle="miter"><v:formulas><v:f eqn="if lineDrawn pixelLineWidth 0"><v:f eqn="sum @0 1 0"><v:f eqn="sum 0 0 @1"><v:f eqn="prod @2 1 2"><v:f eqn="prod @3 21600 pixelWidth"><v:f eqn="prod @3 21600 pixelHeight"><v:f eqn="sum @0 0 1"><v:f eqn="prod @6 1 2"><v:f eqn="prod @7 21600 pixelWidth"><v:f eqn="sum @8 21600 0"><v:f eqn="prod @7 21600 pixelHeight"><v:f eqn="sum @10 21600 0"><v:path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"><o:lock aspectratio="t" v:ext="edit"><v:shape id="_x0000_i1025" style="WIDTH: 24pt; HEIGHT: 24pt" type="#_x0000_t75" alt="Singly Linked List">Sample screenshot<o:p>

<o:p> 

<o:p> 

<o:p> 

__gc struct LinkedList<o:p>

{<o:p>

      System::Object *value;<o:p>

      LinkedList *Address;<o:p>

};<o:p>

<o:p> 

<o:p> 

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

 <o:p>

The above structure is very simple structure that is fulfilling the requirement of singly linked list.<o:p>

<o:p> 

<o:p> 

void push(System::Object* obj)<o:p>

{<o:p>

if (this->StackPosition == -1) // checking Stack is empty<o:p>

      {<o:p>

            LinkedList* node; <o:p>

            node = new LinkedList; // creating the object of LinkedList structure<o:p>

            node->value = obj; // setting the given value<o:p>

            node->Address = NULL; // setting Address to Null on insertion of first item<o:p>

            this->PreviousNode = node;<o:p>

            this->StackPosition +=1; // incrementing StackPosition by one to keep track how many items are inserted in Stack.<o:p>

      }<o:p>

      else<o:p>

      {<o:p>

            this->StackPosition += 1;<o:p>

            LinkedList* node;<o:p>

            node = new LinkedList;<o:p>

            node->value = obj;<o:p>

            node->Address = this->PreviousNode;<o:p>

            this->PreviousNode = node; <o:p>

      }<o:p>

}<o:p>

<o:p> 

<o:p> 

The above method of push() is for inserting item in the collection where StackPosition is checking that how many items are present in the collection and PreviousNode is the last node inserted.<o:p>

<o:p> 

 <o:p>

System::Object* pop()<o:p>

            {<o:p>

                  System::Object *temp;<o:p>

                  if (this->StackPosition > -1)//checking is there any item present in the collection<o:p>

                  {<o:p>

                        this->StackPosition -= 1;<o:p>

                        temp = this->PreviousNode->value;<o:p>

                        if(this->StackPosition > -1)// checking After POP() items are present in collection<o:p>

                        {<o:p>

                              this->PreviousNode = this->PreviousNode->Address; // setting the last node <o:p>

                        }<o:p>

                        else<o:p>

                              this->PreviousNode = NULL;<o:p>

                  }<o:p>

                  else<o:p>

                  {<o:p>

                        throw new System::Exception(S"Item is not present in the Collection");<o:p>

                  }<o:p>

                  return temp;<o:p>

            }<o:p>

<o:p> 

 <o:p>

POP() is used to pull out the last inserted item from the collection which is pointed by  PreviousNode.<o:p>

 <o:p>

So I think this article help beginners in some way.<o:p>

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Other Other
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
General'new' without 'delete' !!! Pin
Kybert2-May-07 5:36
Kybert2-May-07 5:36 
JokeHi Pin
Yasir cheeta13-Sep-06 4:09
Yasir cheeta13-Sep-06 4:09 
The download file is corrupted please upload the file again Smile | :)

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.