Click here to Skip to main content
Licence 
First Posted 23 May 2006
Views 19,962
Bookmarked 7 times

Dynamic Stack

By | 9 Jul 2006 | Article
Dynamic Stack by using Linked list concept

Introduction

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.

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().

PUSH() means to insert the item in the structure.

POP() means to pull out the last inserted item from the structure.

 

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.

 

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

 

Description

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

 

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.

 

Sample screenshot

 

 

 

__gc struct LinkedList

{

      System::Object *value;

      LinkedList *Address;

};

 

 

 

 

 

 

 

 

The above structure is very simple structure that is fulfilling the requirement of singly linked list.

 

 

void push(System::Object* obj)

{

if (this->StackPosition == -1) // checking Stack is empty

      {

            LinkedList* node;

            node = new LinkedList; // creating the object of LinkedList structure

            node->value = obj; // setting the given value

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

            this->PreviousNode = node;

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

      }

      else

      {

            this->StackPosition += 1;

            LinkedList* node;

            node = new LinkedList;

            node->value = obj;

            node->Address = this->PreviousNode;

            this->PreviousNode = node;

      }

}

 

 

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.

 

 

System::Object* pop()

            {

                  System::Object *temp;

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

                  {

                        this->StackPosition -= 1;

                        temp = this->PreviousNode->value;

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

                        {

                              this->PreviousNode = this->PreviousNode->Address; // setting the last node

                        }

                        else

                              this->PreviousNode = NULL;

                  }

                  else

                  {

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

                  }

                  return temp;

            }

 

 

POP() is used to pull out the last inserted item from the collection which is pointed by  PreviousNode.

 

So I think this article help beginners in some way.

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

About the Author

dizzybuzy

Web Developer

Other Other

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General'new' without 'delete' !!! PinmemberKybert5:36 2 May '07  
JokeHi PinmemberYasir cheeta4:09 13 Sep '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 10 Jul 2006
Article Copyright 2006 by dizzybuzy
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid