Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming Activity 14-2 =====================================================

ID: 3599354 • Letter: P

Question

Programming Activity 14-2

=====================================================

/* LinkList
* Anderson, Franceschi
*/

/**
* this class is a concrete implementation of the AbstractList.
*
* properties of this implementation are such that: - the list is singly linked
* - data contained in the nodes is limited to integers - nodes are sorted in
* ascending order of data - duplicate data is allowed - note that duplicate
* data may cause inconsistent behavior in the Visualizer because the delete
* method searches for the first instance of data. if a node besides the first
* one is highlighted, the first one will still be deleted.
*/
public class LinkList extends AbstractList
{
private Node head = null;

public LinkList()
{
    super(500, 400);
    v.drawList(head);
}

public LinkList(Node head)
{
    super(500, 400); // set size for visualizer
    // set up the list
    head = head;

    animate(head);
}

public void insert(int i)
{
    // ***** Student writes the body of this method *****

    // code the insert method of a linked list of ints
    // the int to insert in the linked list is i

    // we call the animate method inside the body of this method
    // as you traverse the list looking for the place to insert,
    // call animate as follows:

    //    animate(head, current);
    // where    head is the instance variable head of the linked list
    //          current is the node that you are visiting

    // you can start coding now

    // in order to improve the animation (this is optional):
    // just before inserting, i.e. connecting the nodes,
    // make the call

    //    animate(head, previous, Visualizer.ACTION_INSERT_AFTER);

    // where    head is the instance variable head of the linked list
    //          previous is the node (not null) after which to insert

    // if you are inserting at the beginning of the list,
    // just before inserting, make the call

    //    animate(head, head, Visualizer.ACTION_INSERT_BEFORE);

    // where    head is the instance variable head of the linked list
    //
    // Part 1 student code starts here:

    // Part 1 student code ends here.

    numberNodes++;
    // call animate again to show the status of the list
    animate(head);
}

public boolean delete(int i)
{
    // ***** Student writes the body of this method *****

    // code the delete method of a linked list of ints
    // the int to delete in the linked list is i
    // if deletion is successful, return true
    // otherwise, return false

    // we call the animate method inside the body of this method
    // as you traverse the list looking for the node to delete,
    // call animate as follows:

    //    animate(head, current);

    // where    head is the instance variable head of the linked list
    //          current is the node that you are visiting

    // you can start coding now

    // in order to improve the animation (this is optional):
    // just before deleting, i.e. connecting the nodes,
    // make the call

    //    animate(head, current, Visualizer.ACTION_DELETE);

    // where    head is the instance variable head of the linked list
    //          current is the node that you are deleting
    //
    // Part 2 student code starts here:

    // Part 2 student code ends here.

    // At this point, the item has been deleted.
    // Decrement the number of nodes:
    numberNodes--;
    // Call animate again to show the status of the list:
    animate(head);
    return true;
}

public int count()
{
    int n = 0;
    Node current = head;
    while (current != null)
    {
      animate(head, current);
      n++;
      current = current.getNext();
    }
    return n;
}

public void traverse()
{
    traversal = "";
    Node current = head;
    while (current != null)
    {
      animate(head, current);
      traversal += (current.getData() + " ");
      current = current.getNext();
    }
    v.drawList(head);
}

public void clear()
{
    head = null;
    v.drawList(head);
}

public void animate(Node h, Node nd)
{
    v.drawList(h, nd);
    delay(1000);
}

public void animate(Node h)
{
    v.drawList(h);
}

public void animate(Node h, Node nd, int mode)
{
    v.drawList(h, nd, mode);
    delay(1000);
}
}

Explanation / Answer

