加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

【数据结构】队列-链表的实现

发布时间:2020-12-15 06:04:47 所属栏目:安全 来源:网络整理
导读:链表相比队列最大的不同依然在于链表的插入比较省时间,但是全局操作来看还是数组很省时间,因为链表的构建和连接代价比数组的创建要高很多。 由于队列不再是头插头删了,所以需要维护一个尾指针进行尾插头删 基本结构如下 private Node head;//head using f

链表相比队列最大的不同依然在于链表的插入比较省时间,但是全局操作来看还是数组很省时间,因为链表的构建和连接代价比数组的创建要高很多。

由于队列不再是头插头删了,所以需要维护一个尾指针进行尾插头删

基本结构如下

	private Node head;	//head using for delete before
	private Node tail;	//tail using of insert after

然后是入队操作
	public void enqueue(String str) {
		Node newNode = new Node(str);

		if (isEmpty()) {
			head = newNode;
			tail = newNode;
		} else {
			tail.next = newNode;
			tail = newNode;
		}
	}

然后是出队操作
	public String dequeue() {
		String deItem;

		if (!isEmpty()) {
			deItem = head.item;
			head = head.next;
			return deItem;
		} else
			return "Queue is empty";
	}


判空
	public boolean isEmpty() {
		return head == null;
	}

size

	public int size() {
		int size = 0;
		while (head != null) {
			head = head.next;
			size++;
		}
		return size;
	}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读