NULL
,这样从表中任一节点出发,均可访问到链表中的所有节点 //节点的结构
class Cnode {
constructor(data) {
this.data = data
this.next = null
}
}
//链表的结构
class CycleLinkList {
constructor() {
this.head = null
this.length = 0
}
}
append(ele) {
let newnode = new Cnode(ele)
if(this.head==null){
//空链表
this.head=newnode
newnode.next=this.head
}else{
//非空链表
//需要找到最后一个节点
let current=this.head
while(current.next!=this.head){
current=current.next
}
//找到了current表示最后一个节点
current.next=newnode
newnode.next=this.head
}
this.length++
}
toString(){
let current=this.head,index=0,str=""
while(index
3)insert() 向链表中插入元素
insert(position, ele) {
if (position < 0 || position > this.length || Number.isInteger(position)) {
return false
}
let newnode = new Cnode(ele)
let current = this.head,
index = 0
if (position == 0) {
//在头部插入
if (this.length == 0) {
//空链表
this.head = newnode
newnode.next = this.head
} else {
//非空链表
while (current.next != this.head) {
current = current.next
}
newnode.next = this.head
current.next = newnode
this.head = newnode
}
this.length++
} else if (position == this.length) {
//在尾部插入
this.append(ele)
} else {
//在中间插入
while (index < position - 1) {
current = current.next
index++
}
newnode.next = current.next
current.next = newnode
this.length++
}
}
4)removeAt() 移除在指定位置的元素
removeAt(position) {
//判断位置是否合法(越界判断)
if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
return false
}
//非空链表
let current = this.head,
index = 0
if (position == 0) {
//移除头部
if (this.length == 1) {
//只有一个节点
this.head = null
} else {
//找到最后一个节点 最后一个节点的next指向新头部
while (current.next != this.head) {
current = current.next
}
this.head = this.head.next
current.next = this.head
}
} else {
//移除任意位置(包括末尾)
while (index < position - 1) {
current = current.next
index++
}
current.next = current.next.next
}
this.length--
}
5)indexOf() 查找指定元素
indexOf(ele) {
let current = this.head,
index = 0
while (index < this.length) {
if (current.data == ele) {
return index
} else {
current = current.next
index++
}
}
return -1
}
6)remove() 移除指定元素
remove(ele) {
let index = this.indexOf(ele)
this.removeAt(index)
}
(四)完整代码&操作示例
//节点的结构
class Cnode {
constructor(data) {
this.data = data
this.next = null
}
}
//链表的结构
class CycleLinkList {
constructor() {
this.head = null
this.length = 0
}
//1.append() 向链表尾部添加一个元素
append(ele) {
let newnode = new Cnode(ele)
if (this.head == null) {
//空链表
this.head = newnode
newnode.next = this.head
} else {
//非空链表
//需要找到最后一个节点 最后一个节点的next指向头结点
let current = this.head
while (current.next != this.head) {
//继续往下找
current = current.next
}
//找到了current表示最后一个节点
current.next = newnode
newnode.next = this.head
}
this.length++
}
//2.toString() 将链表中的数据链接为字符串
toString() {
let current = this.head,
index = 0,
str = ""
while (index < this.length) {
str += "-" + current.data
current = current.next
index++
}
return str.slice(1)
}
//3.向链表中插入元素
insert(position, ele) {
if (position < 0 || position > this.length || !Number.isInteger(position)) {
return false
}
let newnode = new Cnode(ele)
let current = this.head,
index = 0
if (position == 0) {
//在头部插入
if (this.length == 0) {
//空链表
this.head = newnode
newnode.next = this.head
} else {
//非空链表
while (current.next != this.head) {
current = current.next
}
newnode.next = this.head
current.next = newnode
this.head = newnode
}
this.length++
} else if (position == this.length) {
//在尾部插入
this.append(ele)
} else {
//在中间插入
while (index < position - 1) {
current = current.next
index++
}
newnode.next = current.next
current.next = newnode
this.length++
}
}
//4.移除在指定位置的元素
removeAt(position) {
//判断位置是否合法(越界判断)
if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
return false
}
//非空链表
let current = this.head,
index = 0
if (position == 0) {
//移除头部
if (this.length == 1) {
//只有一个节点
this.head = null
} else {
//找到最后一个节点 最后一个节点的next指向新头部
while (current.next != this.head) {
current = current.next
}
this.head = this.head.next
current.next = this.head
}
} else {
//移除任意位置(包括末尾)
while (index < position - 1) {
current = current.next
index++
}
current.next = current.next.next
}
this.length--
}
//5.查找指定元素
indexOf(ele) {
let current = this.head,
index = 0
while (index < this.length) {
if (current.data == ele) {
return index
} else {
current = current.next
index++
}
}
return -1
}
//6.移除指定元素
remove(ele) {
let index = this.indexOf(ele)
this.removeAt(index)
}
}
let clist = new CycleLinkList()
//链表操作
//在末尾添加元素
for (let i = 0; i < 9; i++) {
clist.append(i)
}
console.log(clist.toString(),"添加了元素")
//在指定位置插入元素
clist.insert(3, "newnode")
console.log(clist.toString(),"在3的位置插入了newnode")
//移除指定位置的元素
clist.removeAt(4)
console.log(clist.toString(),"移除了第4个节点--3")
//查找元素
console.log(clist.indexOf(3),"查找3,已经被移除了所以返回-1")
console.log(clist.indexOf(5),"存在,返回下标")
//移除指定元素
clist.remove(8)
console.log(clist.toString(),"删除了8")
打印结果:
二、双向循环链表
(一)概念
- 可以从两个方向进行遍历,可以利用中间的一个节点推出下一个节点和上一个节点
(二)双向循环链表的结构图
(三)程序封装双向循环链表
1.创建链表类和节点类
//节点的结构
class Dnode {
constructor(data) {
this.prev = null;
this.data = data;
this.next = null;
}
}
//链表的结构
class DoubleCycleLinkList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0
}
}
2.单向循环链表的操作(方法)
1)append() 向链表尾部添加一个节点
append(ele) {
let newnode = new Dnode(ele);
if (this.length == 0) { //空链表
this.head = newnode;
this.tail = newnode;
newnode.prev = newnode;
newnode.next = newnode; //自己连自己
} else {
//将新节点连接
newnode.prev = this.tail; //新节点.prev = 尾节点
newnode.next = this.head; //新节点的next指向头结点
//断开原来的指向,重新指向新节点
this.tail.next = newnode; //尾节点.next指向新节点
this.head.prev = newnode; //头结点的prev指向新节点
this.tail = newnode; //将尾节点指向新节点
}
this.length++;
}
2)insert() 向链表中插入元素
insert(position, ele) {
// 位置是否合法
if (position < 0 || position > this.length || !Number.isInteger(position)) {
return false
}
let newnode = new Dnode(ele);
if (position == 0) {
//在头部插入
if (this.length == 0) { //空链表
this.head = newnode;
this.tail = newnode;
newnode.prev = this.tail
newnode.next = this.head
} else {
//将新节点连接
newnode.next = this.head; //新节点.next指向头节点
newnode.prev = this.tail; //新节点的prev指向尾结点
//断开原来的指向
this.head.prev = newnode; //头节点.prev指向新节点
this.tail.next = newnode //尾节点的next指向新节点
this.head = newnode; //头节点指向新节点
}
this.length++
} else if (position == this.length) {
//在尾部插入
this.append(ele)
} else {
//在中间任意位置
let current = this.head,
index = 0;
while (index < position - 1) {
current = current.next;
index++;
}
// 1.将新节点连上去
newnode.prev = current;
newnode.next = current.next;
// 2.断开原来的指向
current.next = newnode;
newnode.next.prev = newnode;
this.length++;
}
}
3)removeAt() 移除在指定位置的元素
removeAt(position) {
//判断位置的合法性(越界判断)
if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
return false
}
if (this.length == 0) {
//空链表
return
} else {
if (position == 0) {
//移除头节点
if (this.length == 1) {
this.head = null
this.tail = null
} else {
this.head = this.head.next
this.tail.prev=this.head
this.head.prev = this.head
}
} else if (position == this.length - 1) {
//移除尾结点
this.tail = this.tail.prev
this.tail.next = this.head
this.head.prev=this.tail
} else {
//移除中间任意位置的节点
let current = this.head,
index = 0
while (index < position - 1) {
current = current.next
index++
}
current.next = current.next.next
current.next.prev = current
}
this.length--
}
}
4)indexOf() 查找指定元素
indexOf(ele) {
let current = this.head,
index = 0
while (index < this.length) {
if (current.data == ele) {
return index
} else {
current = current.next
index++
}
}
return -1
}
5)remove() 移除指定元素
remove(ele) {
let index = this.indexOf(ele)
this.removeAt(index)
}
6)正向遍历
forwardString() {
let current = this.head,
index = 0,
str = "";
while (index < this.length) {
str += "-" + current.data
current = current.next
index++
}
return str.slice(1)
}
7)反向遍历
reverseString() {
let current = this.tail,
index = this.length - 1,
str = "";
while (index >= 0) {
str += "-" + current.data
current = current.prev
index--
}
return str.slice(1)
}
(四)完整代码&操作示例
//节点的结构
class Dnode {
constructor(data) {
this.prev = null;
this.data = data;
this.next = null;
}
}
//链表的结构
class DoubleCycleLinkList {
constructor() {
this.head = null;
this.tail = null;
this.length = 0
}
//1.在链表尾部添加元素
append(ele) {
let newnode = new Dnode(ele);
if (this.length == 0) { //空链表
this.head = newnode;
this.tail = newnode;
newnode.prev = newnode;
newnode.next = newnode; //自己连自己
} else {
//将新节点连接
newnode.prev = this.tail; //新节点.prev = 尾节点
newnode.next = this.head; //新节点的next指向头结点
//断开原来的指向,重新指向新节点
this.tail.next = newnode; //尾节点.next指向新节点
this.head.prev = newnode; //头结点的prev指向新节点
this.tail = newnode; //将尾节点指向新节点
}
this.length++;
}
// 2.向链表指定位置插入元素
insert(position, ele) {
// 位置是否合法
if (position < 0 || position > this.length || !Number.isInteger(position)) {
return false
}
let newnode = new Dnode(ele);
if (position == 0) {
//在头部插入
if (this.length == 0) { //空链表
this.head = newnode;
this.tail = newnode;
newnode.prev = this.tail
newnode.next = this.head
} else {
//将新节点连接
newnode.next = this.head; //新节点.next指向头节点
newnode.prev = this.tail; //新节点的prev指向尾结点
//断开原来的指向
this.head.prev = newnode; //头节点.prev指向新节点
this.tail.next = newnode //尾节点的next指向新节点
this.head = newnode; //头节点指向新节点
}
this.length++
} else if (position == this.length) {
//在尾部插入
this.append(ele)
} else {
//在中间任意位置
let current = this.head,
index = 0;
while (index < position - 1) {
current = current.next;
index++;
}
// 1.将新节点连上去
newnode.prev = current;
newnode.next = current.next;
// 2.断开原来的指向
current.next = newnode;
newnode.next.prev = newnode;
this.length++;
}
}
//3.移除指定位置的节点
removeAt(position) {
//判断位置的合法性(越界判断)
if (position < 0 || position > this.length - 1 || !Number.isInteger(position)) {
return false
}
if (this.length == 0) {
//空链表
return
} else {
if (position == 0) {
//移除头节点
if (this.length == 1) {
this.head = null
this.tail = null
} else {
this.head = this.head.next
this.tail.prev=this.head
this.head.prev = this.head
}
} else if (position == this.length - 1) {
//移除尾结点
this.tail = this.tail.prev
this.tail.next = this.head
this.head.prev=this.tail
} else {
//移除中间任意位置的节点
let current = this.head,
index = 0
while (index < position - 1) {
current = current.next
index++
}
current.next = current.next.next
current.next.prev = current
}
this.length--
}
}
//4.查找指定元素的位置
indexOf(ele) {
let current = this.head,
index = 0
while (index < this.length) {
if (current.data == ele) {
return index
} else {
current = current.next
index++
}
}
return -1
}
//5.移除指定元素
remove(ele) {
let index = this.indexOf(ele)
this.removeAt(index)
}
//6.正向遍历
forwardString() {
let current = this.head,
index = 0,
str = "";
while (index < this.length) {
str += "-" + current.data
current = current.next
index++
}
return str.slice(1)
}
//7.反向遍历
reverseString() {
let current = this.tail,
index = this.length - 1,
str = "";
while (index >= 0) {
str += "-" + current.data
current = current.prev
index--
}
return str.slice(1)
}
}
let clist = new DoubleCycleLinkList()
//向链表尾部添加元素
for (let i = 0; i < 9; i++) {
clist.append(i)
}
console.log(clist.forwardString(),"向链表尾部添加元素后的链表")
//向链表中的3的位置插入元素
clist.insert(3,"newnode")
console.log(clist.forwardString(),"插入元素后的链表")
//移除指定位置的元素
clist.removeAt(5)
console.log(clist.forwardString(),"移除位置为5的元素(4)的链表")
//查找指定元素
console.log(clist.indexOf(4),"4已经被移除,不存在返回-1")
console.log(clist.indexOf(6),"6存在返回下标")
//移除指定元素
clist.remove(8)
console.log(clist.reverseString(),"移除元素8后的反向遍历的链表")
打印结果:
你可能感兴趣的:(数据结构,数据结构,链表,循环链表)