HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Is SList class an ADT?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
adtclassslist

Problem

As per the definition given in lecture, ADT is,

With reference to this definition, I would like to call SList class an ADT following 6 invariants:

  • Multiple users of SList class should be consistent in usage with same SList instance. For example multiple users may insert items in same shopping list.



  • No runtime exceptions while using SList class when list is empty.



  • SList size member variable is always correct.



  • SList is never circularly linked list.



  • SList never returns SListNode, instead it return only an item.



  • Methods of the SlistNode class can be modified in future without any affect to SList class users. I guess this is nothing but encapsulation.



The below two classes will be part of a package and be used by classes from other packages:

class SListNode{
    Object item;
    SListNode next;
    public SListNode(){
        this.item = null;
        this.next = null;
    }
    public SListNode(Object item, SListNode next){
        this.item = item;
        this.next = next;
    }
    public SListNode(Object item){
        this(item,null);
    }
    public void insertAfter(Object item){
        this.next = new SListNode(item,this.next);
    }
    public SListNode nth(int position){
        if(position ==1){
            return this;
        }else if((position < 1) || (this.next == null)){ /* error checking */
            return null;
        }else{
            SListNode tempNode = this.next.nth(position-1);
            return tempNode;
        }
    }

}


```
public class SList {
private SListNode head; // First node in list.
private int size; // Number of items in list.
public SList() { // Here’s how to represent an empty list.
head = null;
size = 0;
}
public void insertFront(Object item) {
head = new SListNode(item, head);
size++;
}
public void insertEnd(Object item){
SListNode lastNode = this.head;
while(lastNode.next != null){
l

Solution

Bug:

public void insertEnd(Object item){
    SListNode lastNode = this.head;
    while(lastNode.next != null){
        lastNode = lastNode.next;
    }
    lastNode.next = new SListNode(item,lastNode.next);
    size++;
}


Make a new list, add to end. this.head is null, so lastNode.next is a NullPointerException.

Bug:

public void insertAt(Object item, int position){
    if (position == 1){
        insertFront(item);
        size++;
    }else if(position < 1){
        System.exit(1);
    }else{
        SListNode currentNode = this.head.nth(position);
        if(currentNode != null){
            currentNode.insertAfter(item);
            size++;
        }else{
            System.exit(1); //position value exceeds 
        }
    }
}


Make new list, insertAt("hello", 1), insertAt("world", 2). The application shuts down.

Please test your code before putting it up for review. You'll get higher grades too, especially if you show your tests to your professor or whoever is teaching you.

Code Snippets

public void insertEnd(Object item){
    SListNode lastNode = this.head;
    while(lastNode.next != null){
        lastNode = lastNode.next;
    }
    lastNode.next = new SListNode(item,lastNode.next);
    size++;
}
public void insertAt(Object item, int position){
    if (position == 1){
        insertFront(item);
        size++;
    }else if(position < 1){
        System.exit(1);
    }else{
        SListNode currentNode = this.head.nth(position);
        if(currentNode != null){
            currentNode.insertAfter(item);
            size++;
        }else{
            System.exit(1); //position value exceeds 
        }
    }
}

Context

StackExchange Code Review Q#63008, answer score: 8

Revisions (0)

No revisions yet.