Question

In: Computer Science

Using Linked List, create a Java program that does the following without using LinkedList from the...

Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function.

Create a menu that contains the following options :

1. Add new node at the end of LL. ( as a METHOD )

2. Add new node at the beginning of LL. ( as a METHOD )

3. Delete a node from the end of LL. ( as a METHOD )

4. Delete a node from the beginning of LL. ( as a METHOD )

5. Print all data in the LL. ( as a METHOD )

6. Exit. ( as a METHOD )

Important notes:

1. write the operations from the pseudocode.

2. The user should insert all data even in the first node (head)

3. The menu should be repeated each time until the user enter 6.

Solutions

Expert Solution

import java.io.*;
import java.util.Scanner;

public class LinkedList {

    Node head, tail;
    int list_size = 0;

    static class Node {

        int data;
        Node next;

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

    // Method to insert a node at the beginning of LL
    public static LinkedList insertHead(LinkedList list, int data) {
        Node new_node = new Node(data);
        new_node.next = list.head;
        list.head = new_node;
        if (list.list_size == 0)
            list.tail = list.head;
        list.list_size++;
        return list;
    }

    // Method to insert a node at the end of LL
    public static LinkedList insertTail(LinkedList list, int data) {
        Node new_node = new Node(data);
        new_node.next = null;
        if (list.list_size == 0) {
            list.tail = new_node;
            list.head = list.tail;
        } else {
            list.tail.next = new_node;
            list.tail = new_node;
        }

        list.list_size++;
        return list;
    }

    // Method to delete a node from the beginning of LL
    public static LinkedList deleteHead(LinkedList list) {
        if (list.list_size == 0) {
            System.out.println("List is empty. Nothing to delete!");
            return list;
        }
        if (list.list_size == 1) {
            list.head = null;
            list.tail = null;
            list.list_size--;
            return list;
        }
        list.head = list.head.next;
        list.list_size--;
        return list;
    }

    // Method to delete a node from the end of LL
    public static LinkedList deleteTail(LinkedList list) {
        if (list.list_size == 0) {
            System.out.println("List is empty. Nothing to delete!");
            return list;
        }
        if (list.list_size == 1) {
            list.head = null;
            list.tail = null;
            list.list_size--;
            return list;
        }
        Node iterator = list.head;
        while (iterator.next.next != null) {
            iterator = iterator.next;
        }
        list.tail = iterator;
        list.tail.next = null;
        list.list_size--;
        return list;
    }

    // Method to print the LL.
    public static void printList(LinkedList list) {
        Node currNode = list.head;
        System.out.print("LinkedList: ");
        while (currNode != null) {
            System.out.print(currNode.data + " ");
            currNode = currNode.next;
        }
        System.out.println();
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        while (true) {
            int choice;
            System.out.println("Choose a number from below menu:");
            System.out.println("1. Add new node at the end of LL.");
            System.out.println("2. Add new node at the beginning of LL.");
            System.out.println("3. Delete a node from the end of LL.");
            System.out.println("4. Delete a node from the beginning of LL.");
            System.out.println("5. Print all data in the LL.");
            System.out.println("6. Exit.");

            Scanner sc = new Scanner(System.in);
            choice = sc.nextInt();
            if (choice == 6)
                break;
            if (choice == 1) {
                System.out.println("Enter the number to be added");
                int data = sc.nextInt();
                list = insertTail(list, data);
            } else if (choice == 2) {
                System.out.println("Enter the number to be added");
                int data = sc.nextInt();
                list = insertHead(list, data);
            } else if (choice == 3)
                list = deleteTail(list);
            else if (choice == 4)
                list = deleteHead(list);
            else if (choice == 5)
                printList(list);
        }
    }
}

Related Solutions

Remove the minimum element from the linked list in Java public class LinkedList {      ...
Remove the minimum element from the linked list in Java public class LinkedList {       // The LinkedList Node class    private class Node{               int data;        Node next;               Node(int gdata)        {            this.data = gdata;            this.next = null;        }           }       // The LinkedList fields    Node head;       // Constructor    LinkedList(int gdata)   ...
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only),...
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only), the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined. p1=23x 9 + 18x 7+3 1. Class Node ● Private member variables: coefficient (double), exponents (integer), and next pointer. ● Setter and getter functions to set and get all member variables ● constructor 2. Class PolynomialLinkedList ● Private member variable to represent linked list (head)...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create...
Introduction: In this project you will create a generic linked list using Java Generics. Description: Create a generic class called GenLinkedList. GenLinkedList will use nodes that store a value of the generic type to store its contents. It should have the following methods. The methods should all operate on the object making the call (none are static). Perform checking of the parameters and throw exceptions where appropriate. The linked list should be singly-linked. It should not use sentinel nodes (empty...
Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double...
Exercise 2: Write a program in Java to manipulate a Double Linked List: 1. Create Double Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Double Linked List. 5. Insert a new node at the end of a DoubleLinked List 6. Insert a new node after the value 5 of Double Linked List 7. Delete the node with value 6. 8. Search an existing element in a...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly...
Exercise 1: Write a program in Java to manipulate a Singly Linked List: 1. Create Singly Linked List 2. Display the list 3. Count the number of nodes 4. Insert a new node at the beginning of a Singly Linked List. 5. Insert a new node at the end of a Singly Linked List 6. Insert a new node after the value 5 of Singly Linked List 7. Delete the node with value 6. 8. Search an existing element in...
Linked List: Complete the following code to create a linked list from an Array. After creating...
Linked List: Complete the following code to create a linked list from an Array. After creating the list, display the elements of the linked list iteratively. Write two others function called as RDisplayTailRecursion(first) and RDisplayTailRecursion(first) which will print elements of the linked list using the tail and head recursions respectively. #include <stdio.h> #include <stdlib.h> struct Node { }*first=NULL; void create(int A[], int n) { for(i=1; i<n; i++) { } } void Display(struct Node*p) { while(p!=NULL) { } } void RDisplayTailRecursion...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT