Question

In: Computer Science

JAVA: Provide two different implementations, an array and a linked list, to maintain a list of...

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.

Solutions

Expert Solution

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!!!


Related Solutions

Java Problem: Array lists and linked lists are both implementations of lists. Give an example of...
Java Problem: Array lists and linked lists are both implementations of lists. Give an example of a situation where an array list would be the better choice and one where a linked list would. Explain the reasons in each case. Provide example.
In java. Using a linked list only, iterator and scanner. Develop a program to maintain a...
In java. Using a linked list only, iterator and scanner. Develop a program to maintain a Linked List of homework assignments name and due date. When an assignment is assigned, add it to the list, and when it is completed, remove it. You should keep track of the due date. Your program should provide the following services each contained within its own method: Add a new assignment. (3 pts) Remove an assignment. (3pts) Provide a list of the assignments in...
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list...
Objective: Learning linked list. Problem Specification:             An employer would like to maintain a linked list for employees, the data stored is ·An employee number (a positive integer) ·A yearly salary (a float). ·Number of dependents (a short positive integer) The employer would like you as the programmer to design and implement a linked list using classes. For each class two files are needed, one to define the class, the other to implement the methods. In addition, the client uses...
how do you add two matrices linked list in java? (am using linked list because 2D...
how do you add two matrices linked list in java? (am using linked list because 2D arrays are not allowed.) ex [1st matrix] 1 3 2 4 2 1 3 2 4 + [2nd matrix] 3 2 3 2 1 4 5 2 3 = [3rd matrix] 4 5 5 6 3 5 8 4 7
In Java In this lab we will creating two linked list classes: one that is a...
In Java In this lab we will creating two linked list classes: one that is a singly linked list, and another that is a doubly linked list ( This will be good practice for your next homework assignment where you will build your own string class using arrays and linked list ) . These LinkedList classes should both be generic classes. and should contain the following methods: Print Add - Adds element to the end of the linked list. IsEmpty...
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...
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in...
Array-Based Linked List Implementation: JAVA Decide how to write the methods with items being stored in an array. NOT in linked List. Implement an array-based Linked List in your language. Use double as the item. You need to create a driver includes several items and inserts them in order in a list. Identify the necessary methods in a List Linked implementation. Look at previous Data Structures (stack or queue) and be sure to include all necessary methods. DO NOT USE...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked...
Java Generic 2D Linked List Problem How to convert a 1D linked List into multiple linked lists with sequential values together? //Example 1: [1,1,2,3,3] becomes [[1,1],[2],[3,3]] //Example 1: [1,1,2,1,1,2,2,2,2] becomes [[1,1],[2],[1,1],[2,2,2,2]] //Example 3: [1,2,3,4,5] becomes [[1],[2],[3],[4],[5]] public <T> List<List<T>> convert2D(List<T> list) { // Given a 1D, need to combine sequential values together. }
Write a java method to swap between two values in a singly linked list
Write a java method to swap between two values in a singly linked list
Objective: Manipulate the Linked List Pointer. Write a java subclass to extend LList.java. Provide a reverse...
Objective: Manipulate the Linked List Pointer. Write a java subclass to extend LList.java. Provide a reverse list method in the subclass to reverse the order of the linked list. Print the original linked list and the reverse ordered linked list at the end of program. You can use the gamescore.txt to test the reverse method. _____________________________________________________________________________________________________________________________________________________ /** Source code example for "A Practical Introduction to Data     Structures and Algorithm Analysis, 3rd Edition (Java)"     by Clifford A. Shaffer     Copyright 2008-2011 by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT