Using the linked list class you created in Step 1 create stack and queue classes
ID: 3808607 • Letter: U
Question
Using the linked list class you created in Step 1 create stack and queue classes. I will leave it up to you as to whether to use composition or inheritance but whatever way you choose to go you should be able to explain why you chose the relationship type you did. The stack class should have the following interface push pop peek The queue should have the following interface enqueue dequeue peek Again I will leave it up to you as to the parameters to the functions and the return type. As usual please have good reasoning for why you have been doing thingsExplanation / Answer
//change filename in main accordingly.
import java.util.*;
import java.io.*;
class Queue extends LinkedClass // assuming class cerated in before assignment is LinkedClass
{
protected String Queue[] ;
protected int front, rear, size, len;
/* Constructor */
public Queue(int n)
{
size = n;
len = 0;
Queue = new String[size];
front = -1;
rear = -1;
}
/* Function to check if queue is empty */
public boolean isEmpty()
{
return front == -1;
}
/* Function to check if queue is full */
public boolean isFull()
{
return front==0 && rear == size -1 ;
}
/* Function to get the size of the queue */
public int getSize()
{
return len ;
}
/* Function to check the front element of the queue */
public String peek()
{
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
return Queue[front];
}
/* Function to insert an element to the queue */
public void enqueue(String i)
{
if (rear == -1)
{
front = 0;
rear = 0;
Queue[rear] = i;
}
else if (rear + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
else if ( rear + 1 < size)
Queue[++rear] = i;
len++ ;
}
/* Function to remove front element from the queue */
public String dequeue()
{
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
else
{
len-- ;
String ele = Queue[front];
if ( front == rear)
{
front = -1;
rear = -1;
}
else
front++;
return ele;
}
}
boolean isPalindrome(String s)
{
int n = s.length();
for(int i=0; i< (n/2); i++)
{
if(s.charAt(i) != s.charAt(n - i - 1))
{
return false;
}
}
return true;
}
}
//implement stack class
class Stack extends LinkedClass // assuming class cerated in before assignment is LinkedClass
{
private int maxSize;
private long[] stackArray;
private int top;
public Stack(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;
}
public long pop() {
return stackArray[top--];
}
public long peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
}
public class ReadWrite
{
public static void main(String[] args)
{
Queue q = new Queue();
try
{
BufferedReader reader = new BufferedReader(new FileReader(filename)); //provide filename here or take input from user
String line;
while((line = reader.readLine()) != null )
{
String upper = line.toUpperCase();
q.enqueue(upper);
}
reader.close();
}catch(Exception e){
System.err.format("Exception occurred trying to read '%s'.", filename);
e.printStackTrace();
return null;
}
//open up file for writing
try{
BufferedReader bw = new BufferedReader(new FileWriter("pal.txt", "true")); //filename to write to
}catch(Exception e){
e.printStackTrace();
}
//check which string is palindromes
for(int i=0; i < q.getSize(); i++){
String s = q.dequeue();
if(q.isPalindrome(s))
{
try{
bw.write(s);
bw.newLine();
bw.flush();
}catch(Exception e){
e.printStackTrace();
}
}
}
bw.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.