473,382 Members | 1,622 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,382 software developers and data experts.

LinkedList Problems!

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 list. It keeps returning null. Hope that helps! Thanks! Heres the code:

Expand|Select|Wrap|Line Numbers
  1. /**
  2. Casey Holgado
  3. Computer Science 4
  4. Period 2
  5. 02/19/08
  6.  */
  7. import java.util.*;
  8. public class LinkedList {
  9.  
  10.     public LinkedList()
  11.     {
  12.         first = null;
  13.         last = null;
  14.     }
  15.  
  16.     public Object getFirst()
  17.     {
  18.         if (first == null)
  19.             throw new NoSuchElementException();
  20.         return first.data;
  21.     }
  22.  
  23.     public Object getLast()
  24.     {
  25.         if (last == null)
  26.             throw new NoSuchElementException();
  27.         return last.data;
  28.     }
  29.  
  30.     public Object removeFirst()
  31.     {
  32.         if (first == null)
  33.             throw new NoSuchElementException();
  34.         Object element = first.data;
  35.         first = first.next;
  36.         currentSize--;
  37.         return element;
  38.     }
  39.  
  40.     public Object removeLast()
  41.     {
  42.         if (last == null)
  43.             throw new NoSuchElementException();
  44.         Object element = last.data;
  45.         last = last.previous;
  46.         currentSize--;
  47.         return element;
  48.     }
  49.  
  50.     public void addFirst(Object element)
  51.     {
  52.         Node newNode = new Node();
  53.         newNode.data = element;
  54.         newNode.next = first;
  55.         first = newNode;
  56.         currentSize++;
  57.     }
  58.  
  59.     public void addLast(Object element)
  60.     {
  61.         Node newNode = new Node();
  62.         newNode.data = element;
  63.         newNode.previous = last; 
  64.         last = newNode;
  65.         currentSize++;
  66.     }
  67.  
  68.     public int currentSize() 
  69.     {
  70.         return currentSize;
  71.     }
  72.  
  73.     public ListIterator listIterator()
  74.     {
  75.         return new LinkedListIterator();
  76.     }
  77.  
  78.     private Node last;
  79.     private Node first;
  80.     private int currentSize = 0;
  81.  
  82.     private class Node
  83.     {
  84.         public Object data;
  85.         public Node next;
  86.         public Node previous;
  87.     }
  88.  
  89.     private class LinkedListIterator implements ListIterator
  90.     {
  91.         public LinkedListIterator()
  92.         {
  93.             position = null;
  94.             previous = null;
  95.             next = null;
  96.         }
  97.  
  98.         public Object next()
  99.         {
  100.             if (!hasNext())
  101.                 throw new NoSuchElementException();
  102.             previous = position;
  103.  
  104.             if (position == null)
  105.                 position = first;
  106.             else
  107.                 position = position.next;
  108.  
  109.             return position.data;
  110.         }
  111.  
  112.         public Object previous()
  113.         {
  114.             if (!hasBefore())
  115.                 throw new NoSuchElementException();
  116.             next = position;
  117.  
  118.             if (position == null)
  119.                 position = last;
  120.             else
  121.                 position = position.previous;
  122.  
  123.             return position.data;
  124.         }
  125.  
  126.         public boolean hasNext()
  127.         {
  128.             if (position == null)
  129.                 return first != null;
  130.             else
  131.                 return position.next != null;
  132.         }
  133.  
  134.         public boolean hasBefore()
  135.         {
  136.             if (position == null)
  137.                 return last != null;
  138.             else
  139.                 return position.previous != null;
  140.         }
  141.  
  142.         public void add(Object element)
  143.         {
  144.             if (position == null)
  145.             {
  146.                 addFirst(element);
  147.                 position = first;
  148.  
  149.             }
  150.             else
  151.             {
  152.                 Node newNode = new Node();
  153.                 newNode.data = element;
  154.                 newNode.next = position.next;
  155.                 position.next = newNode;
  156.                 position = newNode;
  157.                 currentSize++;
  158.             }
  159.             previous = position;
  160.         }
  161.  
  162.         public void remove()
  163.         {
  164.             if (previous == position)
  165.                 throw new IllegalStateException();
  166.  
  167.             if (position == first)
  168.             {
  169.                 removeFirst();
  170.             }
  171.             else
  172.             {
  173.                 previous.next = position.next;
  174.                 currentSize--;
  175.             }
  176.             position = previous;
  177.         }
  178.  
  179.         public void set(Object element)
  180.         {
  181.             if (position == null)
  182.                 throw new NoSuchElementException();
  183.             position.data = element;
  184.         }
  185.  
  186.         private Node next;
  187.         private Node position;
  188.         private Node previous;
  189.     }
  190.  
  191.         public static void main() 
  192.     {
  193.         LinkedList list = new LinkedList();
  194.         ListIterator itr = list.listIterator();
  195.  
  196.         itr.add("second");
  197.         itr.add("third");
  198.         itr.add("fourth");
  199.         list.addFirst("first");
  200.         list.addLast("fifth");
  201.  
  202.         System.out.println("currentSize: " + list.currentSize());
  203.         System.out.println("getFirst: " + list.getFirst());
  204.         System.out.println("getLast: " + list.getLast());
  205.         System.out.println("removeFirst: " + list.removeFirst());
  206.         System.out.println("removeLast: " + list.removeLast());
  207.         System.out.println("getCurrentSize: " + list.currentSize());
  208.         System.out.println("getFirst: " + list.getFirst());
  209.         System.out.println("getLast: " + list.getLast());
  210.     }
  211. }





Expand|Select|Wrap|Line Numbers
  1. /**
  2. Casey Holgado
  3. Computer Science 4
  4. Period 2
  5. 02/19/08
  6.  */
  7. public interface ListIterator
  8. {
  9.     Object next();
  10.     Object previous();
  11.     boolean hasNext();
  12.     boolean hasBefore();
  13.     void add(Object element);
  14.     void remove();
  15.     void set(Object element);
  16. }
Feb 22 '08 #1
1 1938
Ganon11
3,652 Expert 2GB
In addFirst, when you add the first element, first correctly points to that element. What does last point to? Since you never use addLast, last never changes, and when you call removeLast, it still points to its original value. You need a way to set last in addFirst, and first in addLast, if appropriate.
Feb 22 '08 #2

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...
1
by: Spockie | last post by:
I do not use linkedlist stdlibrary, vector i make my own, but i have problems with one issue, and that is checking if there are duplicates in the middle of inputing information line 294 ...
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...
3
by: huiling25 | last post by:
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... //ListNode.java public class ListNode{ private Object...
5
by: Alberto Bencivenni | last post by:
Hi All, Is there a reason why it is not possible to serialize a geenric LinkedList<Tobject containing a custom class marke serializable? Thanks, Alberto
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.