3 Replies - 703 Views - Last Post: 09 September 2010 - 07:34 PM Rate Topic: -----

#1 jhar131   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 149
  • Joined: 03-February 10

Accessing an Element from a protected node

Posted 09 September 2010 - 10:19 AM

Hi all,

I have a question regarding accessing a node. I need to read from a file, load it into a singly linked list, and several other functions. I know that my program is loading correctly; because when I print from my LinkedList class, the data is true. However, for the final program I want to print from my main, and I cannot figure out how to access it. Any tips would be greatly appreciated.

My main:
import java.util.Scanner;

//import all file processing methods
import java.io.*;

public class prog2
{
   public static void main(String[] args) throws IOException
   {
          Employee data; //an array of records of class Employee

          int  choice,     //user menu selection
               ID,         //employee ID number
               age,        //employee age
               option = 0; //declartion of user option

          String first,    //student's first name
                 last,     //student's last name
                 name,     //array of complete names
                 phone;     //the student's classification


          LinkedList full = new LinkedList();
          Node current;

          //create a LinkedList object at work
          LinkedList work = new LinkedList();

          //create a scanner object for file input
          File input = new File("empdb.data");
          Scanner emp = new Scanner(input);

          //populate arrays with file info
          while(emp.hasNext())
          {
            ID = emp.nextInt();
            name = emp.next();
            phone = emp.next();
            age = emp.nextInt();
            data = new Employee(ID,name,phone,age);
            work.Fillup(data,full);
          }

          //close the input file
          emp.close();
         
     }

}



And my LinkedList class:
public class LinkedList
{

  public class Node
  {
       Employee info;
       Node next;


     Node(Employee data, Node after)
     {
        info = data;
        next = after;
     }

     Node(Employee data)
     {
       this(data, null);
     }

   }

  private Node first;
  private Node last;

  public LinkedList()
  {
     first = null;
     last = null;
  }

  public boolean noMem()
  {
    return first == null;
  }

  public int listSize()
  {
    int count = 0;
    Node p = first;

    while (p != null)
    {
      count ++;
      p = p.next;
    }
    return count;
  }


  public LinkedList Fillup(Employee data,LinkedList full)
  {
     if (noMem())
     {
       first = new Node(data);
       last = first;
     }
     else
     {
       last.next = new Node(data);
       last = last.next;
     }

    return full;
  }


  public void Fillup(int pos, Employee data)
  {
    if(pos == 0)
    {
      first = new Node(data,first);
      if (last == null)
         last = first;
      return;
    }

   Node previous = first;

   for (int I = 1; I <= pos - 1; I++)
   {
     previous = previous.next;
   }

   previous.next = new Node(data, previous.next);

   if (previous.next.next == null)
        last = previous.next;

 }

 public Employee Takeout(int pos)
 {
   Employee details = new Employee();

   if (pos == 0)
   {
      details = first.info;

      first = first.next;

      if (first == null)
         last = null;
   }
   else
   {
     Node previous = first;

     for (int I = 1; I <= pos -1; I++)
        previous = previous.next;

      details = previous.next.info;

      previous.next = previous.next.next;

      if (previous.next == null)
          last = previous;

    }

    return details;

  }

  public boolean Takeout(Employee details)
  {
    if (noMem())
      return false;

    if (details == first.info)
    {
       first = first.next;

       if(first == null)
          last = null;

       return true;
     }

     Node previous = first;

     while (previous.next != null)
       while(previous.next.info != details)
        {
          previous = previous.next;
        }

        if(previous.next == null)
           return false;
       
        previous.next = previous.next.next;

        if (previous.next == null)
           last = previous;

        return true;

   }

}



I obviously also have another class file, but all it does is construct the data into a single object and pass it to the LinkedList. Anyway, thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing an Element from a protected node

#2 Nasm   User is offline

  • New D.I.C Head

Reputation: 27
  • View blog
  • Posts: 47
  • Joined: 10-June 10

Re: Accessing an Element from a protected node

Posted 09 September 2010 - 11:24 AM

Well if you simply want to print it, perhaps the toString method would be good which is used when printing an object;
@Override
public void toString(){
if(start == null) return "Empty";
StringBuilder sb = new StringBuilder();
for(Node n = start;n != null;n = n.next()){
sb.append(n.getEmployer());
}
return sb.toString();
}



Or perhaps an iterator that would allow you to access each element in turn:
public Iterator iterator(){
return new ListIterator();
}
private class ListIterator implements Iterator{
private node;
public ListIterator(){
node = start;
}
@Override
public boolean hasNext() {
return node != null;
}

@Override
public Object next() {
Node t = node;
node = node.next();
return t;
}

@Override
public void remove() {
 LinkedList.remove(node);		
}

}



And your Node class really should be private.
Was This Post Helpful? 1
  • +
  • -

#3 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12355
  • View blog
  • Posts: 45,464
  • Joined: 27-December 08

Re: Accessing an Element from a protected node

Posted 09 September 2010 - 02:52 PM

You might also want to check out my Linked List Tutorial.
Was This Post Helpful? 1
  • +
  • -

#4 jhar131   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 149
  • Joined: 03-February 10

Re: Accessing an Element from a protected node

Posted 09 September 2010 - 07:34 PM

Guys, sorry for my late response. I haven't checked the thread in a while because I figured out how to create the iterator. I really appreciate though. Thanks for your time.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1