Programming Activity 14-2
=====================================================
/* LinkList
* Anderson, Franceschi
*/
/**
* this class is a concrete implementation of the AbstractList.
*
* properties of this implementation are such that: - the list is singly linked
* - data contained in the nodes is limited to integers - nodes are sorted in
* ascending order of data - duplicate data is allowed - note that duplicate
* data may cause inconsistent behavior in the Visualizer because the delete
* method searches for the first instance of data. if a node besides the first
* one is highlighted, the first one will still be deleted.
*/
public class LinkList extends AbstractList
{
private Node head = null;
public LinkList()
{
super(500, 400);
v.drawList(head);
}
public LinkList(Node head)
{
super(500, 400); // set size for visualizer
// set up the list
head = head;
animate(head);
}
public void insert(int i)
{
// ***** Student writes the body of this method *****
// code the insert method of a linked list of ints
// the int to insert in the linked list is i
// we call the animate method inside the body of this method
// as you traverse the list looking for the place to insert,
// call animate as follows:
// animate(head, current);
// where head is the instance variable head of the linked list
// current is the node that you are visiting
// you can start coding now
// in order to improve the animation (this is optional):
// just before inserting, i.e. connecting the nodes,
// make the call
// animate(head, previous, Visualizer.ACTION_INSERT_AFTER);
// where head is the instance variable head of the linked list
// previous is the node (not null) after which to insert
// if you are inserting at the beginning of the list,
// just before inserting, make the call
// animate(head, head, Visualizer.ACTION_INSERT_BEFORE);
// where head is the instance variable head of the linked list
//
// Part 1 student code starts here:

Node current = head;

// traverse the linked list and find the suitable place for the data to insert
// traverse till the data is greateer than the number we need to insert
while (current.getData() < i)
{
//animate
animate(head, current);
//get next node
current = current.getNext();
}

//create new node
Node newNode = new Node();
//set data of new node, data = number we need to insert
newNode.data = i;
//next node of the new node will be the next node of the current node
newNode.next = current.getNext();
//animate
animate(head, current, Visualizer.ACTION_INSERT_AFTER);
//set next node of current to the new node, so that new node comes between current and the next node.
current.next = newNode;

// Part 1 student code ends here.
numberNodes++;
// call animate again to show the status of the list
animate(head);
}
public boolean delete(int i)
{
// ***** Student writes the body of this method *****
// code the delete method of a linked list of ints
// the int to delete in the linked list is i
// if deletion is successful, return true
// otherwise, return false
// we call the animate method inside the body of this method
// as you traverse the list looking for the node to delete,
// call animate as follows:
// animate(head, current);
// where head is the instance variable head of the linked list
// current is the node that you are visiting
// you can start coding now
// in order to improve the animation (this is optional):
// just before deleting, i.e. connecting the nodes,
// make the call
// animate(head, current, Visualizer.ACTION_DELETE);
// where head is the instance variable head of the linked list
// current is the node that you are deleting
//
// Part 2 student code starts here:
  
Node current = head;
Node previous = head;

// traverse the linked list and find the number we nee to delete
// check the current node to not be null
while (current!=null && current.getData() != i)
{
//set previous to current node
previous = current;
//animate
animate(head, current);
//get next node
current = current.getNext();
}

//if current is null => the above number is not present in the list.
if(current == null){
return false;
}
//animate for deletion
animate(head, current, Visualizer.ACTION_DELETE);
//we need to set the next node of the previous node to the next node of current node, so that curent node gets deleted from the list
previous.next = current.getNext();

// Part 2 student code ends here.
// At this point, the item has been deleted.
// Decrement the number of nodes:
numberNodes--;
// Call animate again to show the status of the list:
animate(head);
return true;
}
public int count()
{
int n = 0;
Node current = head;
while (current != null)
{
animate(head, current);
n++;
current = current.getNext();
}
return n;
}
public void traverse()
{
traversal = "";
Node current = head;
while (current != null)
{
animate(head, current);
traversal += (current.getData() + " ");
current = current.getNext();
}
v.drawList(head);
}
public void clear()
{
head = null;
v.drawList(head);
}
public void animate(Node h, Node nd)
{
v.drawList(h, nd);
delay(1000);
}
public void animate(Node h)
{
v.drawList(h);
}
public void animate(Node h, Node nd, int mode)
{
v.drawList(h, nd, mode);
delay(1000);
}
}