学习链表的一些基本操作,初始化链表,插入与删除节点。
代码:
package com;
public class LinkedList {
/**
* 节点
*/
class Node {
int data;//数据
Node next;//指向下一个节点
public Node(int paraValue) {
data = paraValue;
next = null;
}
}
Node header;//头结点的数据域不存放数据
/***
* 构造空链表
*/
public LinkedList() {
header =new Node(0);
}
public String toString() {
String resultString = "";
if(header.next == null) {
return "empty";
}
Node tempNode = header.next;
while(tempNode != null) {
resultString += tempNode.data + ", ";
tempNode = tempNode.next;
}
return resultString;
}
//清空链表
public void reset() {
header.next = null;
}
/**
* 查找给定值的位置,若有多个返回第一个
* @param paraValue 需要查找的值
* @return 位置,返回值为-1代表没有找到
*/
public int locate(int paraValue) {
int tempPosition = -1;
Node tempNode = header.next;
int tempCurrentPosition = 0;
while (tempNode != null) {
if(tempNode.data == paraValue) {
tempPosition = tempCurrentPosition;
break;
}
tempNode = tempNode.next;
tempPosition++;
}
return tempPosition;
}
/**
* 插入
* @param paraPosition 插入位置
* @param paraValue 插入值
* @return 成功返回true ,失败返回false
*/
public boolean insert(int paraPosition,int paraValue) {
Node tempNode = header;
Node tempNewNode;
for(int i = 0; i < paraPosition; i++) {
if(tempNode.next == null) {
System.out.println("The position " + paraPosition + " is illegal.");
return false;
}
tempNode = tempNode.next;
}
tempNewNode = new Node(paraValue);
tempNewNode.next = tempNode.next;
tempNode.next = tempNewNode;
return true;
}
/**
* 删除
* @param paraPosition 需要删除的位置
* @return 成功返回true ,失败返回false
*/
public boolean delete(int paraPosition) {
if(header.next == null) {
System.out.println("Cannot delete element from an empty list.");
return false;
}
Node tempNode = header;
for(int i = 0; i < paraPosition; i++) {
if(tempNode.next.next == null) {
System.out.println("The position " + paraPosition + " is illegal.");
return false;
}
tempNode = tempNode.next;
}
tempNode.next = tempNode.next.next;
return true;
}
public static void main(String[] args) {
LinkedList tempFirstList = new LinkedList();
System.out.println("Initialized, the list is: " + tempFirstList.toString());
for (int i = 0; i < 5; i++) {
tempFirstList.insert(0, i);
}
System.out.println("Inserted, the list is: " + tempFirstList.toString());
tempFirstList.insert(6, 9);
tempFirstList.delete(4);
tempFirstList.delete(2);
System.out.println("Deleted, the list is: " + tempFirstList.toString());
tempFirstList.delete(0);
System.out.println("Deleted, the list is: " + tempFirstList.toString());
for (int i = 0; i < 5; i++) {
tempFirstList.delete(0);
System.out.println("Looped delete, the list is: " + tempFirstList.toString());
}
}
}
运行结果:
Initialized, the list is: empty
Inserted, the list is: 4, 3, 2, 1, 0,
The position 6 is illegal.
Deleted, the list is: 4, 3, 1,
Deleted, the list is: 3, 1,
Looped delete, the list is: 1,
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty
Cannot delete element from an empty list.
Looped delete, the list is: empty