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.
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
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...
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...
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
Create a generic Linked List that does NOT use the Java library linked list. Make sure...
Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and...
Data Structures on Java Basic Linked List exercises a. Suppose x is a linked-list node and not the last node on the list. What is the effect of the following code fragment? x.next = x.next.next b. Singly Linked List has two private instance variables first and last as that point to the first and the last nodes in the list, respectively. Write a fragment of code that removes the last node in a linked list whose first node is first....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT