Skip to main content

Stack in C using functions

Stack in C: Stack is a very useful data structure in C, It is used in many situations. Implementing stack can be done using a global array and a global pointer of the stack. although this implementation reveals how good OOP is for such situations. There will be three basic functions of the stack. One will be push() operation, which will push the given element to the stack. Pop() function  will pop out the element from the top. A show function to show the content of the stack.

The size of the stack will be controlled by a preprocessor called SIZE. we will always check for boundaries of the stack. Modular approach for such situation is not safe. A better OO way is there, that I will discuss later.
The code to implement Stack in C using functions is as follows :-

Code :
#include<stdio.h>
#include<dos.h>

#define SIZE 5
int cursor=0;//global
int stack[SIZE];
void initstack()
{
        int i=0;
        for(i=0;i<SIZE;i++)
       {
            stack[i]=0;//set all element of the stack to 0
         }
}
void show()
{
       int i=0;
       for(i=0;i<SIZE;i++)
       {
           printf("\n %d",stack[i]);
        }
}
void push(int v)
{
       if(cursor==SIZE)
        {
             printf("\n -----Stack is full------");
         }
         else
         {

           stack[cursor]=v;
           cursor++;
          }

}
int pop()
{
        int t;
         if(cursor==0)
         {
            printf("\n Stack is empty");
         }
        else
        {
               cursor--;
                t=stack[cursor];
              stack[cursor]=0;
          return(t);
        }
}


int main()
{
           char c='a';
           int v,k;
             k=1;

while(k)
{
               printf("\n Enter your choice: 1:Push\n 2:Pop \n3:Show \n4:Exit\nyour choice::");
               scanf("%c",&c);
               switch(c)
               {
                   case '1':
                   printf("\n enter a value:");
                  scanf("%d",&v);
                      push(v);
                      break;
                  case '2':
                  pop();
                  break;
                  case '3':
                  show();
                  break;
                  case '4':
                     k=0;//will break the while loop
                     break;
                 }



   }
}

Popular posts from this blog

Overloading Unary Operator in C++

In this Example I will demonstrate overloading a unary operator. The overloading is done in similar way as we did for binary operator. In this example I will be using a Vector Class. The operator we are going to over load is the unary "~" (tilde) operator. although you can use any unary overloadable operator. Method: In this example the overloaded operator will be the part of the class i.e. the over loaded definition will be a member function. (As we know overloading can be done using non member functions too). So the unary operator will not have any arguments in it. We will overload ~ operator so that it will reflect the vector so that its z component becomes x component and vice versa. So input vector will be 2i+3j+5k and its output vector will be 5i+3j+2k. although you can do anything you like. Code: # include < iostream > using namespace std ; class vector { int x , y , z ;      public : vector ( ) { x = y = z = 0 ; ...

constructor overloading in c++

Constructor play an important part in c++. They can initialize the values of a given object to some known values. This is important as constructor will be able to set some values in objects which otherwise may have some garbage values. Most uninitialized value in objects are main cause of the bugs. But the concept of constructor can allow us to do just anything beside initializing values. Constructor are always called when an object is created. This property of constructor is very useful in situation when you need to perform some task as soon as an object is created. So constructor is a special function that gets called automatically when an object is created. This function can like any other function do anything (like initializing values). Overloading Constructor : This process is very similar to the overloading of simple functions. Since constructor are also function , they can be overloaded like any other functions. This allows user to create many constructor, which can help u...