1. What happens when the 11th item is added to a stack or queue?  2. What is the number of the next item, after the 11th, that would cause the same process you just described (in Question 1) to occur? 3. Assume you have 5 items in a stack and 5 items in a queue. Thinking about the underlying structure (i.e. the array) holding the data, what happens when you remove one item from the stack and one item from the queue?   import java.util.EmptyStackException;   public class ArrayStack implements Cloneable {   private int[ ] data; private int manyItems; public ArrayStack( ) { final int INITIAL_CAPACITY = 10; manyItems = 0; data = (int[]) new int[INITIAL_CAPACITY]; }   public ArrayStack(int initialCapacity) { if (initialCapacity < 0) throw new IllegalArgumentException ("initialCapacity too small " + initialCapacity); manyItems = 0; data = (int[]) new int[initialCapacity]; }   public ArrayStack clone( ) { // Clone an ArrayStack. ArrayStack answer;   try { answer = (ArrayStack) super.clone( ); } catch (CloneNotSupportedException e) {   throw new RuntimeException ("This class does not implement Cloneable"); }   answer.data = data.clone( );   return answer; }     public void ensureCapacity(int minimumCapacity) { int biggerArray[ ];   if (data.length < minimumCapacity) { biggerArray = (int[]) new int[minimumCapacity]; System.arraycopy(data, 0, biggerArray, 0, manyItems); data = biggerArray; } } public int getCapacity( ) { return data.length; }   public boolean isEmpty( ) { return (manyItems == 0); }   public int peek( ) { if (manyItems == 0) // EmptyStackException is from java.util and its constructor has no argument. throw new EmptyStackException( ); return data[manyItems-1]; } public int pop( ) { if (manyItems == 0)   throw new EmptyStackException( ); return data[--manyItems]; }   public void push(int item) { if (manyItems == data.length) {   } data[manyItems] = item; manyItems++; }   public int size( ) { return manyItems; } public void trimToSize( ) { int trimmedArray[ ];   if (data.length != manyItems) { trimmedArray = (int[]) new int[manyItems]; System.arraycopy(data, 0, trimmedArray, 0, manyItems); data = trimmedArray; } } }   import java.util.NoSuchElementException; public class ArrayQueue implements Cloneable {   private E[ ] data; private int manyItems; private int front; private int rear; public ArrayQueue( ) { final int INITIAL_CAPACITY = 10; manyItems = 0; data = (E[]) new Object[INITIAL_CAPACITY];   } public ArrayQueue(int initialCapacity) { if (initialCapacity < 0) throw new IllegalArgumentException ("initialCapacity is negative: " + initialCapacity); manyItems = 0; data = (E[]) new Object[initialCapacity];   } public ArrayQueue clone( ) { ArrayQueue answer;   try { answer = (ArrayQueue) super.clone( ); } catch (CloneNotSupportedException e) {   throw new RuntimeException ("This class does not implement Cloneable"); }   answer.data = data.clone( );   return answer; } public void add(E item) { if (manyItems == data.length) { ensureCapacity(manyItems*2 + 1); }   if (manyItems == 0) { front = 0; rear = 0; } else rear = nextIndex(rear);   data[rear] = item; manyItems++; }   public void ensureCapacity(int minimumCapacity) { E[ ] biggerArray; int n1, n2;   if (data.length >= minimumCapacity)   return; else if (manyItems == 0)   data = (E[]) new Object[minimumCapacity]; else if (front <= rear) { biggerArray = (E[]) new Object[minimumCapacity]; System.arraycopy(data, front, biggerArray, front, manyItems); data = biggerArray; } else { biggerArray = (E[]) new Object[minimumCapacity]; n1 = data.length - front; n2 = rear + 1; System.arraycopy(data, front, biggerArray, 0, n1); System.arraycopy(data, 0, biggerArray, n1, n2); front = 0; rear = manyItems-1; data = biggerArray; } }   public int getCapacity( ) { return data.length; } public boolean isEmpty( ) { return (manyItems == 0); }   private int nextIndex(int i)   { if (++i == data.length) return 0; else return i; }   public E remove( ) { E answer;   if (manyItems == 0) throw new NoSuchElementException("Queue underflow"); answer = data[front]; front = nextIndex(front); manyItems--; return answer; }     public int size( ) { return manyItems; }   public void trimToSize( ) { E[] trimmedArray; int n1, n2;   if (data.length == manyItems)   return; else if (manyItems == 0)   data = (E[]) new Object[0]; else if (front <= rear) { trimmedArray = (E[]) new Object[manyItems]; System.arraycopy(data, front, trimmedArray, front, manyItems); data = trimmedArray; } else { trimmedArray = (E[]) new Object[manyItems]; n1 = data.length - front; n2 = rear + 1; System.arraycopy(data, front, trimmedArray, 0, n1); System.arraycopy(data, 0, trimmedArray, n1, n2); front = 0; rear = manyItems-1; data = trimmedArray; } } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

1. What happens when the 11th item is added to a stack or queue? 

2. What is the number of the next item, after the 11th, that would cause the same process you just described (in Question 1) to occur?

3. Assume you have 5 items in a stack and 5 items in a queue. Thinking about the underlying structure (i.e. the array) holding the data, what happens when you remove one item from the stack and one item from the queue?

 

import java.util.EmptyStackException;

 

public class ArrayStack implements Cloneable

{

 

private int[ ] data;

private int manyItems;

public ArrayStack( )

{

final int INITIAL_CAPACITY = 10;

manyItems = 0;

data = (int[]) new int[INITIAL_CAPACITY];

}

 

public ArrayStack(int initialCapacity)

{

if (initialCapacity < 0)

throw new IllegalArgumentException

("initialCapacity too small " + initialCapacity);

manyItems = 0;

data = (int[]) new int[initialCapacity];

}

 

public ArrayStack clone( )

{ // Clone an ArrayStack.

ArrayStack answer;

 

try

{

answer = (ArrayStack) super.clone( );

}

catch (CloneNotSupportedException e)

{

 

throw new RuntimeException

("This class does not implement Cloneable");

}

 

answer.data = data.clone( );

 

return answer;

}

 

 

public void ensureCapacity(int minimumCapacity)

{

int biggerArray[ ];

 

if (data.length < minimumCapacity)

{

biggerArray = (int[]) new int[minimumCapacity];

System.arraycopy(data, 0, biggerArray, 0, manyItems);

data = biggerArray;

}

}

public int getCapacity( )

{

return data.length;

}

 

public boolean isEmpty( )

{

return (manyItems == 0);

}

 

public int peek( )

{

if (manyItems == 0)

// EmptyStackException is from java.util and its constructor has no argument.

throw new EmptyStackException( );

return data[manyItems-1];

}

public int pop( )

{

if (manyItems == 0)

 

throw new EmptyStackException( );

return data[--manyItems];

}

 

public void push(int item)

{

if (manyItems == data.length)

{

 

}

data[manyItems] = item;

manyItems++;

}

 

public int size( )

{

return manyItems;

}

public void trimToSize( )

{

int trimmedArray[ ];

 

if (data.length != manyItems)

{

trimmedArray = (int[]) new int[manyItems];

System.arraycopy(data, 0, trimmedArray, 0, manyItems);

data = trimmedArray;

}

}

}

 

import java.util.NoSuchElementException;

public class ArrayQueue<E> implements Cloneable

{

 

private E[ ] data;

private int manyItems;

private int front;

private int rear;

public ArrayQueue( )

{

final int INITIAL_CAPACITY = 10;

manyItems = 0;

data = (E[]) new Object[INITIAL_CAPACITY];

 

}

public ArrayQueue(int initialCapacity)

{

if (initialCapacity < 0)

throw new IllegalArgumentException

("initialCapacity is negative: " + initialCapacity);

manyItems = 0;

data = (E[]) new Object[initialCapacity];

 

}

public ArrayQueue<E> clone( )

{

ArrayQueue<E> answer;

 

try

{

answer = (ArrayQueue<E>) super.clone( );

}

catch (CloneNotSupportedException e)

{

 

throw new RuntimeException

("This class does not implement Cloneable");

}

 

answer.data = data.clone( );

 

return answer;

}

public void add(E item)

{

if (manyItems == data.length)

{

ensureCapacity(manyItems*2 + 1);

}

 

if (manyItems == 0)

{

front = 0;

rear = 0;

}

else

rear = nextIndex(rear);

 

data[rear] = item;

manyItems++;

}

 

public void ensureCapacity(int minimumCapacity)

{

E[ ] biggerArray;

int n1, n2;

 

if (data.length >= minimumCapacity)

 

return;

else if (manyItems == 0)

 

data = (E[]) new Object[minimumCapacity];

else if (front <= rear)

{

biggerArray = (E[]) new Object[minimumCapacity];

System.arraycopy(data, front, biggerArray, front, manyItems);

data = biggerArray;

}

else

{

biggerArray = (E[]) new Object[minimumCapacity];

n1 = data.length - front;

n2 = rear + 1;

System.arraycopy(data, front, biggerArray, 0, n1);

System.arraycopy(data, 0, biggerArray, n1, n2);

front = 0;

rear = manyItems-1;

data = biggerArray;

}

}

 

public int getCapacity( )

{

return data.length;

}

public boolean isEmpty( )

{

return (manyItems == 0);

}

 

private int nextIndex(int i)

 

{

if (++i == data.length)

return 0;

else

return i;

}

 

public E remove( )

{

E answer;

 

if (manyItems == 0)

throw new NoSuchElementException("Queue underflow");

answer = data[front];

front = nextIndex(front);

manyItems--;

return answer;

}

 

 

public int size( )

{

return manyItems;

}

 

public void trimToSize( )

{

E[] trimmedArray;

int n1, n2;

 

if (data.length == manyItems)

 

return;

else if (manyItems == 0)

 

data = (E[]) new Object[0];

else if (front <= rear)

{

trimmedArray = (E[]) new Object[manyItems];

System.arraycopy(data, front, trimmedArray, front, manyItems);

data = trimmedArray;

}

else

{

trimmedArray = (E[]) new Object[manyItems];

n1 = data.length - front;

n2 = rear + 1;

System.arraycopy(data, front, trimmedArray, 0, n1);

System.arraycopy(data, 0, trimmedArray, n1, n2);

front = 0;

rear = manyItems-1;

data = trimmedArray;

}

}

}

 
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education