单链表

public class LinkList {

	public Node head;
	public LinkList() {
		head = new Node();
		head.next = null;
	}
	
	//尾插法
	public void createByTail(int[] arr, int n) {
		Node tail = head;
		for(int i=0; i<n; i++) {
			Node c = new Node(arr[i]);
			tail.next = c;
			tail = c;
		}
		tail.next = null;
	}
	
	//头插法
	public void createByHead(int[] arr, int n) {
		head.next = null;
		for(int i=0; i<n; i++) {
			Node c = new Node(arr[i]);
			c.next = head.next;
			head.next = c;
		}
	}
	
	public void display() {
		Node p = head.next;
		while(p != null) {
			if(p.next != null)
				System.out.print(p.data + "->");
			else 
				System.out.println(p.data);
			p = p.next;
		}
	}
	public boolean empty() {
		return head.next == null;
	}
	
	public int length() {
		Node p = head.next;
		int len = 0;
		while(p != null) {
			len++;
			p = p.next;
		}
		return len;
	}
	
	public int[] get(int i) {
		int[] arr = new int[2];
		int j = 0;
		Node p = head;
		while(j<i && p != null) {
			j++;
			p = p.next;
		}
		if(p == null) {
			arr[0] = 0;
			return arr;
		}
		arr[0] = 1;
		arr[1] = p.data;
		return arr;
	}
	public int locate(int e) {
		int j = 0;
		Node p = head.next;
		while(p != null) {
			j++;
			if(p.data == e) {
				break;
			}
			p = p.next;
		}
		if(p == null) {
			return 0;
		}
		return j;
	}
	
	public boolean insert(int i, int e) {
		int j = 0;
		Node p = head;
		while(j<i-1 && p!=null) {
			j++;
			p = p.next;
		}
		if(p == null) {
			return false;
		}
		Node n = new Node(e);
		n.next = p.next;
		p.next = n;
		return true;
	}
	
	public int[] delete(int i) {
		int[] arr = new int[2];
		int j = 0;
		Node p = head;
		while(j<i-1 && p!=null) {
			j++;
			p = p.next;
		}
		if(p == null) {
			arr[0] = 0;
			return arr;
		}
		Node q = p.next;
		if(q == null) {
			arr[0] = 0;
			return arr;
		}
		arr[0] = 1;
		arr[1] = q.data;
		p.next = p.next.next;
		return arr;
		
	}
	public static void main(String[] args) {
		int[] arr = new int[]{5,6,1,6,9,3,7,8};
		LinkList list = new LinkList();
		list.createByHead(arr, arr.length);
		list.display();
		list.createByTail(arr, arr.length);
		list.display();
		System.out.println(list.empty());
		System.out.println(list.length());
		if(list.get(9)[0] == 0) {
			System.out.println("error");
		} else {
			System.out.println(list.get(9)[1]);
		}
		if(list.get(8)[0] == 0) {
			System.out.println("error");
		} else {
			System.out.println(list.get(8)[1]);
		}
		System.out.println(list.locate(9));
		System.out.println(list.locate(4));
		if(!list.insert(10, 10)) {
			System.out.println("error");
		} else {
			list.display();
		}
		if(!list.insert(8, 10)) {
			System.out.println("error");
		} else {
			list.display();
		}
		int[] tmp = list.delete(10);
		if(tmp[0] == 0) {
			System.out.println("error");
		} else {
			System.out.println("delete " + tmp[1]);
			list.display();
		}
		tmp = list.delete(4);
		if(tmp[0] == 0) {
			System.out.println("error");
		} else {
			System.out.println("delete " + tmp[1]);
			list.display();
		}
		
		
		
	}

}
class Node {
	public int data;
	public Node next;
	public Node(){}
	public Node(int data) {
		this.data = data;
	}
}


结果:


8->7->3->9->6->1->6->5
5->6->1->6->9->3->7->8
false
8
error
8
5
0
error
5->6->1->6->9->3->7->10->8
error
delete 6
5->6->1->9->3->7->10->8


ps:插入或者删除一个节点一定要先找到插入该节点的或者被删节点的前驱节点!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值