Wednesday, 25 July 2012

STACK IMPLEMENTED USING LINKED LIST


/*STACK IMPLEMENTATION USING LINKED LIST*/
/**header files for predefined function prototypes**/
#include<stdio.h>  /*FOR STANDARD INPUT OUTPUT i.e for printf,scanf*/
#include<stdlib.h>  /*FOR STANDARD LIBRARY FUNCTIONS I.E exit() */

/*macros for easy modification*/
#define MAX_STACK 10
#define INVALID -1

/*single element of stack represented by a NODE of list*/
typedef struct node
{
        int data;
        struct node *link;
}NODE;

/*function prototypes*/

void PUSH(NODE *,int);
int POP(NODE *);
int PICK(NODE);
void PRINT(NODE);
void INIT_STACK(NODE *);

/*main program for control the flow of execution*/
int main(int argc,char *argv[])
{
        int choice,n;
        NODE tos;          /*tos is the node which always point to
                             top of the stack*/
        INIT_STACK(&tos);
        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(&tos,n);
                               break;
                        case 2:n=POP(&tos);
                               if(n!=INVALID)
                                       printf("\n%d is popped from stack\n",n);
                               break;
                        case 3:n=PICK(tos);
                               if(n!=INVALID)
                                       printf("\n%d is present at the top of stack\n",n);
                               break;
                        case 4:PRINT(tos);break;
                        case 5:printf("\nTHANKYOU......\n");
                               exit(0);break;
                        default:printf("\nINVALID CHOICE.TRY AGAIN.....\n");break;
                }/*switch 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(NODE *ATOS);
 */
void INIT_STACK(NODE *atos)
{
        (atos)->data=0;
        (atos)->link=NULL;
}/* INIT() ends*/

/*
   This function perform PUSH operation on stack
   prototype
   void PUSH(NODE *ATOS,int ele);
   perform push of data ele on to the stack
 */

void PUSH(NODE *atos,int ele)
{
        NODE *new;
        if((atos)->data>=MAX_STACK)
        {
                printf("\nSTACK IS ALREADY FULL\n");
                return;
        }
        else
        {
                (atos)->data+=1;
                new=malloc(sizeof(NODE));
                new->data=ele;
                new->link=atos->link;
                atos->link=new;
        }
}/*PUSH() ends*/
/*
   This function perform POP operation on stack
   prototype
   int POP(NODE *ATOS);
   perform pop of data ele from the stack
   it return an integer from top of stack and remove it from stack
 */
int POP(NODE *atos)
{
        NODE *temp;
        int n;
        if((atos)->data==0)
        {
                printf("\nSTACK IS EMPTY\n");
                return INVALID;
        }
        else
        {
                temp=(atos)->link;
                (atos)->link=temp->link;
                (atos)->data-=1;
                n=temp->data;
                free(temp);
                return n;
        }

}/*POP() ends*/
/*
   This function perform PICK operation on stack
   prototype
   int PICK(NODE TOS);
   perform pick of data ele from the stack
   it return an integer from top of stack but not remove it from stack
 */
int PICK(NODE tos)
{
        if(tos.data==0)
        {
                printf("\nSTACK IS EMPTY\n");
                return INVALID;
        }
        else
        {
                return (tos.link->data);
        }
}/*PICK() ends*/
/* this function print the hole stack
   we use this for verification of stack operation
prototype:-
void PRINT(NODE TOS);
 */
void PRINT(NODE tos)
{
        NODE *temp;
        printf("\n::::::::::STACK:::::::::\n");
        if(tos.data==0)
        {
                printf("\nSTACK IS EMPTY\n");
                return;
        }
        else
        {
                temp=tos.link;
                while(temp)
                {
                        printf("%d\n",temp->data);
                        temp=temp->link;
                }
        }
}/*PRINT() ends*/

No comments:

Post a Comment