本次实现单链表基本功能共分为
1. 节点类
.
.
.
2. 链表类
.
.
.
3. 测试类
.
.
.
.
=============================================================
****Node.java****
package com.ash.www;
//节点类
public class Node {
//数据域与指针域
protected Node next;
protected int data;
//构造函数
public Node(int data){
this.data = data;
}
//打印节点
public void display(){
System.out.print(data + " ");
}
}
.
.
.
.
.
.
.
.
****LinkedList.java****
package com.ash.www;
public class LinkedList {
public Node first; //
public int positon = 0;
public LinkedList(){
this.first = null;
}
// 插入一个头节点
public void addFirstNode(int data){
Node node = new Node(data);
node.next = first;
first = node;
System.out.println("Add completed!");
}
// 删除一个头结点
public void deleteFirstNode(){
first = first.next;
System.out.println("Delete completed!");
}
// 在任意位置插入节点, index表示插入后的位置
public void add(int index, int data){
Node node = new Node(data);
Node current = first;
Node previous = first;
while(positon != index){
if(current.next == null){
System.out.println("Sorry, index "+ index +" is not found!");
break;
}
previous = current;
current = current.next;
positon++;
}
if(positon == index){
node.next = current;
previous.next = node;
System.out.println("Add completed!");
}
positon = 0;
}
// 删除任意位置的节点, index表示插入后的位置
public void delete(int index){
Node current = first;
Node previous = first;
while(positon != index){
if(current.next == null){
System.out.println("Sorry, index "+ index +" is not found!");
break;
}
previous = current;
current = current.next;
positon++;
}
if(positon == index){
if(current == first){
first = first.next;
System.out.println("Delete completed!");
}
else {
System.out.println("Delete completed!");
previous.next = current.next;
}
}
positon = 0;
}
// 根据节点的data删除节点
public void deleteByData(int data){
Node previous = first;
Node current = first;
while(current.data != data){
if(current.next == null){
System.out.println("Sorry, data "+ current.data +" is not found!");
break;
}
previous = current;
current = current.next;;
}
if(current.data == data){
previous.next = current.next;
System.out.println("Delete completed!");
}
}
// 显示出所有的节点信息
public void displayAllNode(){
Node current = first;
while(current != null){
System.out.print(current.data + " ");
current = current.next;
}
System.out.println(" ");
}
// 根据位置查找节点信息
public void findbyPos(int index){
Node previous = first;
Node current = first;
while(positon != index){
if(current.next == null){
System.out.println("Sorry, index "+ index +" is not found!");
break;
}
previous = current;
current = current.next;;
positon++;
}
if(positon == index){
System.out.println("Search Completed! \nData is "+ current.data +".");
}
positon = 0;
}
// 根据数据查找节点信息
public void findByData(int data){
Node previous = first;
Node current = first;
while(current.data != data){
if(current.next == null){
System.out.println("Sorry, data "+ current.data +" is not found!");
break;
}
previous = current;
current = current.next;;
positon++;
}
if(current.data == data){
System.out.println("Search Completed! \nData position is "+ positon +".");
}
positon = 0;
}
}
.
.
.
.
.
.
.
.
//测试类
****Text****
package com.ash.www;
public class Text {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList linkedList = new LinkedList();
//add FirstNode
System.out.println();
linkedList.addFirstNode(30); //10
linkedList.addFirstNode(20); //10, 20
linkedList.addFirstNode(10); //10, 20, 30
System.out.println();
linkedList.displayAllNode(); //10, 20, 30
System.out.println("-------------------------------------------------------------");
System.out.println();
//add index
linkedList.add(1, 1); //10, 1, 20, 30
linkedList.add(2, 2); //10, 1, 2, 20, 30
linkedList.add(4, 33); //10, 1, 2, 20, 33, 30
linkedList.add(7, 7); //not found
System.out.println();
linkedList.displayAllNode();
System.out.println("-------------------------------------------------------------");
System.out.println();
//delete FirstNode
linkedList.deleteFirstNode();
linkedList.deleteFirstNode();
System.out.println();
linkedList.displayAllNode(); //2, 20, 33, 30
System.out.println("-------------------------------------------------------------");
System.out.println();
//add FirstNode
linkedList.addFirstNode(300);
linkedList.addFirstNode(200); //200, 300, 2, 20, 33, 30
System.out.println("-------------------------------------------------------------");
System.out.println();
//delete by index
linkedList.delete(3); //200, 300, 2, 33, 30
linkedList.delete(1); //200, 2, 33, 30
linkedList.delete(4); //not found
System.out.println();
linkedList.displayAllNode(); //200, 2, 33, 30
System.out.println("-------------------------------------------------------------");
System.out.println();
linkedList.addFirstNode(100); //100, 200, 2, 33, 30
linkedList.addFirstNode(50); //50, 100, 200, 2, 33, 30
//delete by data
linkedList.deleteByData(100); //50, 200, 2, 33, 30
linkedList.deleteByData(2); //50, 200, 33, 30
linkedList.deleteByData(500); //not found
System.out.println();
linkedList.displayAllNode(); //50, 200, 33, 30
System.out.println("-------------------------------------------------------------");
System.out.println();
//find by Pos
linkedList.findbyPos(2); //33
linkedList.findbyPos(0); //50
linkedList.findbyPos(5); //not found
System.out.println("-------------------------------------------------------------");
System.out.println();
//find by Data
linkedList.findByData(30); //3
linkedList.findByData(100); //not found
System.out.println("-------------------------------------------------------------");
}
}