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.
----------------------------------------------------------------------------------------------
A C PROGRAM TO IMPLEMENT OPERATIONS IN STACK -LINKED LIST IMPLEMENTATION
COMPILER EMPLOYED: DEV C++ COMPILER-4.9.9.2
SOURCE FILE SIZE :3 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
int DATA;
struct NODE
{
int DATA;
struct NODE *LINK;
};
struct NODE *TOP=NULL,*TEMP;
int main()
{
int CH,OPT;
do//infinite loop is used to insert/delete infinite number of nodes
{
printf("\n STACK OPERATIONS BY DEPLOYMENT OF LINKED LIST...");
printf("\n\n MENU:");
printf("\n[1].PUSH ELEMENT");
printf("\n[2].POP ELEMENT");
printf("\n[3].DISPLAY");
printf("\n[4].EXIT");
printf("\n\n OPTION:");
scanf("%d",&CH);
switch(CH)
{
case 1:
printf("\n\n PUSH OPERATION INVOKED...");
PUSH();
break;
case 2:
printf("\n\n POP OPERATION INVOKED...");
POP();
break;
case 3:
printf("\n\n TRAVERSAL OPERATION INVOKED...");
DISPLAY();
break;
case 4:
exit(0);
}
printf("\n\n DO YOU WISH TO CONTINUE:1~0:");
scanf("%d" ,&OPT);
}while(OPT==1);
printf("\n TERMINATING OPERATION...");
system("pause");
return 0;
}
int PUSH()
{
TEMP=(struct NODE *)malloc(sizeof(struct NODE));
printf("\n ENTER DATA:");
scanf("%d",&DATA);
TEMP->DATA=DATA;
TEMP->LINK=TOP;
TOP=TEMP;
printf("\nPUSHING DATA INTO STACK...");
printf("\n\nPUSHING OPERATION COMPLETED SUCCESSFULLY...");
}
int POP()
{
if(TOP!=NULL)
{
printf("\n THE DATA POPPED FROM STACK: %d",TOP->DATA);
TOP=TOP->LINK;
}
else
{
printf("\n ERROR!!! STACK UNDERFLOW...");
}
printf("\nPOPPING DATA INTO STACK...");
printf("\n\nPOPPING OPERATION COMPLETED SUCCESSFULLY...");
}
int DISPLAY()
{
int S=1;
TEMP=TOP;
if(TEMP==NULL)
{
printf("\nERROR!!! STACK IS EMPTY...");
}
while(TEMP!=NULL)
{
printf("\n|ELEMENT %d:%d|",S,TEMP->DATA);
printf("\n^^^^^^^^^^^^^^^^");
TEMP=TEMP->LINK;
S++;
}
}
----------------------------------------------------------------------------------------------
Your's friendly,
[MOHANRAM.G],
ADMIN...
0 comments:
Post a Comment