Results 1 to 8 of 8
Thread: LinkedList problem
- 02-16-2009, 01:59 AM #1
Member
- Join Date
- Dec 2008
- Location
- Davao Oriental
- Posts
- 29
- Rep Power
- 0
LinkedList problem
Hi ..
this code is correct:
public class LinkedList
{
// reference to the head node.
private Node head;
private int listCount;
// LinkedList constructor
public LinkedList()
{
// this is an empty list, so the reference to the head node
// is set to a new node with no data
head = new Node(null);
listCount = 0;
}
public void add(Object data)
// post: appends the specified element to the end of this list.
{
Node temp = new Node(data);
Node current = head;
// starting at the head node, crawl to the end of the list
while(current.getNext() != null)
{
current = current.getNext();
}
// the last node's "next" reference set to our new node
current.setNext(temp);
listCount++;// increment the number of elements variable
}
public void add(Object data, int index)
// post: inserts the specified element at the specified position in this list.
{
Node temp = new Node(data);
Node current = head;
// crawl to the requested index or the last element in the list,
// whichever comes first
for(int i = 1; i < index && current.getNext() != null; i++)
{
current = current.getNext();
}
// set the new node's next-node reference to this node's next-node reference
temp.setNext(current.getNext());
// now set this node's next-node reference to the new node
current.setNext(temp);
listCount++;// increment the number of elements variable
}
public Object get(int index)
// post: returns the element at the specified position in this list.
{
// index must be 1 or higher
if(index <= 0)
return null;
Node current = head.getNext();
for(int i = 1; i < index; i++)
{
if(current.getNext() == null)
return null;
current = current.getNext();
}
return current.getData();
}
public boolean remove(int index)
// post: removes the element at the specified position in this list.
{
// if the index is out of range, exit
if(index < 1 || index > size())
return false;
Node current = head;
for(int i = 1; i < index; i++)
{
if(current.getNext() == null)
return false;
current = current.getNext();
}
current.setNext(current.getNext().getNext());
listCount--; // decrement the number of elements variable
return true;
}
public int size()
// post: returns the number of elements in this list.
{
return listCount;
}
public String toString()
{
Node current = head.getNext();
String output = "";
while(current != null)
{
output += "[" + current.getData().toString() + "]";
current = current.getNext();
}
return output;
}
private class Node
{
// reference to the next node in the chain,
// or null if there isn't one.
Node next;
// data carried by this node.
// could be of any type you need.
Object data;
// Node constructor
public Node(Object _data)
{
next = null;
data = _data;
}
// another Node constructor if we want to
// specify the node to point to.
public Node(Object _data, Node _next)
{
next = _next;
data = _data;
}
// these methods should be self-explanatory
public Object getData()
{
return data;
}
public void setData(Object _data)
{
data = _data;
}
public Node getNext()
{
return next;
}
public void setNext(Node _next)
{
next = _next;
}
}
}
However, my main method is my problem. this code,it will ask the user data.
import javax.swing.JOptionPane;
public class List {
public static void main(String []args) {
LinkedList list=new LinkedList();
char ans='y';
int choice;
Object data;
Object next;
System.out.println("\t\tWhat do you want to do?");
System.out.print("\n");
System.out.println("1.Add data at the end of list");
System.out.println("2.Add data at specific index");
System.out.println("3.Remove data from list");
System.out.println("4. Print the list");
while(ans=='y') {
choice=Integer.parseInt(JOptionPane.showInputDialo g("Enter your choice"));
if(choice==1) {
data=(JOptionPane.showInputDialog("Enter your data"));
list.add(data);
System.out.println(list);
}else
if(choice==2){
data=(JOptionPane.showInputDialog("Enter your data"));
int index=Integer.parseInt(JOptionPane.showInputDialog ("Enter # of index"));
list.add(data, index);
System.out.println(list);
}else
if(choice==3) {
int index=Integer.parseInt(JOptionPane.showInputDialog ("Index to be removed"));
list.remove(index);
//if(list.remove(index)) {
//System.out.println("\n\n You have successfully remove the index");
// }else{
// System.out.println("\n\n You loose the turn");
// System.out.println(list);
}
if(choice==4) {
list.size();
System.out.println(list);
}
String x=(JOptionPane.showInputDialog("Do you want to continue?"));
ans=x.charAt(0);
}
}
}
The comment is the problem because there is an error.I want to display if the element have succesfully remove otherwise it will display you loose the turn.
Thank you. Your help is very appreciated.
- 02-16-2009, 08:10 AM #2
Senior Member
- Join Date
- Dec 2008
- Posts
- 526
- Rep Power
- 0
try to use with null )choice=Integer.parseInt(JOptionPane.showInputDialo g(null,"Enter your choice"));
- 02-17-2009, 10:56 AM #3
Member
- Join Date
- Dec 2008
- Location
- Davao Oriental
- Posts
- 29
- Rep Power
- 0
"//if(list.remove(index)) {
//System.out.println("\n\n You have successfully remove the index");
// }else{
// System.out.println("\n\n You loose the turn");
// System.out.println(list);"
Actually, this part is my problem because when i remove some index the output is wrong
Example:
When the user enter 5 data:
a b c d e
If ever the user want to delete or remove some index, it will display "You have successfully remove the index" otherwise "You loose the turn"
Example:
Enter index to be remove:
2
so, it must the "b" will delete and display " you have....."
but then, the wrong is the "c" will also delete.So the output will
"a d e" that actually is not the right output it should be:
"a c d e".
- 02-17-2009, 11:03 AM #4
Member
- Join Date
- Dec 2008
- Location
- Davao Oriental
- Posts
- 29
- Rep Power
- 0
Continuation:
If it has only one remaining index ,
Example:
a - the remaining index
then,the user want to delete it so that the list now is empty.
Enter index to be remove:
1
so, "a" now will delete but then the wrong is, it will display " Ypu loose a turn". It must be " You have succesfully remove the index". What should i do?Pls help me..
- 02-17-2009, 01:36 PM #5
size?
What does list.size() do? It's not assigning a value to a variable and it's not being used.Java Code:if(choice==4) { list.size(); System.out.println(list); }
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-17-2009, 01:42 PM #6
It's working...
Remember... you are working with indexes. Indexes start with 0, therefore if you remove index 2, you are removing the third element (0.1.2) in the linked list.Example:
Enter index to be remove:
2
so, it must the "b" will delete and display " you have....."
but then, the wrong is the "c" will also delete.So the output will
"a d e" that actually is not the right output it should be:
"a c d e".
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-18-2009, 01:02 AM #7
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
if(choice==4) {
list.size();
System.out.println(list);
}
actually this code is just print the list after the user remove an index.
- 02-18-2009, 02:10 PM #8
Member
- Join Date
- Dec 2008
- Location
- Davao Oriental
- Posts
- 29
- Rep Power
- 0
Similar Threads
-
LinkedList-java
By kamali in forum New To JavaReplies: 2Last Post: 09-26-2008, 03:29 PM -
Problem with LinkedList Java
By paul in forum Advanced JavaReplies: 2Last Post: 08-07-2007, 04:57 AM -
how to use LinkedList
By fred in forum Advanced JavaReplies: 1Last Post: 07-24-2007, 01:52 AM -
Problem with LinkedList
By Eric in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks