Question

In: Computer Science

Complete the following functions in the Stack.java/Stack.h files (ATTACHED BELOW): a. void push(int val) b. int...

Complete the following functions in the Stack.java/Stack.h files (ATTACHED BELOW):
a. void push(int val)
b. int pop()
c. int getSize()

public class Stack {
  
private int maxStackSize, topOfStack;
private int[] stack;
  
public Stack(int maxStackSize) {
if (maxStackSize <= 0)
System.out.println("Stack size should be a positive integer.");
else {
this.maxStackSize = maxStackSize;
topOfStack = -1;
stack = new int[maxStackSize];
}
}
  
public void push(int val) { // complete this function
}
  
public int pop() { // complete this function
}
  
public int getSize() { // complete this function
}
}


Solutions

Expert Solution

public class Stack {

    private int maxStackSize, topOfStack;
    private int[] stack;

    public Stack(int maxStackSize) {
        if (maxStackSize <= 0)
            System.out.println("Stack size should be a positive integer.");
        else {
            this.maxStackSize = maxStackSize;
            topOfStack = -1;
            stack = new int[maxStackSize];
        }
    }

    public void push(int val) { // complete this function
        if (topOfStack >= maxStackSize - 1)
            System.out.println("Stack is already full.");
        else
            stack[++topOfStack] = val;
    }

    public int pop() { // complete this function
        if (topOfStack == -1) {
            System.out.println("Stack is already empty");
            return -1;
        }
        else 
            return stack[topOfStack--];
    }

    public int getSize() { // complete this function
        return topOfStack+1;
    }
}

Related Solutions

Implement the \void enqueue(int val)" function in the QueueUsingStack.java/QueueUsingStack.h file (ATTACHED BELOW). Here are some hints...
Implement the \void enqueue(int val)" function in the QueueUsingStack.java/QueueUsingStack.h file (ATTACHED BELOW). Here are some hints on how to implement this using two stacks: - Create a stack \tempStack", which has the same maximum capacity as the mainStack. - Pop all numbers from mainStack and push them onto tempStack. - Push the new number onto mainStack. - Pop all numbers from tempStack and push them onto mainStack. Implement the \int dequeue()" function in the QueueUsingStack.java/QueueUsingStack.h le. Recall that this is...
Consider the following program written in C syntax: void swap(int a, int b) { int temp;...
Consider the following program written in C syntax: void swap(int a, int b) { int temp; temp = a; a = b; b = temp;} void main() { int value = 2, list[5] = {1, 3, 5, 7, 9}; swap(value, list[0]); swap(list[0], list[1]); swap(value, list[value]); } For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap? Passed by value Passed by reference Passed...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
rite a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c,...
Write a method with the following header: public static void showGradeDistribution(int a, int b, int c, int d, int f) It should print a graph (using asterisks) for each of the letters entered in the reverse order of the parameter list and with a label. In addition, if A and B grades sum is equal or exceeds that of grades C and D and F, the message “Strong class!” should be displayed. For example a method call of: showGradeDistribution(5,7,4,4,3); Would...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void...
Given a program as shown below: #include <stdio.h> void function1(void); void function2 (int, double x); void main (void) { int m; double y; m=15; y=308.24; printf ("The value of m in main is m=%d\n\n",m); function1(); function2(m,y); printf ("The value of m is main still m = %d\n",m); } void function1(void) { printf("function1 is a void function that does not receive\n\\r values from main.\n\n"); } void function2(int n, double x) { int k,m; double z; k=2*n+2; m=5*n+37; z=4.0*x-58.4; printf ("function2 is...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[])...
#include <stdio.h> #define MAX 8 //Byte = 8 bits void func_and(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] & b[i]; } } void func_or(int a[], int b[], int result[]) { for(int i=0; i < MAX; i = i + 1){ result[i] = a[i] || b[i]; } } void func_not(int a[], int result[]) { for (int i = 0; i < MAX; i = i + 1) { result[i]...
R5.18The following function swaps two integers, without requiring a temporary variable(C++): void tricky_swap(int& a, int& b)...
R5.18The following function swaps two integers, without requiring a temporary variable(C++): void tricky_swap(int& a, int& b) { a = a - b; b = a + b; a = b - a; } However, it fails in one important case, namely when calling tricky_swap(x, x). Explain what should happen and what actually happens.
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT