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
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.
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...
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...
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...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT