LinkedList implementation
Single LinkedList implementation in java without using standard linked list library:
package example.linkedlist;
public class LinkedList
{
int data ;
LinkedList next;
public LinkedList(int data)
{
this.data = data;
this.next = null;
}
void setdata(int value)
{
this.data = value;
}
int getdata()
{
return this.data;
}
void setnext(LinkedList node)
{
this.next = node;
}
LinkedList getnext()
{
return this.next;
}
int listLength(LinkedList node)
{
int count = 0;
if (node == null)
{}
else
{
while (node != null)
{
count++;
node = node.getnext();
}
}
return count;
}
boolean insert(LinkedList headnode, int data, int index)
{
LinkedList newnode = new LinkedList(data);
if (headnode == null)
{
return true;
}
if (index == 1)
{
newnode.setnext(headnode.getnext());
headnode.setnext(newnode);
return true;
}
if (index < 0 || index > listLength(headnode))
{
System.out.println("index not supported");
return false;
}
for (int i = 0; i < index && headnode.getnext() != null ; i++)
{
headnode = headnode.getnext();
}
newnode.setnext(headnode.getnext());
headnode.setnext(newnode);
return true;
}
void printlist(LinkedList head)
{
while (head != null)
{
System.out.println("list vallues: " + head.data);
head = head.getnext();
}
}
boolean equal(LinkedList node)
{
if (this.data == node.data && this.next == node.next)
{
return true;
}
return false;
}
void delete(LinkedList headnode, int node)
{
LinkedList obj = null;
while (headnode != null)
{
if (headnode.data == node)
{
if (headnode.getnext() == null)
{
headnode = null;
}
else
{
if (obj == null)
{
headnode.setdata(headnode.getnext().data);
headnode.setnext(headnode.getnext());
}
else
{
obj.setnext(headnode.getnext());
}
}
}
obj = headnode;
headnode = headnode.getnext();
}
}
public static void main(String agrs[])
{
System.out.println("linked list main block");
LinkedList head = new LinkedList(2);
System.out.println("length of linked list: " + head.listLength(head));
head.printlist(head);
head.insert(head, 3 , 1);
head.insert(head, 4 , 1);
head.insert(head, 5 , 1);
head.insert(head, 6 , 1);
head.insert(head, 7 , 1);
head.printlist(head);
System.out.println("head data: " + head.data);
head.delete(head, 2);
head.printlist(head);
}
}
Reverse the linkedList:
LinkedList reverseRescursive(LinkedList list) // Reverse linked list Recursively
{
if (list == null)
{
return null;
}
if (list.getnext() == null)
{
return list;
}
LinkedList secondElem = list.getnext();
list.setnext(null);
LinkedList reverseRest = reverseRescursive(secondElem);
secondElem.setnext(list);
return reverseRest;
}
LinkedList reverseIterative(LinkedList head) // Reverse linked list iterative
{
LinkedList next = null, pre = null;
while (head.getnext() != null)
{
next = head.getnext();
head.setnext(pre);
pre = head;
head = next;
}
head.setnext(pre);
return head;
}
find node from a linked list at given position from end of list:
int findNodeFromEnd(LinkedList node, int position)
{
LinkedList back = node;
LinkedList front = node;
for (int i = 1 ; i < position; i++)
{
if (front.getnext() != null)
{
front = front.getnext();
}
}
System.out.println("element " + front.getdata());
while (front.getnext() != null)
{
back = back.getnext();
front = front.getnext();
}
return back.getdata();
}
package example.linkedlist;
public class LinkedList
{
int data ;
LinkedList next;
public LinkedList(int data)
{
this.data = data;
this.next = null;
}
void setdata(int value)
{
this.data = value;
}
int getdata()
{
return this.data;
}
void setnext(LinkedList node)
{
this.next = node;
}
LinkedList getnext()
{
return this.next;
}
int listLength(LinkedList node)
{
int count = 0;
if (node == null)
{}
else
{
while (node != null)
{
count++;
node = node.getnext();
}
}
return count;
}
boolean insert(LinkedList headnode, int data, int index)
{
LinkedList newnode = new LinkedList(data);
if (headnode == null)
{
return true;
}
if (index == 1)
{
newnode.setnext(headnode.getnext());
headnode.setnext(newnode);
return true;
}
if (index < 0 || index > listLength(headnode))
{
System.out.println("index not supported");
return false;
}
for (int i = 0; i < index && headnode.getnext() != null ; i++)
{
headnode = headnode.getnext();
}
newnode.setnext(headnode.getnext());
headnode.setnext(newnode);
return true;
}
void printlist(LinkedList head)
{
while (head != null)
{
System.out.println("list vallues: " + head.data);
head = head.getnext();
}
}
boolean equal(LinkedList node)
{
if (this.data == node.data && this.next == node.next)
{
return true;
}
return false;
}
void delete(LinkedList headnode, int node)
{
LinkedList obj = null;
while (headnode != null)
{
if (headnode.data == node)
{
if (headnode.getnext() == null)
{
headnode = null;
}
else
{
if (obj == null)
{
headnode.setdata(headnode.getnext().data);
headnode.setnext(headnode.getnext());
}
else
{
obj.setnext(headnode.getnext());
}
}
}
obj = headnode;
headnode = headnode.getnext();
}
}
public static void main(String agrs[])
{
System.out.println("linked list main block");
LinkedList head = new LinkedList(2);
System.out.println("length of linked list: " + head.listLength(head));
head.printlist(head);
head.insert(head, 3 , 1);
head.insert(head, 4 , 1);
head.insert(head, 5 , 1);
head.insert(head, 6 , 1);
head.insert(head, 7 , 1);
head.printlist(head);
System.out.println("head data: " + head.data);
head.delete(head, 2);
head.printlist(head);
}
}
Reverse the linkedList:
LinkedList reverseRescursive(LinkedList list) // Reverse linked list Recursively
{
if (list == null)
{
return null;
}
if (list.getnext() == null)
{
return list;
}
LinkedList secondElem = list.getnext();
list.setnext(null);
LinkedList reverseRest = reverseRescursive(secondElem);
secondElem.setnext(list);
return reverseRest;
}
LinkedList reverseIterative(LinkedList head) // Reverse linked list iterative
{
LinkedList next = null, pre = null;
while (head.getnext() != null)
{
next = head.getnext();
head.setnext(pre);
pre = head;
head = next;
}
head.setnext(pre);
return head;
}
find node from a linked list at given position from end of list:
int findNodeFromEnd(LinkedList node, int position)
{
LinkedList back = node;
LinkedList front = node;
for (int i = 1 ; i < position; i++)
{
if (front.getnext() != null)
{
front = front.getnext();
}
}
System.out.println("element " + front.getdata());
while (front.getnext() != null)
{
back = back.getnext();
front = front.getnext();
}
return back.getdata();
}
Comments
Post a Comment