package helloworld;
public class Linked <T> {
private class Node { //链表存储的节点
private T t;
private Node next;
public Node(T t, Node next) {
this.t = t;
this.next = next;
}
public Node(T t) {
this(t, null);
}
}
private Node head; //定义头结点
private int size; //定义链表元素个数
//构造函数
public Linked() {
this.head = null;
this.size = 0;
}
//获取元素个数
public int getsize() {
return this.size;
}
//判断链表是否为空
public boolean isEmpty() {
return this.size == 0;
}
//链表头部添加元素
public void addFirst(T t) {
Node node = new Node(t); //节点对象
node.next = this.head; //头结点指向原本头结点的下一个
this.head = node; // 把插入的节点当做是头结点
this.size++;
}
//向链表尾部插入元素
public void addLast(T t) {
this.add(t, this.size);
}
//向链表中间插入元素
public void add(T t, int index) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("index is error"); //索引是否是非法值
}
if (index == 0) {
this.addFirst(t);
return;
}
Node preNode = this.head;
//找到插入节点的前一个节点
for (int i = 0; i < index - 1; i++) {
preNode = preNode.next; //找到index的前一个节点,就是index-2的next
}
Node node = new Node(t);
//找到要插入节点的下一个节点,将它指向prenode的后一个节点
node.next = preNode.next;
//preNode的下一个节点指向要插入的node
preNode.next = node;
this.size++;
}
//删除链表元素
public void remove(T t) {
if (head == null) {
System.out.println("无元素可删除");
return;
}
//要删除的元素与头结点的元素相同
while (head != null && head.t.equals(t)) {
head = head.next;
this.size--;
}
Node cur = this.head;
while (cur != null && cur.next != null) {
if (cur.next.t.equals(t)) {
this.size--;
cur.next = cur.next.next;
} else cur = cur.next;
}
}
//删除链表第一个元素
public T removeFirst() {
if (this.head == null) {
System.out.println("无元素可删除");
return null;
}
Node delNode = this.head;
this.head = this.head.next;
delNode.next = null;
this.size--;
return delNode.t;
}
//删除链表的最后一个元素
public T removeLast() {
if (this.head == null) {
System.out.println("无元素可删除");
return null;
}
//只有一个元素
if (this.getsize() == 1) {
return this.removeFirst();
}
Node cur = this.head; //记录当前节点 声明cur变量指向头结点
Node pre = this.head; //记录要删除节点的前一个节点
while (cur.next != null) {
pre=cur;
cur=cur.next;
}//找到最后一个元素
pre.next=cur.next;
this.size--;
return cur.t;
}
//链表中是否包含某个元素
public boolean contains(T t){
Node cur=this.head;
while(cur !=null){
if(cur.t.equals(t)){
return true;
}
else cur=cur.next;
}
return false;
}
@Override
public String toString(){
StringBuffer sb=new StringBuffer();
Node cur=this.head;
while(cur !=null){
sb.append(cur.t+"->");
cur=cur.next;
}
sb.append("Null");
return sb.toString();
}
public static void main(String[] args) {
Linked<Integer> linked =new Linked<>();
for(int i=0;i<10;i++){
linked.addFirst(i);
System.out.println(linked);
}
linked.addLast(33);
linked.addFirst(33);
linked.add(33,5);
System.out.println(linked);
linked.remove(33);
System.out.println(linked);
}
}
单链表的实现
最新推荐文章于 2024-10-16 10:13:45 发布