public class LinkList {
private Node head;//链表头
private int size;//节点个数
/*
* 构造一个节点 结构
* 这个节点类 有两个构造方法
* Node node1 = new Node();
* Node node2 = new Node(val);
* 这个现象叫方法重载
* 一个节点有两个属性,节点值 val 和 节点后驱 next,两个属性都是 public
* 所以除了通过构造方法初始化属性,还可以通过类以下方式直接改变节点属性值
* node1.val = value;
* node1.next = null;
*/
class Node{
public int val;
public Node next;
public Node(){}
public Node(int val)
{
this.val = val;
this.next = null;//默认空
}
}
/*
* 构造方法
*/
public LinkList() {}
public LinkList(int val)
{
head = new Node(val);
}
/*
* 清空线性表
*/
public void clear()
{
head = null;
size = 0;
}
/*
* 判断线性表是否为空
*/
public boolean isEmpty()
{
return size == 0;
}
/*
* 输出线性表中元素的个数
*/
public int length()
{
return size;
}
/*
* 获取特定下标的元素
*/
public int get(int index)
{
if(index >= size || index < 0)//当指定的索引位置超过边界或者位置为负数 则抛出越界错误
throw new ArrayIndexOutOfBoundsException();
Node node = head; //不能直接用head 进行遍历
//head是链表的头地址 是链表完整存在的重要属性
//如果直接使用head遍历链表 会导致head所指向的地址发生改变
//一旦head属性地址发生改变 那么链表将找不到原先链表的表头 从而使得这个数据结构不合要求
int count = 0; //计数器
while(true)
{
if(count == index)//当计数器的值等于索引值 则返回此时的 节点val
return node.val;
node = node.next;
count ++;
}
}
/*
* 给一个目标元素
* 返回在链表中遇到的第一个与之值相等的节点的位置
* 如果链表中不存在与之相等的元素则返回 -1
*/
public int locateEle(int e)
{
Node node = head;
int index = -1;
while(node != null)
{
index ++;
if(node.val == e)
return index;
node = node.next;
}
return -1;//如果数组中没有目标元素则返回 0
}
/*
* 在链表末尾插入元素
*/
public void insert(int e)
{
if(head == null)
head = new Node(e);
else
{
Node node = head;
while(node.next!=null)
node = node.next;
node.next = new Node(e);
}
size++;
}
/*
* 在指定位置 插入元素
*/
public void insert(int e, int index)
{
if(index < 0)//当指定的位置为负数 则抛出越界错误
throw new ArrayIndexOutOfBoundsException();
if(index == 0)//在表头插入元素
{
Node node = new Node(e);
node.next = head;
head = node;
}
Node node = head;
int count = 0;
while(node.next!=null && count<index-1)
{
node = node.next;
count++;
}
if(node.next == null)//在表尾插入元素 当指定的插入位置 超过链表长度时 默认末尾插入
node.next = new Node(e);
else
{//在表中插入元素
Node temp = node.next;
node.next = new Node(e);
node.next.next = temp;
}
size++;
}
/*
* 删除指定位置的元素
*/
public void delete(int index)
{
if(index < 0 || index >= size)//当指定的位置 超过最大元素下标 或者 为负数 则抛出越界异常
throw new ArrayIndexOutOfBoundsException();
if(index == 0)//删除头位置节点
head = head.next;
else {
Node node = head;
int count = 0;
while(count<index-1)
{
node = node.next;
count++;
}
node.next = node.next.next;
}
size--;
}
/*
* 更新元素
*/
public void updata(int e,int index)
{
if(index < 0 || index >= size)//当指定的位置 超过最大元素下标 或者 为负数 则抛出越界异常
throw new ArrayIndexOutOfBoundsException();
Node node = head;
int count = 0;
while(count<index)
{
node = node.next;
count++;
}
node.val = e;
}
/*
* 输出数组中的所有元素
*/
public void outPut()
{
Node node = head;
while(node!=null)
{
System.out.print(node.val+" ");
node = node.next;
}
System.out.println();
}
}
测试代码
public class Main {
public static void main(String[] args)
{
LinkList list = new LinkList();
System.out.println("空吗"+list.isEmpty());
System.out.println("长度"+list.length());
list.insert(9);
list.insert(8);
list.insert(7);
list.insert(3);
list.insert(2);
list.insert(1);
list.insert(8);
list.outPut();
System.out.println("空吗"+list.isEmpty());
System.out.println("长度"+list.length());
System.out.println();
System.out.print("原数组\t\t:");
list.outPut();
System.out.println("链表长度:"+list.length());
list.delete(1);
System.out.print("删除下标为1的元素: ");
list.outPut();
System.out.println("链表长度:"+list.length());
System.out.println();
System.out.print("原数组\t\t:");
list.outPut();
System.out.println("链表长度:"+list.length());
list.delete(0);
System.out.print("删除下标为0的元素: ");
list.outPut();
System.out.println("链表长度:"+list.length());
System.out.println();
System.out.print("原数组\t\t:");
list.outPut();
System.out.println("链表长度:"+list.length());
list.insert(10);
System.out.print("在末尾插入元素10: ");
list.outPut();
System.out.println("链表长度:"+list.length());
System.out.println();
System.out.print("原数组\t\t:");
list.outPut();
System.out.println("链表长度:"+list.length());
list.insert(8,1);
System.out.print("在下标为1的位置插入元素8: ");
list.outPut();
System.out.println("链表长度:"+list.length());
System.out.println();
System.out.print("原数组\t\t:");
list.outPut();
System.out.println("第0位上的元素:"+list.get(0));
System.out.println("第一次出现8的位置:"+list.locateEle(8));
System.out.print("修改第1位上的元素,改成6:");
list.updata(6, 1);
list.outPut();
System.out.println();
}
}