Question

In: Computer Science

Laboratory Tasks public class LinkedList {              Node head;               class Node {    &nbsp

Laboratory Tasks

public class LinkedList {

             Node head;

              class Node {  

                    int data;

                    Node next;

                    Node(int d) {

                            data = d;

                            next = null;

                    }

                }

}

Complete the above java program by adding the following methods:

Part1:

  1. isEmpty() checks if the linked list is empty or not. (return type is boolean)
  2. printList() prints all data in the linked list. (void method)
  3. insertFirst(int newData) add newData at the head of the linked list. (void method)
  4. insertLasL(int newData) add newData at the tail of the linked list. (void method)
  5. findMiddle() finds the middle element in the list and displays its value. (void method)
  6. deleteFirst() deletes the first element of the list. (void method)
  7. sizeList() returns the number of elements in the list. (return type is int)
  8. Main method that calls all the previous seven methods.

Solutions

Expert Solution

Hello, I am putting the whole code below. please go through it and if you have any doubt, feel free to ask in comment and If you like this answer, give me a thumb up.

Thank you, Enjoy you answer.

// Java code


class Main
{
   Node head; // head of list

   class Node
   {
       int data;
       Node next;
       Node(int d){
       data = d;
       next = null; }
   }

   /* Inserts a new Node at front of the list. */
   public void insertFirst(int new_data)
   {
       Node new_node = new Node(new_data);

       // Make next of new Node as head
       new_node.next = head;

       // Move the head to point to new Node
       head = new_node;
   }

   /* Inserts a new node after the given prev_node. */
   public void insertAfter(Node prev_node, int new_data)
   {
       // Check if the given Node is null
       if (prev_node == null)
       {
           System.out.println("The given previous node cannot be null");
           return;
       }

       Node new_node = new Node(new_data);

       new_node.next = prev_node.next;

       prev_node.next = new_node;
   }
  
   // insertLasts a new node at the end.
   public void insertLast(int new_data)
   {
       Node new_node = new Node(new_data);
// Check if linked list is null or not
       if (head == null)
       {
           head = new Node(new_data);
           return;
       }
// As we are inserting at last, next node will be null
       new_node.next = null;

       Node last = head;
       // If list is not empty, We have to go at last node
       while (last.next != null)
           last = last.next;
  
// when we reach at last, Add it there
       last.next = new_node;
       return;
   }

   /* This function prints contents of linked list starting from
       the given node */
   public void printList()
   {
       Node tnode = head;
       while (tnode != null)
       {
           System.out.print(tnode.data+" ");
           tnode = tnode.next;
       }
       System.out.println();
   }

void printMiddle()
{
Node slow_ptr = head;
Node fast_ptr = head;
if (head != null)
{
while (fast_ptr != null && fast_ptr.next != null)
{
fast_ptr = fast_ptr.next.next;
slow_ptr = slow_ptr.next;
}
System.out.println("The middle element is " + slow_ptr.data);
}
}
  
boolean isEmpty()
{
// if we found the head as null, which means it is empty list
if(head == null){
return true;
}
return false;
}
  
void deleteFirst()
{
// Move the head pointer to the next node
Node temp = head;
head = head.next;
}
  
public int getLength()
{
Node temp = head;
int count = 0;
while (temp != null)
{
count++;
temp = temp.next;
}
return count;
}
  
   public static void main(String[] args)
   {
       /* Start with the empty list */
       Main llist = new Main();
// As List is empty, It will return true
System.out.println("isEmpty? :" + llist.isEmpty());
       llist.insertFirst(7);
// Link list is: 7
llist.insertLast(4);
// Link list is: 7 -> 4
       llist.insertFirst(1);
       // Link list is: 1 -> 7 -> 4
       llist.insertLast(6);
       // Link list is: 1 -> 7 -> 4 -> 6
llist.insertLast(10);
// Link List is: 1 -> 7 -> 4 -> 6 -> 10
System.out.println("\nCreated Linked list is: ");
       llist.printList();
       // Trying to print middle
llist.printMiddle();
// will delete first
llist.deleteFirst();
       System.out.println("\nCreated Linked list is: ");
       llist.printList();
System.out.println("Length of Linked list is: "+ llist.getLength());
   }
}


Related Solutions

Remove the Head element from the code below: public class LinkedList {    class Node{ int...
Remove the Head element from the code below: public class LinkedList {    class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element);    if(first == null) { first = newValueNode; } else { last.nextElement = newValueNode; } last = newValueNode; } public void displayValues() { Node recent = first; if(first ==...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse();...
Consider the following definition of a doubly linked-list: class LinkedList{ public: LinkedList():head(0), tail(0){} ~LinkedList(); void reverse(); //reverses the order of elements in the linked list void insert(int value); private: struct Node{ int data; Node* next; Node* prev; }; Node* head; Node* tail; //Add your helper function here that recursively reverses the order of elements in the linked list }; Write the declaration of a helper function in the class provided above that recursively reverses the order of elements in the...
public class MyLinked {    static class Node {        public Node (double item, Node...
public class MyLinked {    static class Node {        public Node (double item, Node next) { this.item = item; this.next = next; }        public double item;        public Node next;    }    int N;    Node first;     // remove all occurrences of item from the list    public void remove (double item) {        // TODO    } Write the remove function. Do NOT add any fields to the node/list classes, do...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with...
Java Language Add a method (deleteGreater ()) to the LinkedList class to delete the node with the higher value data. Code: class Node { int value; Node nextNode; Node(int v, Node n) { value = v; nextNode = n; } Node (int v) { this(v,null); } } class LinkedList { Node head; //head = null; LinkedList() { } int length() { Node tempPtr; int result = 0; tempPtr = head; while (tempPtr != null) { tempPtr = tempPtr.nextNode; result =...
in java This class will require the following fields: Node<T> head: the first Node in the...
in java This class will require the following fields: Node<T> head: the first Node in the chain Node<T> tail: the last Node in the chain int size: Keeps a count of the number of Nodes in the chain Your LinkedList class must also support the following public methods. LinkedList(): A default constructor sets both pointers to null and sets the size to 0. int size(): Returns the size of the LinkedList. void push_back(T): Creates a new Node and assigns it...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new...
import java.util.LinkedList; public class StudentLinkedList { public static void main(String[] args) { LinkedList<Student> linkedlist = new LinkedList<Student>(); linkedlist.add(new Student("Ahmed Ali", 20111021, 18, 38, 38)); linkedlist.add(new Student("Sami Kamal", 20121021, 17, 39, 35)); linkedlist.add(new Student("Salem Salim", 20131021, 20, 40, 40)); linkedlist.add(new Student("Rami Mohammed", 20111031, 15, 35, 30)); linkedlist.add(new Student("Kim Joe", 20121024, 12, 32, 32)); linkedlist.addFirst(new Student("Hadi Ali", 20111025, 19, 38, 39)); linkedlist.addLast(new Student("Waleed Salim", 20131025, 10, 30, 30)); linkedlist.set(0, new Student("Khalid Ali", 20111027, 15, 30, 30)); linkedlist.removeFirst(); linkedlist.removeLast(); linkedlist.add(0, new Student("John Don",...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get;...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get; set; } public Node (T item, Node<T> next) { … } } public class Polynomial { // A reference to the first node of a singly linked list private Node<Term> front; // Creates the polynomial 0 public Polynomial ( ) { } // Inserts term t into the current polynomial in its proper order // If a term with the same exponent already exists...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT