Question: Please help me with this java project. I attached the stack class we did in class: Create a Deque class based on the discussion of
Please help me with this java project. I attached the stack class we did in class:
Create a Deque class based on the discussion of deques in this chapter. It should include insertLeft(), insertRight(), deleteLeft(), deleteRight(), isEmpty() and isFull() methods. It will need to support wrapping around at the end of the arrays as queues do.
After you have created the Deque class, write a Stack class based on the Deque class(Use deque class methods). This Stack class should have the same methods and capabilities as the Stack we implemented in class.
Write a main class that tests both Deque and Stack classes.
ArrayStack.java
public class ArrayStack { private int[] array; int top; public ArrayStack(int size){ array = new int[size]; top = -1; } public boolean isEmpty(){ return top == -1; } public boolean isFull(){ return top == array.length-1; } public void push(int item){ if(isFull()){ System.out.println("Cannot insert"); } else{ //top++; array[top] = item; } } public int peek(){ if(isEmpty()){ return -1; } else{ return array[top]; } } public int pop(){ if(isEmpty()){ return -1; } else{ return array[top--]; } } }
Stacks.java
import java.util.Scanner; public class Stacks { public static void main(String[] args) { Scanner input = new Scanner(System.in); // System.out.println("Enter a string to parse"); // String in = input.nextLine(); // matching(in); /*ArrayStack s = new ArrayStack(in.length()); for(int i = 0 ; i < in.length(); i++){ s.push(in.charAt(i)); } for(int i = 0 ; i < in.length(); i++){ System.out.print((char)s.pop()); }*/ int i = 5; Integer a = new Integer(i); double j = 5.6; Double d = new Double(j); } public static void matching(String s){ ArrayStack a = new ArrayStack(100); for(int i = 0 ; i < s.length(); i++){ char c = s.charAt(i); switch(c){ case '[': case '(': case '{':a.push(c);break; case ']': case ')': case '}': if(!a.isEmpty()){ char ch = (char)a.pop(); if (c == ']' && ch != '[' || c == ')' && ch != '(' || c == '}' && ch != '{') { System.out.println("Error: not matcing"); } break; } else{ System.out.println("Error: not matching"); break; } default:
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
