主要思路
完成带头结点的单链表类
1、实现一个结点类,包括数据域和next指针域;
2、单链表主要属性包括:数据域为空的头结点;
3、单链表主要成员方法包括:增(在链表末尾添加一个结点,按照顺序添加一个结点),删(删除指定编号的结点),改(更新某个结点信息),查(按照编号进行查询,查询所有的数据)。
结点类说明
属性
1、no:编号,头结点编号默认为0;
2、name:结点的名字;
3、next:指向下一个结点的指针,头结点next域默认为null;
成员方法
1、Node():构造函数,用于初始化当前结点的信息,next初始为null;
2、toString():重写toString 方便后面输出当前节点的信息;
主要代码
//Node class
class Node{
public int no;
public String name;
public Node next; //next
//constructor function
public Node(int no, String name){
this.no = no;
this.name = name;
}
//overwrite toString
@Override
public String toString(){
return "Node [no = "+ this.no +", name = "+ this.name +"]";
}
}
链表类说明
属性
1、head:头结点,数据为空,默认next为null;
成员方法
1、add():添加结点到链表的末尾,通过辅助指针遍历整个链表,找到最后一个结点,然后将新的结点链接到后面。
//add in the rear
public void add(Node node){
//tmp node help to visit
Node tmp = head;
while(true){
//说明在队尾
if(tmp.next == null){
break;
}
tmp = tmp.next; //后移
}
//add node here
tmp.next = node;
}
2、addByOrder():将结点按照编号顺序添加到链表中,如果存在该编号则添加失败,通过辅助指针遍历找到待添加位置的前一个位置,然后完成结点的添加
//add in order
public void addByOrder(Node node){
//tmp node
Node tmp = head;
int flag = 0; //是否添加的结点已经存在,默认为false
while(true){
//in the rear
if(tmp.next == null){
break;
}
//即插入到该结点后面
if(tmp.next.no > node.no){
break;
}else if(tmp.next.no == node.no){
flag = 1;
break;
}
tmp = tmp.next;
}
//add newNode
if(flag == 0){
node.next = tmp.next;
tmp.next = node;
}
}
3、del():删除指定编号的结点,通过辅助指针找到待删除结点的前一个结点,然后将待删除的结点删除
//del node by number
public void del(int no){
int flag = 0; //默认可以删除
Node tmp = head;
while(true){
if(tmp.next == null){
break;
}
if(tmp.next.no == no){
flag = 1;
break;
}
tmp = tmp.next;
}
//del Node
if(flag == 1){
tmp.next = tmp.next.next;
}else{
System.out.println("the node with no is not exist.");
}
}
4、update():将结点的信息更新,通过辅助指针找到需要更新的结点,然后将信息赋值给该结点
//update the info
public void update(Node node){
Node tmp = head.next;
int flag = 0;
while(true){
if(tmp == null){
break;
}else if(tmp.no == node.no){
flag = 1;
break;
}
tmp = tmp.next;
}
//update info
if(flag == 1){
tmp.name = node.name;
}else{
System.out.println("the node is not exist.");
}
}
5、findByNo():通过给定的编号查询相应的结点,通过辅助指针找到编号匹配的结点,将其返回`
//find by no
public Node findByNo(int no){
Node tmp = head.next;
int flag = 0;
while(true){
if(tmp == null){
break;
}else if(tmp.no == no){
flag = 1;
break;
}
tmp = tmp.next;
}
if(flag == 1){
return tmp;
}else{
return null;
}
}
6、list():遍历访问所有的结点,并且输出对应的信息
//list
public void list(){
if(head.next == null){
System.out.println("list is empty.");
return;
}
Node tmp = head.next;
while(true){
if(tmp == null){
break;
}
System.out.println(tmp);
tmp = tmp.next;
}
}
完整代码
import java.io.*;
//Author:Peiliang Gong
//DataStructure:[SingleLinkedList]
//Node class
class Node{
public int no;
public String name;
public Node next; //next
//constructor function
public Node(int no, String name){
this.no = no;
this.name = name;
}
//overwrite toString
@Override
public String toString(){
return "Node [no = "+ this.no +", name = "+ this.name +"]";
}
}
//SingleLinkedList class
class SingleLinkedList
{
private Node head = new Node(0, ""); //head Node
//add in the rear
public void add(Node node){
//tmp node help to visit
Node tmp = head;
while(true){
//说明在队尾
if(tmp.next == null){
break;
}
tmp = tmp.next; //后移
}
//add node here
tmp.next = node;
}
//add in order
public void addByOrder(Node node){
//tmp node
Node tmp = head;
int flag = 0; //是否添加的结点已经存在,默认为false
while(true){
//in the rear
if(tmp.next == null){
break;
}
//即插入到该结点后面
if(tmp.next.no > node.no){
break;
}else if(tmp.next.no == node.no){
flag = 1;
break;
}
tmp = tmp.next;
}
//add newNode
if(flag == 0){
node.next = tmp.next;
tmp.next = node;
}
}
//del node by number
public void del(int no){
int flag = 0; //默认可以删除
Node tmp = head;
while(true){
if(tmp.next == null){
break;
}
if(tmp.next.no == no){
flag = 1;
break;
}
tmp = tmp.next;
}
//del Node
if(flag == 1){
tmp.next = tmp.next.next;
}else{
System.out.println("the node with no is not exist.");
}
}
//update the info
public void update(Node node){
Node tmp = head.next;
int flag = 0;
while(true){
if(tmp == null){
break;
}else if(tmp.no == node.no){
flag = 1;
break;
}
tmp = tmp.next;
}
//update info
if(flag == 1){
tmp.name = node.name;
}else{
System.out.println("the node is not exist.");
}
}
//find by no
public Node findByNo(int no){
Node tmp = head.next;
int flag = 0;
while(true){
if(tmp == null){
break;
}else if(tmp.no == no){
flag = 1;
break;
}
tmp = tmp.next;
}
if(flag == 1){
return tmp;
}else{
return null;
}
}
//list
public void list(){
if(head.next == null){
System.out.println("list is empty.");
return;
}
Node tmp = head.next;
while(true){
if(tmp == null){
break;
}
System.out.println(tmp);
tmp = tmp.next;
}
}
//main function
public static void main (String[] args) throws java.lang.Exception
{
//test
Node node1 = new Node(1,"Jack");
Node node2 = new Node(2,"Tom");
Node node3 = new Node(3,"Peter");
Node node4 = new Node(4,"Bob");
SingleLinkedList linkedlist = new SingleLinkedList();
// Test add function
// linkedlist.add(node1);
// linkedlist.add(node2);
// linkedlist.add(node4);
// Test addByOrder function
linkedlist.addByOrder(node3);
linkedlist.addByOrder(node1);
linkedlist.addByOrder(node4);
// Test del function
// linkedlist.del(1);
// linkedlist.del(4);
// linkedlist.del(5);
//Test update function
linkedlist.update(new Node(1, "Token"));
//Test findByNo function
Node rest = linkedlist.findByNo(2);
System.out.println(rest);
// Test list function
linkedlist.list();
}
}