队列:先进先出的数据结构,可以用数组来模拟
1 const queue = [];
2 queue.push(1);
3 queue.push(2);
4 const item1 = queue.shift();
5 const item2 = queue.shift();
链表:元素存储是不连续的,之间通过next链接、JS里面没有链表,但是可以用Object来模拟链表。
1 const a = { val: 'a'};
2 const b = { val: 'b'};
3 const c = { val: 'c'};
4 const d = { val: 'd'};
5 a.next = b;
6 b.next = c;
7 c.next = d;
8
9 // 遍历链表
10 let p = a;
11 while (p) {
12 console.log(p.val);
13 p = p.next;
14 }
15
16 //插入
17 const e = { val: 'e'};
18 c.next = e;
19 e.next = d;
20
21 //删除
22 c.next = d;