Author : Soumen Mazumder
Page : 1
The Stack
A stack is a pile of item kept one over another,i.e. A stack only have a head or top no bottom. Any item added becames the new op of the stack. And any item deleted or takenout from the stack is also taken out from the top & the top of Stack get reduced by one. That is the first item which goes in to the stack will come out of the stack the last as the last item. From this we conclude that a stack is a LIFO (last in first out) Stack is not the same as array. From the definition we found that stack provide provisions for adding of items and deleting of item from it, But it can be done from only with on end which is the top of the stack. A stack doesn’t have tail or the end point. When we add any item to stack the head of stack goes up. And when we delete any items from a stack the head(top) of the stack goes down.
Operation of Stack
From the above explanation we can fairly understand that stack can do only two work i.e. adding of item and deleting of item from it. We now points to the operation of stacks
1) Push : It is the operation by which we can add any item to a stack. This operation required the item to be added and a stack in which the item will be added.
2) Pop : It is the operation by which We can delete the upper item from the stack and this operation gives us the item that it is deleted from the stack. In other words this operation is used to take out the upper item from any stack.
3) IsEmpty : This operation is used to check whether the stack we are using is empty or filled by any item. If the stack is empty this operation gives us as appropriate answer.
<u>Code of Stack
Code of Stack in C.
Here we use a Structure to hold stack. The structure members are an integer called top to hold the stack top position and an integer array to hold the stack data. We only check the top variable if it is equal to zero than your stack is empty. If it is equal to the maximum value of stack member than the stack is full. Otherwise the top shows the current position or index of the top value in the stack array member. If we want to add any item to stack we increase the value of top by one and add any item to the stack array with the increased top as the index of the array. If we want to delete or want to Pop any item from the stack we decrease the top by one and return the current or top item of the stack array.
//codeing of Stack in C.
#include<stdio.h>
#include<conio.h>
#define max 5
/*Here we declare a Stack with the help of Structure where we
used an Array
to store the data in the array. The Top or Head of the stack is
denoted by
a integer data called top.
*/
struct stack
{
int top; //Data to hold top of stack
stackarray[max]; //Data to hold stack item
};
//This function is used to check wheter the stack is empty or not
int isempty(tmp)
struct stack tmp;
{
printf("%d",tmp.top);
if (tmp.top==0)
return 0;
else
return 1;
}
//This function is used to put an item in the stack.
void push(tmp,item)
struct stack *tmp; /*We have refrence the stack by pointer
the value of stack is chaced by the func*/
int item;
{
if(tmp->top==max)
printf("Data overflow\n");
else
{
tmp->top++;
tmp->stackarray[tmp->top]=item;
printf("The item is Push %d in %d\n",item,tmp->top);
}
}
//This is used to delete or take out an item from the stack.
void pop(tmp)
struct stack *tmp;
{
if(tmp->top==0)
printf("Data underflow\n");
else
{
printf("The Data Poped is
%d\n",tmp->stackarray[tmp->top--]);
}
}
void main()
{
int l;
struct stack stk={0};
if(isempty(stk))
printf("Stack is Empty\n");
else
printf("Stack is not empty\n");
for(l=1; l<=10;l++)
push(&stk,l);
for(l=1;l<=10;l++)
pop(&stk);
}
Code in C++
In C++ we used a class to denote a stack Data. We use a private member data as top (integer) and stackarray(as integer array). Here also the work of both the variables are same as that of in C Expained earlier. We also here defined memeber functions as Public for Push, Pop, IsEmpty. These member function are declared as Public so as to call them from outside the class. As we declared the member variables as Private so as to avoid the misused of data variables from outside the class. Only the member
functions can used the private data.
//Code in C++
#include<iostream.h>
#include<conio.h>
#define max 5
class stack
{
int top;
int stackarray[max];
public :
//Constructor
stack()
{
top=0;
}
//Push function used to put item in the stack.
void push(int item)
{
if(top==max)
cout<<"Data over flow\n";
else
{
stackarray[++top]=item;
cout<<"The item "<<item<<" is
pushed in "<<top<<"\n";
}
}
//pop function is used to delete or take out item from the stack.
void pop()
{
if(top==0)
cout<<"Data under flow\n";
else
{
cout<<"The item
"<<stackarray[top]<<" is poped from
"<<top--<<"\n";
}
}
//This function is used to check weather the stack is empty or
not.
int isempty()
{
if (top==0)
return 1;
else
return 0;
}
};
void main()
{
stack stk;
if(stk.isempty())
cout<<"The stack is empty\n";
for(int l=1;l<=10;l++)
stk.push(l);
for(l=1;l<=10;l++)
stk.pop();
}
Importance & Use of Stack in Programming
COMMING SOON NOT UPLOADED.
Page : 1