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...
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. }
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...
Write a program in C++ that will make changes in the list of strings by modifying...
Write a program in C++ that will make changes in the list of strings by modifying its last element. Your program should have two functions: 1. To change the last element in the list in place. That means, without taking the last element from the list and inserting a new element with the new value. 2. To compare, you need also to write a second function that will change the last element in the list by removing it first, and...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
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...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program...
TITLE Updating Accounts Using Doubly Linked List TOPICS Doubly Linked List DESCRIPTION General Write a program that will update bank accounts stored in a master file using updates from a transaction file. The program will maintain accounts using a doubly linked list. The input data will consist of two text files: a master file and a transaction file. See data in Test section below.  The master file will contain only the current account data. For each account, it will contain account...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with class SLLNode listed as inner class. TestIntegerSLL.java that tests the SLL class by using a linked list of Integer. In SLL class add the following method:                                                                    public void moveToEnd (int i) It will move the element at the i -th position to the end of the list. You can assume i to be within the list, and that the first element has the...
so the assigment is is a data strucutre using c++ to make a doubly linked list...
so the assigment is is a data strucutre using c++ to make a doubly linked list that will be able to do math mostly addition and multiplication, subratction and division is extra and would be nice. so the program is going to to open files and read them via a argumentmanager.h in a linux server try not to worry to much about this part just getting the program to work. i was able to complete part of the given code...
Java The List ADT has an interface and a linked list implementation whose source code is...
Java The List ADT has an interface and a linked list implementation whose source code is given at the bottom of this programming lab description. You are to modify the List ADT's source code by adding the method corresponding to the following UML: +hasRepeats() : boolean hasRepeats() returns true if the list has a value that occurs more than once in the list hasRepeats() returns false if no value in the list occurs more than once in the list For...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT