Question

In: Computer Science

Make following changes. step(A) step 1 Implementation a Generic Linked List using the program developed in...

Make following changes. step(A)
step 1 Implementation a Generic Linked List using the program developed in the class.
step 2  Implement StackImpl class using the generic linked list.
step 3 Test the program with different type and number of matching and un-matching brackets.
This is how it works!
When you run the class MatchBrackets a popup dialog appears asking you to select a file. You are provided two input files in the project. input1.txt contains matching brackets and input2.txt does not match the brackets.
You can select any of these files to verify.

Then do steb(B)

step 1 Externalize the inner classes Node and ListIterator.
step 2 Make the appropriate changes to turn in the LinkedList class into a generic class.
step3 Use the one exception classes EmptyList where applicable.
step4 . Implement StackImpl class using the generic linked list.
step 5 Add test cases in the main method to demonstrate the working of your code.
. step 6 Run match bracket to check whether the brackets were matched or not.

Solutions

Expert Solution

//=======linkedList.java==================
public class linkedList<T> {
   private class node{
       public T data;
       public node next;
       public node(T data) {
           this.data=data;
           next=null;
       }
   }
   private node head;
   //__________________________
   public linkedList() {
       head=null;
   }
   //__________________________
   public boolean isEmpty() {
       return head==null;
   }
   //__________________________
   public void insertAtFront(T data) {
       node temp=new node(data);
       temp.next=head;
       head=temp;
   }
   //__________________________
   public T peek() {
       if(isEmpty())
           return null;
       return head.data;
   }
   //__________________________
   public void removeFront() {
       if(isEmpty())
           return;
       head=head.next;
   }
   //__________________________
   public String toString() {
       String result="";
       node temp=head;
       while(temp!=null) {
           result=result+temp.data+" ";
           temp=temp.next;
       }
       return result;
   }
   //__________________________
}
//=======end of linkedList.java=========
//=========StackImpl.java=============
public class StackImpl {
   private linkedList<Character> list;
   //_________________________
   public StackImpl() {
       list=new linkedList<Character>();
   }
   //________________________
   public void push(Character ch) {
       list.insertAtFront(ch);
   }
   //________________________
   public boolean isEmpty() {
       return list.isEmpty();
   }
   //________________________
   public Character top() {
       if(isEmpty())
           return null;
       return list.peek();      
   }
   //__________________________
   public void pop() {
       if(!isEmpty())
           list.removeFront();
   }
   //__________________________
   public String toString() {
       return list.toString();
   }
}
//=====end of StackImpl.java

//==========MatchBrackets.java=================
import java.util.Scanner;
import java.io.*;

public class MatchBrackets {
   //_____________________________
   public static boolean match(String expression) {
       StackImpl S=new StackImpl();
       for(int i=0;i<expression.length();i++) {
           if(expression.charAt(i)=='[')
               S.push('[');
           else if(expression.charAt(i)==']') {
               if(S.isEmpty())
                   return false;
               else if(S.top()=='[')
                   S.pop();
               else
                   return false;
           }
           else {
               return false;
           }
       }
       if(S.isEmpty()==true)
           return true;
      
       return false;
   }
   //_________main()__________________
   public static void main(String[] args) {
       try {
       String expression="";
       Scanner in=new Scanner(System.in);
       File fp;
       Scanner scr;
      
      
       System.out.println("1. input1.txt");
       System.out.println("2. input2.txt");
       System.out.print("Enter choice(1,2): ");
       int choice=in.nextInt();
      
           if(choice==1) {
               fp=new File("input1.txt");
               scr=new Scanner(fp);
               while (scr.hasNextLine()) {
                   expression=scr.nextLine();
                   System.out.println(expression+" Bracket match: "+match(expression));
               }
           }
           else if(choice==2) {
               fp=new File("input2.txt");
               scr=new Scanner(fp);
               while (scr.hasNextLine()) {
                   expression=scr.nextLine();
                   System.out.println(expression+" Bracket match: "+match(expression));
               }
              
           }else {
               System.out.println("Invalid choice!");
           }
           in.close();
       }catch(Exception e) {
           System.out.println(e);
       }
      
   }
}
//=======end of MatchBrackets.java===============
//output


Related Solutions

Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
. Implement your own custom linked list array implementation with the following changes: (a) Fill in...
. Implement your own custom linked list array implementation with the following changes: (a) Fill in the public E get(int index) method (b) Also add code to the get method to print out a message for each time an element in the list is checked while searching for the element You may want to study how the toString method goes from element to element in the list Java
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
Give a complete implementation of the queue ADT using a singly linked list that includes a...
Give a complete implementation of the queue ADT using a singly linked list that includes a header sentinel (in Python).
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create a generic class called GenLinkedList. GenLinkedList will use nodes that store a value of the generic type to store its contents. It should have the following methods. The methods should all operate on the object making the call (none are static). Perform checking of the parameters and throw exceptions where appropriate. The linked list should be singly-linked. It should not use sentinel nodes (empty...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to...
1. Adapt the custom array list implementation code with the following changes: (a) Add code to the ensureCapacity() method to print out a message including how many elements are copied to the new array on resizing Array List Implementation: public class MyArrayList<E> implements MyList<E> { public static final int INITIAL_CAPACITY = 16; private E[] data = (E[])new Object[INITIAL_CAPACITY]; private int size = 0; // Number of elements in the list public MyArrayList() { }    public MyArrayList(E[] objects) { for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT