JAVA的链表实现

   其实很早就想开始在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();
	}
}


 

实现了最基本的功能,没有写注释,主要是希望自己好好看看,呵呵
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值