单链表
接口 LinkList.java
package Struct;
public interface LinkList {
//判断链表为空
public boolean linkListIsEmpty();
//头插
public void linkListPushFront(int value);
//头删
public SNode linkListPopFront();
//打印链表
public void disPlayList();
//查找元素在链表中的位置
public int linkListFind(int to_find);
//删除指定值的元素
public SNode linkListRemove(int to_delete);
//求链表长度
public int linkListSize();
}
实现类LinkList1.java
package Struct;
//单链表节点
class SNode{
int data;//定义数据类型为int型的数据
SNode next;//定义下一个节点
public SNode(int data) {
super();
this.data = data;
}
public void disPlay() {
System.out.print(this.data+"->");
}
}
public class LinkList1 implements LinkList {
private SNode first;
public LinkList1(){
first = null;
}
//判断链表是否为空
public boolean linkListIsEmpty(){
return (first == null);
}
//头插
public void linkListPushFront(int value) {
SNode newNode = new SNode(value);
newNode.next = first;
first = newNode;
}
//头删
public SNode linkListPopFront() {
SNode temp = first;
first = first.next;
return temp;
}
//查找指定元素在链表中的位置
public int linkListFind(int to_find) {
if(first == null){
return 0;//空链表无需查找,直接返回无效值-1
}
SNode current = first;
while(current.data != to_find){
if(current.next == null){
return -1;//表示未找到要查找的元素
}
current = current.next;//移动current
}
return 1;//找到了
}
//打印链表
public void disPlayList(){
SNode current = first;
while(current!=null){
current.disPlay();
current = current.next;
}
System.out.println("");
}
//删除指定值的元素
public SNode linkListRemove(int to_delete) {
if(first== null){
System.out.println("空链表无需此操作");//空链表无需此操作
}
SNode current = first;
SNode pre = first;
while(current.data!=to_delete){
if(current.next == null){
System.out.println("该链表中不存在要指定删除的元素");
}
pre = current;
current = current.next;
}
if(current==first){
//要删除的元素为首元素
first = first.next;
}
else{
//要删除的元素为除首元素外的元素
pre.next = current.next;
}
return current;//找到了要删除元素
}
public int linkListSize(){
int size = 1;
SNode current = first;
while(current.next!=null){
size++;
}
return size;
}
}
测试类TestLinkList.java
package Struct;
public class TestLinkList {
public static void main(String[] args) {
LinkList1 theLink = new LinkList1();
System.out.println(theLink.linkListIsEmpty());
//在空链表中查找指定元素
System.out.println(theLink.linkListFind(5));
theLink.linkListPushFront(1);
theLink.linkListPushFront(2);
theLink.linkListPushFront(3);
theLink.linkListPushFront(4);
System.out.println("头插四个元素后链表为:");
theLink.disPlayList();
System.out.println("链表的长度为:");
System.out.println(theLink.linkListSize());
System.out.println("查找指定元素");
//在链表中查找不存在的元素
System.out.println(theLink.linkListFind(5));
//在链表中查找存在的元素
System.out.println(theLink.linkListFind(2));
theLink.disPlayList();
theLink.linkListPopFront();
theLink.linkListPopFront();
theLink.linkListPopFront();
theLink.linkListPopFront();
System.out.println("头删四个元素后链表为:");
theLink.disPlayList();
theLink.linkListPushFront(1);
theLink.linkListPushFront(2);
theLink.linkListPushFront(3);
theLink.linkListPushFront(4);
theLink.disPlayList();
//要删除的元素为首元素
theLink.linkListRemove(4);
System.out.println("删除元素4后链表为:");
theLink.disPlayList();
//要删除元素为非首元素
theLink.linkListRemove(2);
System.out.println("删除元素2后链表为:");
theLink.disPlayList();
}
}