Please answer this in java. I have done the stack class and I am using linked li
ID: 3671235 • Letter: P
Question
Please answer this in java. I have done the stack class and I am using linked list for that, and that is posted just above the assignment
public class GenLinkedListStack <T> {
private class ListNode
{
private T data;
private ListNode link;
public ListNode()
{
data=null;
link=null;
}
}
private ListNode head;
private ListNode tail;
public GenLinkedListStack()
{
head=tail=null;
}
public void push(T data)
{
ListNode newNode=new ListNode();//reserved space inside heap part of memory
newNode.data=data;
newNode.link=head;
head=newNode;
}
public T pop()
{
if(head==null)
{
System.out.println("Empty");
return null;
}
T returnData=head.data;
head=head.link;
return returnData;
}
}
Objective:
Write a program that checks whether or not a string of characters that has enclosing symbols are well-formed. Enclosing meaning there is an open version of the symbol it and a closing symbol. Furthermore, well-formed means that if there is an opening symbol, then there must be a matching closing symbol also in the string.
The symbols to check are:
Parenthesis: ()
Double Quotes: “”
Curly Brackets / Braces: {}
Angle Brackets: <>
Write a stack, and use it to solve this problem
You may NOT use the built-in java stack
You may either implement the stack as a linked structure or an array. If you use an array assume the max size is 100;
Write another class that has a method which takes in a string and returns a Boolean that corresponds to whether or not the string is well formed. Use the following algorithm to determine if the string is well-formed.
Algorithm overview:
Examine each character one-by-one
If the character is the opening version of one of the enclosing symbols, then push that character onto the stack
If the character is the closing version of one of the enclosing symbols, then pop and check if it matches. If it does continue, but if not or if the stack is empty return false.
If there is a symbol that is not one of the enclosing symbols then ignore it
After each character has been process if the stack is empty return true. Otherwise return false
Finally write a class (it could be the same class as the class above) which uses a main method to test the following conditions:
() – should return true
<<<>>> - should return true
{(“tacos”)} – should return true
if(pass == true){return “yay!”;} – should return true
abcd – should return true
“””– should return false
<(“) – should return false
:-) – should return false
<3 – should return false
(<{“”}> - should return false
Explanation / Answer
import java.util.*;
class GenLinkedListStack <T> {
class ListNode
{
T data;
ListNode link;
public ListNode()
{
data=null;
link=null;
}
}
public ListNode head;
public ListNode tail;
public GenLinkedListStack()
{
head=tail=null;
}
public void push(T data)
{
ListNode newNode=new ListNode();//reserved space inside heap part of memory
newNode.data=data;
newNode.link=head;
head=newNode;
}
public T pop()
{
if(head==null)
{
System.out.println("Empty");
return null;
}
T returnData=head.data;
head=head.link;
return returnData;
}
}
public class Demo
{
public static void main(String [] args)
{
GenLinkedListStack<Character> ob=new GenLinkedListStack<Character>();
Scanner sc=new Scanner(System.in);
System.out.println("enter well formed string");
String wff=sc.next();
for (int i = 0; i < wff.length(); i++)
{
if(wff.charAt(i)=='('|| wff.charAt(i)=='{'||wff.charAt(i)=='<' )
ob.push(wff.charAt(i));
else if(ob.head.data==wff.charAt(i))
{
char ch=ob.pop();
System.out.println("matched symbol");
}
else
continue;
}
if(ob.head==null)
System.out.println("given string is well formed");
else
System.out.println("given string is not well formed");
}
}