1. Engineering
  2. Computer Science
  3. c program here we see a stack adt implemented using...

Question: c program here we see a stack adt implemented using...

Question details

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 <stdio.h>

#include <stdlib.h>

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...\n",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\n");   

        return;

    }

    printf("Pushing %d\n",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\n",StackPeek(&myStack));

    StackPush(&myStack,2);

    StackPush(&myStack,5);

    StackPush(&myStack,8);

    StackPush(&myStack,9);

    StackPush(&myStack,7);

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

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

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

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

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

    StackPush(&myStack,11);

    StackPush(&myStack,10);

    printf("Peek: %d\n",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:

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution