1

I've been asked to create and manage a link list from scratch without the use of the java util.

If I were to create an object which has more then one attribute e.g. name & age then would it be possible to store the object within the link list?

I'm having a hard time trying to get my head around this and would appreciate any help!

Here's my pseudo code:

Classes: Node LList Person Address

add_person
sout "Enter name"
scan.next(String name)
pass name to setName (a Person class function)
sout "Enter postcode"
scan.next(String postCode)
pass postCode to setPostCode (a Address class function)

How would I then go about linking these two bits of information together within the same link list?

Edit: Thanks for the input guys, I'll have a good read about based upon your recommendations! Once again many thanks! :)

silverzx
  • 1,209
  • 4
  • 14
  • 18

4 Answers4

1

Try looking up what a linked list is and how it needs to be constructed. Your psuedo code has nothing to do with a linked list, only some rudimentary data entry. I suggest you look over the following link to understand what it is and how it works. The actual coding is fairly simple once you understand the structure.

I encourage others to not do your homework for you.

Wikipedia

Robin
  • 24,062
  • 5
  • 49
  • 58
0

You should write your own LinkedList that uses generics; let it handle any object type.

You name and post code and age and whatnot ought to be encapsulated in an object that you'd be able to add to the LinkedList.

package linkedlist;

public class Node<T> {
    private Node<T> prev;
    private Node<T> next; 
    private T value;
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
0

Its not too tough, you just need to create your own Node class. This class might look something like this:

public class Node{
    protected String name;
    protected int age;
    //any additional data you need...
    protected Node next;

    //methods...

This class would contain many data fields and would provide methods to interact with these fields. The key component is the "protected Node next;" line, which is the next node in the linked list. All nodes in the list would have a next node, except for the tail. The tail node would set next equal to null.

theck01
  • 193
  • 7
0

First you need to define a basic building bloc of a linked list which is Node. Nodes are like containers that store whatever you want. That's why storedData variable is of type Object. You would define it like this:

public class MyNode{
Object storedData; // this is a reference to the object that you want stored in the list
MyNode next; //this is a reference to the next node in your list
...
}

Then you can define your linked list class, which would go like this:

public class MyLinkedList{
 MyNode head; //this is a reference to the top element of your list
 int nodeCount // 
 //put all the requkired methods here
}
Mr1159pm
  • 774
  • 1
  • 10
  • 21