Please, I would like a thorough answer for this problem. I get the concept but I
ID: 3870746 • Letter: P
Question
Please, I would like a thorough answer for this problem. I get the concept but I am having difficulty implementing it into a code.
Thanks!
Sinlgy and doubly linked lists (Java)
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
Explanation / Answer
package Sample;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Scanner;
public class LinkedList_ADT
{
LinkedList<Integer> al=new LinkedList<Integer>();
Iterator<Integer> itr=al.iterator();
//Iterator<Integer> x = al.descendingIterator();
public LinkedList_ADT()
{
}
public void insert_first(int node)
{
al.addFirst(node);
System.out.println("The Element is Added at first position
successfully");
}
public void insert_last(int node)
{
al.addLast(node);
System.out.println("The Element is Added at last position
successfully");
}
public void remove_first()
{
al.removeFirst();
System.out.println("The Element is Deleted from front end
successfully:");
}
public void remove_last()
{
al.removeLast();
System.out.println("The Element is Deleted from back end
successfully:");
}
public void insert(int position,int node)
{
al.add(position, node);
System.out.println("The Element inserted successfully:");
}
public int Last_elemnt()
{
return al.getLast();
}
public int sizen()
{
return al.size();
}
public void duplicate()
{
}
public void reverse_list()
{
}
public void display()
{
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public static void main(String[] args)
{
LinkedList_ADT obj=new LinkedList_ADT();
Scanner sc=new Scanner(System.in);
int data,position;
System.out.println("****Double Linked List Implementation ***");
while(true)
{
System.out.println("****MENU***");
System.out.println("1.insert node at front of the list");
System.out.println("2.insert node at back of the list");
System.out.println("3.removes first node of the list.");
System.out.println("4.removes last node of the list.");
System.out.println("5.inserts a new node with position");
System.out.println("6.deletes a node with Duplicates");
System.out.println("7.Returns last element of a list.");
System.out.println("8.Size of the List");
System.out.println("9.reverses a linked list");
System.out.println("10.Merging a Linked list:");
System.out.println("11.Display a list:");
System.out.println("Select any one option:");
int opt=sc.nextInt();
switch(opt)
{
case 1:
System.out.println("Enter The Element to insert at front end:");
data=sc.nextInt();
obj.insert_first(data);
break;
case 2:
System.out.println("Enter The lement to Insert at backend:");
data=sc.nextInt();
obj.insert_last(data);
break;
case 3:
System.out.println("Removing The First Node from The
Linked List:");
obj.remove_first();
break;
case 4:
System.out.println("Removing The Last Node from The
Linked List:");
obj.remove_last();
break;
case 5:
System.out.println("Enter position to insert");
position=sc.nextInt();
System.out.println("Enter The New Element:");
data=sc.nextInt();
obj.insert(position,data);
break;
case 6:
System.out.println("Deleting dupplicate Data Elements:");
obj.duplicate();
break;
case 7:
System.out.println("The Last element is"+obj.Last_elemnt());
case 8:
System.out.println("The Size Of Linked List: is");
obj.sizen();
case 9:
System.out.println("The Reversing Of Linked List is:");
obj.reverse_list();
break;
case 10:
System.out.println("The Merging of list");
break;
case 11:
System.out.println("The List is");
obj.display();
default:
System.out.println("Wrong Option!");
}
}
}
}