Question

c program Here we see a Stack ADT implemented using array. We would like the stack...

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:

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#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...
", 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 stack->data[stack->top];
}

int StackPop(Stack *stack) {
    int data = StackPeek(stack);
    stack->top--;
    return data;
}

int StackDestroy(Stack *stack) {
    free(stack->data);
    return 0;
}

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;
}

Add a comment
Know the answer?
Add Answer to:
c program Here we see a Stack ADT implemented using array. We would like the stack...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns...

    Suppose we decide to add a new operation to our Stack ADT called sizeIs, which returns a value of primitive type int equal to the number of items on the stack. The method signature for sizeIS is public int sizeIs() a.) Write the code for sizeIs for the ArrayStack class b.) Write the code for sizeIs for the LinkedStack class (do not add any instance variables to the class; each time sizeIs is called you must "walk" through the stack...

  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

  • I have added a little Code but I need help with the rest. /** A class...

    I have added a little Code but I need help with the rest. /** A class of stacks whose entries are stored in a chain of nodes. Implement all methods in MyStack class Main Reference : text book or class notes Do not change or add data fields */ package PJ2; public class MyStack<T> implements StackInterface<T> {    // Data fields    private Node<T> topNode; // references the first node in the chain    private int numberOfEntries;       public...

  • Need help. write a C program stack-ptr.c that implements a stack using a link list. Below...

    Need help. write a C program stack-ptr.c that implements a stack using a link list. Below is a skeleton code to start with.Jjust edit to make thread friendly. examplpe: push(5, &top); push(10, &top); push(15, &top); int value = pop(&top); value = pop(&top); value = pop(&top); this program currently has a race condition. use Pthread mutex locks to fix the race conditions. test you now thread safe stack by creating 200 concurrent threads in main() that push and pop values. -use...

  • I was told I need three seperate files for these classes is there anyway to tie...

    I was told I need three seperate files for these classes is there anyway to tie all these programs together into one program after doing that. I'm using netbeans btw. import java.util.ArrayList; import java.util.Scanner; /** * * */ public class MySorts {       public static void main(String[] args) {             Scanner input = new Scanner(System.in);             String sentence;             String again;             do {                   System.out                               .println("Enter a sentence, I will tell you if it is a palindrome: ");...

  • In C, Implement each of the functions to create a working stack 6 II-Implement each of...

    In C, Implement each of the functions to create a working stack 6 II-Implement each of the functions to create a working stack. 7 II -Do not change any of the function declarations I-(i.e. stack t* create stack) should not have additional arguments) II-You should not have any 'printf' statements in your stack functions 10 II(You may consider using these printf statements to debug, but they should be removed from your final version) 12 #ifndefMYSTACK_A 13 #define MYSTACKH 14 15...

  • There is a data structure called a drop-out stack that behaves like a stack in every...

    There is a data structure called a drop-out stack that behaves like a stack in every respect except that if the stack size is n, then when the n+1element is pushed, the bottom element is lost. Implement a drop-out stack using links, by modifying the LinkedStack code. (size, n, is provided by the constructor. Request: Please create a separate driver class, in a different file, that tests on different types of entries and show result of the tests done on...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT