- #1
XodoX
- 203
- 0
I'm trying a practice problem from the book. It's 10 books with title, ISBN, and year. The input needs to be sorted by year and it needs to let you search for a book title.
Would this work as basic frame? I do not need the tostring here, though, do I? I would do a loop to search for a title. And I'm not sure about how to sort it by year.
If not, I would appreciate if someone could show me where to find a basic frame for this. Then I could look at it and understand it, and then I could add the other things. I feel a little stupid because I don't seem to get it.
Code:
class LinkedStack
{
/**
The Node class is used to implement the
linked list.
*/
private class Node
{
String value;
Node next;
Node(String val, Node n)
{
value = val;
next = n;
}
}
private Node top = null; // Top of the stack
/**
The empty method checks for an empty stack.
@return true if stack is empty, false otherwise.
*/
public boolean empty()
{
return top == null;
}
/**
The push method adds a new item to the stack.
@param s The item to be pushed onto the stack.
*/
public void push(String s)
{
top = new Node(s, top);
}
/**
The toString method computes a string
representation of the contents of the stack.
@return The string representation of the
stack contents.
*/
public String toString()
{
StringBuilder sBuilder = new StringBuilder();
Node p = top;
while (p != null)
{
sBuilder.append(p.value);
p = p.next;
if (p != null)
sBuilder.append("\n");
}
return sBuilder.toString();
}
}
public class LinkedStack
{
public static void main(String [ ] args)
{
LinkedStack st = new LinkedStack();
System.out.println("Contents of Stack:");
st.push("Amy");
st.push("Bob");
st.push("Chuck");
System.out.println(st);
}
}
Would this work as basic frame? I do not need the tostring here, though, do I? I would do a loop to search for a title. And I'm not sure about how to sort it by year.
If not, I would appreciate if someone could show me where to find a basic frame for this. Then I could look at it and understand it, and then I could add the other things. I feel a little stupid because I don't seem to get it.