I wrote a single linked list with a little bit like a pointer to Java.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.