Question

In: Computer Science

a/  write  4 numbers from your choice b/ Write a java code to create a linked list containing...

a/  write  4 numbers from your choice

b/ Write a java code to create a linked list containing the 4 numbers

Solutions

Expert Solution

Hi,

Please find below code as per your requirement.

I have added two approach in the answer, Please let me know if you require otherway/other approach.

Let me know if you have any doubt/concerns in this via comments.

Hope this answer helps you.

Thanks.

/********************JAVA CODE********************/

/*******************LinkedListTest.java**********************/

import java.util.Arrays;
import java.util.LinkedList;

public class LinkedListTest {

        public static void main(String[] args) {
                
                //Task# a and b
                //One way
                //Creating LinkedList of Integer
                LinkedList<Integer> numberLinkedList = new LinkedList<Integer>();
                //Adding numbers into the linked list
                numberLinkedList.add(1);
                numberLinkedList.add(2);
                numberLinkedList.add(3);
                numberLinkedList.add(4);
                
                //Task#c
                //You can consider one of those approach to print linked list
                //Printing linked list to console
                System.out.println("Printing linked list directly");
                System.out.println(numberLinkedList);
                
                System.out.println("Printing linked list using for loop");
                for (int i = 0; i < numberLinkedList.size(); i++) {
                        System.out.print(numberLinkedList.get(i)+ " ");
                }
                
                System.out.println("\nPrinting linked list using for each loop");
                for (Integer number : numberLinkedList) {
                        System.out.print(number + " ");
                }
                System.out.println("\nRemoving third element from list");
                //Task#d
                //here removing 3rd element using index, as index start from 0
                numberLinkedList.remove(2);
                
                System.out.println("Printing linked list after removing element");
                for (int i = 0; i < numberLinkedList.size(); i++) {
                        System.out.print(numberLinkedList.get(i)+ " ");
                }
                System.out.println("");
                
                
                /*
                //you can ignore below
                
                //This is another way to create linked list from array of Integers
                Integer[] numberArray = {3,2,1,0};
                //Creating linked list of integers
                LinkedList<Integer> numberLinkedList1 = new LinkedList<Integer>();
                //Using Arrays.asList converts array of integer to list and adding this list to linked list using addAll method
                //which takes another collection type
                numberLinkedList1.addAll(Arrays.asList(numberArray));
                //Printing linked list to console
                System.out.println(numberLinkedList1);
                */
                
        }
        
}

/***************Sample output****************/


Related Solutions

write a java code to implement a linked list, called CupList, to hold a list of...
write a java code to implement a linked list, called CupList, to hold a list of Cups. 1.Define and write a Cup node class, called CupNode, to hold the following information about a cup: •number (cup number) •capacity (cup capacity in ml) •Write a method size() that returns the number of elements in the linkedlist CupList. •Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method. •Write...
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...
Using Doubly Linked List, create a java code that does the following Without using LinkedList from...
Using Doubly Linked List, create a java code 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 operations : 1. Add new node to DLL. ( as a METHOD ) 2. Delete a node from DLL. ( as a METHOD ) 3. Show how many nodes in DLL. ( as a METHOD ) 4. Print all data in the DLL. ( as a METHOD...
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...
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...
In JAVA, students will create a linked list structure that will be used to support a...
In JAVA, students will create a linked list structure that will be used to support a role playing game. The linked list will represent the main character inventory. The setting is a main character archeologist that is traveling around the jungle in search of an ancient tomb. The user can add items in inventory by priority as they travel around (pickup, buy, find), drop items when their bag is full, and use items (eat, spend, use), view their inventory as...
I know this code takes in a set of numbers into a doubly linked list and...
I know this code takes in a set of numbers into a doubly linked list and sorts it using insertion sort. Could you explain exactly how this code is working? main.c Source Code: #include #include #include "node.h" int main() { struct mynode *head=NULL; int value; printf("Give first value: \n"); scanf("%d",&value); printf("Give next values, and input 0 to end list: \n"); do{ if(value>0){    head = pushNode(head, value);    scanf("%d",&value); } }while (value>0); printf("Before insertion sort: "); printlist(head); head=insertsort(head); printf("After insertion...
WRITE CODE IN JAVA it is now your turn to create a program of your choosing....
WRITE CODE IN JAVA it is now your turn to create a program of your choosing. If you are not sure where to begin, think of a task that you repeat often in your major that would benefit from a program. For example, chemistry unit conversions, finding the area for geometric shapes, etc. You can also create an interactive story that changes based on the user input/decisions. The possibilities are endless. The program must include instructions for the user. Be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT