Question: c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so

c program

Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well.

#include

#include

typedef struct {

int *data; // stack data, we assume integer for simplicity

int top; // top of the stack

int maxSize; // max size of the stack

} Stack;

void StackInit(Stack* stack, int size) {

// this function initializes a stack for first use

printf("Initializing stack to hold %d integers... ",size);

stack->maxSize = size;

stack->top = -1;

stack->data = (int*) malloc(sizeof(int) * size);

}

void StackPush(Stack* stack, int data) {

if (stack->top >= stack->maxSize - 1) {

printf("Stack already full, returning ");

return;

}

printf("Pushing %d ",data);

stack->top++;

stack->data[stack->top] = data;

}

int StackPeek(Stack* stack) {

// return what's on the top without removing

// TODO: implement

}

int StackPop(Stack* stack) {

// return what's on the top and remove it

// TODO: implement

}

int StackDestroy(Stack* stack) {

// free up memory used up by data

// TODO: implement

}

int main()

{

Stack myStack;

StackInit(&myStack,8);

StackPush(&myStack,3);

StackPush(&myStack,4);

StackPush(&myStack,6);

StackPush(&myStack,1);

printf("Peek: %d ",StackPeek(&myStack));

StackPush(&myStack,2);

StackPush(&myStack,5);

StackPush(&myStack,8);

StackPush(&myStack,9);

StackPush(&myStack,7);

printf("Peek: %d ",StackPeek(&myStack));

printf("Pop: %d ",StackPop(&myStack));

printf("Pop: %d ",StackPop(&myStack));

printf("Pop: %d ",StackPop(&myStack));

printf("Pop: %d ",StackPop(&myStack));

StackPush(&myStack,11);

StackPush(&myStack,10);

printf("Peek: %d ",StackPop(&myStack));

StackDestroy(&myStack);

return 0;

}

Problem 1: [Finishing the Stack]

Please complete the above implementation so that the three functions, StackPeek, StackPop and StackDestory can work properly. The correct output, if you do not modify the main, should look like this:

Initializing stack to hold 8 integers... Pushing 3 Pushing 4 Pushing 6 Pushing 1 Peek: 1 Pushing 2 Pushing 5 Pushing 8 Pushing 9 Stack already full, returning Peek: 9 Pop: 9 Pop: 8 Pop: 5 Pop: 2 Pushing 11 Pushing 10 Peek: 10

Notice that:

  1. You may find the Stack session of the lecture notes on "Abstract Data Type, Stack/Queue" helpful.
  2. Do NOT modify the Stack struct; if you modify it, there will be NO MARKS for the exercises
  3. If a stack is empty, we can assume Peek and Pop to return -1;

Please paste your finished program here:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!