Tuesday, 24 July 2012

STACK IMPLEMENTED USING ARRAY IN C


/*This is a menu driven program which use an array of integer as a STACK*/

/*Header files for prototype of standard library function*/
#include<stdio.h>/*for standarad input and out put opereation*/
#include<stdlib.h>/*for standard C library support exit function */

/*Macros for Easy Modification*/

#define MAX_STACK 10
#define INVALID -1
/*global variables for stack*/

int tos;/*top of the stack*/
int bos;/*bottom of the stack*/

/*proto type for designed functions*/
void PUSH(int *,int);
int POP(int *);
int PICK(int *);
void PRINT(int *);
void INIT_STACK(void);

/*main program which control all the operation*/
int main(int argc,char *argv[])
{/*main starts*/
        int STACK[MAX_STACK];
        int choice,n;
        INIT_STACK();
        while(1) /*infanite loop for menu driven program*/
        {
                choice=menu();
                switch(choice)
                {
                        case 1:printf("\nEnter an integer to push in to stack::");
                               scanf("%d",&n);
                               PUSH(STACK,n);
                               break;
                        case 2:n=POP(STACK);
                               if(n!=INVALID)
                                       printf("\n%d is popped from stack\n",n);
                               break;
                        case 3:n=PICK(STACK);
                               if(n!=INVALID)
                                       printf("\n%d is present at the top of stack\n",n);
                               break;
                        case 4:PRINT(STACK);break;
                        case 5:printf("\nTHANKYOU......\n");
                               exit(0);break;
                        default:printf("\nINVALID CHOICE.TRY AGAIN.....\n");break;
                }/*switch case ends*/
        }/*while loop ends*/
}/*main ends*/
/*menu function for displaying menu   **
 ** prototype:-                        **
 ** int menu(void);                    **
 ** return an integer according to user**
 ** choice.                            */
int menu()
{
        int n;
        printf("\n::::::::::::MENU:::::::::::::\n");
        printf("1.PUSH\n2.POP\n3.PICK\n4.PRINT\n5.EXIT");
        printf("\nEnter your choice: ");
        scanf("%d",&n);
        return n;
}/*menu() ends*/

/* this function initialise the stack for further operation on it
   proto type
   void INIT(void);
 */

void INIT_STACK(void)
{
        tos=-1;
        bos=-1;
}/*INIT() ends*/

/*
   This function perform PUSH operation on stack
   prototype
   void PUSH(int *stack,int ele);
   perform push of data ele on to the stack
 */
void PUSH(int STACK[],int ele)
{
        if(tos>=MAX_STACK-1)
        {
                printf("\nSTACK IS ALREADY FULL\n");
                return;
        }
        else
        {
                ++tos;
                STACK[tos]=ele;
        }
}/* PUSH() ends*/
/*
   This function perform POP operation on stack
   prototype
   int POP(int *stack);
   perform pop of data ele from the stack
   it return an integer from top of stack and remove it from stack
 */
int POP(int STACK[])
{
        int n=INVALID;
        if(tos<0)
        {
                printf("\nSTACK ALREADY EMPTY\n");
                return n;
        }
        n=STACK[tos];
        STACK[tos]=INVALID;
        --tos;
        return n;
}/*POP() ends*/
/*
   This function perform PICK operation on stack
   prototype
   int PICK(int *stack);
   perform pick of data ele from the stack
   it return an integer from top of stack but not remove it from stack
 */
int PICK(int STACK[])
{
        int n=INVALID;
        if(tos<0)
        {
                printf("\nSTACK ALREADY EMPTY\n");
                return n;
        }
        n=STACK[tos];

        return n;
}/*PICK() ends*/
/* this function print the hole stack
   we use this for verification of stack operation
prototype:-
void PRINT(int *stack);
 */
void PRINT(int STACK[])
{
        int i;
        printf("\n::::::::STACK::::::::\n");
        if(tos<0)
        {
                printf("STACK IS EMPTY\n");
                return;
        }
        for(i=tos;i>=0;i--)
        {
                printf("%d\n",STACK[i]);
        }
}/*PRINT() ends*/

No comments:

Post a Comment