473,394 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Unable to insert into LinkedList

I don't know why the customer records cannot be inserted into the linked list and the head of the linked list keep pointing to null...
Expand|Select|Wrap|Line Numbers
  1. //ListNode.java
  2. public class ListNode{
  3.     private Object item;
  4.     private ListNode next,head;
  5.     public ListNode(Object newItem)
  6.     {    item = newItem;    next = null;     }
  7.     public ListNode(Object newItem, ListNode newNext)
  8.     {    item = newItem;    next = newNext;    }
  9.     public void setItem(Object newItem)
  10.     {    item = newItem;    }
  11.     public void setNext(ListNode newNext)
  12.     {    next = newNext;    }
  13.     public Object getItem()
  14.     {     return item;    }
  15.     public ListNode getNext()
  16.     {    return next;    }}
  17.  
  18. //LinkedList.java
  19. public class LinkedList{
  20.     protected ListNode head; //pointer to the first object
  21.     protected int numOfItems; //total number of objects
  22.     public LinkedList()
  23.     {    head = null;     numOfItems = 0; }
  24.     public boolean isEmptyList(){
  25.         if(numOfItems==0)  return true;
  26.         else    return false;    }
  27.     protected ListNode find(int index) {   
  28.          ListNode   cur = head;
  29.          if (index > numOfItems) return null;
  30.          for (int count=1; count<index; count++)     cur = cur.getNext();
  31.          return cur;}
  32.     public boolean insert(int at, Object newItem)
  33.     {    ListNode   pre, cur;
  34.          if (at < 1 || at > numOfItems+1) return false;
  35.             numOfItems++;
  36.          if (at == 1){
  37.             if (isEmptyList()) head = new ListNode(newItem);
  38.             else head = new ListNode(newItem, head);
  39.             return true;}
  40.         pre = find(at-1);
  41.     cur = pre.getNext();
  42.     pre.setNext(new ListNode(newItem, cur));
  43.     return true;}
  44.     public int size()
  45.     {    return numOfItems;    }
  46.  
  47. //Customer.java
  48. public class Customer{
  49.     protected int acc_id;
  50.     protected String name,address,birthdate,phone_no;
  51.     protected double balance, interest;
  52.     protected String type;
  53.     public Customer(){....} //default constructor
  54.     public Customer(int id,int name....){....}
  55.     //all the set and get methods 
  56.  
  57. //Main Program
  58. Customer[] records = new Customer[30];
  59. LinkedList custRecords = new LinkedList();
  60. for(int i=0;i<records.length;i++){ records[i] = new Customer(); }
  61. //ask user for filename
  62. //call readfile(filename,records,custRecords)
  63. public static void readfile(String filename,Customer[] record,LinkedList cust_list){
  64. int i=0;
  65. try{
  66.          File file = new File(filename);
  67.             Scanner readfile = new Scanner(file);
  68.             while(readfile.hasNext()){
  69.                   //read all inputs from file
  70.                   //e,g, record[i].setID(readfile.nextInt());
  71.                     cust_list.insert(i+1,record[i]); //Something wrong here?
  72.                   i++;
  73.              }
  74. }catch(FileNotFoundException){
  75.     System.out.println("File not found");
  76. }}
Sep 23 '07 #1
3 4189
JosAH
11,448 Expert 8TB
I don't know why the customer records cannot be inserted into the linked list and the head of the linked list keep pointing to null...
System.out.println() is a very good debugging tool. Sprinkle them all over your
code where you expect something fishy's going on and run your program again.

kind regards,

Jos
Sep 23 '07 #2
Thanx for ur help~ I solved the problem already~
Sep 23 '07 #3
JosAH
11,448 Expert 8TB
Thanx for ur help~ I solved the problem already~
Great; care to elaborate on what the mistake was? (just for the posterity).

kind regards,

Jos
Sep 23 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: J Peterman | last post by:
Im having a nightmare trying to understand these nodes and linked lists. I've posted my code for my node.h, node.cpp, linkedlist.h and linkedlist.cpp files in separates replies. Can someone...
10
by: cody | last post by:
Why isn't there a LinkedList in .NET? Was the reason that the mark&sweep GC algorithm has problems with heavily linked data? A LinkedList is very important is you have huge lists and append a...
10
by: LP | last post by:
Hi, I was asked at the tech screening what the linked list was which I answered with "academic" definition. Then a guy asked me how I would implement a linked list in C# and what would be a good...
2
by: Justin Crites | last post by:
I have an object which I want to be serializable. I have marked with with . The object only has a single data member, which is a LinkedList<int>. This linked list is a private member and cannot...
15
by: mathon | last post by:
Hi, i am currently working on creating a LinkedList, for that I using a predefined Node-class which offers a LinkedList Toolkit to manipulate the Linked List...
6
by: Phillip.Ross.Taylor | last post by:
When I designed my application I created an object called "Orderable" which exposes a public property "sequence". Then a few objects inherit from this. I'll just call them ObjectX for the sake...
1
by: sejong510 | last post by:
MSDN has an example of creating a LinkedList populated with String objects: http://msdn2.microsoft.com/en-us/library/he2s3bh7.aspx But, how would you have a LinkedList of a created class?...
1
by: CaseySimplified | last post by:
I am writing a LinkedList class from scratch without using the already defined LinkedList class. The only thing that doesn't seem to be working is adding removing and getting the last link in the...
2
by: monkey0525 | last post by:
Hi, I'm writing a program that reverses the lines of a String using the LinkedList structure, which includes an iterator. I have my code written out, but every time my program runs, it takes a very...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.