CS 501 Chapter 4

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 5

Dhara Patel Chapter 4 PP 4.2) Create a Deque class based on the discussion of deques (double-ended queues) in this chapter.

It should include insertLeft(), insertRight(), removeLeft(), remo veRight(), isEmpty(), and isFull() methods. It will need to support wrap around at the end of the array, as queues do. Answer 4.2: class Deque { private int maxSize; private long[] dekArray; private int left; private int right; private int nItems; public Deque(int s) { maxSize = s; dekArray = new long[maxSize]; int center = maxSize/2 - 1; left = center+1; right = center; nItems = 0; } public void insertLeft(long j) { if(left == 0) left = maxSize; dekArray[--left] = j; nItems++; } public void insertRight(long j) { if(right == maxSize-1) right = -1; dekArray[++right] = j; nItems++; } public long removeLeft() { long temp = dekArray[left++]; if(left == maxSize) left = 0; nItems--; return temp; } public long removeRight() { long temp = dekArray[right--];

if(right == -1) right = maxSize-1; nItems--; return temp; } public boolean isEmpty() { return (nItems==0); } public boolean isFull() { return (nItems == maxSize); } public int size() { return nItems; } public void display() { System.out.print("Array: "); for(int j=0; j<maxSize; j++) System.out.print( dekArray[j] + " " ); System.out.println(""); System.out.print("Deque: "); if(left <= right) for(int j=left; j<=right; j++) System.out.print( dekArray[j] + " "); else if( isEmpty() == false ) { for(int j=left; j<maxSize; j++) System.out.print( dekArray[j] + " "); for(int j=0; j<=right; j++) System.out.print( dekArray[j] + " "); } System.out.println(""); } } class DequeApp { public static void main(String[] args) throws IOException { Deque theDeque = new Deque(10); while(true) { long value; System.out.println(""); if( theDeque.isFull() ) System.out.println("*** Deque is full. No insertions. ***"); if( theDeque.isEmpty() ) System.out.println("*** Deque is empty. No deletions. ***"); System.out.print("Enter first letter of "); System.out.println("insertLeft, InsertRight, "); System.out.print("removeLeft, RemoveRight, or display: "); int choice = getChar(); switch(choice) { case 'd': theDeque.display();

break; case 'i': System.out.print("Enter value to insert left: "); value = getLong(); theDeque.insertLeft(value); break; case 'I': System.out.print("Enter value to insert right: "); value = getLong(); theDeque.insertRight(value); break; case 'r': value = theDeque.removeLeft(); System.out.println("Removed left: " + value); break; case 'R': value = theDeque.removeRight(); System.out.println("Removed right: " + value); break; default: System.out.print("Invalid entry\n"); } } } public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } public static char getChar() throws IOException { String s = getString(); return s.charAt(0); } public static long getLong() throws IOException { String s = getString(); return (long)Integer.parseInt(s); } } PP 4.3) Write a program that implements a stack class that is based on the Deque class i n PP 4.2. This stack class should have the same methods and capabilities as the StackX class in the stack.java program. Answer 4.3: class Deque { private int maxSize; private long[] dekArray; private int left;

private int right; private int nItems; public Deque(int s) { maxSize = s; dekArray = new long[maxSize]; int center = maxSize/2 - 1; left = center+1; right = center; nItems = 0; } public void insertLeft(long j) { if(left == 0) left = maxSize; dekArray[--left] = j; nItems++; } public void insertRight(long j) { if(right == maxSize-1) right = -1; dekArray[++right] = j; nItems++; } public long removeLeft() { long temp = dekArray[left++]; if(left == maxSize) left = 0; nItems--; return temp; } public long removeRight() { long temp = dekArray[right--]; if(right == -1) right = maxSize-1; nItems--; return temp; } public boolean isEmpty() { return (nItems==0); } public boolean isFull() { return (nItems == maxSize); } public int size() { return nItems; } } class StackX {

private int maxSize; private Deque theDeque; public StackX(int s) { maxSize = s; theDeque = new Deque(maxSize); } public void push(long j) { theDeque.insertLeft(j); } public long pop() { return theDeque.removeLeft() ; } public boolean isEmpty() { return ( theDeque.isEmpty() ); } public boolean isFull() { return( theDeque.isFull() ); } } class StackApp { public static void main(String[] args) { StackX theStack = new StackX(10); for(int j=0; !theStack.isFull(); j++) theStack.push(j*10); while( !theStack.isEmpty() ) { long value = theStack.pop(); System.out.print(value + " "); } System.out.println(""); } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy