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

In this assignment, you will implement additional member functions for the linke

ID: 3697866 • Letter: I

Question

In this assignment, you will implement additional member functions for the linked list data structure. Following are the member functions you need to implement. You can create additional member functions if you want to. Note: You can also put Node as an inner class in LinkedList class. You can’t use the STL library for this assignment.

LinkedList::LinkedList(const LinkedList& alinked_list)

Copy constructor.

int LinkedList::find(double x)

Searches the linked list and returns the number of times a node with data = x occurs in the linked list.

int LinkedList::search(double x)

Searches the linked list for a specified value (x). Returns the position of first node with data = x. If the value is not found in the linked list, return 0. The first node has position = 1.

void LinkedList::reverse()

Reverses the order of the nodes in the linked list.

void LinkedList::swap(int position1, int position2)

Swaps nodes at the two positions indicated by the two integers - position1 and position2. The first node has position = 1.

void LinkedList::InsertList(LinkedList aList)

Appends all the nodes in the argument LinkedList aList to the calling LinkedList object.

int LinkedList::size()

Returns the number of nodes in a liked list.

bool LinkedList::isSorted()

Returns a boolean true if the linked list is sorted (either descending or ascending) otherwise returns false.

void LinkedList::erase(int position1, int position2)

Deletes all nodes with location starting at position1 through position2. The first node has position = 1. Assume that position2> position1.

void LinkeList::sort()

Sorts the nodes in ascending order. You can implement either a bubble sort or selection sort.

Now write a main function that does the following. If you change the list, display the list after the operation.

Create a linked list (list1) that contains 10 nodes with data: 1, 2, 1, 4, 5, 2, 7, 8, 9, 10.

Create another linked list (list2) from list1 using copy constructor.

Reverse the order of nodes in list2.

Find and display the position of node with data = 4 in list1 and list2.

Deletes the nodes located at positions 5, 6, 7, 8, 9, and 10 in list2.

Test if list1 and list2 are sorted.

Find the number of nodes in list1 and list2.

Find and display the number of times a node with data = 2 occurs in list1 and list2.

Append all the nodes which are in list2 into list1.

Finally sort list1

Explanation / Answer

import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(int d,Node n) { data = d; link = n; } /* Function to set link to next Node */ public void setLink(Node n) { link = n; } /* Function to set data to current Node */ public void setData(int d) { data = d; } /* Function to get link to next node */ public Node getLink() { return link; } /* Function to get data from current Node */ public int getData() { return data; } } /* Class linkedList */ class linkedList { protected Node start; protected Node end ; public int size ; /* Constructor */ public linkedList() { start = null; end = null; size = 0; } /* 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 insertAtStart(int val) { Node nptr = new Node(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 insertAtEnd(int val) { Node nptr = new Node(val,null); size++ ; if(start == null) { start = nptr; } else { end.setLink(nptr); end = nptr; } } /* Function to insert an element at position */ public void insertAtPos(int val , int pos) { Node nptr = new Node(val, null); Node ptr = start; pos = pos - 1 ; for (int i = 1; i list.getSize() ) System.out.println("Invalid position "); else list.deleteAtPos(p); break; case 5 : System.out.println("Empty status = "+ list.isEmpty()); break; case 6 : System.out.println("Size = "+ list.getSize() +" "); 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'); } }