其实很早就想开始在CSDN写博客了,但是苦于时间或者是懒,一直没有动手,想了想,最后还是开始吧,第一篇博客,开始我的CSDN之旅。
java动态链表奉上
package com.bird.node; public class IntSLLNode { public int info; public IntSLLNode next; public IntSLLNode (int i){ this(i,null); } public IntSLLNode(int i, IntSLLNode n){ info = i; next = n; } }package com.bird.node; public class IntSLLList { protected IntSLLNode head, tail; public IntSLLList(){ head = tail = null; } public boolean isEmpty(){ return null == head; } public void addToHead(int el){ head = new IntSLLNode(el,head); if(tail == null){ tail = head; } } public void addToTail(int el){ if(!isEmpty()){ tail.next = new IntSLLNode(el); tail = tail.next; } else{ head = tail = new IntSLLNode(el); } } public int deleteFormHead(){ int el = head.info; if(head == tail){ head = tail = null; }else{ head = head.next; } return el; } public int deleteFormTail(){ int el = tail.info; if(head == tail){ head = tail = null; }else{ IntSLLNode tmp; for(tmp = head; tmp.next != tail; tmp = tmp.next); tail = tmp; tail.next = null; } return el; } public void printALL(){ for(IntSLLNode tmp = head; tmp != null; tmp = tmp.next) System.out.println(tmp.info + ""); } public boolean isIntList(int el){ IntSLLNode tmp; for(tmp = head; tmp != null && tmp.info != el; tmp = tmp.next); return tmp != null; } public void delete(int el){ if(!isEmpty()){ if(head == tail && el == head.info){ head = tail = null; } else if(el == head.info) head = head.next; else{ IntSLLNode pred, tmp; for(pred = head, tmp = head.next; tmp != null && tmp.info != el; pred = pred.next, tmp = tmp.next); if(tmp != null){ pred.next = tmp.next; if(tmp == tail){ tail = pred; } } } } } public static void main(String [] args){ IntSLLList list = new IntSLLList(); System.out.println(list.isEmpty()); list.addToHead(6); list.addToTail(14); list.printALL(); list.deleteFormHead(); list.isIntList(6); list.addToHead(15); list.printALL(); list.delete(15); list.printALL(); } }
实现了最基本的功能,没有写注释,主要是希望自己好好看看,呵呵
JAVA的链表实现
最新推荐文章于 2024-08-17 08:59:42 发布