Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PROJECT This Assignment contains two parts. Part - 1: Implement a singly li

ID: 3890391 • Letter: J

Question

JAVA PROJECT

This Assignment contains two parts.

Part - 1: Implement a singly linked list ADT to store a collection of doubles.

Part - 2: Implement a Double linked List ADT to store a collection of integers.

Your program will include the following member functions:

--- a default constructor linkedList()

-- a custom constructor that takes a value and create a node with that value. Ex: linkedList(Type data)

1. a member function pushFront(data) that inserts a node with data at the front of the list

2. a member function pushBack(data) that appends a node with data at the back of the list

3. a member function popFront() that removes first node of the list.

4. a member function popBack() that removes last node of the list.

5. a member function insert(index, val) that inserts a new node with value "val" at a specific position mentioned by the index argument.

6. a member function deleteDuplicates(val) that deletes a node with that number and all its copies from the list, where these copies can be located anywhere in the list.

7. a member function mtoLastElement(M) that returns Mth to the last element of a list such that when M = 0, the last element of the list is returned.

8. a member function size() that returns the size of the list.

9. a member function reverseList() that reverses a linked list without recreating a temporary copy of this linked list. In other words, your function CAN NOT use the 'new' operator. Here is an example, if a list contains the following data items, 3 -> 5 -> 1 -> 7; this reverse() function will change the list to 7 -> 1 -> 5 -> 3.

10. a MemberFunction mergeLists(linkedList one, linkedList two) which will create a new list that contains the elements of two lists in sorted order(ascending). Assume the two lists are in sorted order for this case

Ex: one: 1 -> 3 -> 7 -> 25 -> 50 (sorted)

two: 5 -> 9 -> 11 - > 12 -> 29 (sorted)

result list : 1 -> 3 -> 5 -> 7 - > 9 -> 11 -> 12 -> 25 -> 29 -> 50 (Sorted)

Submission: submit your all three .java files as a whole zip and Follow the grading, programming rubric while submitting your assignment. Please make sure your programming is interactive with the tests.

Explanation / Answer

Solution: According to the policy of Chegg, only first four parts of a question are to be solved. For other parts, put it under separate question. Following code performs the first 4 parts.

import java.util.Scanner;

