Java Programming: Doubly Linked List?
-
I need to fill in the Delete, Retrieve and Empty methods. 1) Delete method - takes no parameter, it removes the top double from the stack. If the stack is empty, it throws the exception EmptyException 2) Retrieve method - takes no parameter, it returns a double. This should be the double on the top of the stack. If the stack is empty, it throws the exception EmptyException 3) Empty - takes no parameters, it returns true if the stack is empty, it returns false if it is not empty public class DStack { public static class EmptyException extends Exception { private static final long serialVersionUID=0; }; public DStack() { // Set up the dummy node. Head = new node(); Head.Next = Head; Head.Previous = Head; }; // Put an item onto the stack public void Store(double Item) { // Store on the top of the stack, that is // insert it at the front of the doubly linked list. node afterMe = Head; node newNode = new node(); newNode.Item = Item; newNode.Previous = afterMe; newNode.Next = afterMe.Next; newNode.Previous.Next = newNode; newNode.Next.Previous = newNode; }; // Retreive a copy of the top item on the stack // Throws EmptyException if the stack is empty public double Retrieve() throws EmptyException { // The next line is fake, delete it. // You implement the real thing return Head.Item; }; // Delete the item on the top of stack // Throws EmptyException if the stack is empty public void Delete() throws EmptyException { // Delete the top element in the stack. // You implement the real thing }; // Return true if empty // return false if not empty public boolean Empty() { // You implement the real thing return false; }; // Print the contents of the stack. public void SummarizeForward() { // go to the first node in the list node Cur = Head.Next; // Cycle until we come back to the dummy node while (Cur != Head) { System.out.print(Cur.Item + " " ); Cur = Cur.Next; }; System.out.println(""); }; public void SummarizeBackward() { // go to the dummy node node Cur = Head; // go to the last node in the list Cur = Head.Previous; // Cycle until we come back to the dummy node while (Cur != Head) { System.out.print(Cur.Item + " " ); Cur = Cur.Previous; }; System.out.println(""); }; // The node record // Can't be static if we are going to genericize it. private class node { double Item; node Next; node Previous; }; // The head of the doubly linked list, // for the stack this points to the // dummy node. private node Head; };
-
Answer:
public void Delete() throws EmptyException { if(Empty() == true) throw new EmptyException(); Head = Head.Next; if(Head != null) Head.Previous = null; }; public double Retrieve() throws EmptyException { if(Empty() == true) throw new EmptyException() Node Temp = Head; Head = Head.Next if(Head != null) Head.Previous = null; return(Temp.Item); }; public boolean Empty() { return(Head == null); // AKA. True if it is empty, false otherwise };
ben at Yahoo! Answers Visit the source
Related Q & A:
- How to start programming a game in java?Best solution by Game Development
- How to Convert a Procedural Programming into Object-Oriented Programming?Best solution by Stack Overflow
- Computer Science Java Programming?Best solution by AllExperts
- Is java or visual basic a machine level, low level, high level or binary level programming language?Best solution by Quora
- How to traverse linked list in c++?Best solution by algolist.net
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.