Stack

 Stack

In this article, we will learn about the stack algorithm in the programming language.you will also learn about the operations and application of the algorithm


Introduction of the stack:


This is the abstract data type , look like the piles of the plates. The first plate remains in the last position in the piles. The last plate in the first position of the piles of the plates.

If we want to take the first plate then we remove all the plates of the piles and find the first plate of the piles.

The first in last out rule is followed in the piles of the plate. Same as in the stack algorithm.


Operation of the stack:


 The following are the operations(interface)provided by the algorithm.

Push: 

By push function, we can add the element on the top.

Pop:

 By pop function, we can get and remove the element from the top.

IsEmpty:

By IsEmpty we can check the stack is empty or not.

IsFull:

By IsFull we can check full or not.

Implementation of the stack

stack-data-structure

Data Member:

                        The pointer name top is used to track the upper element and initialize it by -1 which shows it is empty.


Member functions:

Push function:

                        In this method, we increase the value of the top pointer from -1 and pointed toward the new element.first of all, check the full or not, then insert the new element into the structure. Following is the code of the push function in the CPP.

void push()

{

if(isfull()==0)

{

top=top+1;

stack[top]=newelement;

}

else

{
cout<<" not have space to insert ";
}
}

Pop function:

                    In this method, we return the element of the top , top pointer pointed to the next element, and reduce the value of top. Pop function is the reverse function of the push function.


In the pop, check structure empty or not then take an element from the structure, Following is the code of the pop function in the CPP.


int pop()

{

if(isEmpty()==0)

{

int data stack[top];

top=top-1;

return data;

}

else

{
cout<<" not have space to insert ";
}
}

IsFull function:

                           Check the stack is empty or not. In this function, the check top is equal to the maxvalue or not. If the value is equal to the max value, then return true otherwise return false.

if(top==maxvalue)

{

return true;

}

else

{

return false;

}

IsEmpty function:

                           Check the stack is full or not. In this function, only check the top is equal to zero or not. If the value is equal to the -1 then return true, otherwise, return false.

if(top==-1)
{
return true;
}
else
{
return false;
}

Application of stack:


We use stack algorithm where we need to store the things for a short time but the following are the famous applications:


Compiler the stack to evaluate the expression of the prefix or infix. Also, use converting  the prefix to infix or infix to prefix


During the browsing of the URLs on the browser. The browser use to store the URLs you visited previously and increase the top when the new page you visit. In this way, we access multiple URLs in very easy on the browser.


In the data structure, tree and graph algorithms use the stack in the traversing method.



Comments

Hire a programmer

Popular posts from this blog

CSS selector

C program to convert decimal to binary number

C Program to convert binary to decimal number