/* Class Node */
class SLLNode
{
protected double data;
protected SLLNode link;

/* Constructor */
public SLLNode()
{
link = null;
data = 0;
}
/* Constructor */
public SLLNode(double d,SLLNode n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(SLLNode n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(double d)
{
data = d;
}
/* Function to get link to next node */
public SLLNode getLink()
{
return link;
}
/* Function to get data from current Node */
public double getData()
{
return data;
}
}

/* Class linkedList */
class linkedList
{
protected SLLNode start;
protected SLLNode end ;
public int size ;

/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/*custom constructor*/
public linkedList(double d)
{
SLLNode nptr = new SLLNode(d,null);
start = nptr;
end = nptr;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert an element at begining */
public void pushFront(double val)
{
SLLNode nptr = new SLLNode(val, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
/* Function to insert an element at end */
public void pushBack(double val)
{
SLLNode nptr = new SLLNode(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
/* Function to insert an element at position */
public void insertAtPos(double val , int pos)
{
SLLNode nptr = new SLLNode(val, null);
SLLNode ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
SLLNode tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getLink();
size--;
return ;
}
if (pos == size)
{
SLLNode s = start;
SLLNode t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
SLLNode ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
SLLNode tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
  
public void popFront()
{
double d = end.getData();
System.out.println(d);
deleteAtPos(1);
}
public void popBack()
{
double d = end.getData();
System.out.println(d);
deleteAtPos(size);
}
/* Function to display elements */
public void display()
{
System.out.print(" Singly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (start.getLink() == null)
{
System.out.println(start.getData() );
return;
}
SLLNode 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()+ " ");
}
}

/* Class SinglyLinkedList */
public class SinglyLinkedList
{
public static void main(String[] args)
{   
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedList */
linkedList list = new linkedList();
System.out.println("Singly Linked List Test ");
char ch;
/* Perform list operations */
do
{
System.out.println(" Singly Linked List Operations ");
System.out.println("1. Push Front");
System.out.println("2. Push Back");
System.out.println("3. Pop Front");
System.out.println("4. Pop Back");
System.out.println("5. insert");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter element to insert");
list.pushFront( scan.nextDouble() );   
break;
case 2 :
System.out.println("Enter element to insert");
list.pushBack( scan.nextDouble() );   
break;   
case 3:
list.popFront();
break;
case 4:
list.popBack();
break;
case 5 :
System.out.println("Enter element to insert");
double num = scan.nextDouble() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos <= 1 || pos > list.getSize() )
System.out.println("Invalid position ");
else
list.insertAtPos(num, pos);
break;

default :
System.out.println("Wrong Entry ");
break;   
}
/* Display List */
list.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');   
}
}import java.util.Scanner;

/* Class Node */
class SLLNode
{
protected double data;
protected SLLNode link;

/* Constructor */
public SLLNode()
{
link = null;
data = 0;
}
/* Constructor */
public SLLNode(double d,SLLNode n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(SLLNode n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(double d)
{
data = d;
}
/* Function to get link to next node */
public SLLNode getLink()
{
return link;
}
/* Function to get data from current Node */
public double getData()
{
return data;
}
}

/* Class linkedList */
class linkedList
{
protected SLLNode start;
protected SLLNode end ;
public int size ;

/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/*custom constructor*/
public linkedList(double d)
{
SLLNode nptr = new SLLNode(d,null);
start = nptr;
end = nptr;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert an element at begining */
public void pushFront(double val)
{
SLLNode nptr = new SLLNode(val, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
/* Function to insert an element at end */
public void pushBack(double val)
{
SLLNode nptr = new SLLNode(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
/* Function to insert an element at position */
public void insertAtPos(double val , int pos)
{
SLLNode nptr = new SLLNode(val, null);
SLLNode ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
SLLNode tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getLink();
size--;
return ;
}
if (pos == size)
{
SLLNode s = start;
SLLNode t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
SLLNode ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
SLLNode tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
  
public void popFront()
{
double d = end.getData();
System.out.println(d);
deleteAtPos(1);
}
public void popBack()
{
double d = end.getData();
System.out.println(d);
deleteAtPos(size);
}
/* Function to display elements */
public void display()
{
System.out.print(" Singly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (start.getLink() == null)
{
System.out.println(start.getData() );
return;
}
SLLNode 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()+ " ");
}
}

/* Class SinglyLinkedList */
public class SinglyLinkedList
{
public static void main(String[] args)
{   
Scanner scan = new Scanner(System.in);
/* Creating object of class linkedList */
linkedList list = new linkedList();
System.out.println("Singly Linked List Test ");
char ch;
/* Perform list operations */
do
{
System.out.println(" Singly Linked List Operations ");
System.out.println("1. Push Front");
System.out.println("2. Push Back");
System.out.println("3. Pop Front");
System.out.println("4. Pop Back");
System.out.println("5. insert");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter element to insert");
list.pushFront( scan.nextDouble() );   
break;
case 2 :
System.out.println("Enter element to insert");
list.pushBack( scan.nextDouble() );   
break;   
case 3:
list.popFront();
break;
case 4:
list.popBack();
break;
case 5 :
System.out.println("Enter element to insert");
double num = scan.nextDouble() ;
System.out.println("Enter position");
int pos = scan.nextInt() ;
if (pos <= 1 || pos > list.getSize() )
System.out.println("Invalid position ");
else
list.insertAtPos(num, pos);
break;

default :
System.out.println("Wrong Entry ");
break;   
}
/* Display List */
list.display();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');   
}
}