OrderedLikedList Class - please help me!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: OrderedLikedList Class - please help me!

  1. #1
    Join Date
    Sep 2006
    Posts
    1

    OrderedLikedList Class - please help me!

    Hello, I'm new to these forums. I have an assignment due soon and I'm not sure how to make it work. Basically, I had to create three classes - Person, Node, and OrderedLinkedList. I have to create a Person and make it a Node of my OrderedLinkedList. The program seems to create the person and the node and add it to my List, but then it says the list is empty. Can someone please help me? Thanks.

    Here's my code so far. Hope I explained it well enough.

    public class ProjectOne
    {
    public static void main(String[] args)
    {
    OrderedLinkedList myList = new OrderedLinkedList();

    myList.add(new Node(new Person("Dr.","Alca","Holic",'F',52)));
    myList.add(new Node(new Person("Mr.","Hugh","Jass",'M',33)));
    myList.add(new Node(new Person("Ms.","Enya","Face",'F',19)));
    myList.add(new Node(new Person("Mr.","Seymour","Butz",'M',39)));
    myList.add(new Node(new Person("Mrs.","Ivana","Tinkle",'F',22)));
    myList.add(new Node(new Person("Mrs.","Amanda","Hugginkiss",'F',40)));
    myList.add(new Node(new Person("Mr.","Lon","Drebag",'M',30)));
    myList.add(new Node(new Person("Miss","Kara","Meldrop",'F',16)));
    myList.add(new Node(new Person("Dr.","Oliver","Clothesoff",'M',61)));
    myList.add(new Node(new Person("Mr.","Jerry","Atric",'M',77)));
    myList.add(new Node(new Person("Dr.","Bea","O'Problem",'F',36)));
    myList.add(new Node(new Person("Ms.","Ann","Thropic",'F',34)));

    System.out.println(myList.getSize());
    System.out.println(myList.isEmpty());
    myList.printOut();

    System.exit(0);
    }
    }

    class Person
    {
    private String title;
    private String fName;
    private String lName;
    private char sex;
    private int age;

    public Person(String title, String fName, String lName, char sex, int age)
    {
    this.title = title;
    this.fName = fName;
    this.lName = lName;
    this.sex = sex;
    this.age = age;
    }

    public String getTitle()
    {
    return title;
    }

    public String getLastName()
    {
    return lName;
    }

    public char getSex()
    {
    return sex;
    }

    public int getAge()
    {
    return age;
    }

    public String toString()
    {
    String temp = (title + ". " + fName + " " + lName + ", " + sex +
    ", " + age);
    return temp;
    }
    }

    class Node
    {
    private Person person;
    private Node next;

    public Node(Person person)
    {
    this.person = person;
    next = null;
    }

    public Person getPerson()
    {
    return person;
    }

    public Node getNext()
    {
    return next;
    }

    public void setNext(Node next)
    {
    this.next = next;
    }

    }

    class OrderedLinkedList
    {
    private Node head;


    public OrderedLinkedList()
    {
    head = null;
    }

    public void add(Node node)
    {
    Node temp;

    if(head != null)
    {
    temp = head.getNext();
    node.setNext(temp);
    head = node;
    System.out.println("Node added.");

    }
    else if(head == null)
    {
    head = node;
    System.out.println("First node added.");
    }
    }

    public void delete(String lastName)
    {
    Node temp = head.getNext();
    Node previous = head;
    String lName;
    boolean delete = false;

    while(temp != null)
    {
    lName = temp.getPerson().getLastName();
    if(lName == lastName)
    {
    previous.setNext(temp.getNext());
    delete = true;
    }

    else
    {
    previous = temp;
    temp = temp.getNext();
    }
    }

    if(delete == false)
    System.out.println("Last name not found in database.");
    }

    public boolean isEmpty()
    {
    return (head == null);
    }

    public int getSize( )
    {
    int x = 0;
    Node temp = head.getNext();

    while(temp != null)
    {
    x++;
    temp = temp.getNext();
    }

    return x;
    }

    public void destroy()
    {
    head = null;
    }

    public void printOut()
    {
    Node temp = head.getNext();

    if(temp == null)
    System.out.println("List is empty.");

    while(temp != null)
    {
    System.out.println(temp.getPerson().toString() + "\n");
    temp = temp.getNext();
    }
    }

    public void countSex(char sex)
    {
    Node temp = head.getNext();
    char personSex;
    int total = 0;

    while(temp != null)
    {
    personSex = temp.getPerson().getSex();

    if(personSex == sex)
    {
    System.out.println(temp.getPerson().toString() + "\n");
    total++;
    }

    temp = temp.getNext();
    }

    System.out.println("Total people: " + total);
    }

    public void countTitle(String title)
    {
    Node temp = head.getNext();
    String personTitle;
    int total = 0;

    while(temp != null)
    {
    personTitle = temp.getPerson().getTitle();

    if(personTitle == title)
    {
    System.out.println(temp.getPerson().toString() + "\n");
    total++;
    }

    temp = temp.getNext();
    }

    System.out.println("Total people: " + total);
    }

    public void countAgeGreater(int age)
    {
    Node temp = head.getNext();
    int personAge;
    int total = 0;

    while(temp != null)
    {
    personAge = temp.getPerson().getAge();

    if(personAge >= age)
    {
    System.out.println(temp.getPerson().toString() + "\n");
    total++;
    }

    temp = temp.getNext();
    }

    System.out.println("Total people: " + total);
    }

    public void countAgeLess(int age)
    {
    Node temp = head.getNext();
    int personAge;
    int total = 0;

    while(temp != null)
    {
    personAge = temp.getPerson().getAge();

    if(personAge <= age)
    {
    System.out.println(temp.getPerson().toString() + "\n");
    total++;
    }

    temp = temp.getNext();
    }

    System.out.println("Total people: " + total);
    }
    }

  2. #2
    Join Date
    Oct 2006
    Posts
    4

    found error

    hey, here is what I found and it works now:

    Code:
    public void add(Node node)
    {
    Node temp;
    
    if(head != null)
    {
    temp = head.getNext();
    temp = node;
    //node.setNext(temp);
    node.setNext(head);
    head = node;
    System.out.println("Node added.");
    
    }
    else if(head == null)
    {
    head = node;
    System.out.println("First node added.");
    }
    }
    and one more thing, I think it would be a good idea to implement ur linked list as a doubly linked list so it makes it easier to search and delete items.

    Cheers

Similar Threads

  1. Objects disposed incorrectly
    By Osiris43 in forum .NET
    Replies: 1
    Last Post: 08-04-2006, 12:15 PM
  2. JDOM Classpath Help Required
    By kpandya in forum Java
    Replies: 5
    Last Post: 01-15-2006, 07:10 PM
  3. Help with class/applet
    By none_none in forum Java
    Replies: 17
    Last Post: 04-28-2005, 03:00 PM
  4. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  5. Assembly class
    By Shailesh C.Rathod in forum .NET
    Replies: 2
    Last Post: 03-13-2002, 07:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center