美文网首页Node.jsNode.js专题
js实现单链表、队列、栈

js实现单链表、队列、栈

作者: e042cbe4da21 | 来源:发表于2017-04-13 23:38 被阅读55次

单链表

'use strict';

function Node(value) {
  this.value = value;
  this.next = null;
}

function LList() {
  this.head = new Node('head');
  this.find = find;
  this.insert = insert;
  this.remove = remove;
  this.display = display;
}

function find(item) {
  let currNode = this.head;
  while (currNode.value !== item && currNode !== null) {
    currNode = currNode.next;
  }
  return currNode;
}

function insert(value, item) {
  const newNode = new Node(value);
  let current;
  if (item) {
    current = this.find(item);
    //console.log(current.value)
    newNode.next = current.next;
  } else {
    current = this.head;
    while (current.next !== null) {
      current = current.next;
    }
  }
  current.next = newNode;
}

function display() {
  let currNode = this.head;
  while (currNode !== null) {
    console.log(currNode.value);
    currNode = currNode.next;
  }
}

function remove(item) {
  let currNode = this.head;
  while (currNode.next !== null && currNode.next.value !== item) {
    currNode = currNode.next;
  }
  if (currNode.next !== null) {
    let rmNode = currNode.next;
    currNode.next = rmNode.next;
    rmNode.next = null;
  }
}

const cities = new LList();
cities.insert('Conway', 'head');
cities.insert('Russellville', 'Conway');
cities.insert('Alma', 'Russellville');
cities.insert('testNode');
cities.display();
console.log('-------------');
cities.remove('Alma');
cities.display();

队列

'use strict';

function Queue() {
  this.dataScore = [];
  this.head = 0;
  this.tail = 0;
  this.enqueue = enqueue;
  this.dequeue = dequeue;
  this.front = front;
  this.back = back;
  this.queueToString = queueToString;
  this.empty = empty;
};

function enqueue(element) {
  this.dataScore[this.tail++] = element;
};
function dequeue() {
  this.tail--;
  return this.dataScore.shift();
};
function front() {
  if (this.empty()) return '';
  return this.dataScore[this.head];
};
function back() {
  if (this.empty()) return '';
  return this.dataScore[this.tail - 1];
}
function queueToString() {
  if (this.empty()) return '';
  let str = '';
  let i = this.head;
  while (i < this.tail) {
    str += this.dataScore[i] + ' ';
    i++;
  }

  return str;
};
function empty() {
  if (this.tail === this.head) return true;

  return false;
};

const q = new Queue();
q.enqueue('a');
q.enqueue('b');
q.enqueue('c');
q.enqueue('d');
q.enqueue('e');
q.enqueue('f');
console.log(q.queueToString())
console.log(q.front(), q.back())
q.dequeue();
console.log(q.front(), q.back())

'use strict';

function Stack() {
  this.dataStore = [];
  this.top = 0;
  this.push = push;
  this.pop = pop;
  this.peek = peek;
  this.length = length;
  this.clear = clear;
  this.empty = empty;
};

function push(element) {
  this.dataStore[this.top++] = element;
};

function pop() {
  const item = this.dataStore[this.top-- - 1];
  this.dataStore.splice(this.top, 1);

  return item;
};

function peek() {
  return this.dataStore[this.top - 1];
};

function length() {
  return this.top;
};

function clear() {
  this.dataStore.splice(0, this.top);
  this.top = 0;
}

function empty() {
  if (this.top === 0) return true;
  return false;
};

function mulBase(num, base) {
  let s = new Stack();
  if (num < base) return num;
  let tmp = num;
  while (tmp / base >= 1) {
    s.push(tmp % base);
    tmp = parseInt(tmp / base, 10);
  }
  s.push(1);
  let converted = '';
  while (s.length() > 0) {
    converted += s.pop();
  }

  return converted;
}

console.log(mulBase(17, 2))

function Queue(stack1, stack2) {
  this.tailStack = stack1;
  this.headStack = stack2;
  this.enqueue = enqueue;
  this.dequeue = dequeue;
  this.queueToString = queueToString;
  this.empty = queueEmpty;
}

function enqueue(element) {
  this.tailStack.push(element);
}
function dequeue() {
  if (this.headStack.empty()) {
    while (!this.tailStack.empty()) {
      this.headStack.push(this.tailStack.pop());
    }
  }
  return this.headStack.pop();
}
function queueToString() {
  if (this.empty()) return '';
  let str = '';
  if (!this.headStack.empty()) {
    let i = this.headStack.top - 1;
    while (i >= 0) {
      str += this.headStack.dataStore[i--] + ' ';
    }
  }
  if (!this.tailStack.empty()) {
    let i = 0;
    while (i < this.tailStack.top) {
      str += this.tailStack.dataStore[i++] + ' ';
    }
  }

  return str;
}
function queueEmpty() {
  if (this.tailStack.empty() && this.headStack.empty()) return true;
  return false;
}
const s1 = new Stack();
const s2 = new Stack();
const q = new Queue(s1, s2);

console.log(q.empty());
q.enqueue('a');
q.enqueue('b');
q.enqueue('c');
q.enqueue('d');
console.log(q.queueToString());
q.dequeue();
console.log(q.queueToString());
q.dequeue();
console.log(q.queueToString());
console.log(q.empty());

相关文章

  • 数据结构-系列文章

    线性表 单链表 单链表-OC实现 双链表 循环链表 栈 栈 队列 待完善 数组 待完善 树 待完善 图 待完善 哈...

  • js实现单链表、队列、栈

    单链表 队列 栈

  • 用数组实现栈、队列

    用数组实现一个栈 用数组实现一个队列 用单链表实现给队列

  • 数据结构与算法之数组与链表

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

  • 数据结构与算法之栈与队列

    线性表包括数组,链表(单链表,双向链表,循环链表,双向循环链表,静态链表),栈(顺序栈,链式栈),队列(普通队列,...

  • 数据结构java描述

    接口 栈 队列 集合 并查集 映射 数组 链表 栈 数组实现 链表实现 队列 数组实现 链表实现 二分搜索树 集合...

  • 单链表和双链表

    单链表(可以用来实现栈和队列) 删除链表的元素 添加元素 双向链表(实现LinkedList) 添加元素 删除元素

  • 5-玩转数据结构-链表与递归

    前面我们实现了一个单链表,用它又实现了栈和队列,实现队列时对于链表进行了改进。链表与递归,递归和树联系在一起。但链...

  • C语言第七次作业:链表

    707. 设计链表 空指针 空节点 225. 用队列实现栈 链式存储栈 双队列实现栈 232. 用栈实现队列 链式...

  • 基础算法学习与实践

    数组&链表 1. 快慢指针的方式实现判断链表是否有环 栈和队列 1. 栈实现队列(负负得正) ...

网友评论

    本文标题:js实现单链表、队列、栈

    本文链接:https://www.haomeiwen.com/subject/suiiattx.html