Circular Linked List---Need Help ASAP (before midnight)
I really need help with the code of a Circular Linked List...it seems I suck at writing code. It has errors gallore and I would appreciate any help I could get. Unfortunately it needs to be done by midnight tonight so...PLEASE HELP ME!!!!!!! Here is what I got so far. If you can help me in time I will forever be your best friend! :-bd
Code Java:
import java.util.Scanner;
public interface CircularListADT<T> {
public int count;
public Node top;
public void insertNode(T value){
Node newNode = new Node();
top.setNext(newNode);
top = newNode;
}
public boolean deleteNode(T value){
top = top.getNext();
}
public Node getCurrentNode(){
return top;
}
public Node forward(){
Node<T> current = top;
current = top.getNext();
return current;
}
/**
* Move the current node reference backward to its previous node,
* and return the updated current node reference
* @return
*/
public Node backword(){
}
/**
* Find if the list hold a node with its element being
* the passed value
* @param value
* @return null if no such node exists, or the node
* reference that points to the object with the same element
*/
public Node search(T value){
}
public int size(){
int count = 0;
Node<T> t = top;
while(t!=top){
count++;
t=t.getNext();
}
if(t.getNext()==top){
break;
}
return count;
}
public String toString(){
String result = "";
Node<T> current = top;
while (current != null) {
result = result + (current.getElement()).toString() + "\n";
current = current.getNext();
}
return result;
}
}
public class Node<T> {
private Node<T> next;
private T element;
public Node() {
next = null;
element = null;
}
public Node(T elem) {
next = null;
element = elem;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> node) {
next = node;
}
public T getElement() {
return element;
}
public void setElement(T elem) {
element = elem;
}
public String toString(){
return ""+element + " with the next node of "+next.getElement();
}
}
public class CircularListDriver {
public static void main(String[]args){
String operation = "none";
Scanner scan = new Scanner(System.in);
System.out.println("Select an option: ");
operation = scan.next();
if(operation.equalsIgnoreCase("insert"))
insertNode();
else if(operation.equalsIgnoreCase("delete"))
deleteNode();
else if(operation.equalsIgnoreCase("current node"))
getCurrentNode();
else if(operation.equalsIgnoreCase("forward"))
forward();
else if(operation.equalsIgnoreCase("backword"))
backward();
else if(operation.equalsIgnoreCase("search"))
search();
else if(operation.equalsIgnoreCase("size"))
size();
}
}
Re: Circular Linked List---Need Help ASAP (before midnight)
Wrap your code in highlight tags as seen in my signature and post the exact issues and exception messages your having.
Nobody wants to have to compile and run your code in order to discover the issues.
Re: Circular Linked List---Need Help ASAP (before midnight)
Sorry...I am new to this. Here it is wrapped in highlight tags. Please Help...I beg of thee.
Code Java:
import java.util.Scanner;
public interface CircularListADT<T> {
public int count;
public Node top;
public void insertNode(T value){
Node newNode = new Node();
top.setNext(newNode);
top = newNode;
}
public boolean deleteNode(T value){
top = top.getNext();
}
public Node getCurrentNode(){
return top;
}
public Node forward(){
Node<T> current = top;
current = top.getNext();
return current;
}
/**
* Move the current node reference backward to its previous node,
* and return the updated current node reference
* @return
*/
public Node backword(){
}
/**
* Find if the list hold a node with its element being
* the passed value
* @param value
* @return null if no such node exists, or the node
* reference that points to the object with the same element
*/
public Node search(T value){
}
public int size(){
int count = 0;
Node<T> t = top;
while(t!=top){
count++;
t=t.getNext();
}
if(t.getNext()==top){
break;
}
return count;
}
public String toString(){
String result = "";
Node<T> current = top;
while (current != null) {
result = result + (current.getElement()).toString() + "\n";
current = current.getNext();
}
return result;
}
}
public class Node<T> {
private Node<T> next;
private T element;
public Node() {
next = null;
element = null;
}
public Node(T elem) {
next = null;
element = elem;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> node) {
next = node;
}
public T getElement() {
return element;
}
public void setElement(T elem) {
element = elem;
}
public String toString(){
return ""+element + " with the next node of "+next.getElement();
}
}
public class CircularListDriver {
public static void main(String[]args){
String operation = "none";
Scanner scan = new Scanner(System.in);
System.out.println("Select an option: ");
operation = scan.next();
if(operation.equalsIgnoreCase("insert"))
insertNode();
else if(operation.equalsIgnoreCase("delete"))
deleteNode();
else if(operation.equalsIgnoreCase("current node"))
getCurrentNode();
else if(operation.equalsIgnoreCase("forward"))
forward();
else if(operation.equalsIgnoreCase("backword"))
backward();
else if(operation.equalsIgnoreCase("search"))
search();
else if(operation.equalsIgnoreCase("size"))
size();
}
}
Re: Circular Linked List---Need Help ASAP (before midnight)
The issues I am having are that the Method head names are saying that they do not specify a body. I am also having trouble figuring out how to write the code for the backword method and the search method, the descriptions are in the comments. I am also having trouble with allowing the user to select the methods in the main method. I am not sure exactly what I am doing wrong and I would REALLY appreciate any help I could get.
Re: Circular Linked List---Need Help ASAP (before midnight)
First, the code posted is still a bit hard to read with the lack of indenting.
Second, you've got quite a few questions associated with this, and its hard to direct all of them. That being said, there are some glaring errors that indicate you should research some fundamentals. For example, an interface is defined as containing methods which are not implemented. As CircularListADT is defined as an interface you can define CircularListADT with non-implemented methods, or change its definition to a class ( see What Is an Interface? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts) ). Another example: in main you attempt to call insertNode without parameters, but that method is defined to take a parameter (see Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects) ).
Lastly, for future reference, urgency is relative. You may have priorities, but so do all other contributors to the forums - thus marking threads as ASAP could be interpreted as disrespectful to others asking questions and those providing trying to provide help.
Re: Circular Linked List---Need Help ASAP (before midnight)
Sorry...I did not mean for it to be disrespectful. I will mark it as solved because I do not know how to close the thread. Sorry for any trouble I might have caused.