数据结构题目35:链接队列的操作
2020-05-07 本文已影响0人
玲儿珑
链接队列的操作基本有以下:
- 初始化链接队列
- 测试链接队列是否为空
- 取当前队头元素
- 链接队列的插入
- 链接队列的删除
- 链接队列的销毁
具体实现:
class Node{
constructor (data, link) {
this.data = data,
this.link = link
}
}
class QueenLink{
constructor () {
this.front = null
this.rear = null
this.getItem = null
this.delItem = null
}
initialQLink() {
return new QueenLink()
}
emptyQLink() {
return this.front == null
}
getLinkQ() {
if ( this.emptyQLink() ) {
return 0
}
this.getItem = this.front.data
return 1
}
addLinkQ(item) {
let p
if ( !(p=new Node(null, null)) ) {
return 0
}
p.data = item
p.link = null
if ( this.front == null ) {
this.front = p
} else {
this.rear.link = p
}
this.rear = p
return 1
}
delLinkQuer() {
let p
if ( this.emptyQLink() ) {
return 0
}
p = this.front
this.delItem = p.data
this.front = p.link
p = null
return 1
}
desLinkQ() {
while (this.front) {
this.rear = this.front.link
this.front = null
this.front = this.rear
}
}
}
测试:
var queenLink = new QueenLink()
queenLink.initialQLink()
queenLink.addLinkQ(1)
queenLink.addLinkQ(2)
queenLink.addLinkQ(3)
queenLink.addLinkQ(4)
queenLink.addLinkQ(5)
queenLink.emptyQLink()
queenLink.getLinkQ()
queenLink.getItem
queenLink.delLinkQuer()
queenLink.delItem
queenLink.desLinkQ()