Вы находитесь на странице: 1из 9

#include #include #include #include

<stdlib.h> <string.h> <stdio.h> <conio.h>

#undef LEAK_DETECTION #ifdef LEAK_DETECTION #include "vld.h" #endif /*LEAK_DETECTION*/ #ifdef _MSC_VER #define CLEARSCREEN #define GETCH #define ITOA #else /*_MSC_VER*/ #define CLEARSCREEN #define GETCH #define ITOA #endif /*_MSC_VER*/ #define #define #define #define #define #define #define IN OUT MAX_STACK_SIZE (20) SUCCESS FAILURE TRUE FALSE (system("cls")) _getch _itoa_s getch itoa

(0) (-1) (1) (0)

typedef struct _Stack { int m_Array[MAX_STACK_SIZE]; int m_Stack_Size; int m_Top; }tStack; typedef int gError; /********************************************************** Name: gInitializeStack Return: SUCCESS: If Initialization is success FAILURE: If Initialization is failure Argument: aStack: Stack Handle **********************************************************/ gError gInitializeStack(IN OUT tStack **aStack, IN int aStackSize); /********************************************************** Name: gDeInitializeStack Return: SUCCESS: If De-Initialization is success FAILURE: If De-Initialization is failure Argument: aStack: Stack Handle **********************************************************/ gError gDeInitializeStack(IN OUT tStack **aStack); /********************************************************** Name: gDisplayStack

Return:

If Stack is displayed In error situation Argument: aStack: Stack Handle **********************************************************/ gError gDisplayStack(IN tStack *aStack); FAILURE: /********************************************************** Name: gInsertElementStack Return: SUCCESS: If element is inserted successfully FAILURE: In error situation Argument: aStack: Stack Handle aElement: Element to be inserted **********************************************************/ gError gInsertElementStack(IN tStack *aStack, IN int aElement); /********************************************************** Name: gDeleteElementStack Return: SUCCESS: If element is deleted successfully FAILURE: In error situation Argument: aStack: Stack Handle aElement: Element deleted in success, else zero **********************************************************/ gError gDeleteElementStack(IN tStack *aStack, OUT int *aElement); /********************************************************** Name: gIsStackFull Return: SUCCESS: If Success FAILURE: In error situation Argument: aStack: Stack Handle aResult: TRUE if Stack is Full FALSE otherwise **********************************************************/ gError gIsStackFull(IN tStack *aStack, OUT bool *aResult); /********************************************************** Name: gIsStackEmpty Return: SUCCESS: If Success FAILURE: In error situation Argument: aStack: Stack Handle aResult: TRUE if Stack is Empty FALSE otherwise **********************************************************/ gError gIsStackEmpty(IN tStack *aStack, OUT bool *aResult); int main(int argc, char* argv[]) { tStack *lStack = NULL; int lChoice = 0; int lElement = 0; int lReturn = SUCCESS; int lStackSize = 0;

SUCCESS:

printf("\nEnter Stack Size[ 0 < Size < %d]: ",MAX_STACK_SIZE); scanf("%d",&lStackSize); lReturn = gInitializeStack(&lStack,lStackSize); if(SUCCESS != lReturn) { printf("\ngInitializeStack failed"); printf("\nPress any key to exit..."); GETCH(); return 0; } do { lElement = 0; CLEARSCREEN; printf("\n------------------"); printf("\n----- STACK ------"); printf("\n1. Display Stack"); printf("\n2. Insert Element"); printf("\n3. Delete Element"); printf("\n0. Exit"); printf("\n------------------"); printf("\n\nCHOICE: "); scanf("%d",&lChoice); switch(lChoice) { case 0: { lReturn = gDeInitializeStack(&lStack); if(SUCCESS != lReturn) { printf("\ngDeInitializeStack failed"); } printf("\nPress any key to exit..."); GETCH(); } break; case 1: { lReturn = gDisplayStack(lStack); if(SUCCESS != lReturn) { printf("\ngDisplayStack failed"); } printf("\nPress any key to continue..."); GETCH(); } break; case 2: { bool lResult = FALSE; lReturn = gIsStackFull(lStack,&lResult); if(SUCCESS != lReturn) { printf("\ngIsStackFull failed"); } else {

if(TRUE == lResult) { printf("\nStack is FULL"); } else { printf("\nEnter Element to be en tered in Stack: "); scanf("%d",&lElement); lReturn = gInsertElementStack(lS tack,lElement); if(SUCCESS != lReturn) { printf("\ngInsertElement Stack failed"); } else { printf("\nElement added successfully"); } } } printf("\nPress any key to continue..."); GETCH(); } break; case 3: { bool lResult = FALSE; lReturn = gIsStackEmpty(lStack,&lResult); if(SUCCESS != lReturn) { printf("\ngIsStackEmpty failed"); } else { if(TRUE == lResult) { printf("\nStack is EMPTY"); } else { lReturn = gDeleteElementStack(lS tack,&lElement); if(SUCCESS != lReturn) { printf("\ngDeleteElement Stack failed"); } else { printf("\nElement Delete d: %d",lElement); } } } printf("\nPress any key to continue..."); GETCH(); }

break; default: { printf("\nInvalid Choice...\nPress any key to co ntinue..."); GETCH(); } break; } }while(lChoice); return 0; } /********************************************************** Name: gInitializeStack Return: SUCCESS: If Initialization is success FAILURE: If Initialization is failure Argument: aStack: Stack Handle **********************************************************/ gError gInitializeStack(IN OUT tStack **aStack, IN int aStackSize) { if(NULL == aStack) { printf("\n[gInitializeStack]Stack handle is NULL"); return FAILURE; } if(aStackSize > MAX_STACK_SIZE || aStackSize < 1) { printf("\n[gInitializeStack]Invalid Stack Size"); return FAILURE; } if(NULL != (*aStack)) { printf("\n[gInitializeStack]Stack Handle already created"); return SUCCESS; } else { tStack *lStack = NULL; lStack = (tStack *)malloc(sizeof(tStack)); if(NULL == lStack) { printf("\n[gInitializeStack]Memory Allocation failed"); return FAILURE; } else { memset(lStack,0,sizeof(tStack)); lStack->m_Stack_Size = aStackSize; lStack->m_Top = -1; *aStack = lStack; } } return SUCCESS; }

