In: Computer Science
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.
//=======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