import java.util.NoSuchElementException; import java.util.Scanner; /** * Questio
ID: 3691379 • Letter: I
Question
import java.util.NoSuchElementException;
import java.util.Scanner;
/**
* Question 10:
*
* Finish the implementation of the deleteHead() method.
* This method will remove the head (first element) of the list and
* return the head node's value.
* deleteHead() should throw a NoSuchElementException if the
* list is empty.
*
* For instance, given the list
* 1 2 3
* deleteHead() would return 1 and the list would then look like this:
* 2 3
*
* 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 LinkedList10 {
// 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 LinkedList10(){
head = null;
}
public int deleteHead() 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);
LinkedList10 list = new LinkedList10();
list.printList();
try {
System.out.println("Deleting " + list.deleteHead());
} catch (NoSuchElementException e) {
System.out.println("Can't delete. The list is empty!");
}
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.deleteHead());
} catch (NoSuchElementException e) {
System.out.println("Can't delete. The list is empty!");
}
list.printList();
input.close();
}
}
Explanation / Answer
LinkedList10.java
import java.util.NoSuchElementException;//package for Exception class
import java.util.Scanner;//keyboard inputting class
public class LinkedList10 {
public int data;//data
public Node head;
// Private inner class Node
private class Node{
int data;//data value for node
Node link;//address for next node
@SuppressWarnings("unused")
public Node(){//constructor with out any arguments
data = Integer.MIN_VALUE;
link = null;
}
public Node(int x, Node p){
data = x;//constructor with two args
link = p;
}
}
// End of Node class
public LinkedList10(){
head = null;
}
public int deleteHead() throws NoSuchElementException {
/** Begin your code here **/
if (head == null) throw new NoSuchElementException();
int o = head.data;//returns the headdata
head = head.link;//addressof head
data--;//deleiting head data
return o;
}
/** Paste addToEnd() method here **/
public void addToEnd(int data) {
/** Begin your code here **/
if(head == null)
{
head = new Node(data, head);//adding data if head is null
}
else
{
Node position = head;
while(position.link != null)
{//inserting data to the end of the node
position = position.link;
}
position.link = new Node(data, null);
}
/** end your code here **/
}
public void printList(){//ddisplay values
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) {//driver program
Scanner input = new Scanner(System.in);//keyboard inputting
LinkedList10 list = new LinkedList10();
list.printList();
try {
System.out.println("Deleting " + list.deleteHead());//calling deletehead
} catch (NoSuchElementException e) {
System.out.println("Can't delete. The list is empty!");
}
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));//addingvalues to node
list.addToEnd(input.nextInt());
}
try {
System.out.println("Deleting " + list.deleteHead());//calling deleteHead
} catch (NoSuchElementException e) {
System.out.println("Can't delete. The list is empty!");
}
list.printList();//displaay values
input.close();
}
}
output
The list is empty.
Can't delete. The list is empty!
How many values do you want to add to the list?
5
Enter value 1
23
Enter value 2
12
Enter value 3
1
Enter value 4
56
Enter value 5
7
Deleting 23
12 1 56 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.