Create a single linked list and implement the below methods
Create a single linked list and implement the below methods (do not import from java library)
1.Insert at the tail of the linked list
2.Insert at the head of the linked list
3.Insert a specific position of the linked list
4.Delete a Node of the linked list
5.Print the linked list in reverse order
Solution
Hi, Please find my code.
Please let me know in case of any issue.
####### Node.java ##########
class Node
{
private int data;
private Node link;
/* Constructor */
public Node()
{
link = null;
data = 0;
}
/* Constructor */
public Node(int d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public int getData()
{
return data;
}
}
####### LinkedList.java ##############
public class LinkedList
{
protected Node head;
protected Node tail ;
public int N ;
/* Constructor */
public LinkedList()
{
head = null;
tail = null;
N = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return head == null;
}
/* Function to get N of list */
public int getSize()
{
return N;
}
/* Function to insert an element at begining */
public void insertAtHead(int val)
{
Node nptr = new Node(val, null);
N++ ;
if(head == null)
{
head = nptr;
tail = head;
}
else
{
nptr.setLink(head);
head = nptr;
}
}
/* Function to insert an element at tail */
public void insertAtTail(int val)
{
Node nptr = new Node(val,null);
N++ ;
if(head == null)
{
head = nptr;
tail = head;
}
else
{
tail.setLink(nptr);
tail = nptr;
}
}
/* Function to insert an element at position */
public void insertAtPos(int val , int pos)
{
if(pos > (N+1)){
System.out.println(\"Total Number of nodes in list is :\"+N);
return;
}
Node nptr = new Node(val, null);
Node ptr = head;
pos = pos - 1 ;
for (int i = 1; i < N; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
N++ ;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if(pos > N){
System.out.println(\"Total Number of nodes in list is :\"+N);
return;
}
if (pos == 1)
{
head = head.getLink();
N--;
return ;
}
if (pos == N)
{
Node s = head;
Node t = head;
while (s != tail)
{
t = s;
s = s.getLink();
}
tail = t;
tail.setLink(null);
N --;
return;
}
Node ptr = head;
pos = pos - 1 ;
for (int i = 1; i < N - 1; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
N-- ;
}
/* Function to display elements */
public void traverseReverse()
{
System.out.print(\"\ Singly Linked List = \");
if (N == 0)
{
System.out.print(\"empty\ \");
return;
}
reversePrintUtil(head);
System.out.println();
}
private void reversePrintUtil(Node node){
if(node == null){
return;
}
reversePrintUtil(node.getLink());
System.out.print(node.getData()+\" \");
}
}
############## LinkedListTest.java #############
public class LinkedListTest {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.insertAtHead(23);
list.insertAtHead(45);
list.insertAtTail(34);
list.insertAtPos(21, 5);
list.traverseReverse();
list.deleteAtPos(1);
list.traverseReverse();
}
}






