Menu

(Solved) : Participation Activity Must Work Collaboratively Friend Develop Correct Code Programming T Q35630794 . . .

Participation In this activity you must work collaborativelywith a friend to develop correct code for the programming tasks setout below. They are related to the linked list class discussed inlectures.

One person must take on the role of programmer, and the otherthe role of tester. Swap roles so that everyone gains theexperience both as a tester and a programmer.

In the role of programmer you must look at the specification andwrite a method in the linked list class that implements it. In therole of tester, without writing the code (!) you must write a setof JUnit tests that a correctly-written program would pass.

Make sure that the testers and programmers do not collaboratewhilst writing the code and tests, but when both teams are done tryout the tests on the code. Look at each other’s work and comment,write the following questions: Did the code pass the tester’stests? Did the tests cover all the main cases?

Write a program that takes two linked lists (l1 and l2) andinterleaves them so that the returned list is the first node froml1, followed by the rst node from l2, then the second node from l1followed by the second node from l2 etc. If any nodes are leftafter the interleaving then they should be appended.

public SLList interleave (SLList l1, SLList l2);

Write a program that takes a linked list l1 of integers, and aninteger x and removes all the instances of x from the list, leavingthe remaining nodes connected in the same relative order as theywere before.

11

public SLList removeALL (SLList l1, int x);

* This is based on the linked list class provided in DataStructures and Algorithms in
* Java by Adam Drozdek.
* The basic infrastructure has been extended by AnnabelleMcIver.
*/

package lectures;

// A node in an integer singly linked list class

//************************ SLLNode.java*******************************

class SLLNode {
public Object info;    // This is the data
public SLLNode next;    // this is the address of thenext node
public SLLNode() {       // Here’s how weconstruct an empty list.
next = null;
}
public SLLNode(Object el) {
info = el; next = null;
}
public SLLNode(Object el, SLLNode ptr) {
info = el; next = ptr;
}
}

/*********************** SLList.java***************************
* generic singly linked list class with head only
*/

class SLList {
protected SLLNode head = null;
public SLList() {
}
  
public void setToNull() {
head = null;
}
public boolean isEmpty() {
return head == null;
}
public Object first() {
return head.info;
}
public SLLNode head() {
return head;
}
public void printAll() {
for (SLLNode tmp = head; tmp != null; tmp = tmp.next)
System.out.print(tmp.info.toString());
}
public void add(Object el) {
head= new SLLNode(el,head);
}
public Object find(Object el) {
SLLNode tmp = head;
for ( ; tmp != null && !el.equals(tmp.info); tmp =tmp.next);
if (tmp == null)
return null;
else return tmp.info;
}
public Object deleteHead() { // remove the head and return itsinfo;
Object el = head.info;
head = head.next;
return el;
}
public void delete(Object el) { // find and remove el;
if (head != null) // if non-empty list;
if (el.equals(head.info)) // if head needs to be removed;
head = head.next;
else {
SLLNode pred = head, tmp = head.next;
for ( ; tmp != null && !(tmp.info.equals(el));
pred = pred.next, tmp = tmp.next);
if (tmp != null) // if found
pred.next = tmp.next;
}
}
  
public void join (SLList l2) {
   // Precondition: none
   // Postcondition: Links l2 to the end of the currentlist
   // The performance of this depends on the length ofthe current list, but not l2. How do we test this?
   SLList temp= this;
   if (temp.isEmpty()) { this.head= l2.head;} // Is thecurrent list empty?
   else { // If not, find the tail of the current listand join it to the top of l2.
       SLLNode t= this.head;
       for ( ; t.next!=null; t=t.next){}// Invariant: t!=null
       t.next= l2.head;      
   }
}
  
public void llreverse() {
   // Precondition: none
   // Postcondition: Reverses the items in the currentlist
   if ((head != null)) {
       SLList x= new SLList();
       Object y= deleteHead(); // removeshead from current list, stores the info in y
       x.add(y);
       llreverse(); // recursive call, butto a smaller instance.
       join(x);
   }
}
  
public void iterativeReverse() { // This just uses a single passthrough the list.
   // Precondition: none
   // Postcondition: Reverses the items in the currentlist
   SLList y= new SLList();
   Object n;
   for (SLLNode x= head; x != null; x= x.next) {
       n= deleteHead();
       y.add(n);
       }
   head= y.head;
}

   public void insert(Object p, Object q){
       // Precondition: p and q are notnull
       // Postcondition: Insert object pafter object q,
       //                  or at theend of the current list if q does not appear in the list
       SLLNode y= new SLLNode(p);// make anew node to insert;
       SLLNode x= head;
       if (x == null) {head= y;}
       else{
           for (; x.next !=null && !x.info.equals(q); x= x.next){}// First find q, ifit’s there.
           // Ontermination x points to the last node, or to a node which has infoequal to q
           if (x.next ==null) {x.next= y;}// just add y to the end.
           else
           {
           y.next=x.next;
           x.next= y;
           }     
       }
   }
  
   public boolean equals(SLList p) {
       // Precondition: p is notnull
       // Postcondition: Returns true iffthe contents of the current list are the same
       // and in the same order as theinput list p
       SLLNode x= head;
       SLLNode y= p.head;
       for(; y!=null && x!=null;y= y.next, x=x.next) {
           if(!x.info.equals(y.info)){ return false;   }
       }
       if (y==null && x==null)return true;
       else { return false; }
   }  
}
  

Expert Answer


Answer to Participation Activity Must Work Collaboratively Friend Develop Correct Code Programming T Q35630794 . . .

OR