前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java数据结构之单链表

java数据结构之单链表

作者头像
林老师带你学编程
发布2022-11-30 10:31:28
2540
发布2022-11-30 10:31:28
举报
文章被收录于专栏:强仔仔

在单链表中对表头进行插入或者删除时,时间复杂度为O(1)。

单链表查询指定节点时因为要进行循环查找,平均需要查找N/2次,所以时间复杂度为O(N)。

存储密度=数据占用的存储量/整个结点占用的存储量。根据这个公式可以得出单链表的存储密度为大于1,在空间利用率上面比顺序表要差;

所以可以得出以下结论:单链表一般作为插入或者删除频繁,查询比较少的场景下使用。空间使用率上面是比较顺序表要低。

下面直接上代码举例说明:

代码语言:javascript
复制
public class LinkedList {
	private class Node {//定义一个节点类
		private Object value;
		private Node next = null;
		Node(Object value) {
			this.value = value;
		}
	}
	private Node first = null;//定义First节点对象
	public void insertFirst(Object obj) {//在first节点前加入值为obj的节点
		Node node = new Node(obj);
		node.next = first;
		first = node;
	}

	public void insertFirstValue(Object value,Object obj) throws Exception {
		//在单链表中值为value的节点前加入值为obj的节点
		if(first==null){
			throw new Exception("LinkedList is empty!");
		}else if(find(value)==null){
			throw new Exception("not find this value!");
		}
		Node node=new Node(obj);
		if(first.value.equals(value)){	
			node.next=first;
			first=node;
		}else{
			Node pre=first;
			Node cur=first.next;
			while(cur!=null){
				if(cur.value.equals(value)){
					node.next=cur;
					pre.next=node;
				}
				pre=cur;
				cur=cur.next;
			}
		}
	}
	
	public void insertEndValue(Object value,Object obj) throws Exception{
		//在单链表中值为value的节点后加入值为obj的节点
		if(first==null){
			System.out.println("LinkedList is empty!");
		}else if(find(value)==null){
			System.out.println("not find this value!");
		}
		Node node=new Node(obj);
		if(first.value.equals(value)){
			node.next=first.next;
			first.next=node;
		}else{	
			Node pre=first;
			Node cur=first.next;
			while(cur!=null){
				if(cur.value.equals(value)){
					node.next=cur.next;
					cur.next=node;
				}
				pre=cur;
				cur=cur.next;
			}
		}	
	}
	public Object deleteFirst() throws Exception {//删除First节点
		if (first == null)
			throw new Exception("empty!");
		Node temp = first;
		first = first.next;
		return temp.value;
	}

	public Object find(Object obj) throws Exception {//查询单链表中是否有这么一个值,并把结果返回
		if (first == null)
			throw new Exception("LinkedList is empty!");
		Node cur = first;
		while (cur != null) {
			if (cur.value.equals(obj)) {
				return cur.value;
			}
			cur = cur.next;
		}
		return null;
	}

	public void remove(Object obj) throws Exception {//删除单链表中值为obj的节点对象。
		if (first == null)
			throw new Exception("LinkedList is empty!");
		if (first.value.equals(obj)) {
			first = first.next;
		} else {
			Node pre = first;
			Node cur = first.next;
			while (cur != null) {
				if (cur.value.equals(obj)) {
					pre.next = cur.next;
				}
				pre = cur;
				cur = cur.next;
			}
		}
	}

	public boolean isEmpty() {//判断单链表是否为空
		return (first == null);
	}

	public void display() {//打印单链表中的所有数据
		if (first == null)
			System.out.println("empty");
		Node cur = first;
		while (cur != null) {
			System.out.print(cur.value.toString() + " -> ");
			cur = cur.next;
		}
		System.out.print("\n");
	}

	public static void main(String[] args) throws Exception {
		LinkedList ll = new LinkedList();
		ll.insertFirst(4);
		ll.insertFirst(3);
		ll.insertFirst(2);
		ll.insertFirst(1);
		ll.display();
		//ll.insertFirstValue(1,10);
		ll.insertEndValue(1,10);
		ll.display();
//		ll.deleteFirst();
//		ll.display();
//		ll.remove(3);
//		ll.display();
//		System.out.println(ll.find(1));
//		System.out.println(ll.find(4));
	}
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2016-05-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档