Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a simple problom

 
Remo Fernandez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I hav a linkedlist problom.i hav tried a lot to resolve it.but i cant.
this linked list allowing new elements to be added to the front and deleted from the front.how can it be modified so that it adds elements to the end of the list rather than the front of the list?.
that means,
when i run this program,if i input 3 Strings as "aa","bb","cc",the current output will be,
cc
aa
bb
i want to change the program so it prints,
aa
bb
cc
it can be done by adding elements to the end of the list rather than the front of the list.anyone can do it??.here's the hint.
HINT one way is to store a Reference to the last element of the list and use it to access the end of the list. The other way is to traverse from the front to the last node in a similar way to traverse, then once there create a new node and put a reference to it from the last node. You will also need to be careful about the case when you are adding to an empty list.

 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the code for the output u wanted.
import javax.swing.*;
import java.util.*;
import java.io.*;
class Node
{
private String message;
private Node next;
Node(String m, Node n)
{
message = m;
next = n;
}
String getMessage()
{
return message;
}
Node getNext()
{
return next;
}
}
class LinkedList
{
private Node start;
LinkedList()
{
start = null;
}
Node getStart()
{
return start;
}
void addLink (String word)
{
Node n;
n = new Node(word,start);
start=n;
}
public void removeLink ()
{
if (start == null)
System.out.println("Cannot remove from an empty list");
else
{
System.out.println("Removing " + start.getMessage());
start = start.getNext();
}
}
public void printList ()
{
Node current = start;
System.out.println(current.getMessage() + " ");
current = current.getNext();
}
}
public class LinkedListTest
{
public static void main(String [] args) throws IOException
{
LinkedList l = new LinkedList ();
for (int i = 0; i < 3; i++)
{
String word = JOptionPane.showInputDialog("Next word: ");
if(i==0)
System.out.println("...traversing the list to see what is in it...");
l.addLink(word);
l.printList();
}
System.exit(0);
}
}

I think this is the only possible way to get output as u expected. Moreover if u want to print the output after giving entire input, use "Vector".
Also the way the program executes is correct. The thing is the way u r priting ur output is wrong. If u want how ur program executes, send ur e-mail. So that I'll send u the information regarding execution in detail.
 
Remo Fernandez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my email is
remofernandez9090@yahoo.com.
i wanna add new records at the end of the list ..now adding in the front.can u bring me a solution that does'nt have any changes in printList() method then prints the list as
aa
bb
cc
?
it can possible.look at the hint.can u solve this??.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can't you just create a new
private Node end, like your start, and add logic for it? when you add a node to the end of the list, you point your current end node to the newly created one, then move where end points to...
i'd probably change the constructor of Node to create a node that points to nothing. then make a "addFront" method and a "addRear" method.

you'll probably need some setters and getters you may not have, but this shouldn't be hard.
 
Remo Fernandez
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred,
Do u write full code by using addRear method?.
it's becoming very confusing for me.i hav tried in various ways but just i dont getting the logic.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not going to write the code FOR you, if that's what you're asking. Why don't you post what you've written/tried, and ask specific questions about where you're stuck.
To be honest, this sounds like a class assignment, and i'm really not one to do homework for other people. You'll find that a pretty commonly held belief around here.
 
Naren Chivukula
Ranch Hand
Posts: 577
Java Notepad Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Remo,
As Fred said, anybody don't dedicate their time on somebody's assignment. In any forum you can only expect closed ended solutions but not open ended solutions. Anyway as I'm interested in your topic, I gave you the information. But, I'll try to find the solution for your problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic