public class Linklist {
Private Node Firstnode;
private Integer position;
Public linklist () {
Super ();
}
Public linklist (Node firstnode) {
Super ();
This.firstnode = Firstnode;
}
Public Node Getfirstnode () {
return firstnode;
}
public void Setfirstnode (Node firstnode) {
This.firstnode = Firstnode;
}
Public Integer getPosition () {
return position;
}
public void SetPosition (Integer position) {
This.position = position;
}
/**
* No positional parameters: added at the end of the list
*
* @param node
* @return
*/
public linklist Add (node node) {
Start from the beginning, find out in turn, until you know it's empty.
Node CurrentNode = This.firstnode;
if (firstnode.getnextnode () = = null) {
Node.setpriornode (Firstnode);
Firstnode.setnextnode (node);
return this;
}
while (currentnode.getnextnode () = null) {
CurrentNode = Currentnode.getnextnode ();
}
Currentnode.setnextnode (node);
Currentnode.getpriornode (). Setnextnode (CurrentNode);
Node.setpriornode (CurrentNode);
return this;
}
/**
* Add a node at the specified location
*
* @param node
* @param position
* @return
*/
public boolean Add (node node, int position) {
int count = 0;
Node CurrentNode = This.firstnode;
if (position = = 1) {
if (firstnode.getnextnode () = = null) {
Node.setpriornode (Firstnode);
Firstnode.setnextnode (node);
return true;
}
Node.setnextnode (Firstnode.getnextnode ());
Node.setpriornode (Firstnode);
Firstnode.setnextnode (node);
return true;
}
while (count < position) {
if (currentnode.getnextnode () = = null) {
if (count = = position-1) {
Add (node);
return true;
}
return false;
}
CurrentNode = Currentnode.getnextnode ();
count++;
}
Currentnode.getpriornode (). Setnextnode (node);
Node.setpriornode (Currentnode.getpriornode ());
Node.setnextnode (CurrentNode);
Currentnode.setpriornode (node);
Node.setpriornode (CurrentNode);
return true;
}
/**
* Remove node at the specified location
*
* @param position
* @return
*/
public boolean remove (int position) {
Node canditate = find (position);
if (canditate! = null && canditate.getnextnode () = null) {
Canditate.getpriornode (). Setnextnode (Canditate.getnextnode ());
Canditate.getnextnode (). Setpriornode (Canditate.getpriornode ());
} else if (canditate! = null && canditate.getnextnode () = = null) {
Canditate.getpriornode (). Setnextnode (NULL);
} else {
return false;
}
return true;
}
/**
* Find node
*
* @param position
* @return
*/
public Node Find (int position) {
int count = 0;
node Node = This.firstnode;
while (Count < position) {
if (Node.getnextnode ()! = null) {
& Nbsp; node = Node.getnextnode ();
}
if (node.getnextnode () = = null && count < position-1) {
return null;
}
count++;
}
return node;
}
/**
* Update node information based on location
*
* @param newvalue
* @param position
* @return
*/
public boolean update (String newvalue, int position) {
Node canditate = find (position);
if (canditate! = null) {
Canditate.setnodevalue (NewValue);
} else {
return false;
}
return true;
}
}
Package projectlinklist;
/**
* Entity class
*nodevalue is the value of the node
*nextnode for the next node
*/
public class Node {
Private Node Priornode;
Public Node Getpriornode () {
return priornode;
}
public void Setpriornode (Node priornode) {
This.priornode = Priornode;
}
Private String NodeValue;
Private Node NextNode;
Public String Getnodevalue () {
return nodevalue;
}
Public Node (String nodevalue) {
Super ();
This.nodevalue = NodeValue;
}
Public Node () {
Super ();
}
public void setNodeValue (String nodevalue) {
This.nodevalue = NodeValue;
}
Public Node Getnextnode () {
return nextnode;
}
public void Setnextnode (Node nextnode) {
This.nextnode = NextNode;
}
}
I wrote a single linked list with a little bit like a pointer to Java.