Write a java program to demonstrate linked lists and its imp

Write a java program to demonstrate linked lists and its implementations

a. Draw the structure

b. Create a LinkedListNode class

c. Write a method to display information from a linked list with four nodes

d. Write a method to delete or insert an item from the list

Solution

LinkedListNode.java

public class LinkedListNode {

private int value;

   private LinkedListNode next;

  

   public LinkedListNode(int value)

   {

       this.value = value;

       this.next = null;

   }

  

   public void setNext(LinkedListNode node)

   {

       this.next = node;

   }

  

   public int getValue()

   {

       return value;

   }

  

   public LinkedListNode getNext()

   {

       return next;

   }

}

LinkedList.java

public class LinkedList {

   private LinkedListNode head;

  

   public LinkedList()

   {

      

   }

  

   public LinkedListNode getHead()

   {

       return head;

   }

  

public void insert(int value)

   {

       LinkedListNode temp = head;

       if(temp == null)

       {

           head = new LinkedListNode(value);          

       }

       else

       {

           while(temp.getNext() != null)

           {

               temp = temp.getNext();              

           }

           temp.setNext(new LinkedListNode(value));

          

       }

   }

  

public void delete(int value)

   {

       LinkedListNode temp = head;

       LinkedListNode previous = head;

       while(temp != null && temp.getValue() != value)

       {

           previous = temp;

           temp = temp.getNext();

       }

      

       if(temp == null)

           return;

       else

       {

           previous.setNext(temp.getNext());

       }      

   }

  

   public void print()

   {

       LinkedListNode temp = head;

       while(temp != null)

       {

           System.out.print(temp.getValue());

           temp = temp.getNext();

           if(temp != null)

               System.out.print(\" --> \");

       }

       System.out.println();

   }

}

LinkedListTester.java

public class LinkedListTester {

  

   public static void main(String args[])

   {

       LinkedList linkedList = new LinkedList();

       linkedList.insert(1);

       linkedList.insert(2);

       linkedList.insert(3);

       linkedList.insert(4);

       linkedList.print();

       linkedList.delete(3);

       linkedList.print();

       linkedList.insert(5);

       linkedList.print();

   }

}

Write a java program to demonstrate linked lists and its implementations a. Draw the structure b. Create a LinkedListNode class c. Write a method to display inf
Write a java program to demonstrate linked lists and its implementations a. Draw the structure b. Create a LinkedListNode class c. Write a method to display inf
Write a java program to demonstrate linked lists and its implementations a. Draw the structure b. Create a LinkedListNode class c. Write a method to display inf
Write a java program to demonstrate linked lists and its implementations a. Draw the structure b. Create a LinkedListNode class c. Write a method to display inf

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site