Singly Linked List Implementation

For this implementation of a linked list in Java, I created my own class for a node called SLNode since a different node is needed for a singly and a doubly.

Here is the SLNode class:
class SLNode
{
public Object data;
public SLNode next;
public SLNode(Object d)
{
data=d;
next=null;
}
}

The next code is the class for the singly linked list called the SLinkedList

The attributes:
SLNode head;
SLNode tail;
We only need two attribute since a singlylinked list only has a head and a tail

The constructor:
public SLinkedList()
{
head=null;
tail=null;
}

It all attributes are set to null since from the beggining, the list is empty.

3 methods for adding data. Why 3? It is because there are three situations to take note when adding data within a singly linked list namely: when adding at the beginning, at the middle (1<1

public void addFirst(SLNode newNode) //add at the beginning
{
newNode.next=head;
head=newNode;
}
public void addLast(SLNode newNode) //add at the end of the list
{
tail.next=newNode;
tail=newNode;
}
public void add(SLNode newNode, int pos) //add at a specified position
{
SLNode current = head;
SLNode previous = head;
int ctr=0;
if(head!=null && tail!=null) //if list is not empty
{
while(current != null)
{
ctr++;
if(ctr == pos)
{
if(ctr==1)
addFirst(newNode);
else
{
newNode.next=current;
previous.next=newNode;
}
return;
}
previous = current;
current = current.next;
}
}
else //if list is empty
{
System.out.println("The list is empty. The node was added as the first and the last node.");
addFirst(newNode);
tail=head;
return;
}
addLast(newNode);
if(pos > ctr+1)
System.out.println("The desired position is still unavailable."
+ "\nThe node was inserted at position " + ctr
+ " which is considered as the last position.");
}

When deleting an item within the list, you only need to consider 2 things: if deleting the beginning of deleting the end. In this implementation, I incoporate the two situations into one function. But the 2 situations are still noted. Also, in the codes presented, two functions are presented, first if deleting based on the object you want to delete and the other is based on the what node you want to delete (eg. Node #2 or the 2nd Node)

public SLNode delete(Object data) //delete based on the item you want to delete
{
SLNode current=head;
SLNode previous=head;
int ctr=0;
while(current!=null)
{
ctr++;
if(current.data==data)
{
if(ctr==1)
head=head.next;
else if(ctr==length())
{
previous.next=null;
tail=previous;
}
else
previous.next=current.next;
return current;
}
previous=current;
current=current.next;
}
return null;
}
public SLNode delete(int i) //delete based on what node you want to delete starting at 1
{
SLNode current=head;
SLNode previous=head;
int ctr=0;
while(current!=null)
{
ctr++;
if(ctr==i)
{
if(ctr==1)
head=head.next;
else if(ctr==length())
{
previous.next=null;
tail=previous;
}
else
previous.next=current.next;
return current;
}
previous=current;
current=current.next;
}
return null;
}

The other methods are just accessory for extra functions for the SLinkedList class
public boolean search(Object node) //search a data based on a given key
{ //if found return true, else false
SLNode current=head;
while(current!=null)
if(current==node)
return true;
return false;
}
public SLNode getAt(int i) //this is similary to peek 
//returning a value at a given Node number
{
SLNode current=head;
int ctr=0;
while(current!=null)
ctr++;
if(ctr==i)
return current;
current=current.next;
}
return null;
}
public int length() //returns the current length/size of the list
{
int ctr=0;
SLNode current=head;
while(current!=null)
{
ctr++;
current=current.next;
}
return ctr;
}

Here are some codes on the implementation of the Singly-Linked List
public static void main(String args[])  
{  
SLinkedList list=new SLinkedList();  
list.add(new SLNode((Object)"Abcd"),2);  
list.add(new SLNode((Object)"CDeFG"),2);  
list.add(new SLNode("HigjDH"),2);
System.out.println("\nThe list size is now " + list.length());
displayList(list); System.out.println(list.delete(2));
displayList(list);
}

public static void displayNode(SLNode node) // to display the data of a single node
{
System.out.print(node.data);
}

public static void displayList(SLinkedList list) // to display the an entire list
{
SLNode current=list.head;
int ctr=0;
while(current!=null)
{
ctr++;
System.out.print("Node " + ctr + " ");
displayNode(current);
System.out.println();
current = current.next;
}
}

3 comments:

Anonymous said...

thnx for the sourcecodes..my problem solved after i saw the codes posted here..

http://www.processorcores.blogspot.com

Tyanak said...

glad I could help out. Suggest some problems if you may please.

Anonymous said...

thank everybody for sharing this code!!

Post a Comment