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...
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...
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...
It is very common, for program managers to have a portion of their bonus linked to...
It is very common, for program managers to have a portion of their bonus linked to program sales and specifically, how monthly sales match up with target monthly sales goals. Your supervisor, the program manager has explained to you the bonus calculation and has asked for your help. This months sales were amazing and your supervisor has asked you on the last day of the month to hold off entering your last 2 sales until the next day to "push"...
Given an array of Student type and size 10, create a linked list of students by...
Given an array of Student type and size 10, create a linked list of students by linking students with an odd index first and then linking students with an even index. Write a loop to print out the students in the linked list #include<iostream> #include<string> #include<fstream> using namespace std; const int NUM = 10; struct Student{ string fName; string lName; Student * next; }; int main() {        Student stuArr[NUM];        ifstream myfile;        myfile.open("Test.txt");        for(int i = 0;...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file...
Complete the provided partial C++ Linked List program. Main.cpp is given and Link list header file is also given. The given testfile listmain.cpp is given for demonstration of unsorted list functionality. The functions header file is also given. Complete the functions of the header file linked_list.h below. ========================================================= // listmain.cpp #include "Linked_List.h" int main(int argc, char **argv) {      float           f;      Linked_List *theList;      cout << "Simple List Demonstration\n";      cout << "(List implemented as an Array - Do...
Given a doubly linked list in c++, how do I create a function that returns the...
Given a doubly linked list in c++, how do I create a function that returns the pointer to first node in the given pattern, For example, given mainList (a -> b -> c -> d) and sublist  (b -> c), our function should return a Node pointer that points to first node of the sublist in the mainList. If the pattern doesn't exist in the mainList, we should return a nullptr, there are multiple of the same sublist in the mainList,...
Write a program where you- 1. Create a class to implement "Double Linked List" of integers....
Write a program where you- 1. Create a class to implement "Double Linked List" of integers. (10) 2. Create the list and print the list in forward and reverse directions. (10)
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT