I am new in data structure. I perform append
and palindrome
or not operation but i get Cannot Find Symbol
error while initize popFromStack
variable and i am stuck there...
Here down is my code:
class Node
{
String data;
Node next;
Node(String data)
{
this.data = data;
}
}
public class MyClass {
Node head;
public void append(String data)
{
if(head == null)
{
head = new Node(data);
return;
}
Node current = head;
while(current.next != null)
{
current = current.next;
}
current.next = new Node(data);
}
public boolean isPalindrome(Node head)
{
Node slow = head;
boolean isPalin = true;
Stack<String> stack = new Stack<String>();;
while(slow != null)
{
stack.push(slow.data);
slow = slow.next;
}
while(head != null)
{
String popFromStack = stack.pop; // here is the problem
if(head.data == popFromStack)
{
isPalin = true;
}
else
{
isPalin = false;
}
head = head.next;
return isPalin;
}
}
public void printList()
{
Node current = head;
while(current != null)
{
System.out.print(current.data " ");
current = current.next;
}
}
public static void main(String[] args) {
MyClass m = new MyClass();
m.append("A");
m.append("B");
m.append("D");
m.append("B");
m.append("A");
m.printList();
System.out.println("\n");
System.out.print("Palindrome : " m.isPalindrome(m.head));
}
Stack trace:
MyClass.java:79: error: cannot find symbol
String popFromStack = stack.pop;
^
symbol: variable pop
location: variable stack of type Stack<String>
1 error
CodePudding user response:
pop is a method so you must add ()
String popFromStack = stack.pop();
Btw. you must not compare String with == but with calling equals.
if(head.data.equals(popFromStack))
Please read more about String comparision: https://www.javatpoint.com/string-comparison-in-java
CodePudding user response:
pop is not an attribute, it's a method. make it like this:
stack.pop();