In: Computer Science
IN JAVA
Minimal Documentation Required (no javadoc)
Purpose
The purpose of this assignment is to introduce you to basic operations on a linked list.
Specifics
Design a program that generates a linked list of randomly generated Integer objects. Present a menu at program start that gives the user the following options (most of these options will have corresponding methods in a Linked List class):
1. Create a new list. The size will be specified by the user, make sure a non-negative value is entered. If there is a pre-existing list when this option is chosen, make sure you delete the contents of that list before creating your new list.
2. Sort the list. How you implement this is up to you. You can insert items in the list such that the list is always in sorted order or you can write a sort routine. You may not call a sort routine provided by the Java API.
3. Print the list (to the screen), one number per line.
4. Print the list in reverse order (to the screen), one number per line.
5. Generate a sub-list that contains all the even numbers in the current list. This list should be returned and the contents should be displayed (to the screen), one number per line.
6. Print the contents of every "nth" node in the list. Obtain the "n" from the user, ensure it is greater than 0.
7. Delete node(s) containing an integer value entered by the user. You should report how many were deleted to the user.
8. Delete the contents of the current list.
9. Quit
You may use any linked list implementation you wish (singly linked, doubly linked, dummy head node, circular).
In addition to your LinkedList class, include a driver file called ListTester that will contain the menu and anything else you deem necessary (perhaps a utility to generate random Integers...).
Keep things modular and encapsulate your data properly - do not let anything outside the Linked List class have direct access to any of the nodes.
Do not accept Invalid input of any type from the user and do not let user input crash your program.
PLEASE IMPLEMENT ALL
Dear Student,
I have developed the code for all the requirements. Created separate classes for the functionality and driver.
Please use any editor for testing the code
Let me know if you find any difficulties in understanding. I have provided adequate comments for your understanding.
I am pasting the code and output. I have used java inbuilt linked list for the assignment.
Added menu options for the selection of the functionality. You have to run linkedlist driver for testing the solution.
Code:
---------------------------------------------------------
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
//core class with functinality
public class LinkedListProblem {
//java inbuilt linked list
LinkedList<Integer> list;
//function for creating list. It will refresh the
elements whenever called
public void createList(int size) {
/*
if (list.size() >
0&&list!=null)
list.clear();
*/
list = new
LinkedList<Integer>();
// random number object
Random rand = new Random();
// random numbers of list size elements. Inserting into list
for (int iter = 0; iter <
size; iter++) {
int rand_int1 =
rand.nextInt(100);
list.add(rand_int1);
}
}
//displaying elements of list
void printList() {
int n = list.size();
System.out.println("Size of linked
list: " + n);
for (int i = 0; i < n;
++i)
System.out.println(list.get(i) );
}
//bubble sorting
public void sort() {
// get size of the array
int size = list.size();
// comparing with adjacent
elements. If left element is greater than right
// element, swap the elements
for (int i = 0; i < size - 1;
i++)
for (int j = 0;
j < size - i - 1; j++)
if (list.get(j) > list.get(j + 1)) {
// swap arr[j+1] and
arr[i]
int temp = list.get(j);
// arr[j] = arr[j+1];
int toAdd = list.get(j +
1);
list.remove(j);
list.add(j, toAdd);
list.remove(j + 1);
list.add(j + 1, temp);
// arr[j+1] = temp;
}
}
//reverse the list. Traverse the list from back side and
printing
public void reverseList() {
int size = list.size();
int counter = size - 1;
while (counter >= 0) {
System.out.println(list.get(counter));
counter--;//
reducing count after each loop
}
}
//for genreating even numbers
public List<Integer> generateEvenList() {
List<Integer> evenNumbers=new
ArrayList<Integer>();
for(int
i=0;i<list.size();i++)
{
//if n is
divisible by 2.then its even number
if(list.get(i)%2==0)
evenNumbers.add(list.get(i));
}
return evenNumbers;
}
public void printNthNumber(int n) {
int count=n-1;
while(count<list.size())
{
System.out.println(list.get(count));
count=count+n;//increment by n as we need to print nth number
}
}
public void deleteNumber(int number) {
//delete all the instances of
the number in the list
if(!list.contains(number))
System.out.println("Number is not present in the list to delete.
Enter right number");
//check if the number is present in
the list
while(list.contains(number))
list.removeFirstOccurrence(number);
}
public void clearListContent() {
//java inbuilt method to clear the content
list.clear();
}
}
----------------------------------------------
import java.util.List;
import java.util.Scanner;
//driver program for testing the linkedlist
public class LinkedListDriver {
public static void main(String[] args) {
//creating object
LinkedListProblem obj = new
LinkedListProblem();
//creating scan object for taking
input
Scanner scan = new
Scanner(System.in);
int input = 0;
while (true) {
//menu for user. We have to enter the corresponding number for the
functionality
System.out.println("Welcome To Assignment: ");
System.out.println("Please choose an option");
System.out.println("1.Create LinkedList of size");
System.out.println("2.Sort the linked list");
System.out.println("3.Print the list");
System.out.println("4.Print the reverse list");
System.out.println("5.print the even list");
System.out.println("6.Print the nth Number list");
System.out.println("7.Delete the Number");
System.out.println("8.Clear content from list");
System.out.println("9.Exit");
input =
scan.nextInt();//input number
if (input ==
9)
break;
else if (input
== 1) {
//creating list
System.out.println("Enter the size of linked
list");
int size = scan.nextInt();
obj.createList(size);
obj.printList();
} else if
(input == 2) {
//sorting
obj.sort();
System.out.println("List after sorting.");
obj.printList();
} else if
(input == 3) {
obj.printList();
} else if (input
== 4) {
//reversing
obj.reverseList();
System.out.println("List after reversing");
} else if
(input == 5) {
List<Integer> list =
obj.generateEvenList();
int n = list.size();
for (int i = 0; i < n; ++i)
System.out.println(list.get(i));
} else if (input
== 6) {
System.out.println("Enter nth number to
display");
int n = scan.nextInt();
obj.printNthNumber(n);
} else if (input
== 7) {
System.out.println("Enter the number to
delete");
int number = scan.nextInt();
System.out.println("List before delete");
obj.printList();
obj.deleteNumber(number);
System.out.println("List after delete");
obj.printList();
}
else
if(input==8)
{
obj.clearListContent();
}
else
System.out.println("Invalid input. enter numbers
from 1 to 9");
}
}
}
---------------------
output:
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
1
Enter the size of linked list
5
Size of linked list: 5
13
53
61
28
78
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
2
List after sorting.
Size of linked list: 5
13
28
53
61
78
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
4
78
61
53
28
13
List after reversing
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
5
28
78
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
6
Enter nth number to display
2
28
61
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
7
Enter the number to delete
28
List before delete
Size of linked list: 5
13
28
53
61
78
List after delete
Size of linked list: 4
13
53
61
78
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
8
Welcome To Assignment:
Please choose an option
1.Create LinkedList of size
2.Sort the linked list
3.Print the list
4.Print the reverse list
5.print the even list
6.Print the nth Number list
7.Delete the Number
8.Clear content from list
9.Exit
9