Friday, October 29, 2010

STACK -ARRAY IMPLEMENTATION



In computer science, a stack is a last in, first out (LIFO) abstract data type and data structure. A stack can have any abstract data type as an element, but is characterized by only two fundamental operations: push and pop. The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the list, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty list.

A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition: therefore, the lower elements are typically those that have been in the list the longest.

The array implementation aims to create an array where the first element (usually at the zero-offset) is the bottom. That is, array[0] is the first element pushed onto the stack and the last element popped off. The program must keep track of the size, or the length of the stack. The stack itself can therefore be effectively implemented as a two-element structure in C.

The push() operation is used both to initialize the stack, and to store values to it. It is responsible for inserting (copying) the value into the ps->items[] array and for incrementing the element counter (ps->size). In a responsible C implementation, it is also necessary to check whether the array is already full to prevent an overrun.

The pop() operation is responsible for removing a value from the stack, and decrementing the value of ps->size. A responsible C implementation will also need to check that the array is not already empty.

----------------------------------------------------------------------------------------------

A C PROGRAM TO IMPLEMENT OPERATIONS IN STACK -ARRAY IMPLEMENTATION

COMPILER EMPLOYED: DEV C++ COMPILER-4.9.9.2

SOURCE FILE SIZE :2 kb

EXE FILE SIZE :22 kb

NOTE: PLEASE INCLUDE THE DESIRED HEADER FILE

---------------------------------------------------------------------------------------------

C PROGRAM SOURCE & EXE DOWNLOAD:

Click download button to download

DISCLAIMER: The following program cannot be ensured of perfection.so any flaws in the program can be notified in the comments section.

----------------------------------------------------------------------------------------------

CODE:



#include

# define MAXSIZE 200
void display(void);

int stack[MAXSIZE];
int TOP; //index pointing to the TOP of stack
int main()
{
void PUSH(int);
int POP();
int OPT,CH,i,num;
do

{
printf("\n ARRAY IMPLEMENTATION OF STACK");
printf("\n\n MAIN MENU: ");
printf("\n\n[1].PUSH ELEMENT");
printf("\n[2].POP ELEMENT");
printf("\n[3].TRAVERSAL");
printf("\n\nOPTION:");
scanf("%d",&OPT);

switch(OPT)
{
case 1:
printf("\n\n PUSH OPERATION INVOKED...");
printf("\n ENTER ELEMENT: ");
scanf("%d",&num);
printf("\n\n PUSHING ELEMENT INTO STACK...");
PUSH(num);
break;
case 2:
i=POP();
printf("\n\n POP OPERATION INVOKED...");
printf("\n\n POPPING ELEMENT FROM STACK...");
printf("\n\n ELEMENT POPPED: %d ",i);
break;
case 3:
printf("\n\n TRAVERSAL OPERATION INVOKED...");
DISPLAY();
break;

default:
printf("\n\n INVALID CHOICE... ");
break;
}

printf("\n\n DO YOU WISH TO CONTINUE:1~0:");
scanf("%d" ,&CH);
}while(CH==1); //END OF DO WHILE
system("pause");
return 0;
} //END OF MAIN


void PUSH(int y)
{

if(TOP>MAXSIZE)
{
printf("\nSTACK FULL");
return;
}
else
{
TOP++;
stack[TOP]=y;
}
}

int POP()
{
int a;
if(TOP<=0)
{
printf("\n STACK EMPTY");
return 0;
}
else
{
a=stack[TOP];
TOP--;
}
return(a);

}

int DISPLAY()
{

int i=1,j=1;
if(TOP>0)
{

printf("\n\n TRAVERSING STACK...");
while(i<=TOP)
{
printf("\n ELEMENT %d:%d",j,stack[i++]);
j++;
}
printf("\n");

}

else


printf("\n\nERROR!!! STACK IS EMPTY...");

}


----------------------------------------------------------------------------------------------
Your's friendly,

[MOHANRAM.G],
ADMIN...

0 comments:

Post a Comment