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

**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...
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...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Create a Linked List and conduct the following operations. Portion of the program is given. The...
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 it...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT