STACK IMPLEMENTATION USING ADT STACK
/**
*THIS PROGRAM IS A REPRESENTATION OF STACK IMPLEMENTATION.
*HERE A USER DEFINED STRUCTURE IS USED TO RE-PRESENT A STACK
*AND BASIC STACK OPERATION ARE DESCRIBED USING SOME BASIC FUNCTION
*MENU DRIVEN FOR BETTED UNDERSTANDING
*
*
*FILE NAME :stack_using_adt.c
*DATE :2/08/2012
*PROGMMER :RAJESH KUMAR SAHOO.
*BOLOG :http://rajesh6115.blogspot.in/
*/
/*
header files for function prototypes of inbuild library functions
*/
#include <stdio.h>
#include <stdlib.h>
/*
macros for easy modification
*/
#define MAX_STACK 10
#define INVALID -1
/**
* A user defined data type for stack representation.
* data member ele is pointing to the stack which is a dynamic array of integers.
* data member size represents the size of stack that is it can contain maximum size no of element.
* data member tos represents top of stack(index of dynamic array) where data insertion
and data deletion going to happen
*/
typedef struct stack
{
int *ele;
int size;
int tos;
}STACK;
/**proto types of basic operation in a stack data structure
*/
void STACK_ADT_PUSH(STACK *,int);
int STACK_ADT_POP(STACK *);
int STACK_ADT_PICK(STACK);
void STACK_ADT_PRINT(STACK);
void STACK_ADT_INIT_STACK(STACK *,int);
int STACK_MENU(void);
/*
This function perform PUSH operation on stack
prototype
void STACK_ADT_PUSH(STACK *SP,int n);
perform push of data n on to the stack ADT
NOTE:-
modification in stack required so call by reference
*/
void STACK_ADT_PUSH(STACK *SP,int n)
{
if((SP->tos)>=(SP->size)-1)
{
printf("\nSTACK IS ALREADY FULL\n");
return;
}
else
{
++SP->tos;
SP->ele[SP->tos]=n;
}
}
/*
This function perform POP operation on stack
prototype :
int STACK_ADT_POP(STACK *SP);
perform pop of data element from the top of stack
it return an integer from top of stack and remove it from stack
NOTE:-
modification in stack required so call by reference
*/
int STACK_ADT_POP(STACK *SP)
{
int n=INVALID;
if((SP->tos)<0 br=""> {
printf("\nSTACK ALREADY EMPTY\n");
return n;
}
n=SP->ele[SP->tos];
SP->ele[SP->tos]=INVALID;
--SP->tos;
return n;
}
/*
This function perform PICK operation on stack
prototype :
int STACK_ADT_PICK(STACK S);
perform pick of data element from the top of stack
it return an integer from top of stack without remove it from stack
NOTE:-
no modification in stack so simply call by value
*/
int STACK_ADT_PICK(STACK S)
{
int n=INVALID;
if(S.tos<0)
{
printf("\nSTACK ALREADY EMPTY\n");
return n;
}
n=S.ele[S.tos];
return n;
}
/* this function print the hole stack
we use this for verification of stack operation
prototype:-
void STACK_ADT_PRINT(STACK S);
NOTE:-
no modification in stack so simply call by value
*/
void STACK_ADT_PRINT(STACK S)
{
int i;
printf("\n::::::::STACK::::::::\n");
if(S.tos<0)
{
printf("STACK IS EMPTY\n");
return;
}
for(i=S.tos;i>=0;i--)
{
printf("%d\n",S.ele[i]);
}
}
/* this function initialise the stack for further operation on it
proto type
void STACK_ADT_INIT(STACK *SP,int n);
NOTE:-
modification in stack required so call by reference
*/
void STACK_ADT_INIT(STACK *SP,int n)
{
int i;
SP->size=n;
SP->ele=(int *)malloc(sizeof(int)*SP->size);
SP->tos=-1;
for(i=0;i<(SP->size);i++)
{
SP->ele[i]=INVALID;
}
}
/* this function delete the stack for free the dynamically allocated space for operation
proto type :
void STACK_DEL(STACK *SP);
NOTE:-
modification in stack required so call by reference
*/
void STACK_DEL(STACK *SP)
{
free(SP->ele);
}
/*main program for control the flow of execution*/
int main()
{
int choice,n;
STACK s1;
STACK_ADT_INIT(&s1,MAX_STACK);
while(1)
{
choice=STACK_MENU();
switch(choice)
{
case 1:printf("\nEnter an integer to push in to stack::");
scanf("%d",&n);
STACK_ADT_PUSH(&s1,n);
break;
case 2:n=STACK_ADT_POP(&s1);
if(n!=INVALID)
printf("\n%d is popped from stack\n",n);
break;
case 3:n=STACK_ADT_PICK(s1);
if(n!=INVALID)
printf("\n%d is present at the top of stack\n",n);
break;
case 4:STACK_ADT_PRINT(s1);break;
case 5:STACK_DEL(&s1);
printf("\nTHANKYOU......\n");
exit(0);break;
default:printf("\nINVALID CHOICE.TRY AGAIN.....\n");break;
}/*switch end*/
}/*while loop end*/
return 0;
}/*main end*/
/*STACK_MENU function for displaying menu **
** prototype:- **
** int STACK_MENU(void); **
** return an integer according to user **
** choice. **
*/
int STACK_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;
}
No comments:
Post a Comment