/* This class define a linked list that stores integer values. */ public class L
ID: 3751682 • Letter: #
Question
/*
This class define a linked list that stores integer values.
*/
public class LinkedList
{
public Node head, tail;
//constructor method to create a list of object with head, tail, and size.
public LinkedList()
{
head = null;
tail = null;
}
//method add node to end of list
public void addLastNode(int data)
{
if (tail == null)
head = tail = new Node(data); //empty list
else
{
tail.next = new Node(data); //link new node as last node
tail = tail.next; //make tail pointer points to last node
}
}
//======== Your part to complete for this assignment =========
//method #1: add first node
public void addFirstNode(int data)
{
//complete this method
}
//method #2: add node at index
public void addAtIndex(int index, int data)
{
//complete this method
}
//method #3: remove first node
public void removeFirstNode()
{
//complete this method
}
//method #4: remove last node
public void removeLastNode()
{
//complete this method
}
//method #5: remove node at index
public void removeAtIndex(int index)
{
//complete this method
}
//method #6: countNodes
public int countNodes()
{
int listSize= 0;
//complete this method
//this methods returns the list size
return listSize;
}
//method #7: pritnInReverse (Recursive method)
public void printInReverse(Node L)
{
//complete this method as recursive methods
}
//================= end of your part ==============
//method to print out the list
public void printList()
{
Node temp;
temp = head;
while (temp != null)
{
System.out.print(temp.data + " ");
temp = temp.next;
}
}
//class to create nodes as objects
private class Node
{
private int data; //data field
private Node next; //link field
public Node(int item) //constructor method
{
data = item;
next = null;
}
}
}
Explanation / Answer
Please find the modified code below.
CODE
================
/*
This class define a linked list that stores integer values.
*/
public class LinkedList
{
public Node head, tail;
public int size;
//constructor method to create a list of object with head, tail, and size.
public LinkedList()
{
head = null;
tail = null;
size = 0;
}
//method add node to tail of list
public void addLastNode(int data)
{
if (tail == null)
head = tail = new Node(data); //empty list
else
{
tail.next = new Node(data); //link new node as last node
tail = tail.next; //make tail pointer points to last node
}
}
//======== Your part to complete for this assignment =========
//method #1: add first node
public void addFirstNode(int data)
{
Node nptr = new Node(data);
size ++ ;
if(head == null)
{
head = nptr;
tail = head;
}
else
{
nptr.next = head;
head = nptr;
}
}
//method #2: add node at index
public void addAtIndex(int index, int data)
{
Node nptr = new Node(data);
Node ptr = head;
for (int i = 1; i < size; i++)
{
if (i == index)
{
Node tmp = ptr.next ;
ptr.next = nptr;
nptr.next = tmp;
break;
}
ptr = ptr.next;
}
size++ ;
}
//method #3: remove first node
public void removeFirstNode()
{
head = head.next;
size --;
}
//method #4: remove last node
public void removeLastNode()
{
Node s = head;
Node t = head;
while (s != tail)
{
t = s;
s = s.next;
}
tail = t;
tail.next = null;
size --;
}
//method #5: remove node at index
public void removeAtIndex(int index)
{
if (index == 0)
{
head = head.next;
size--;
return ;
}
if (index == size - 1)
{
Node s = head;
Node t = head;
while (s != tail)
{
t = s;
s = s.next;
}
tail = t;
tail.next = null;
size --;
return;
}
Node ptr = head;
for (int i = 1; i < size - 1; i++)
{
if (i == index)
{
Node tmp = ptr.next;
tmp = tmp.next;
ptr.next = tmp;
break;
}
ptr = ptr.next;
}
size-- ;
}
//method #6: countNodes
public int countNodes()
{
int listSize= 0;
//complete this method
//this methods returns the list size
return listSize;
}
//method #7: pritnInReverse (Recursive method)
public void printInReverse(Node L)
{
if (L == null) return;
// print list of head node
printInReverse(L.next);
// After everything else is printed
System.out.print(L.data + " ");
}
//================= tail of your part ==============
//method to print out the list
public void printList()
{
Node temp;
temp = head;
while (temp != null)
{
System.out.print(temp.data + " ");
temp = temp.next;
}
}
//class to create nodes as objects
private class Node
{
private int data; //data field
private Node next; //link field
public Node(int item) //constructor method
{
data = item;
next = null;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.