Important alert: (current site time 7/15/2013 3:03:44 PM EDT)
 

VB icon

Advanced Linked List class

Email
Submitted on: 4/18/2002 3:48:03 PM
By: Martin Dahl 
Level: Intermediate
User Rating: By 3 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 23243
(About the author)
 
     Here is my version of the Linked List data structure with allmost the same methods as the one found in java.util. (I know there is no comments in the code but its pretty easy to understand anyway) Enjoy!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Advanced Linked List class
// Description:Here is my version of the Linked List data structure with allmost the same methods as the one found in java.util. (I know there is no comments in the code but its pretty easy to understand anyway) Enjoy!
// By: Martin Dahl
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2817&lngWId=2//for details.//**************************************

class MyLinkedList
{
 private int count;
 ListNode header;
 ListNode temp;
 public MyLinkedList()
 {
 header = new ListNode(null);
	count = 0;
 }
 public boolean isEmpty()
 {
 return header.next == null;
 }
 public void makeEmpty()
 {
 count = 0;
 header.next = null;
 }
 public void add(Object o)
 {
 ListNode node = new ListNode(o);
 if (isEmpty()==true)
 {
 header.next = node;
	count++;
 }
 else
 {
 temp = header;
while (temp.getnext() != null)
{
	 temp = temp.getnext();
}
 temp.next = node;
 count++;
 }
 }
 public boolean add(int index, Object o)
 {
 if (index>count || index<1)
 {
return false;
 }
 else
 {
ListNode node = new ListNode(o);
count++;
temp = header;
for(int i=1; i<index; i++)
{
temp = temp.getnext();
}
node.next = temp.next;
temp.next = node;
return true;
 }
 }
 public void addFirst(Object o)
 {
 count++;
 ListNode node = new ListNode(o);
 temp = header;
 temp = temp.getnext();
 header.next = node;
 node.next = temp;
 }
 public boolean set(int index,Object o)
 {
 if (index>count || index<1)
 {
return false;
 }
 else
 {
temp = header;
for(int i=1; i<index; i++)
{
temp = temp.getnext();
}
temp.next.element = o;
return true;
 }
 }
 public Object get(int index)
 {
 if (index>count || index<1)
 {
return null;
 }
 else
 {
temp = header;
for(int i=0; i<index;i++)
{
temp = temp.getnext();
}
return temp.element;
 }
 }
 public Object getFirst()
 {
 temp = header;
 return temp.next.element;
 }
 public Object getLast()
 {
 temp = header;
 while (temp.getnext() != null)
 {
temp = temp.getnext();
 }
 return temp.element;
 }
 public int size()
 {
 return count;
 }
 public Object [] toArray()
 {
 Object [] ret = new Object[count];
 temp = header;
 for (int i=0; i<count; i++)
 {
 ret[i] = temp.next.element;
 temp = temp.getnext();
 }
 return ret;
 }
 public boolean remove(int index)
 {
 if (index>count || index<1)
 {
return false;
 }
 else
 {
count--;
temp = header;
for(int i=1; i<index; i++)
{
temp = temp.getnext();
}
temp.next = temp.next.next;
return true;
 }
 }
 public boolean removeFirst()
 {
 if (isEmpty()==false)
 {
count--;
temp = header;
header.next = temp.next.next;
return true;
 }
 else
 {
return false;
 }
 }
 public boolean removeLast()
 {
 if (isEmpty()==false)
 {
count--;
temp = header;
while (temp.next.getnext() != null)
{
temp = temp.getnext();
}
temp.next = null;
return true;
 }
 else
 {
return false;
 }
 }
}
class ListNode
{
 Object element;
 ListNode next;
 ListNode(Object theElement)
 {
 this(theElement,null);
 }
 ListNode(Object theElement,ListNode n)
 {
 element = theElement;
 next = n;
 }
 public ListNode getnext()
 {
 return this.next;
 }
}


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/11/2002 9:17:49 AMAdam Coulter

Great work!
(If this comment was disrespectful, please report it.)

 
12/28/2002 6:48:22 AM

Nice work and good program
but you have 4 error and all error in the same thinges and this error is in line 89 & 126 & 151 & 217 in word"Index" it must be "index"
(If this comment was disrespectful, please report it.)

 
2/15/2007 8:29:00 AMAhmad

It is very good code and i interested in this code , thanks a lot
(If this comment was disrespectful, please report it.)

 
4/23/2007 3:09:57 AMpavani

can u give me the same type of code for arraylist
(If this comment was disrespectful, please report it.)

 
11/30/2009 8:40:37 PMlerry

good code,thx
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.