Add your own method (or use one or more of the existing methods) to insert the...

80.2K

Verified Solution

Question

Programming

Add your own method (or use one or more of the existing methods)to insert the following set of numbers (1, 5, 19, 7, 23, 17, 2) ina linked list (use one function-call per number, and preserve theorder of insertion). Once inserted print the linked list such thatthe output displays the numbers in reverse order. (2, 17, 23, 7,19, 5, 1)

JAVA CODE BELOW

class aNode {

char data;
aNode next;

aNode(char mydata) { // Constructor
data = mydata;
next = null;
}
};
//-----------------------------------------------------

class linkedList {

aNode head; // Head of the linked list
int size;

linkedList() { // Constructor
head = null;
size = 0;
}
//-----------------------------------------------------

public void insert_at_beginning(char value) {
aNode newNode = new aNode(value); // create aNew node
newNode.next = head;
head = newNode;
size++;
}
//-----------------------------------------------------

public void insert_at_end(char value) {
aNode newNode = new aNode(value); // create aNew node
if (isEmpty()) {
newNode.next = head;
head = newNode;
size++;
} else {
//find the last node
aNode ptr;
ptr = head;
while (ptr.next != null) {
ptr = ptr.next;
}
ptr.next = newNode; //add the node to the end
size++;
}
}
//-----------------------------------------------------

public void insert_after(char value, char searchValue){
if (isEmpty()) {
System.out.println(\"Linked List is empty, no way to insert \" +value + \" after \" + searchValue);
} else {
//find the node with searchValue
aNode ptr;
boolean found = false;
ptr = head;
while (ptr != null && found == false) {
if (ptr.data == searchValue) {
found = true;
} else {
ptr = ptr.next;
}
}
if (ptr == null) {
System.out.println(\"Did not find \" + searchValue + \"NothingInserted\");
} else {
aNode newNode = new aNode(value); // create aNew node
newNode.next = ptr.next;
ptr.next = newNode; //add the node after the searchValue
size++;
}
}
}
//-----------------------------------------------------
// Delete the first node with the value

public void delete(char deleteValue) {
if (isEmpty()) {
System.out.println(\"Linked List is empty, nothing todelete\");
} else {
aNode deletePtr = head; // create a reference to head
if (head.data == deleteValue) {
head = head.next; // remove the head
deletePtr = null; // make the node available for garbagecollection.
size--;
} else {
aNode prevPtr;
deletePtr = prevPtr = head;
boolean found = false;
//find the value to be deleted
while (deletePtr != null && found == false) {
if (deletePtr.data == deleteValue) { // Read about the differencebetween == and .equals()
found = true;
prevPtr.next = deletePtr.next;
deletePtr = null; // make deletePtr available to garbagecollection
size--;
} else {
prevPtr = deletePtr;
deletePtr = deletePtr.next;
}
}
if (found == false) {
System.out.println(\"Not able to find/delete \" + deleteValue + \" inthe Linked List\");
}
}
}
}
//-----------------------------------------------------

public boolean isEmpty() {
if (head == null) {
return (true);
} else {
return (false);
}
}
//-----------------------------------------------------

public void print() {
aNode ptr;
ptr = head;
System.out.print(\"Head--> \");
while (ptr != null) {
System.out.print(ptr.data + \" --> \");
ptr = ptr.next;
}
System.out.println(\"NULL\");
}
//-----------------------------------------------------

public int getSize() {
return (size);
}
//-----------------------------------------------------

public void freeAll() {

aNode freePtr = head;
while (head != null) {
head = head.next;
// the next two lines are unnecessary, but are included for
// illustration of how memory is freed up
//
freePtr = null; // make the node available for garbagecollector
freePtr = head; // now let the freePtr to the new head
}
head = null;
size = 0;

Answer & Explanation Solved by verified expert
3.8 Ratings (509 Votes)
Please find the answer belowPlease do comments in case of any issue Also dont forget to ratethe question Thank You So MuchLinkListTestjavapackage c12class aNode int data aNode next aNodeint mydata Constructor data mydata next null class linkedList aNode head Head of the linked list int size linkedList Constructor head null size 0 public void insertatbeginningint value aNode newNode new aNodevalue create aNew node newNodenext head head newNode size public void insertatendint value aNode newNode new aNodevalue create aNew node if isEmpty newNodenext head head newNode size else find the    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students