Member Avatar for wakz_1

I have created a linked list and I wish for the user to enter a Station and then the output is the number stored for that station.

LinkedList myList = new LinkedList();



            myList.addFirst("London", 5);            
      myList.addNode("Manchester ", 10);

      myList.addNode("Liverpool", 20);
      myList .addNode("Birmingham", 50);

This is the input for the user to enter.

          String name;              
              name = JOptionPane.showInputDialog("Enter Station: ");


   StringNode  temp;

       temp = mylist.head;

       if (temp.Station == (name)) {


           System.out.println("Yes");


       }

For example if the user inputs London it outputs 5 and if they choose london AND Manchester it outputs 15.

The rest of the methods are just adding a new data and printing.

Thanks

The simplest way is to use the list.listIterator(start_pos) function to get a ListIterator instance. Using that you would walk the list using iterator.next() to walk the list. Also, you need to create a class/structure that has two elements (name and number) to insert into the list since your method of addFirst(name, number) and addNode(name, number) is invalid. The LinkedList::addFirst and add methods take a single entity to add to the list. There IS no 'addNode' method. :-)

Anyway, here is a link to the Java6 LinkedList documentation: http://docs.oracle.com/javase/6/docs/api/index.html?java/util/LinkedList.html

One final note: LinkedList is a template class. You have to declare it as such:

class MyClass {
public:
    string m_name;
    int    m_number;

    MyClass(string name, int number)
    {
        m_name = name;
        m_number = number;
    }
};
.
.
.
LinkedList<MyClass> myList = new LinkedList<MyClass>();
myList.addFirst(new MyClass("London", 5));
myList.add(new MyClass("Manchester", 10));
.
.
.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.