searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

如何使用链表实现队列

2023-11-29 08:29:40
2
0

队列是一种先进后出的数据结构。它可以用来实现很多应用,比如缓冲区、消息队列等。

链表是一种数据结构,它由一组节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。

我们可以使用链表来实现队列。队列的头节点指向第一个元素,尾节点指向最后一个元素。当我们向队列中添加一个元素时,我们将新元素插入到尾节点后面。当我们从队列中删除一个元素时,我们将从头节点删除第一个元素。

以下是使用链表实现队列的步骤:

  1. 创建一个头节点和一个尾节点。
  2. 当我们向队列中添加一个元素时,我们将新元素插入到尾节点后面。
  3. 当我们从队列中删除一个元素时,我们将从头节点删除第一个元素。

以下是使用链表实现队列的代码:

class Node {

  constructor(data) {

    this.data = data;

    this.next = null;

  }

}

class Queue {

  constructor() {

    this.head = null;

    this.tail = null;

  }

 

  enqueue(data) {

    const newNode = new Node(data);

 

    if (this.head === null) {

      this.head = newNode;

      this.tail = newNode;

    } else {

      this.tail.next = newNode;

      this.tail = newNode;

    }

  }

 

  dequeue() {

    if (this.head === null) {

      throw new Error("Queue is empty");

    }

 

    const data = this.head.data;

    this.head = this.head.next;

 

    if (this.head === null) {

      this.tail = null;

    }

 

    return data;

  }

}

链表实现的队列非常简单,但是它有一个缺点,就是在插入和删除元素时,需要遍历整个链表。如果链表很长,那么插入和删除元素就会很慢。

为了解决这个问题,我们可以使用双向链表来实现队列。双向链表的每个节点都包含一个指向前一个节点和后一个节点的指针。这样,我们就可以在插入和删除元素时只需要遍历一半的链表。

以下是使用双向链表实现队列的代码:

class Node {

  constructor(data) {

    this.data = data;

    this.next = null;

    this.prev = null;

  }

}

class Queue {

  constructor() {

    this.head = null;

    this.tail = null;

  }

 

  enqueue(data) {

    const newNode = new Node(data);

 

    if (this.head === null) {

      this.head = newNode;

      this.tail = newNode;

    } else {

      this.tail.next = newNode;

      newNode.prev = this.tail;

      this.tail = newNode;

    }

  }

 

  dequeue() {

    if (this.head === null) {

      throw new Error("Queue is empty");

    }

 

    const data = this.head.data;

    this.head = this.head.next;

 

    if (this.head === null) {

      this.tail = null;

    } else {

      this.head.prev = null;

    }

 

    return data;

  }

}

双向链表实现的队列比链表实现的队列更快,但是它需要更多的内存。

我们可以根据自己的需要选择使用链表实现的队列还是双向链表实现的队列。

 

0条评论
0 / 1000
易乾
593文章数
0粉丝数
易乾
593 文章 | 0 粉丝
原创

如何使用链表实现队列

2023-11-29 08:29:40
2
0

队列是一种先进后出的数据结构。它可以用来实现很多应用,比如缓冲区、消息队列等。

链表是一种数据结构,它由一组节点组成,每个节点包含一个数据元素和一个指向下一个节点的指针。

我们可以使用链表来实现队列。队列的头节点指向第一个元素,尾节点指向最后一个元素。当我们向队列中添加一个元素时,我们将新元素插入到尾节点后面。当我们从队列中删除一个元素时,我们将从头节点删除第一个元素。

以下是使用链表实现队列的步骤:

  1. 创建一个头节点和一个尾节点。
  2. 当我们向队列中添加一个元素时,我们将新元素插入到尾节点后面。
  3. 当我们从队列中删除一个元素时,我们将从头节点删除第一个元素。

以下是使用链表实现队列的代码:

class Node {

  constructor(data) {

    this.data = data;

    this.next = null;

  }

}

class Queue {

  constructor() {

    this.head = null;

    this.tail = null;

  }

 

  enqueue(data) {

    const newNode = new Node(data);

 

    if (this.head === null) {

      this.head = newNode;

      this.tail = newNode;

    } else {

      this.tail.next = newNode;

      this.tail = newNode;

    }

  }

 

  dequeue() {

    if (this.head === null) {

      throw new Error("Queue is empty");

    }

 

    const data = this.head.data;

    this.head = this.head.next;

 

    if (this.head === null) {

      this.tail = null;

    }

 

    return data;

  }

}

链表实现的队列非常简单,但是它有一个缺点,就是在插入和删除元素时,需要遍历整个链表。如果链表很长,那么插入和删除元素就会很慢。

为了解决这个问题,我们可以使用双向链表来实现队列。双向链表的每个节点都包含一个指向前一个节点和后一个节点的指针。这样,我们就可以在插入和删除元素时只需要遍历一半的链表。

以下是使用双向链表实现队列的代码:

class Node {

  constructor(data) {

    this.data = data;

    this.next = null;

    this.prev = null;

  }

}

class Queue {

  constructor() {

    this.head = null;

    this.tail = null;

  }

 

  enqueue(data) {

    const newNode = new Node(data);

 

    if (this.head === null) {

      this.head = newNode;

      this.tail = newNode;

    } else {

      this.tail.next = newNode;

      newNode.prev = this.tail;

      this.tail = newNode;

    }

  }

 

  dequeue() {

    if (this.head === null) {

      throw new Error("Queue is empty");

    }

 

    const data = this.head.data;

    this.head = this.head.next;

 

    if (this.head === null) {

      this.tail = null;

    } else {

      this.head.prev = null;

    }

 

    return data;

  }

}

双向链表实现的队列比链表实现的队列更快,但是它需要更多的内存。

我们可以根据自己的需要选择使用链表实现的队列还是双向链表实现的队列。

 

文章来自个人专栏
文章 | 订阅
0条评论
0 / 1000
请输入你的评论
0
0