package mylinkedlist;
public class LinkedList<T>
{
LLNode<T> head;
LLNode tail;
int count;
LinkedList()
{
head = tail = null;
count = 0;
}
void addAtBeginning(T n)
{
count++;
LLNode<T> ln = new LLNode<T>(n);
if (isEmpty())
{
head = ln;
tail = ln;
}
else
{
ln.next = head;
head = ln;
}
}
boolean isEmpty()
{
return head == null;
}
void printAllItems()
{
for (LLNode<T> i = head; i != null; i = i.next)
System.out.println(i.value);
}
T front()
{
if (head == null)
return null;
else
return head.value;
}
T rear()
{
LLNode<T> prev = null;
for (LLNode<T> i = head; i != null; i = i.next)
prev = i;
if (prev == null)
return null;
else
return prev.value;
}
int size()
{
// int count = 0;
// for (LLNode i = head; i != null; i = i.next)
// count++;
return count;
}
void addAtEnd(T n)
{
// LLNode prev = null;
// for (LLNode i = head; i != null; i = i.next)
// prev = i;
//
// if (prev == null)
// ;
// else
// {
// prev.next = new LLNode(n);
// count++;
// }
count++;
LLNode ln = new LLNode<T>(n);
if (isEmpty())
{
head = ln;
tail = ln;
}
else
{
tail.next = ln;
tail = ln;
}
}
}
Thursday, July 15, 2010
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment