Question

In: Computer Science

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:

  1. Add an “H” to the list
  2. Add an “I” to the list
  3. Add 100 to the list
  4. Print the content of the list and its size
  5. Add a “H” to the first place of the list
  6. Add a “R” to the last place of the list
  7. Get the element of position 3 and print it
  8. Get the last element and print it
  9. Add an “U” to the list
  10. Add a “?” to the list
  11. Remove the element of position 5
  12. Get and print the element of position 5
  13. Remove the element of first place and print the element
  14. Print the content of the list and its size

Partial program is:

import java.util.*;

public class LinkedListDemo{

public static void main(String[] args){

    LinkedList link=new LinkedList();

    link.add(" "); //add something here

    link.add(" "); //add something here

    link.add(new Integer( )); //add something here

    System.out.println("The contents of array is" + link);

    System.out.println("The size of an linkedlist is" + link.size());

   

//add your code

//end your code

}

}

MAKE SURE TO ATTACH THE SCREENSHOT OF THE RUNNING RESULT

Solutions

Expert Solution

I have added inline comments for better understanding. Screenshot of code and output is attached as well.

LinkedListDemo.java:

import java.util.*;

public class LinkedListDemo{

    public static void main(String[] args){

        LinkedList linkedList = new LinkedList();

        //Add an “H” to the list
        linkedList.add("H"); //add something here
        //Add an “I” to the list
        linkedList.add("I"); //add something here
        //Add an 100 to the list
        linkedList.add(100);
        //Print the content of the list and its size
        System.out.println("The size of an linkedlist is: " + linkedList.size());
        System.out.print("The contents of linked list are: ");
        //since we are not using generics here, linkedList will return elements of class Object
        for(Object obj : linkedList){
            System.out.print(obj+" ");
        }
        System.out.println();
        //Add a “H” to the first place of the list
        linkedList.add(0, "H"); // linkedList = {"H","H","I",100}
        //Add a “R” to the last place of the list. add() appends the element in the end of list
        linkedList.add("R"); //linkedList = {"H","H","I",100,"R"}
        //Get the element of position 3 (index = 2) and print it
        System.out.println("Element at position 3 is: "+linkedList.get(2));
        //Get the last element and print it. for last element, index = linkedList.size()-1
        System.out.println("Last element in the list is: "+linkedList.get(linkedList.size()-1));
        //Add an “U” to the list
        linkedList.add("U"); //linkedList = {"H","H","I",100,"R","U"}
        //Add a “?” to the list
        linkedList.add("?"); //linkedList = {"H","H","I",100,"R","U","?"}
        //Remove the element of position 5 (index = 4)
        linkedList.remove(4); //linkedList = {"H","H","I",100,"U","?"} "R" removed
        //Get and print the element of position 5 (index = 4)
        System.out.println("Element at position 5 is: "+linkedList.get(4));
        //Remove the element of first place and print the element
        Object obj = linkedList.remove(0); //linkedList = {"H","I",100,"U","?"} "H" removed at index 0
        System.out.println("Removed element is: "+obj);
    }
}

Output:


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 C++ integer linked list program that performs the following methods below: Please create these...
Create a C++ integer linked list program that performs the following methods below: Please create these three source files: intList.h, intList.cpp, & intListTest.cpp. Implement recursive routines in the intList class to do the following: Print the list in reverse order Return the value in the middle node Return the average of all the odd values Remove every node containing an odd value Modify main (in IntListTest.cpp) so it does the following: Insert the numbers 1, 3, 4, 6, 7, 10,...
Bank Linked List Project: Create a bank linked list project program to mimic a simple bank...
Bank Linked List Project: Create a bank linked list project program to mimic a simple bank account system (open account, deposit, withdraw, loans etc.). Requirements: 1. Use linked list (queues and/or stacks) 2. Classes 3. Arrays 4. Add, delete, remove, search methods (use Dev. C++ to create the program)
Java Write a menu driven program that implements the following linked list operations : INSERT (at...
Java Write a menu driven program that implements the following linked list operations : INSERT (at the beginning) INSERT_ALPHA (in alphabetical order) DELETE (Identify by contents, i.e. "John", not #3) COUNT CLEAR
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 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...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
1) a. Write down a C++ program which will create a list (simple linear linked list)...
1) a. Write down a C++ program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a gradepoint average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function...
Write down a C program which will create a list (simple linear linked list) of nodes....
Write down a C program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a grade-point average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function you wrote...
Write a program that create a single linked list and consist of all the necessary functions...
Write a program that create a single linked list and consist of all the necessary functions to do the following Add an element to the list, insertion position can be anywhere in the list (first, last and middle) delete an element from the list, deletion position can be anywhere in the list (first, last and middle) Note: You need to add proper documentation to your programs and you need to include the output of each program C++
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT