In: Computer Science
JAVA: Provide two different implementations, an array and a linked list, to maintain a list of names (two separate programs).The following operations are available: insert rear, insert front, remove a particular element, and print the whole list. Do not implement an ADT(Do not use a class with data and operations) Just set up a fixed size array or a linked list of nodes in main and provide code in main or functions/static methods to perform insert, remove, and print. You can set up a menu or hard code some test cases to test your implementations.
I need JAVA code that's able to run.
Array Implementation:
create a class file named as 'ArrayImplementation .java' then copy and paste the below code and run it
import java.util.Scanner;
public class ArrayImplementation {
static int SIZE = 5;
static String list[] = new String[SIZE];
static int end=0;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
int choice;
while(true) {
System.out.println("Enter the Corresponding number of your choices
below :");
System.out.println("1.Insert at Front\n2.Insert at Rear\n3.Remove
particular element\n4.Print all Names\n5.Exit");
choice =
input.nextInt();
if(choice==5)
{
System.out.println("Exited....");
break;
}
switch(choice)
{
case 1:
insertFront();
break;
case 2:
insertRear();
break;
case 3:
removeParticular();
break;
case 4:
printAll();
break;
default:
System.out.println("Invalid
Input");
break;
}
}
}
static String getNameFromUser() {
System.out.println("Enter the name
: ");
String name = input.next();
return name;
}
static void insertRear() {
if(end<SIZE) {
String name =
getNameFromUser();
list[end++] =
name;
}
else
System.out.println("Array is Full");
}
static void insertFront() {
if(end<SIZE) {
String name =
getNameFromUser();
for(int
i=end;i>0;i--) {
list[i]=list[i-1];
}
list[0] =
name;
end++;
}
else
System.out.println("Array is Full");
}
static void removeParticular() {
boolean found = false;
if(end!=0) {
String name =
getNameFromUser();
for(int
i=end-1;i>=0;i--) {
if(list[i].equalsIgnoreCase(name)) {
for(int j=i;j<end-1;j++)
{
list[j] =
list[j+1];
}
end--;
found = true;
System.out.println(name + "
is removed from the Array");
break;
}
}
if(!found)
System.out.println(name + " is not found in the
Array");
}else
System.out.println("Array is empty");
}
static void printAll() {
if(end!=0) {
int i;
for(i=0;i<end-1;i++)
System.out.print(list[i]+"->");
System.out.println(list[i]);
}
else
System.out.println("Array is empty");
}
}
LinkedList:
create a class file named as 'LinkedList .java' then copy and paste the below code and run it
import java.util.Scanner;
class Node
{
protected String data;
protected Node link;
public Node()
{
link = null;
data = null;
}
public Node(String d,Node n)
{
data = d;
link = n;
}
public void setLink(Node n)
{
link = n;
}
public void setData(String d)
{
data = d;
}
public Node getLink()
{
return link;
}
public String getData()
{
return data;
}
}
public class LinkedList
{
protected Node start;
protected Node end ;
public int size ;
public LinkedList()
{
start = null;
end = null;
size = 0;
}
public boolean isEmpty()
{
return start == null;
}
public int getSize()
{
return size;
}
public void insertAtStart(String val)
{
Node nptr = new Node(val, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
public void insertAtEnd(String val)
{
Node nptr = new Node(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getLink();
size--;
return ;
}
if (pos == size)
{
Node s = start;
Node t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
public void display()
{
System.out.print("\nSingly Linked List = ");
if (size == 0)
{
System.out.print("empty\n");
return;
}
if (start.getLink() == null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.print(start.getData()+ "->");
ptr = start.getLink();
while (ptr.getLink() != null)
{
System.out.print(ptr.getData()+ "->");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ "\n");
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
LinkedList list = new LinkedList();
System.out.println("Enter the Corresponding number of your choices
below :");
char ch;
while(true)
{
System.out.println("1. Insert at front");
System.out.println("2. Insert at rear");
System.out.println("3. Delete at position");
System.out.println("4. Print All");
System.out.println("5. Exit");
int choice = scan.nextInt();
if(choice==5) {
System.out.println("Exited....");
break;
}
switch (choice)
{
case 1 :
System.out.println("Enter the name:");
list.insertAtStart( scan.next() );
break;
case 2 :
System.out.println("Enter the name:");
list.insertAtEnd( scan.next() );
break;
case 3 :
System.out.println("Enter position:");
int p = scan.nextInt() ;
if (p < 1 || p > list.getSize() )
System.out.println("Invalid position\n");
else
list.deleteAtPos(p);
break;
case 4:
list.display();
break;
default :
System.out.println("Invalid option \n ");
break;
}
}
}
}
ScreenShots:
Note:
These code are created and verified on the Eclipse IDE,you can use any IDE to run the Code.
Hope this will help you to learn more.Happy Learning!!!