I made an SListClass
which stands for sinle-linked list class and a node class SListNode
. I'm having a problem with the removeLast
method. When I print out the list of nodes, the first item is still there. I don't understand why
public class SListClass {
private SListNode head;
private double size;
SListClass(){
head = null;
size = 0;
}
SListClass(SListNode node){
head = node;
}
public void insertFront(int item){
head = new SListNode(item, head);
size++;
}
public void addLast(SListNode head, int value){
SListNode first = head;
while(first.next != null){
first = first.next;
}
first.next = new SListNode(value, null);
size++;
}
public void removeFirst(SListNode head){
head = head.next;
}
/*public String toString(){
return String.format(head + "");
}
*/
public String print(){
String result = head.item + " ";
if(head.next != null){
result += head.next.print();
}
return result;
}
public static void main(String[] args) {
SListNode list = new SListNode(21, new SListNode(5, new SListNode(19, null)));
SListClass x = new SListClass(list);
x.insertFront(33);
x.insertFront(100);
x.addLast(list, 123);
x.addLast(list, 9999);
x.removeFirst(list);
System.out.println(x.print());
}
}
output: 100 33 21 5 19 123 9999
The SListNode
class:
public class SListNode {
protected int item;
protected SListNode next;
public SListNode(int item, SListNode next){
this.item = item;
this.next = next;
}
public SListNode(int item){
this(item, null);
}
public int getItem() {
return item;
}
public void setItem(int item) {
this.item = item;
}
public SListNode getNext() {
return next;
}
public void setNext(SListNode next) {
this.next = next;
}
}
Change the removeFirst
to this.head = head.next
. The head
in the parameter list is hiding the class field head
.
Also, consider this: in a removeFirst
method, do you really want a head
parameter, or should you use the head
field instead since that is the real head for the linked list you are trying to update? If you don't want that parameter anymore, just delete the parameter from the method signature; then the field head
is not hidden, so head = head.next
does fine.
First of all, your naming is bad. Every class is a class, so ending the name of a class with Class
is just noise. On the contrary S
doesn't mean anything. If you must explain what SListClass stands for, then it means that the name is bad, and that you should choose another name, which doesn't need any explanation, like SinglyLinkedList
.
The users of your class shouldn't care how the list retains the information. It should never have to pass a Node to any method. Only a value. So the following methods should be modified:
SListClass(SListNode node)
--> SinglyLinkedList(int value)
void addLast(SListNode head, int value)
--> void addLast(int value)
: the list knows what the head node is. It makes no sense to pass it as argument. void removeFirst(SListNode head)
--> void removeFirst()
: the list knows what the first node is. It makes no sense to pass it as argument Once you get the API right, you'll see that everything will be much easier to figure out, because you won't confuse the actual head of the list with the unnecessary head passed as argument.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.