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

Program to find nth Prime number in C

Objective: To print any Prime number in C.  This task may sound easy but is a very good exercise to help optimize CPU intensive programs. Finding Prime number is a very CPU hogging task and it becomes even more dreaded as the number starts to grow. Lets say we want to find 30001th prime. how will we find it?? Here is the program. Programming logic : The easiest method is to check if the given number N is divisible by any number from 2 to N-1. But it may be noted that we don't need to go beyond the number that is square root of N.  why?? lets have a look. Say we are testing number 49 for prime number. Our objective is to find any number that may divide it. so we start from 2,3,4 etc. As soon as we cross 7 there will be no number that will divide 49, if there was we have got it earlier since it will be smaller then 7.  Get it? another example 64 : 2x32 4x16 8x8 16x4 32x2 as you can see when we pass number 8 we don't need to test as the number that will come ,...