Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a C file called push.c, how would I Implement the push function for the g

ID: 3546566 • Letter: I

Question

I have a C file called push.c, how would I Implement the push function for the given Stack data type.


#include <stdlib.h>
#include
"Push.h"


Stack *createStack(int iSize)
{
Stack *pStack = NULL;

if (iSize > 0)
{
pStack = (Stack *) malloc(
sizeof(Stack));
if (pStack != NULL)
{
pStack->pArray = (
void **) malloc(sizeof(void *) * iSize);
if (pStack->pArray == NULL)
{
free(pStack);
pStack = NULL;
}
else
{
pStack->iSize = iSize;
pStack->iTopIndex = -1;
}
}
}
return pStack;
}


int destroyStack(Stack *pStack)
{
int iReturnCode = 0;
if (pStack == NULL)
{
iReturnCode = 1;
}
else
{
if (pStack->pArray == NULL)
{
iReturnCode = 2;
}
else
{
free(pStack->pArray);
}
free(pStack);
}
return iReturnCode;
}


int push(Stack *pStack, void *pValue)
{
return 0;
}


void *pop(Stack *pStack)
{
void *pTop;
if (pStack == NULL || pStack->iTopIndex == -1)
{
pTop = NULL;
}
else
{
pTop = pStack->pArray[pStack->iTopIndex];
pStack->iTopIndex--;
}
return pTop;
}


Explanation / Answer

int push(Stack *pStack, void *pValue)

{

if(pStack->iTop < pStack->iSize-1){

pStack->pArray[++pStack->iSize] = *pValue;

}

else{

return(0); //Stack Overflow, could not push an item.

}

return(1); //Succesfully pushed an item.

}