Neatbeans Project Create software with three classes: A class for a linked list
ID: 672152 • Letter: N
Question
Neatbeans Project
Create software with three classes:
A class for a linked list
A class for a linked list node
A class for the data object in the linked list.
You software should:
Load items into a linked list of objects from a data file.
Display the list -- you could display key data from the underlying class, such as employee name and number from a class of employees.
Demonstrate that the methods for your operations on a linked list work properly
Your software should include the following methods for operations on a linked list:
check for an empty list.
return a Boolean value if the list is empty.
return data from a node in the list.
return a reference value of the underlying data type
traverse and print the list
access all nodes in the list in order, printing key data
for each element.
find the size of the list
return the number of elements in the list;
insert a node in the list before a specified element
insert a node in the list after a specified element
append a node to the list
add a node after the last element in the list
prepend a node to the list
add a node as the new head of the list
delete the head of the list
delete the last element in the list
delete a node in the list before a specified element
delete a node in the list after a specified element
clone the list (without cloning the underlying data)
clone the list and the underlying data
implement the Comparable() Interface for the list class
compare two lists to see if they are identical
for all nodes in each list, does listA1 == listB1,
listA2 == listB2, and so on.
Explanation / Answer
/* Class List Data */
public class ListData {
int d;
ListData(int n)
{
d=n;
}
public int getVal()
{
return d;
}
public void setVal(int n)
{
d=n;
}
}
------------------------------------------------------------
/* Class Node */
public class ListNode extends ListData
{
protected ListData d;
protected ListNode link;
/* Constructor */
public ListNode()
{
super(0);
link = null;
}
/* Constructor */
public ListNode(int num,ListNode n)
{
super(num);
link = n;
}
/* Function to set link to next Node */
public void setLink(ListNode n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(int num)
{
d=new ListData(num);
}
/* Function to get link to next node */
public ListNode getLink()
{
return link;
}
/* Function to get data from current Node */
public int getData()
{
return getVal();
}
}
-----------------------------------------------------------
/* Class linkedList */
public class LinkedList
{
protected ListNode start;
protected ListNode 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)
{
ListNode nptr = new ListNode(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)
{
ListNode nptr = new ListNode(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
/* Function to insert an element at position */
public void insertAtPos(int val , int pos)
{
if(pos==1)
{
ListNode nptr = new ListNode(val, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
if(pos == size)
{
ListNode nptr = new ListNode(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
ListNode nptr = new ListNode(val, null);
ListNode ptr = start;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
ListNode tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
/* Function to delete an element at position */
public void deleteStart()
{
start = start.getLink();
size--;
}
public void deleteEnd()
{
ListNode s = start;
ListNode t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
}
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getLink();
size--;
}
if (pos == size)
{
ListNode s = start;
ListNode t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
ListNode ptr = start;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
ListNode tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
/* Function to display elements */
public void display()
{
System.out.print(" Singly Linked List = ");
if (size == 0)
{
System.out.print("empty ");
return;
}
if (start.getLink() == null)
{
System.out.println(start.getData() );
return;
}
ListNode ptr = start.getLink();
System.out.print(start.getData()+ "->");
while (ptr.getLink() != null)
{
System.out.print(ptr.getData()+ "->");
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ " ");
}
public int search(int element) {
int pos=-1;
if (size == 0)
{
pos=0;
}
if (start.getLink() == null)
{
pos=1;
}
ListNode ptr =start.getLink();
while (ptr.getLink() != null)
{
pos++;
if(element==ptr.getData())
{
break;
}
ptr = ptr.getLink();
}
return pos;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.