Question: Need some help with java code. #3 CODE Array stack. ublic class ArrayStack { int[] stack; // Array containing the data in the stack int

Need some help with java code.

Need some help with java code. #3 CODE Array stack. ublic class

#3 CODE Array stack.

ublic class ArrayStack {

int[] stack; // Array containing the data in the stack

int top; // Index representing the top of the stack

// Constructor

// Preallocates an array of size 10 to be used as a stack

// Initializes the top of the stack

ArrayStack() {

stack = new int[10];

top = 0;

}

// Increases the capacity of the stack by making

// a new array that is double the size of the previous one

void makeNewArray() {

int[] newStack = new int[stack.length*2];

for (int i=0; i

newStack[i] = stack[i];

stack = newStack;

}

// Implements the push operation

void push(int e) {

if (top==stack.length) {

makeNewArray();

}

stack[top] = e;

top++;

}

// Implements the pop operation

int pop() {

top--;

return stack[top];

}

// Implements the peek operation

int peek() {

return stack[top-1];

}

// Implements the isEmpty operation

boolean isEmpty() {

return top==0;

}

// Implements the size operation

int size() {

return top;

}

}

#3--- #4 CODE LINKEDSTACK

public class LinkedStack {

LinkedNode front; // Reference to the first LinkedNode in the list

int count; // Number of nodes in the list

// Constructor - initializes the front and count variables

LinkedStack() {

front = null;

count = 0;

}

// Implements the push operation

void push(int x) {

LinkedNode newNode = new LinkedNode(x);

newNode.next = front;

front = newNode;

count++;

}

// Implements the pop operation

int pop() {

int x = front.x;

front = front.next;

count--;

return x;

}

// Implements the peek operation

int peek() {

return front.x;

}

// Implements the isEmpty operation

boolean isEmpty() {

return front==null;

}

// Implements the size operation

int size() {

return count;

}

// This method returns a String containing

// a space separated representation of the underlying linked list

public String toString() {

String str = "";

LinkedNode cur = front;

while (cur!=null) {

str += cur.x + " ";

cur = cur.next;

}

return str;

}

Exercise 1 Assume a binary search is performed on the following array of integers: (1, 14, 15, 24, 55, 59, 73, 90, 94, 99 Trace through each iteration of the algorithm, writing the number that will be the middle element and the left and right bounds (indexes), when searching for the number 73. Exercise 2 Trace the execution of the insertion and selection sort algorithms when executed on the following array of integers: (1, 29, 14, 15, 94) Show how the array will look like after each iteration of the outer loop. Before beginning the next two exercises, download AdditionalExamples from BlackBoard and add to a new project in Eclipse. Exercise 3 Create a new Driver class with a main method. In this method, do the following: 1. 2. 3. Create an instance of an ArrayStack and a LinkedStack class. Push the following int's onto the two stacks: (1,7,3, 4, 9,2) Pop off all the elements from the stacks, displaying each int as it's removed Exercise 4 Modify the LinkedStack class to include a new method called removeBottomHalf, which removes the half of elements sitting at the bottom of the stack Test the method using the Driver program What is the time complexity of this method

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!