单向链表的各种操作(java语言)

package algorithms;

public class LinkedNode<Item> {
	
	Node first;//链表的头结点
	
	private static class Node<Item>{ 
		Item item;
		Node next=null;
		Node (Item item){
			this.item = item;
		}
	}

	
	public void deleteLast() {
		
		if(first.next==null) { //只有一个结点,既是头结点也是尾结点
			first = null;
		}
		else if(first==null) { //空链表
			StdOut.println("链表为空,无法删除");
		}
		else {  
			Node current = first;
			while(current.next.next!=null) {
				current = current.next;
			}
			current.next = null;
		}
	}
	
	public int length() {
		int N = 0;
		Node current = first;
		while(current!=null) {
			N++;
			current = current.next;
		}
		return N;
	}
	
	public void delete(int k) {  //删除第k个元素
		if(k==1) {
			first = first.next;
		}
		else if(k<=length()){
			Node current = first;
			for(int i = 1 ;i<k-1;i++) { //current移动到要删除的结点之前的一个结点
				current = current.next;
			}
			current.next = current.next.next;
		}
		else {
			StdOut.println("index error");
		}
	}
	
	
	public void addNode(Node last) { //链表末尾加Node
		
		Node current = first;
		
		if(first==null) {
			first = last;
		}
		else {
			while(current.next!=null) {
				current = current.next;
			}
			current.next = last;
		}
	}
	
	
	public boolean find(Item key) {
		Node current = first;
		while(current!=null) {
			if(current.item==key) {
				return true;
			}
			current = current.next;
		}
		
		return false;
	}
	
	
	public void printAll() {
		Node current = first;
		while(current!=null) {
			StdOut.println(current.item);
			current = current.next;
		}
	}
	
	
	public void removeAfter(Node node){
		if(node==null || node.next==null) {
		}
		else {
			node.next = node.next.next;
		}
	}
	
	
	public void insertAfter(Node node,Node insert) {
		if(node!=null && insert!=null) {
			insert.next=node.next;
			node.next = insert;
		}
		else {
			StdOut.println("NullPointer");
		}
	}
	
	public void remove(Item key) {
		Node current = first;
		while(current!=null) {
			
			if(current.item==key) {
				if(current==first || length()==1) { //要删除的是头结点 (包含既是头结点又是尾结点的情况)
					first = first.next;
				}
				else if(current.next == null) { //current是尾结点(不包含既是头结点又是尾结点的情况)
					Node temp = first;
					while(temp.next!=current) {
						temp = temp.next;
					}
					temp.next = null;
				}
				else {//这样删除快,不用去遍历寻找current的前一个结点
					current.item= current.next.item;
					current.next = current.next.next;
				}
			}
			
			current = current.next;
		}
	}
	
	public int max() {  //只有当结点数据为Integer时才行
		int max = (Integer) first.item;
		if(length()==1) {
			return max;
		}
		else {
			Node current = first.next;
			while(current!=null) {
				if( (Integer) current.item > max) {
					max = (Integer) current.item;
				}
				current = current.next;
			}
		}
		
		return max;
	}
	
	
	public int max_by_recursion(Node first) {  //只有当结点数据为Integer时才行
		
		if(first.next==null) {
			return (Integer) first.item;
		}
		
		if(first.next.next==null) {
			return Math.max( (Integer)first.item, (Integer)first.next.item);
		}

		int x = (Integer) first.item;
		first = first.next;
		return Math.max(x, max_by_recursion(first) );
	
	}
	 
	public void reverse() { //将原链表中的结点一个个插到新链表的头
		Node newFirst = null;
		Node current = first;
		while(current!=null) { //将原链表从前到后依次插到新链表的最前面
			Node oldFirst = newFirst;
			newFirst = new Node(current.item);
			newFirst.next = oldFirst;
			
			current = current.next;
		}
		first = newFirst;
	}
	
	
	public Node reverse_byRecursion(Node first) { //返回逆序后链表的头结点
		if(first.next==null) { //只有一个不用逆序,逆序后头结点还是原来的头结点
			return first;
		}
		Node second = first.next; //将指向第二个Node的引用记录下来,不然后面就找不到了
		first.next = null;  //first要变成末尾,所以要这样
		Node newFirst = reverse_byRecursion(second);
		second.next = first;
		
		return newFirst;
	}
	
	
public static void main(String[] args) {
		
		LinkedNode link = new LinkedNode<Integer>();
		
		Node first = new Node(1);
		Node second = new Node(2);
		Node third = new Node(3);
		Node fourth = new Node(4);
		Node last = new Node(5);
		
		link.addNode(first);
		link.addNode(second);
		link.addNode(third);
		link.addNode(fourth);
		link.addNode(last);
		
//		int max1 = link.max();
//		StdOut.println("max : "+max1);  //5
//		StdOut.println("----");
//		int max2= link.max_by_recursion(link.first); 
//		StdOut.println("max : "+max2);  //5
//		StdOut.println("----");
//		
//		link.deleteLast();
//		StdOut.println("length is : "+link.length());  //length is : 4
//		StdOut.println("----");
//		
//		link.delete(1);
//		
//		//剩下2,3,4三个结点了
//		
//		StdOut.println(link.find(2)); //true
//		StdOut.println("----");
//		
//		Node insert = new Node(7);
//		link.insertAfter(third,insert);
//		
//		link.remove(7);
//		
//		link.removeAfter(second); // 2回车4
//		
//		link.printAll();
//		StdOut.println("------");
//		
//		link.reverse();
//		link.printAll();
//		StdOut.println("------");
		
		Node newFirst = link.reverse_byRecursion(link.first);
		link.first = newFirst;
		link.printAll();
		StdOut.println("------");
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值