import java.util.NoSuchElementException; import java.util.Scanner; /** * Questio
ID: 3691380 • Letter: I
Question
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* Question:
*
* Finish the implementation of the deleteLast() method.
* This method will remove the last node of the list and
* return that node's value.
* deleteLast() should throw a NoSuchElementException if the
* list is empty.
*
* For instance, given the list
* 1 2 3
* deleteHead() would return 3 and the list would then look like this:
* 1 2
*
* You will also need to add the "addToEnd()" method that you
* wrote in question 5. You can simply copy and paste that
* into this class.
*
* Only add code in between the comments that say
* "Begin your code here"
* and
* "End your code here"
*
* or immediately after
* "Paste addToEnd() method here"
*/
public class LinkedList {
// Private inner class Node
private class Node{
int data;
Node link;
@SuppressWarnings("unused")
public Node(){
data = Integer.MIN_VALUE;
link = null;
}
public Node(int x, Node p){
data = x;
link = p;
}
}
// End of Node class
public Node head;
public LinkedList(){
head = null;
}
public int deleteLast() throws NoSuchElementException {
/** Begin your code here **/
/** end your code here **/
}
/** Paste addToEnd() method here **/
public void addToEnd(int data) {
/** Begin your code here **/
if(head == null)
{
head = new Node(data, head);
}
else
{
Node position = head;
while(position.link != null)
{
position = position.link;
}
position.link = new Node(data, null);
}
/** end your code here **/
}
public void printList(){
Node position = head;
if (head == null)
System.out.println("The list is empty.");
while(position != null){
System.out.print(position.data + " ");
position = position.link;
}
System.out.println();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedList list = new LinkedList();
list.printList();
try {
System.out.println("Deleting " + list.deleteLast());
} catch (NoSuchElementException e) {
System.out.println("Can't delete. The list is empty!");
}
System.out.println("Enter one number to add to the list");
int firstNum = input.nextInt();
list.addToEnd(firstNum);
System.out.println("The list is:");
list.printList();
try {
System.out.println("Deleting " + list.deleteLast());
} catch (NoSuchElementException e) {
System.out.println("Can't delete. The list is empty!");
}
list.printList();
System.out.println("How many values do you want to add to the list?");
int toAdd = input.nextInt();
for(int i = 0; i < toAdd; i++) {
System.out.println("Enter value " + (i + 1));
list.addToEnd(input.nextInt());
}
list.printList();
try {
System.out.println("Deleting " + list.deleteLast());
} catch (NoSuchElementException e) {
System.out.println("Can't delete. The list is empty!");
}
list.printList();
input.close();
}
}
Explanation / Answer
package com.chegg.testing;
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* Question:
*
* Finish the implementation of the deleteLast() method. This method will remove
* the last node of the list and return that node's value. deleteLast() should
* throw a NoSuchElementException if the list is empty.
*
* For instance, given the list 1 2 3 deleteHead() would return 3 and the list
* would then look like this: 1 2
*
* You will also need to add the "addToEnd()" method that you wrote in question
* 5. You can simply copy and paste that into this class.
*
* Only add code in between the comments that say "Begin your code here" and
* "End your code here"
*
* or immediately after "Paste addToEnd() method here"
*/
public class LinkedList {
// Private inner class Node
private class Node {
int data;
Node link;
@SuppressWarnings("unused")
public Node() {
data = Integer.MIN_VALUE;
link = null;
}
public Node(int x, Node p) {
data = x;
link = p;
}
}
// End of Node class
public Node head;
public LinkedList() {
head = null;
}
public int deleteLast() throws NoSuchElementException {
/** Begin your code here **/
if (head == null) {
throw new NoSuchElementException();
} else {
if (head.link == null) // handle when head is the only node
{
// You must store the data somewhere since head has to be set to
// NULL.
int dataToReturn = head.data;
// Since head is the only node, set it to NULL now.
head = null;
// Now return the data the the last node (head in this case)
// contained.
return dataToReturn;
}
Node position = head;
Node temp = head; // temp has to be initialized to something
int dataToDelete=0;
while (position.link != null) {
temp = position; // safe keep current position
position = position.link; // update position pointer to get the
// next value
}
if(position.link==null)
{
dataToDelete= position.data;
}
position = null;
temp.link = null;// this is what deletes the last node.
return dataToDelete;
}
/** end your code here **/
}
/** Paste addToEnd() method here **/
public void addToEnd(int data) {
/** Begin your code here **/
if (head == null) {
head = new Node(data, head);
} else {
Node position = head;
while (position.link != null) {
position = position.link;
}
position.link = new Node(data, null);
}
/** end your code here **/
}
public void printList() {
Node position = head;
if (head == null)
System.out.println("The list is empty.");
while (position != null) {
System.out.print(position.data + " ");
position = position.link;
}
System.out.println();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
LinkedList list = new LinkedList();
list.printList();
try {
System.out.println("Deleting " + list.deleteLast());
} catch (NoSuchElementException e) {
System.out.println("Can't delete. The list is empty!");
}
System.out.println("Enter one number to add to the list");
int firstNum = input.nextInt();
list.addToEnd(firstNum);
System.out.println("The list is:");
list.printList();
try {
System.out.println("Deleting " + list.deleteLast());
} catch (NoSuchElementException e) {
System.out.println("Can't delete. The list is empty!");
}
list.printList();
System.out.println("How many values do you want to add to the list?");
int toAdd = input.nextInt();
for (int i = 0; i < toAdd; i++) {
System.out.println("Enter value " + (i + 1));
list.addToEnd(input.nextInt());
}
list.printList();
try {
System.out.println("Deleting " + list.deleteLast());
} catch (NoSuchElementException e) {
System.out.println("Can't delete. The list is empty!");
}
list.printList();
input.close();
}
}
Output:
The list is empty.
Can't delete. The list is empty!
Enter one number to add to the list
7
The list is:
7
Deleting 7
The list is empty.
How many values do you want to add to the list?
4
Enter value 1
2
Enter value 2
6
Enter value 3
4
Enter value 4
8
2 6 4 8
Deleting 8
2 6 4
The list is empty.
Can't delete. The list is empty!
Enter one number to add to the list
4
The list is:
4
Deleting 4
The list is empty.
How many values do you want to add to the list?
3
Enter value 1
1
Enter value 2
2
Enter value 3
3
1 2 3
Deleting 3
1 2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.