单链表的头插法、打印、尾插法、任意位置插入、获取单链表长度、判断插入位置是否合法、修改指定位置的值、删除指定位置的元素。
public interface Ilinked {
void headInsert(int data);
void printLinked();
void lastInsert(int data);
boolean indexInsert(int index,int data);
int getLength();
void checkIndexIfTure(int index);
linkListed.Node searchIndex(int index);
boolean contains(int key);
void updateIndexElemt(int index,int data);
void deleteIndexElemt(int index);
}
public class linkListed implements Ilinked {
class Node{
private int data;
private Node next;
public Node(int data) {
this.data = data;
}
}
private Node head;
public linkListed() {
}
@Override
public void headInsert(int data) {
Node node = new Node(data);
if(this.head == null){
this.head = node;
}else {
node.next = this.head;
this.head = node;
}
}
@Override
public void printLinked() {
Node current =this.head;
while (current!=null){
System.out.print(current.data+" ");
current = current.next;
}
}
@Override
public void lastInsert(int data) {
Node node = new Node(data);
Node current = this.head;
if (this.head == null){
this.head = node;
}else {
while (current.next!=null){
current = current.next;
}
current.next = node;
}
}
@Override
public boolean indexInsert(int index, int data) {
Node node = new Node(data);
Node current = searchIndex(index);
if (current == null){
node.next = this.head;
this.head = node;
}else{
node.next = current.next;
current.next = node;
}
return true;
}
@Override
public int getLength() {
Node current = this.head;
int total = 0;
while (current!=null){
current = current.next;
total++;
}
return total;
}
@Override
public void checkIndexIfTure(int index) {
if (index<0 || index>getLength()){
throw new IndexOutOfBoundsException("插入下标异常");
}
}
@Override
public Node searchIndex(int index) {
checkIndexIfTure(index);
if (index == 0){
return null;
}
int count = 0;
Node current = this.head;
while (current.next!=null && count<index-1) {
current = current.next;
count++;
}
return current;
}
@Override
public boolean contains(int key) {
Node current = this.head;
while( current != null){
if(current.data == key){
return true;
}
current = current.next;
}
return true;
}
@Override
public void updateIndexElemt(int index, int data) {
checkIndexIfTure(index);
Node current = searchIndex(index);
if (current == null){
this.head.data=data;
}else{
current.next.data=data;
}
}
@Override
public void deleteIndexElemt(int index) {
checkIndexIfTure(index);
Node current = searchIndex(index);
if (index == 0 ){
this.head = this.head.next;
}else{
current.next=current.next.next;
}
}
}
public class Test {
public static void main(String[] args) {
linkListed linkListed = new linkListed();
linkListed.headInsert(12);
linkListed.headInsert(165);
linkListed.headInsert(85);
linkListed.printLinked();
System.out.println();
linkListed.lastInsert(52);
linkListed.lastInsert(63);
linkListed.lastInsert(75);
linkListed.printLinked();
System.out.println();
System.out.print(linkListed.getLength());
System.out.println();
linkListed.indexInsert(4,100);
linkListed.printLinked();
System.out.println();
System.out.println(linkListed.contains(100));
System.out.println(linkListed.contains(55));
System.out.println();
linkListed.updateIndexElemt(0,200);
linkListed.printLinked();
System.out.println();
linkListed.updateIndexElemt(6,400);
linkListed.printLinked();
System.out.println();
linkListed.deleteIndexElemt(0);
linkListed.printLinked();
System.out.println();
linkListed.deleteIndexElemt(5);
linkListed.printLinked();
System.out.println();
}
}