Question

Write Java code (do not use Java libraries) to perform the following on a linked list...

Write Java code (do not use Java libraries) to perform the following on a linked list of integers: prepend a new value

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any problem with the code feel free to comment.

Program

/*LinkedList implementation*/

class LinkedList {
   Node head;

   //node class for holding data and the next node address
   static class Node {
       int data;
       Node next;

       Node(int data) {
           this.data = data;
       }
   }

   //inserting data to the list
   public void insert(LinkedList list, int data) {
       Node newNode = new Node(data);

       newNode.next = null;

       if (list.head == null)
           list.head = newNode;
       else {

           Node last = list.head;
           // traversing the list to find the last node
           while (last.next != null) {
               last = last.next;
           }
           // putting the value after the last node
           last.next = newNode;
       }
   }

   // insert values at the start of the list
   public void prepend(LinkedList list, int data) {
       Node newNode = new Node(data);

       newNode.next = list.head;

       list.head = newNode;
   }


   // delete value from index
   public void delete(LinkedList list, int index) {
       if (index == 0)
           list.head = list.head.next;
       else {
           Node node = list.head;
           Node temp = null;

           for (int i = 0; i < index - 1; i++) {
               node = node.next;
           }
           temp = node.next;
           node.next = temp.next;
       }
   }

   //printing the values of the list
   public static void printList(LinkedList list) {
       Node currentNode = list.head;
       while (currentNode != null) {
           System.out.print(currentNode.data + " ");
           currentNode = currentNode.next;
       }
       System.out.println();
   }
}

public class Test {//for testing the linked list
   public static void main(String[] args) {
       LinkedList list = new LinkedList();
      
       //inserting values to the linkedlist
       list.insert(list, 1);
       list.insert(list, 2);
       list.insert(list, 3);
      
       System.out.print("LinkedList after insert: ");
       LinkedList.printList(list);
      
       //deleteing value from the list
       list.delete(list, 2);
      
       System.out.print("LinkedList after delete: ");
       LinkedList.printList(list);
      
       //prepending value to the list
       list.prepend(list, 10);
      
       System.out.print("LinkedList after prepend: ");
       LinkedList.printList(list);
   }
}

Output

Add a comment
Know the answer?
Add Answer to:
Write Java code (do not use Java libraries) to perform the following on a linked list...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT