I need help implementing the following methods in the class LinkedStack which im
ID: 3893624 • Letter: I
Question
I need help implementing the following methods in the class LinkedStack which implements the stack interface
A method with the following header
public static boolean palindrome(String str)
This method returns true if string is a palindrome or null, otherwise false. A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward - assuming that you ignore spaces, punctuation, and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. You need to use a stack to implement this method.
A method with the following header
public static boolean equals01(String str)
This method returns true if string is composed of equal number of 0s and 1s, or null, otherwise it returns false. Suppose that you read a binary string - that is, a string of 0s and 1s - one character at a time. This method needs to use a stack but no arithmetic to see whether the number of 0s is equal to the number of 1s.
A method with the following header
public static <T> void display(LinkedStack<T> ls)
This method displays a comma separated string value of all the objects in a stack in the order in which they were pushed onto it. After all the objects are displayed, the stack ls should have the same contents as when it was passed into this method. You need to use a stack in this method.
Here is the stack interface
/**
* An interface for the ADT stack.
*/
public interface StackInterface<T>
{
/**
* Adds a new entry to the top of this stack.
* @param newEntry: An object to be added to the stack.
*/
public void push(T newEntry);
/**
* Removes and returns this stack's top entry.
* @return The object at the top of the stack.
* @throws EmptyStackException if the stack is empty before the operation.
*/
public T pop();
/**
* Retrieves this stack's top entry.
* @return The object at the top of the stack.
* @throws EmptyStackException if the stack is empty.
*/
public T peek();
/**
* Detects whether this stack is empty.
* @return True if the stack is empty.
*/
public boolean isEmpty();
/**
* Removes all entries from this stack.
*/
public void clear();
} // end StackInterface
Here is the code for the linked stack class
/**
* A class of stacks whose entries are stored in a chain of nodes.
* @author Darnesha Randle
*/
public class LinkedStack<T> implements StackInterface<T>
{
private Node topNode;
public LinkedStack()
{
topNode = null;
}
public void push(T newEntry)
{
Node newNode = new Node(newEntry, topNode);
topNode = newNode;
}
public T peek()
{
if (isEmpty())
{
throw new EmptyStackException();
}
else
{
return topNode.getData();
}
}
public T pop()
{
T top = peek(); // Might throw EmptyStackException
assert (topNode != null);
topNode = topNode.getNextNode();
return top;
}
/**
* Sees whether this bag is empty.
* @return True if this bag is empty, or false if not.
*/
public boolean isEmpty()
{
return topNode == null;
}
/**
* Removes all entries from this bag.
*/
public void clear()
{
topNode = null;
}
}
Explanation / Answer
public static boolean palindrome(String str) {
int len = str.length();
//push half of the characters in the string to the stack
for(int i=0; i<len/2; i++) {
push(str.charAt(i));
}
// now compare the top of the stack with the remaining characters
for(int i=len/2; i<len; i++) {
// return false if they are not equal
if(str.charAt(i) != pop())
return false;
}
return true;
}
public static boolean equals01(String str) {
int len = str.length();
for(int i=0; i<len; i++) {
//push 1 into the stack
if(str.cha(i) == '1')
push(str.charAt(i));
}
for(int i=0; i<len; i++) {
//pop 0 from the stack
if(str.cha(i) == '0')
pop(str.charAt(i));
}
// if the stack is empty, then the string has equal number of 0 and 1
if(isEmpty())
return true;
return false;
}
public static <T> void display(LinkedStack<T> ls) {
LinkedStack<T> temp = new LinkedStack<T>();
while(!ls.isEmpty()) {
T chr = ls.pop();
temp.push(chr);
System.out.println(chr + ", ");
}
ls = temp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.