Menu

(Solved) : Part 1 Collections Part 1 Re Implement Java Collections Classes Linkedlist Stack Queue Thr Q37017993 . . .

Part 1: Collections

In part 1, you will re-implement some of the Java Collectionsclasses: LinkedList, Stack and Queue. There are three classes thatrequire implementation: DSList.java, DSStack.java andDSQueue.java.

There is an «interface» provided for List, and «abstractclasses» for both Queue and Stack. You will use the Linked Listimplementation as the basis for implementing Stack and Queue. Forsome methods there may be additional comments in the Javadoc.

DSList will extend the List class defined in List.java. Theimplementation will be a double-linked list and must implement theabstract methods from List.java.

Part 2: Postfix

The second part requires you to use the Collections to create aninfix-to-postfix converter and a postfix notation parser.

**********************************************************************

package ds.interfaces;

import ds.students.Token;

public interface List {

public booleanadd(int index, Token obj);

public boolean contains(Tokenobj);

public boolean remove(Tokenobj);

public Token remove(intindex);

public Token get(intindex);

public int indexOf(Tokentoken);

public boolean add(Tokenobj);

public boolean isEmpty();

public int size();

public String toString();

public boolean equals(Objectobj);

public int hashCode();

}

**********************************************************************

package ds.interfaces;

import ds.students.DSList;
import ds.students.Token;

public abstract class Queue {

   public DSList list;
   public abstract boolean offer(Token t);
   public abstract Token poll();
   public abstract Token peek();
   public abstract String toString();
   public abstract int size();
   public abstract boolean isEmpty();
}

**********************************************************************

package ds.interfaces;

import ds.students.DSList;
import ds.students.Token;
public abstract class Stack {

   protected DSList list;
   public abstract Token peek();
   public abstract Token pop();
   public abstract Token push(Token obj);
   public abstract String toString();
   public abstract boolean isEmpty();
   public abstract int size();

}

**********************************************************************

package ds.students;

import ds.interfaces.List;

public class DSList implements List {
  
   public Node head;
   private Node tail;

   public DSList() {
      
   }
   public DSList(Node head_) {
   }
  
   public DSList(DSList other) { // Copyconstructor.
      
   }

   public Token remove(int index) {
      
       return null;
   }
  
   public int indexOf(Token obj) {
       return 0;
   }

   public Node getNode(int index) {
       return null;
   }

   public Token get(int index) {
       return null;
   }

   public boolean isEmpty() {
       return true;
   }

   public int size() {
       return 0;
   }
  
   @Override
   public String toString() {
       return “”;
   }

   public boolean add(Token obj) {
       return true;
   }

   public boolean add(int index, Token obj) {
       return false;
   }

   public boolean contains(Token obj) {
       return false;
   }

   public boolean remove(Token obj) {
       return true;
   }
  
   @Override
   public int hashCode() {
       return 0;
   }

   @Override
   public boolean equals(Object other) {
       return true;
   }
  
}

**********************************************************************

package ds.students;

import ds.interfaces.Queue;

public class DSQueueextends Queue {

public DSQueue(Queue s) {

}

public DSQueue() {

}

@Override

public boolean offer(Token t){

return false;

}

@Override

public Token poll() {

return null;

}

@Override

public Token peek() {

return null;

}

@Override

public String toString() {

return null;

}

@Override

public int size() {

return 0;

}

@Override

public boolean isEmpty() {

return true;

}

}

**********************************************************************

package ds.students;

import ds.interfaces.Stack;

public class DSStackextends Stack {

@Override

public int hashCode() {

return 0;

}

@Override

public boolean equals(Objectobj) {

return true;

}

public DSStack() {

}

public DSStack(DSStack other) {

}

public Token push(Token obj) {

return null;

}

public Token peek() {

return null;

}

public Token pop() {

return null;

}

public boolean isEmpty() {

return true;

}

public int size() {

return 0;

}

@Override

public String toString() {

return null;

}

}

**********************************************************************

package ds.students;

public class Node {

public Node next;

public Node prev;

private Token t;

public Node(Node next, Node prev, Token token){

this.next = next;

this.prev = prev;

this.t = token;

}

public Token getToken() {

return t;

}

@Override

public boolean equals(Objectother) {

if (this == other)

return true;

if (other == null)

return false;

if (!(other instanceofNode))

return false;

return t.equals(((Node)other).getToken());

}

@Override

public int hashCode() {

if ( t == null )

return 0;

return t.hashCode();

}

}

**********************************************************************

package ds.students;

/**

* @author simont

*

*/

public class Token {

public enum Type {OPERATOR,OPERAND, PAREN};

public Type type;

private String operator;

private double operand;

public Token(double result){

this.operand = result;

this.type =Type.OPERAND;

}

public Token(String op) {

this.operator = op;

this.type =Type.OPERATOR;

if ( this.operator.equals(“(“)|| this.operator.equals(“)”) ) {

this.type =Type.PAREN;

}

}

public Token(Token other) {

this.operator = other.operator;

this.operand = other.operand;

this.type = other.type;

}

public String getOperator() {

return operator;

}

public double getOperand(){

return operand;

}

public int getPrecedence(){

if ( type ==Type.PAREN )

return -1;

if ( type !=Type.OPERATOR )

return 0;

switch ( operator ) {

case “+”:

case “-“:

return 0;

case “*”:

case “/”:

return 2;

}

return 0;

}

@Override

public boolean equals(Objectobj) {

if ( obj == null )

return false;

if ( obj == this )

return false;

if (!obj.getClass().equals(Token.class))

return false;

Token t = (Token)obj;

if ( t.type == this.type ){

if ( this.type ==Type.OPERATOR )

return operator.equals(t.operator);

else

return operand == t.operand;

}

return false;

}

@Override

public int hashCode() {

return 0;

}

public String toString() {

return this.type ==Type.OPERAND ? “” +this.operand : this.operator;

}

}

**********************************************************************

package ds.students;

import ds.students.Token.Type;

public class Calculator {

public DSQueue infixToPostfix(DSQueue infix){

return null;

}

public doubleevaluatePostfix(DSQueue exp)

{

return 0;

}

}

**********************************************************************

Expert Answer


Answer to Part 1 Collections Part 1 Re Implement Java Collections Classes Linkedlist Stack Queue Thr Q37017993 . . .

OR