patternjavaMinor
Is SList class an ADT?
Viewed 0 times
adtclassslist
Problem
As per the definition given in lecture, ADT is,
With reference to this definition, I would like to call
The below two classes will be part of a package and be used by classes from other packages:
```
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
With reference to this definition, I would like to call
SList class an ADT following 6 invariants:- Multiple users of
SListclass should be consistent in usage with sameSListinstance. For example multiple users may insert items in same shopping list.
- No runtime exceptions while using
SListclass when list is empty.
SListsize member variable is always correct.
SListis never circularly linked list.
SListnever returnsSListNode, instead it return only an item.
- Methods of the
SlistNodeclass can be modified in future without any affect toSListclass 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:
Make a new list, add to end.
Bug:
Make new list,
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.
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.