JAVA PROGRAM Please help with the following Data Structures assignment, as the i
ID: 3753066 • Letter: J
Question
JAVA PROGRAM
Please help with the following Data Structures assignment, as the instructions follow. The files used are included.
Please help.
Given the two java files aNode.java and aLinkedList.java each has a class that will be used to create a linked list.
What your job is... for the aLinkedList.java file in a separate document explain what each line/part of the code is doing. This needs to be done as if the person that is reading it has a little programming experience. It also needs to be done in a professional manor not as if you sending an email.
Also for each file you are to comment the code so it is more clear as to what each part is doing. In the appropriate places comment in the Java Doc format.
Once you have completed these tasks write a main driver that will test all methods of the aLinkedList class.
aNode.java
class aNode
{
protected String data;
protected aNode link;
public aNode()
{
link = null;
data = "";
}
public aNode(String d,aNode n)
{
data = d;
link = n;
}
public void setLink(aNode n)
{
link = n;
}
public void setData(String d)
{
data = d;
}
public aNode getLink()
{
return link;
}
public String getData()
{
return data;
}
}
aLinkedList.java
Pageof 2
ZOOM
class aLinkedList
{
protected aNode start;
protected aNode end ;
public int size ;
public aLinkedList()
{
start = null;
end = null;
size = 0;
}
public boolean isEmpty()
{
return start == null;
}
public int getSize()
{
return size;
}
public void insertAtStart(String val)
{
aNode nptr = new aNode(val, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
public void insertAtEnd(String val)
{
aNode nptr = new aNode(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
public void insertAtPos(String val , int pos)
{
aNode nptr = new aNode(val, null);
aNode ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
aNode tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getLink();
size--;
return ;
}
if (pos == size)
{
aNode s = start;
aNode t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
aNode ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
aNode tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
}
When you are done you will need to uploaded four (4) files:
Your document file
Your aNode.java file (that has been commented)
Your aLinkedList.java file (that has been commented)
Your driver.java file
Explanation / Answer
package cheggSolution1;
// this is a linked list class
//which contains objects of type aNode linked together by aNode's link attribute
// so every aNode object points to another aNode object through link attribute
// this contains the chain of aNode objects linked through link attrbute of every aNode object
class aLinkedList
{
// this stores the ist aNode object in linkedList
protected aNode start;
//this points to last aNode object present in LinkedList classt
protected aNode end ;
// this stores the count of aNode objects present in linked list
public int size ;
// this is default constructor which initializes linked list to default values
public aLinkedList()
{
// start and end null means nothing is present at start and end of linkedlist
start = null;
end = null;
size = 0;
}
// this method returns true if linkedlist is empty otherwise false
// linked list is empty when nothing is present at start
public boolean isEmpty()
{
return start == null;
}
//this method returns the size of linkedlist
public int getSize()
{
return size;
}
//this method inserts the element with value val at begining of linked list
public void insertAtStart(String val)
{
// we first create object of type aNode with value val and its next link to null
// because linked list stores aNode objects
aNode nptr = new aNode(val, null);
//here we increase size because we inserted new element in linkedlist
size++ ;
//this checks if linkedlist is empty if so
if(start == null)
{
//we update start to newly created object because we have to insert new object at begining
// so now start is nptr
start = nptr;
//since only ony ob=ne element so end will also be nptr
end = start;
}
//else if linked list is not empty
else
{
//then we connect start to nptr, so now nptr is pointing to what start was pointing to
nptr.setLink(start);
// and update start to nptr, so now start is pointing to nptr
// suppose we had linkedlist as start->1->2->3->null and we had to add 0 at begining
// we pointed 0 to what start was pointing so both start and 0 pointing to same 0->1->2->3-null
// and updated start to point to 0 so start->0->1->2->3->null
start = nptr;
}
}
//
public void insertAtEnd(String val)
{
aNode nptr = new aNode(val,null);
size++ ;
//likewise if linkedlist empty do same as above
if(start == null)
{
start = nptr;
end = start;
}
else
{
//else update end to nptr
end.setLink(nptr);
end = nptr;
}
}
// this method inserts the element at specific position in linkedlist
public void insertAtPos(String val , int pos)
{
//we ist create aNode object from value to be inserted
aNode nptr = new aNode(val, null);
// we hold start in another variable so that if we modify variable start wont change
aNode ptr = start;
//since we count from 0 so update position to position - 1
pos = pos - 1 ;
//loop untill we reach to position pos
for (int i = 1; i < size; i++)
{
//when we reach to position pos
if (i == pos)
{
//store current nodes next in temporary variable to link it to node to insert
aNode tmp = ptr.getLink() ;
//link to current nodes next to new value to
ptr.setLink(nptr);
//link remaining chain to new node inserted
nptr.setLink(tmp);
//break the loop
break;
}
//since ptr was holding start initially
//each iteration of loop will move it forwads
//because it is updated to ptr's next
ptr = ptr.getLink();
}
size++ ;
}
// it deletes the node present at specified position
public void deleteAtPos(int pos)
{
// if we have to delete the ist node we just update the start to next node
if (pos == 1)
{
start = start.getLink();
// and decrease the size
size--;
return ;
}
//if we have to delete last node
if (pos == size)
{
// we store start and end in temporary variables
aNode s = start;
aNode t = start;
// whille we dont reach to end
while (s != end)
{
t = s;
//update s to point to next node
s = s.getLink();
}
//we update end to last element before end
end = t;
// and set next node of end to null
end.setLink(null);
size --;
return;
}
// id=f node to be deleted is in betwwen start and end
aNode ptr = start;
//decrease position by 1
pos = pos - 1 ;
//delete same as deleteAtPost()
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
aNode tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
//decrease size by 1
size-- ;
}
}
package cheggSolution1;
//this is node class which stores int element and link to another object of same type
class aNode
{
// this stores int value
protected String data;
// this stores link to another object of following current object
protected aNode link;
// this is a default constructor which initalizes object to default values
public aNode()
{
link = null;
data = "";
}
// this intializes object with data nd link
public aNode(String d,aNode n)
{
data = d;
link = n;
}
//rest are getters and setters which just return data and update them
public void setLink(aNode n)
{
link = n;
}
public void setData(String d)
{
data = d;
}
public aNode getLink()
{
return link;
}
public String getData()
{
return data;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.