Java 数据结构之单链表

版权声明:聂CC~ https://blog.csdn.net/ncc1995/article/details/86719189

之前好像写过一次单链表,好吧,不只一次了,每次写到单链表就做其他的事情,然后想学数据结构的时候又要从单链表开始写,就像背单词每次都被abandon一样。

单链表是几天前写的了,可能有一些错误,在这里也先不修改了,因为觉得操作和后面要整理的双端链表还有循环链表大同小异。先把代码粘上来吧,因为怕拖着拖着就越来越懒得整理。

package linkList;

/*
 * 定义一个链表
 * 头插法、尾插法、插入、删除等操作
 */
public class SingleLink {
	private linkNode first;
	
	
	/*
	 * 构造器
	 */
	public SingleLink() {
		first = null;
	}
	
	
	/*
	 * 判断链表是否为空
	 */
	public boolean isEmpty() {
		return (first==null);
	}
	
	
	/*
	 * 头插法
	 */
	public void insertFirst(Object d) {
		linkNode newNode = new linkNode(d); 
		newNode.next = first;
		first = newNode;
	}
	
	
	/*
	 * 尾插法
	 */
	public void tailAdd(Object d) {
		linkNode newNode = new linkNode(d);
		linkNode temp = first;
		//最后一个节点的特征就是next为null,新建链表只需将null改为新的node即可
		while(temp.next != null) {
			temp = temp.next;
		}
		temp.next = newNode;
	}
	
	
	/*
	 * 删除第一个节点
	 */
	public Object deleteFirst() {
		linkNode temp = first;
		first = first.next;
		return temp.data;
	}
	
	
	/*
	 * 在任意位置插入
	 */
	public void insertAny(int i, Object d) {
		linkNode newNode = new linkNode(d);
		linkNode current = first;
		int j = 0;
		while((j<i-1) & (current.next!=null)) {
			current = current.next;
			j++;
		}
		if(i<1 || current==null) {
			System.out.println("The location to insert is not exit!");
			return;
		}
		newNode.next = current.next;
		current.next = newNode;

	}
	
	/*
	 * 位置为索引值加1
	 */
	public void insertLoc(int loc, Object d) {
		linkNode current = first;
		linkNode newNode = new linkNode(d);
		int length = 1;
		//System.out.println(length());
		if(loc<1 & loc>length()+1) {
			System.out.println("The location to insert is not exit!");
		}
		
		//遍历链表
		//插入或者计算长度需要循环到最后一个节点,对最后一个节点进行操作,
		//而不是最后current为最后一个节点就好
		while(current != null) {
			//需要的是插入位置的前一个位置
			if(length == loc-1) {
				System.out.println(length);
				newNode.next = current.next;
				current.next = newNode;
				return;
			}
			length++;
			current = current.next;
		}
	}
		
	
	/*
	 * 删除任意节点
	 */
	public Object deleteAny(int i) {
		int j = 1;
		Object e = 0;
		linkNode current = first;
		while(j<i-1 & current.next!=null) {
			current = current.next;
			j++;
		}
		if(i<1 || current==null) {
			System.out.println("The data to delete is not exit!");
		}
		if(i == j) {
			e = first.data;
			first = first.next;
			System.out.println(e);
		}
		else {
			e = current.next.data;
			current.next = current.next.next;
		}
		
		return e;
	}
	
	
	/*
	 * 删除位置是索引值加1
	 */
	public void deleteLoc(int loc) {
		Object e = 0;
		linkNode current = first;
		int length = 1;
		
		if(loc<1 || loc>length+1) {
			System.out.println("The location to delete is not exit!");
		}
		while(current.next != null) {
			if(length == loc-1) {
				e = current.next.data;
				current.next = current.next.next;
				System.out.println("The deleted node is " + e);
				return;
			}
		current = current.next;
		length++;
		}
	}
	
    public void delNode(int index){
        //判断index是否合理
        if(index<1 || index>length()){
            System.out.println("给定的位置不合理");
            return;
        }

        //步骤跟insertNodeByIndex是一样的,只是操作不一样。    
        int length=1;
        linkNode temp = first;
        while(temp.next != null){
            if(index == length++){
                //删除操作。
                temp.next = temp.next.next;    
                return;
            }
            temp = temp.next;
        }    
    }
	
    
	/*
	 * 计算单链表的长度
	 */
	public int length() {
		int length = 0;
		linkNode temp = first;
		while(temp != null) {
			length++;
			temp = temp.next;
		}
		return length;
	}
	
	
	/*
	 * 打印链表
	 */
	public void displayList() {
		linkNode current = first;
		while(current != null) {
			current.displayLink();
			current = current.next;
		}		
		System.out.println();
	}
	
	
	/*
	 * main函数
	 */
	public static void main(String[] args) {
		SingleLink link = new SingleLink();
		link.insertFirst("haha");
		link.insertFirst(22);
		link.insertFirst(4);
		link.insertFirst(3);
		link.insertFirst(66);
		link.displayList();
		link.insertLoc(6, 88);
		link.displayList();
		link.deleteLoc(6);
		link.displayList();
	}
}


/*
 * 节点定义
 */
class linkNode{
	Object data;
	linkNode next;
	
	public linkNode(Object d) {
		this.data = d;
		this.next = null;
	}
	
	public void displayLink() {
		System.out.print(data + " ");
	}
}

猜你喜欢

转载自blog.csdn.net/ncc1995/article/details/86719189