65.9K
CodeProject is changing. Read more.
Home

Dynamic Stack

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.07/5 (9 votes)

May 23, 2006

3 min read

viewsIcon

31580

downloadIcon

268

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.