Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write you answer please I need answer for 2a, 2b , 3a ,3b. they are writting question ArrayStack.java import java.util.Arrays; public class ArrayStack implements StackInterface

Write you answer please I need answer for 2a, 2b , 3a ,3b. they are writting questionimage text in transcribed

ArrayStack.java

import java.util.Arrays; public class ArrayStack implements StackInterface { private final static int capacity = 10; private int top; private int[] stack; public ArrayStack() { this(capacity); } public ArrayStack(int initialCapacity) { top = -1; stack = new int[initialCapacity]; } public void push(int element) { if (size() == stack.length) expandCapacity(); stack[++top] = element; } private class Node { private int data; private Node next; public Node() { this(0, null); } public Node(int data, Node next) { this.data = data; this.next = next; } } private void expandCapacity() { stack = Arrays.copyOf(stack, stack.length * 2); } public int pop() { if (!isEmpty()) return -1; int result = stack[top]; top--; return result; } public int peek() { if (isEmpty()) return -1; return stack[top]; } public boolean isEmpty() { return top == -1; } public int size() { return top + 1; } @Override public void clear() { top = -1; } public String toString() { String s = ""; for(int i=size()-1;i>=0;i--){ s = s + stack[i]+" "; } return s; } } 

ListStack.java

public class ListStack implements StackInterface { private Node top; private int count; private int[] stack; public ListStack() { top = null; count = 0; } public ListStack(int value) { top = new Node(value, null); count = 1; } private class Node { private int data; private Node next; public Node() { this(0, null); } public Node(int data, Node next) { this.data = data; this.next = next; } } public void push(int value) { Node node = new Node(value, null); node.next = top; top = node; count++; } public int pop() { if (count == 0) return -1; Node temp = top.next; top = top.next; count--; return temp.data; } public int peek() { if (count == 0) return -1; return top.data; } @Override public int size() { return count; } @Override public boolean isEmpty() { return count == 0; } public String toString() { String str = ""; Node current = top; while (current != null) { str += current.data; if (current.next != null) { str += " "; } current = current.next; } return str; } @Override public void clear() { top = null; } }

lass Queue implements QueueInterface

{

private int[] arr; // array to store queue elements

private int front; // front points to the front element in the queue

private int rear; // rear points to the last element in the queue

private int capacity; // maximum capacity of the queue

private int count; // current size of the queue

// Constructor to initialize a queue

Queue(int size)

{

arr = new int[size];

capacity = size;

front = 0;

rear = -1;

count = 0;

}

// Utility function to dequeue the front element

public void dequeue()

{

// check for queue underflow

if (isEmpty())

{

System.out.println("Underflow Program Terminated");

System.exit(1);

}

System.out.println("Removing " + arr[front]);

front = (front + 1) % capacity;

count--;

}

// Utility function to add an item to the queue

public void enqueue(int item)

{

// check for queue overflow

if (isFull())

{

System.out.println("Overflow Program Terminated");

System.exit(1);

}

System.out.println("Inserting " + item);

rear = (rear + 1) % capacity;

arr[rear] = item;

count++;

}

// Utility function to return the front element of the queue

public int peek()

{

if (isEmpty())

{

System.out.println("Underflow Program Terminated");

System.exit(1);

}

return arr[front];

}

// Utility function to return the size of the queue

public int size() {

return count;

}

// Utility function to check if the queue is empty or not

public Boolean isEmpty() {

return (size() == 0);

}

// Utility function to check if the queue is full or not

public Boolean isFull() {

return (size() == capacity);

}

}

class Main

{

public static void main (String[] args)

{

// create a queue of capacity 5

Queue q = new Queue(5);

q.enqueue(1);

q.enqueue(2);

q.enqueue(3);

System.out.println("The front element is " + q.peek());

q.dequeue();

System.out.println("The front element is " + q.peek());

System.out.println("The queue size is " + q.size());

q.dequeue();

q.dequeue();

if (q.isEmpty()) {

System.out.println("The queue is empty");

}

else {

System.out.println("The queue is not empty");

}

}

}

2. ArrayStack vs. ListStack 2a Make a similar table as you did above. It should look like the below except the '?' should be replaced with the runtime efficiencies. Method push(int value) pop() peek() isEmpty size clear() toString() equals(Object o) ArrayStack ListStack ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2b As you are aware, Java only provides one Stack class. So you cannot choose an ArrayStack or a ListStack depending on data or data access patterns. You have to pick one and go with it for all of your Stack needs. Using the above table and your knowledge about how Stacks are used, would you choose to implement a Stack with an array or a linked list as its backing data structure? Why? 3. Array Queue vs. ListQueue Make one more table! Hopefully you are getting pretty good at this by now. Method enqueueint value) dequeue() peek() isEmpty size() clear() toString() equals(Objecto) Array Queue ListQueue ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3b If you were creating a new programming language, and writing a Queue class, would you choose to use an array or a linked list as the backing data structure for your queue? Why? 2. ArrayStack vs. ListStack 2a Make a similar table as you did above. It should look like the below except the '?' should be replaced with the runtime efficiencies. Method push(int value) pop() peek() isEmpty size clear() toString() equals(Object o) ArrayStack ListStack ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 2b As you are aware, Java only provides one Stack class. So you cannot choose an ArrayStack or a ListStack depending on data or data access patterns. You have to pick one and go with it for all of your Stack needs. Using the above table and your knowledge about how Stacks are used, would you choose to implement a Stack with an array or a linked list as its backing data structure? Why? 3. Array Queue vs. ListQueue Make one more table! Hopefully you are getting pretty good at this by now. Method enqueueint value) dequeue() peek() isEmpty size() clear() toString() equals(Objecto) Array Queue ListQueue ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 3b If you were creating a new programming language, and writing a Queue class, would you choose to use an array or a linked list as the backing data structure for your queue? Why

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions

Question

How do books become world of wonder?

Answered: 1 week ago