/********************************************************** Name: gDeInitializeStack Return: SUCCESS: If De-Initialization is success FAILURE: If De-Initialization is failure Argument: aStack: Stack Handle **********************************************************/ gError gDeInitializeStack(IN OUT tStack **aStack) { if(NULL == aStack || NULL == (*aStack)) { printf("\n[gDeInitializeStack]Invalid Stack Handle"); return FAILURE; } else { free((*aStack)); *aStack = NULL; } return SUCCESS; } /********************************************************** Name: gDisplayStack Return: SUCCESS: If Stack is displayed FAILURE: In error situation Argument: aStack: Stack Handle **********************************************************/ gError gDisplayStack(IN tStack *aStack) { if(NULL == aStack) { printf("\n[gDisplayStack]Invalid Stack Handle"); return FAILURE; } if(-1 == aStack->m_Top) { printf("\nStack is Empty"); return SUCCESS; } else { int lCount = 0; printf("\nTOP->"); for(lCount = aStack->m_Top; lCount >= 0; lCount--) { printf("%d->",aStack->m_Array[lCount]); } printf("BOTTOM"); } return SUCCESS; } /**********************************************************

Name: Return:

gInsertElementStack SUCCESS: If element is inserted successfully FAILURE: In error situation Argument: aStack: Stack Handle aElement: Element to be inserted **********************************************************/ gError gInsertElementStack(IN tStack *aStack, IN int aElement) { if(NULL == aStack) { printf("\n[gInsertElementStack]Invalid Stack Handle"); return FAILURE; } else { int lReturn = SUCCESS; bool lResult = FALSE; lReturn = gIsStackFull(aStack,&lResult); if(SUCCESS != lReturn) { printf("\n[gInsertElementStack]gIsStackFull failed"); return FAILURE; } else { if(TRUE == lResult) { printf("\n[gInsertElementStack]Stack Full, canno t insert any more elements"); return FAILURE; } else { aStack->m_Top++; aStack->m_Array[aStack->m_Top] = aElement; } } } return SUCCESS; } /********************************************************** Name: gDeleteElementStack Return: SUCCESS: If element is deleted successfully FAILURE: In error situation Argument: aStack: Stack Handle aElement: Element deleted in success, else zero **********************************************************/ gError gDeleteElementStack(IN tStack *aStack, OUT int *aElement) { if(NULL == aStack || NULL == aElement) { printf("\n[gDeleteElementStack]Invalid Argument"); return FAILURE; } else

{ int lReturn = SUCCESS; bool lResult = FALSE; lReturn = gIsStackEmpty(aStack,&lResult); if(SUCCESS != lReturn) { printf("\n[gDeleteElementStack]gIsStackEmpty failed"); return FAILURE; } else { if(TRUE == lResult) { printf("\nStack Empty"); return FAILURE; } else { *aElement = aStack->m_Array[aStack->m_Top]; aStack->m_Array[aStack->m_Top] = 0; aStack->m_Top--; } } } return SUCCESS; } /********************************************************** Name: gIsStackFull Return: SUCCESS: If Success FAILURE: In error situation Argument: aStack: Stack Handle aResult: TRUE if Stack is Full FALSE otherwise **********************************************************/ gError gIsStackFull(IN tStack *aStack, OUT bool *aResult) { if(NULL == aStack || NULL == aResult) { printf("\n[gIsStackFull]Invalid Arguments"); return FAILURE; } if(aStack->m_Top == (aStack->m_Stack_Size - 1)) { *aResult = TRUE; } else { *aResult = FALSE; } return SUCCESS; } /********************************************************** Name: gIsStackEmpty

Return:

If Success In error situation Argument: aStack: Stack Handle aResult: TRUE if Stack is Empty FALSE otherwise **********************************************************/ gError gIsStackEmpty(IN tStack *aStack, OUT bool *aResult) { if(NULL == aStack || NULL == aResult) { printf("\n[gIsStackEmpty]Invalid Arguments"); return FAILURE; } FAILURE: if(-1 == aStack->m_Top) { *aResult = TRUE; } else { *aResult = FALSE; } return SUCCESS; }

SUCCESS:

Вам также может понравиться