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

Find nth Prime Number in C++

c++ program to find prime numbers: The problem of finding prime number can be solved by checking all numbers, testing them for prime and then moving ahead. If you want to calculate nth prime. Then this can be done in a brutal way by checking the number one by one. This may sound odd, by there is no easy way then this  for prime numbers (Well Actually there are like Pollard's Rho Algorithm, Number Sieves or Shor's Quantum Algorithm, but we are talking about the one that most people may understand easily). There may be way to pre-calculate the prime numbers but that again is not sufficient. So how can we use c++ to create a program to find prime numbers.

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 ; ...