How to use currentNode method in Cucumber-gherkin

Best JavaScript code snippet using cucumber-gherkin

dfs.js

Source:dfs.js Github

copy

Full Screen

1// Depth-first search code starts at line 1692class Node {3 constructor(value) {4 this.value = value;5 this.left = null;6 this.right = null;7 }8}9class BinarySearchTree {10 constructor() {11 this.root = null;12 }13 insert(value) {14 const newNode = new Node(value);15 if (this.root === null) {16 this.root = newNode;17 } else {18 let currentNode = this.root;19 while (true) {20 if (value < currentNode.value) {21 // go left22 if (!currentNode.left) {23 currentNode.left = newNode;24 return this;25 }26 currentNode = currentNode.left;27 } else {28 // go right29 if (!currentNode.right) {30 currentNode.right = newNode;31 return this;32 }33 currentNode = currentNode.right;34 }35 }36 }37 }38 lookup(value) {39 if (!this.root) {40 return false;41 }42 let currentNode = this.root;43 while (currentNode) {44 if (value < currentNode.value) {45 currentNode = currentNode.left;46 } else if (value > currentNode.value) {47 currentNode = currentNode.right;48 } else if (currentNode.value === value) {49 return currentNode;50 }51 }52 return false;53 }54 remove(value) {55 if (!this.root) {56 return false;57 }58 let currentNode = this.root;59 let parentNode = null;60 while (currentNode) {61 if (value < currentNode.value) {62 parentNode = currentNode;63 currentNode = currentNode.left;64 } else if (value > currentNode.value) {65 parentNode = currentNode;66 currentNode = currentNode.right;67 } else if (currentNode.value === value) {68 // Option 1: no right child69 if (currentNode.right === null) {70 if (parentNode === null) {71 this.root = currentNode.left;72 } else {73 // if parent > current value, make current left child a child of parent74 if (currentNode.value < parentNode.value) {75 parentNode.left = currentNode.left;76 // if parent < current value, make left child a right child of parent77 } else if (currentNode.value > parentNode.value) {78 parentNode.right = currentNode.left;79 }80 }81 //Option 2: right child which doesnt have a left child82 } else if (currentNode.right.left === null) {83 if (parentNode === null) {84 this.root = currentNode.left;85 } else {86 currentNode.right.left = currentNode.left;87 // if parent > current, make right child of the left the parent88 if (currentNode.value < parentNode.value) {89 parentNode.left = currentNode.right;90 // if parent < current, make right child a right child of the parent91 } else if (currentNode.value > parentNode.value) {92 parentNode.right = currentNode.right;93 }94 }95 //Option 3: right child that has a left child96 } else {97 // find the right child's leftmost child98 let leftmost = currentNode.right.left;99 let leftmostParent = currentNode.right;100 while (leftmost.left !== null) {101 leftmostParent = leftmost;102 leftmost = leftmost.left;103 }104 // parent's left subtree is now leftmost's right subtree105 leftmostParent.left = leftmost.right;106 leftmost.left = currentNode.left;107 leftmost.right = currentNode.right;108 if (parentNode === null) {109 this.root = leftmost;110 } else {111 if (currentNode.value < parentNode.value) {112 parentNode.left = leftmost;113 } else if (currentNode.value > parentNode.value) {114 parentNode.right = leftmost;115 }116 }117 }118 return true;119 }120 }121 }122 // Breadth-first search123 bfs() {124 let currentNode = this.root;125 let list = [];126 let queue = [];127 queue.push(currentNode);128 while (queue.length > 0) {129 currentNode = queue.shift();130 console.log(currentNode.value);131 list.push(currentNode.value);132 if (currentNode.left) {133 queue.push(currentNode.left);134 }135 if (currentNode.right) {136 queue.push(currentNode.right);137 }138 }139 return list;140 }141 // BFS recursive142 bfsRecursive(queue, list) {143 if (!queue.length) {144 return list;145 }146 let currentNode = queue.shift();147 list.push(currentNode.value);148 if (currentNode.left) {149 queue.push(currentNode.left);150 }151 if (currentNode.right) {152 queue.push(currentNode.right);153 }154 return this.bfsRecursive(queue, list);155 }156 // Depth-first search In Order157 dfsInOrder() {158 return traverseInOrder(this.root, []);159 }160 // DFS post order161 dfsPreOrder() {162 return traversePreOrder(this.root, []);163 }164 // DFS pre order165 dfsPostOrder() {166 return traversePostOrder(this.root, []);167 }168}169function traverseInOrder(node, list) {170 //console.log(node.value) // check the path taken171 if (node.left) {172 traverseInOrder(node.left, list);173 }174 list.push(node.value);175 if (node.right) {176 traverseInOrder(node.right, list);177 }178 return list;179}180function traversePreOrder(node, list) {181 //console.log(node.value) // check the path taken182 list.push(node.value);183 if (node.left) {184 traversePreOrder(node.left, list);185 }186 if (node.right) {187 traversePreOrder(node.right, list);188 }189 return list;190}191function traversePostOrder(node, list) {192 //console.log(node.value) // check the path taken193 if (node.left) {194 traversePostOrder(node.left, list);195 }196 if (node.right) {197 traversePostOrder(node.right, list);198 }199 list.push(node.value);200 return list;201}202function traverse(node) {203 const tree = { value: node.value };204 tree.left = node.left === null ? null :205 traverse(node.left);206 tree.right = node.right === null ? null :207 traverse(node.right);208 return tree;209}210const bst = new BinarySearchTree();211bst.insert(9);212bst.insert(4);213bst.insert(6);214bst.insert(20);215bst.insert(170);216bst.insert(15);217bst.insert(1);218console.log(bst.dfsInOrder());219console.log(bst.dfsPreOrder());220console.log(bst.dfsPostOrder());221// 9222// 4 20223//1 6 15 170224// 3 Types of DFS ordering/traversal225// InOrder - [1,4,6,9,15,20,170]226// PreOrder - [9,4,1,6,20,15,170]...

Full Screen

Full Screen

breadth_first_search.js

Source:breadth_first_search.js Github

copy

Full Screen

1class Node {2 constructor(value) {3 this.left = null;4 this.right = null;5 this.value = value;6 }7}8class BinarySearchTree {9 constructor() {10 this.root = null;11 }12 insert(value) {13 const newNode = new Node(value)14 if (this.root === null) {15 this.root = newNode16 } else {17 let currentNode = this.root;18 while (true) {19 if (value < currentNode.value) {20 if (!currentNode.left) {21 currentNode.left = newNode;22 return this;23 }24 currentNode = currentNode.left;25 } else {26 if (!currentNode.right) {27 currentNode.right = newNode;28 return this;29 }30 currentNode = currentNode.right;31 }32 }33 }34 }35 lookup(value) {36 if (this.root === null) {37 return false38 }39 let currentNode = this.root;40 while (currentNode) {41 if (value < currentNode.value) {42 currentNode = currentNode.left43 } else if (value > currentNode.value) {44 currentNode = currentNode.right45 } else if (currentNode.value === value) {46 return currentNode;47 }48 }49 return false;50 }51 remove(value) {52 if (!this.root) {53 return false;54 }55 let currentNode = this.root;56 let parentNode = null;57 while (currentNode) {58 if (value < currentNode.value) {59 parentNode = currentNode;60 currentNode = currentNode.left61 } else if (value > currentNode.value) {62 parentNode = currentNode63 currentNode = currentNode.right64 } else if (currentNode.value === value) {65 // We ave a match66 // option 1 : No right child:67 if (currentNode.right === null) {68 if (parentNode === null) {69 this.root = currentNode.left;70 } else {71 // if parent > current value, make current left chuld a child of parent72 if (currentNode.value < parentNode.value) {73 parentNode.left = currentNode.left;74 // if parent < current value, make left child a right child of parent75 } else if (currentNode.value > parentNode.value) {76 parentNode.right = currentNode.left77 }78 }79 // option 2: Right child which doesn't have a left child80 } else if (currentNode.right.left === null) {81 if (parentNode === null) {82 this.root = currentNode.left;83 } else {84 currentNode.right.left = currentNode.left;85 // if parent > current, make right child of the left the parent86 if (currentNode.value < parentNode.value) {87 parentNode.left = currentNode.right;88 // if parent < current, make right child a right child of the parent89 } else if (currentNode.value > parentNode.value) {90 parentNode.right = currentNode.right;91 }92 }93 // option 3: Right child that has a left child94 } else {95 // find the right child's left most child96 let leftmost = currentNode.right.left;97 let leftmostParent = currentNode.right;98 while (leftmost.eft !== null) {99 leftmostParent = leftmost;100 leftmost = leftmost.left;101 }102 // parent's left subtree is now left's modt right subtree103 leftmostParent.left = leftmost.right;104 leftmost.left = currentNode.left;105 leftmost.right = currentNode.right;106 if (parentNode === null) {107 this.root = leftmost;108 } else {109 if (currentNode.value < parentNode.value) {110 parentNide.left = leftmost;111 } else if (currentNode.value > parentNode.value) {112 parentNode.right = leftmost;113 }114 }115 }116 return true117 }118 }119 }120 breadthFirstSearch() {121 let currentNode = this.root;122 let list = [];123 let queue = [];124 queue.push(currentNode);125 while (queue.length > 0) {126 currentNode = queue.shift();127 list.push(currentNode.value);128 if (currentNode.left) {129 queue.push(currentNode.left)130 }131 if (currentNode.right) {132 queue.push(currentNode.right)133 }134 }135 return list;136 }137 breadthFirstSearchR(queue, list) {138 if (!queue.length) {139 return list;140 }141 let currentNode = queue.shift();142 list.push(currentNode.value)143 if (currentNode.left) {144 queue.push(currentNode.left)145 }146 if (currentNode.right) {147 queue.push(currentNode.right)148 }149 return this.breadthFirstSearchR(queue, list)150 }151 DFSInOrder() {152 return traverseInorder(this.root, [])153 }154 DFSPostOrder() {155 return traversePostorder(this.root, [])156 }157 DFSPreOrder() {158 return traversePreorder(this.root, [])159 }160}161function traverseInorder(node, list) {162 if (node.left) {163 traverseInorder(node.left, list)164 }165 list.push(node.value)166 if (node.right) {167 traverseInorder(node.right, list)168 }169 return list;170}171function traversePreorder(node, list) {172 list.push(node.value)173 if (node.left) {174 traversePreorder(node.left, list)175 }176 if (node.right) {177 traversePreorder(node.right, list)178 }179 return list;180}181function traversePostorder(node, list) {182 if (node.left) {183 traversePostorder(node.left, list)184 }185 if (node.right) {186 traversePostorder(node.right, list)187 }188 list.push(node.value)189 return list;190}191const tree = new BinarySearchTree();192tree.insert(9);193tree.insert(4)194tree.insert(6)195tree.insert(20)196tree.insert(170)197tree.insert(15)198tree.insert(1)199 // tree.remove(170)200 // console.log(tree.breadthFirstSearch())201 // console.log(tree.breadthFirstSearch([tree.root], []));202console.log(tree.DFSPostOrder())203function traverse(node) {204 const tree = { value: node.value };205 tree.left = node.left === null ? null :206 traverse(node.left);207 tree.right = node.right === null ? null :208 traverse(node.right);209 return tree;...

Full Screen

Full Screen

bfs.js

Source:bfs.js Github

copy

Full Screen

1// Breadth-first search code starts at line 1302class Node {3 constructor(value) {4 this.value = value;5 this.left = null;6 this.right = null;7 }8}9class BinarySearchTree {10 constructor() {11 this.root = null;12 }13 insert(value) {14 const newNode = new Node(value);15 if (this.root === null) {16 this.root = newNode;17 } else {18 let currentNode = this.root;19 while (true) {20 if (value < currentNode.value) {21 // go left22 if (!currentNode.left) {23 currentNode.left = newNode;24 return this;25 }26 currentNode = currentNode.left;27 } else {28 // go right29 if (!currentNode.right) {30 currentNode.right = newNode;31 return this;32 }33 currentNode = currentNode.right;34 }35 }36 }37 }38 lookup(value) {39 if (!this.root) {40 return false;41 }42 let currentNode = this.root;43 while (currentNode) {44 if (value < currentNode.value) {45 currentNode = currentNode.left;46 } else if (value > currentNode.value) {47 currentNode = currentNode.right;48 } else if (currentNode.value === value) {49 return currentNode;50 }51 }52 return false;53 }54 remove(value) {55 if (!this.root) {56 return false;57 }58 let currentNode = this.root;59 let parentNode = null;60 while (currentNode) {61 if (value < currentNode.value) {62 parentNode = currentNode;63 currentNode = currentNode.left;64 } else if (value > currentNode.value) {65 parentNode = currentNode;66 currentNode = currentNode.right;67 } else if (currentNode.value === value) {68 // Option 1: no right child69 if (currentNode.right === null) {70 if (parentNode === null) {71 this.root = currentNode.left;72 } else {73 // if parent > current value, make current left child a child of parent74 if (currentNode.value < parentNode.value) {75 parentNode.left = currentNode.left;76 // if parent < current value, make left child a right child of parent77 } else if (currentNode.value > parentNode.value) {78 parentNode.right = currentNode.left;79 }80 }81 //Option 2: right child which doesnt have a left child82 } else if (currentNode.right.left === null) {83 if (parentNode === null) {84 this.root = currentNode.left;85 } else {86 currentNode.right.left = currentNode.left;87 // if parent > current, make right child of the left the parent88 if (currentNode.value < parentNode.value) {89 parentNode.left = currentNode.right;90 // if parent < current, make right child a right child of the parent91 } else if (currentNode.value > parentNode.value) {92 parentNode.right = currentNode.right;93 }94 }95 //Option 3: right child that has a left child96 } else {97 // find the right child's leftmost child98 let leftmost = currentNode.right.left;99 let leftmostParent = currentNode.right;100 while (leftmost.left !== null) {101 leftmostParent = leftmost;102 leftmost = leftmost.left;103 }104 // parent's left subtree is now leftmost's right subtree105 leftmostParent.left = leftmost.right;106 leftmost.left = currentNode.left;107 leftmost.right = currentNode.right;108 if (parentNode === null) {109 this.root = leftmost;110 } else {111 if (currentNode.value < parentNode.value) {112 parentNode.left = leftmost;113 } else if (currentNode.value > parentNode.value) {114 parentNode.right = leftmost;115 }116 }117 }118 return true;119 }120 }121 }122 // Breadth-first search123 bfs() {124 let currentNode = this.root;125 let list = [];126 let queue = [];127 queue.push(currentNode);128 while (queue.length > 0) {129 currentNode = queue.shift();130 console.log(currentNode.value);131 list.push(currentNode.value);132 if (currentNode.left) {133 queue.push(currentNode.left);134 }135 if (currentNode.right) {136 queue.push(currentNode.right);137 }138 }139 return list;140 }141 // BFS recursive142 bfsRecursive(queue, list) {143 if (!queue.length) {144 return list;145 }146 let currentNode = queue.shift();147 list.push(currentNode.value);148 if (currentNode.left) {149 queue.push(currentNode.left);150 }151 if (currentNode.right) {152 queue.push(currentNode.right);153 }154 return this.bfsRecursive(queue, list);155 }156}157function traverse(node) {158 const tree = { value: node.value };159 tree.left = node.left === null ? null :160 traverse(node.left);161 tree.right = node.right === null ? null :162 traverse(node.right);163 return tree;164}165const bst = new BinarySearchTree();166bst.insert(9);167bst.insert(4);168bst.insert(6);169bst.insert(20);170bst.insert(170);171bst.insert(15);172bst.insert(1);173// console.log(bst.bfs());174console.log(bst.bfsRecursive([bst.root], []));175// 9176// 4 20...

Full Screen

Full Screen

链表基础.js

Source:链表基础.js Github

copy

Full Screen

1/**2 * 1) 单链表的插入、删除、查找操作;3 * 2)单链表反转4 */5class Node {6 constructor(value) {7 this.value = value;8 this.next = null;9 }10}11class SinglyLinkedList {12 constructor() {13 this.head = new Node('head');14 }15 // 根据value查找节点16 findByValue(value) {17 let currentNode = this.head.next;18 while (currentNode !== null && currentNode.value !== value) {19 currentNode = currentNode.next;20 }21 console.log(currentNode);22 return currentNode === null ? -1 : currentNode;23 }24 // 根据index查找节点,下标从0开始25 findByIndex(index) {26 let currentNode = this.head.next;27 let currentIndex = 0;28 while (currentNode !== null && currentNode < index) {29 currentNode = currentNode.next;30 currentIndex++;31 }32 console.log(currentNode);33 return currentNode === null ? -1 : currentNode;34 }35 // 向链表末尾追加节点36 append(newValue) {37 const newNode = new Node(newValue);38 let currentNode = this.head;39 while(currentNode.next) {40 currentNode = currentNode.next;41 }42 currentNode.next = newNode;43 }44 // 指定元素向后插入45 insert(newValue, value) {46 const currentNode = this.findByValue(value);47 if (currentNode === -1) {48 console.log('未找到插入位置');49 return50 }51 const newNode = new Node(newValue);52 newNode.next = currentNode.next;53 currentNode.next = newNode;54 }55 // 根据值删除56 remove(value) {57 let currentNode = this.head;58 while (currentNode.next !== null && currentNode.next.value !== value) {59 currentNode = currentNode.next;60 }61 if (currentNode.next === null) {62 console.log('未找到元素');63 return64 }65 const prevNode = currentNode;66 prevNode.next = prevNode.next.next;67 }68 // 遍历显示所有节点69 display() {70 let currentNode = this.head.next; // 忽略头指针的值71 while (currentNode !== null) {72 console.log(currentNode.value);73 currentNode = currentNode.next;74 }75 }76 // 反转单链表77 reverse() {78 // head节点即哨兵,作用就是使所有链表,包括空链表的头节点不为null,并使对单链表的插入、删除操作不需要区分是否为空表或是否在第一个位置进行,79 // 从而与其他位置的插入、删除操作一致,降低编码复杂度80 // 所以反转链表的时候不需要带上头节点81 let currentNode = this.head.next;82 // 第一个节点的前驱结点让其指向 null, 因为头节点不参与链表反转83 let prevNode = null;84 while (currentNode !== null) {85 // 每个循环中不断进行 currentNode, nextNode 和 prevNode 三个指针的赋值,86 // 其中currentNode 和 prevNode 在赋值前就被引用了,因此这两个变量需要在循环体外赋初始值87 // 先保留下一结点的指针,避免指针丢失88 let nextNode = currentNode.next;89 // 这一步真正实现指针反转,指向前驱节点90 currentNode.next = prevNode;91 prevNode = currentNode;92 currentNode = nextNode;93 }94 // 最后给反转后的链表戴上头结点,这里最后一个currentNode为null,最后一个prevNode才指向第一个结点,因此选 prevNode 来赋值95 this.head.next = prevNode;96 }97 // 环的检测,快慢指针法98 checkCircle() {99 let fast = this.head.next;100 let slow = this.head;101 while (fast != null && fast.next != null) {102 fast = fast.next.next;103 slow = slow.next;104 if (show === fast) return true;105 } 106 return false;107 } 108 // 删除倒数第k个节点109 removeByIndexFromEnd(index) {110 // 先判断是否是环链表111 if (this.checkCircle) return false;112 let pos = 1;113 this.reverse()114 let currentNode = this.head.next;115 while (currentNode !== null && pos < index) {116 currentNode = currentNode++117 pos++;118 }119 if (currentNode === null) {120 console.log('该结点不存在');121 return false;122 }123 this.remove(currentNode.value);124 this.reverse();125 }126 // 求链表的中间结点127 findMiddleNode() {128 // 忽略头结点129 let fast = this.head.next;130 let slow = this.head.next;131 while (fast.next != null && fast.next.next != null) {132 fast = fast.next.next;133 slow = slow.next;134 }135 console.log(slow);136 return slow;137 }138}139const mergeSortedLists = (listA, listB) => {140 if (!listA) {141 return listB142 }143 if (!listB) {144 return listA145 }146 let resultList = new Node('head');147 while (a !== null && b !== null) {148 if (a.value < b.value) {149 resultList.next = a;150 a = a.next;151 } else {152 resultList.next = b;153 b = b.next;154 }155 resultList = resultList.next;156 }157 // 如果链表a或链表b还有剩余结点,则接到合并后的链表的后面158 if (a !== null) {159 resultList.next = a;160 } else {161 resultList.next = b;162 }163 return resultList;...

Full Screen

Full Screen

rbtree.js

Source:rbtree.js Github

copy

Full Screen

1import { cloneTree, getUncle, getGrandFather, getSibling, rotate, buildTree } from './functions';2import { RED, BLACK, LEFT, RIGHT } from './constants';34function add(tree, value) {5 const newNode = {6 value,7 left: null,8 right: null,9 parent: null,10 color: RED,11 };1213 if (!tree) {14 newNode.color = BLACK;15 return newNode;16 }1718 const resultTree = cloneTree(tree);1920 let parentNode;21 let currentNode = resultTree;2223 while (currentNode) {24 parentNode = currentNode;25 if (currentNode.value === newNode.value) return resultTree;26 currentNode = newNode.value > currentNode.value ? currentNode.right : currentNode.left;27 }2829 currentNode = newNode;30 currentNode.parent = parentNode;31 if (currentNode.value > parentNode.value) {32 parentNode.right = currentNode;33 } else {34 parentNode.left = currentNode;35 }3637 while (currentNode.color === RED && currentNode.parent && currentNode.parent.color === RED) {38 const uncle = getUncle(currentNode);39 const grandFather = getGrandFather(currentNode);40 const { parent } = currentNode;4142 if (uncle && uncle.color === RED) {43 parent.color = BLACK;44 uncle.color = BLACK;45 grandFather.color = RED;46 currentNode = grandFather;47 if (!currentNode.parent) currentNode.color = BLACK;48 continue;49 }5051 if (!uncle || uncle.color === BLACK) {52 const sideOfTree = parent === grandFather.left ? LEFT : RIGHT;53 const anotherDirection = parent === grandFather.right ? LEFT : RIGHT;5455 if (parent[sideOfTree] === currentNode) {56 currentNode = parent;57 rotate(currentNode, anotherDirection);58 currentNode.color = BLACK;59 currentNode[anotherDirection].color = RED;60 } else {61 rotate(currentNode, sideOfTree);62 rotate(currentNode, anotherDirection);63 currentNode.color = BLACK;64 currentNode[anotherDirection].color = RED;65 }66 }67 }68 return buildTree(currentNode);69}7071function remove(tree, value) {72 const resultTree = cloneTree(tree);73 let deletedNode = resultTree;7475 while (value !== deletedNode.value) {76 if (value > deletedNode.value) deletedNode = deletedNode.right;77 else deletedNode = deletedNode.left;78 if (!deletedNode) return resultTree;79 }8081 let maxInLeft = deletedNode;82 if (deletedNode.left) {83 maxInLeft = deletedNode.left;84 while (maxInLeft.right) {85 maxInLeft = maxInLeft.right;86 }87 }8889 deletedNode.value = maxInLeft.value;9091 const currentNode = maxInLeft;92 const { parent } = currentNode;93 const direction = parent.left === currentNode ? LEFT : RIGHT;94 const anotherDirection = parent.left === currentNode ? RIGHT : LEFT;9596 parent[direction] = null;9798 while (currentNode.color === 0 && currentNode.parent) {99 const { parent: currentParent } = currentNode;100 const sibling = getSibling(currentNode);101 const nephewDirection = sibling[direction];102 const nephewAnother = sibling[anotherDirection];103 if (!sibling) break;104105 if (sibling.color === RED) {106 sibling.color = BLACK;107 currentParent.color = RED;108 rotate(sibling, direction);109 continue;110 }111112 if ((!nephewDirection || nephewDirection.color === BLACK) && (!nephewAnother || nephewAnother.color === BLACK)) {113 sibling.color = RED;114 currentParent.color = BLACK;115 break;116 }117118 if ((nephewDirection && nephewDirection.color === RED) && (!nephewAnother || nephewAnother.color === BLACK)) {119 sibling.color = RED;120 nephewDirection.color = BLACK;121 rotate(nephewDirection, anotherDirection);122 continue;123 }124125 if (nephewAnother && nephewAnother.color === RED) {126 sibling.color = currentParent.color;127 currentParent.color = BLACK;128 nephewAnother.color = BLACK;129 rotate(sibling, direction);130 break;131 }132 }133 return buildTree(currentNode);134}135 ...

Full Screen

Full Screen

singleLinkedList.js

Source:singleLinkedList.js Github

copy

Full Screen

1// 带头结点的单链表 插入、删除、查找2class Node {3 constructor(element) {4 this.element = element5 this.next = null6 }7}8class LinkList {9 // 带头结点10 constructor() {11 this.head = new Node('head')12 }13 // 向链表尾部添加14 append(element) {15 // 创建新的结点16 let newNode = new Node(element)17 // 头结点18 let currentNode = this.head19 while (currentNode.next !== null) {20 currentNode = currentNode.next21 }22 currentNode.next = newNode23 }24 findByValue (item) {25 let currentNode = this.head.next26 while (currentNode !== null && currentNode.element !== item) {27 currentNode = currentNode.next 28 }29 console.log(currentNode)30 return currentNode === null ? -1 : currentNode31 }32 // 在指定元素后插入33 insert (element, newElement) {34 // 查找指定元素的结点35 let currentNode = this.findByValue(element)36 // 若找不到指定元素的结点37 if (currentNode === -1) {38 console.log('找不到要插入的指定元素的位置')39 return40 }41 // 找到指定元素的结点42 // 创建新的结点43 let newNode = new Node(newElement)44 newNode.next = currentNode.next45 currentNode.next = newNode46 }47 // 根据下标index查找结点,从0开始48 findByIndex (index) {49 let currentNode = this.head.next50 let pos = 051 while (currentNode !== null && pos !== index) {52 currentNode = currentNode.next 53 pos++54 }55 console.log(currentNode)56 return currentNode === null ? -1 : currentNode57 }58 // 查找指定元素的前一个元素结点59 findPrev (item) {60 let currentNode = this.head61 while (currentNode.next !== null && currentNode.next.element !== item) {62 currentNode = currentNode.next63 }64 if (currentNode.next === null) {65 return -166 }67 return currentNode68 }69 // 删除指定元素70 remove (item) {71 // 查找指定元素的前一个元素结点72 let prevNode = this.findPrev(item)73 // console.log(currentNode)74 if (prevNode === -1) {75 console.log('不存在指定元素')76 return77 }78 // 删除指定元素79 prevNode.next = prevNode.next.next80 }81 // 遍历显示链表结点82 display() {83 let currentNode = this.head.next // 忽略头结点84 while (currentNode !== null) {85 console.log(currentNode.element)86 currentNode = currentNode.next87 }88 }89}90let linkList = new LinkList()91console.log(linkList)92// LinkList { head: Node { element: 'head', next: null } }93// 向链表尾部添加94linkList.append('computer')95linkList.append('pencil')96linkList.append('water')97linkList.append('milk')98linkList.display()99// computer100// pencil101// water102// milk103// 在指定元素后插入104linkList.insert('pencil', 'mobilephone') 105linkList.display()106// 根据下标index查找结点,从0开始107linkList.findByIndex(2) 108linkList.findByIndex(6) 109// 删除指定元素110linkList.remove('sugar')111linkList.display()112linkList.remove('water')...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

1// 单链表的插入、删除、查找2// 带有头结点的单链表3// 结点4class Node {5 constructor(element) {6 this.element = element // 数据7 this.next = null // 指针8 }9}10class LinkList {11 constructor() {12 this.head = new Node('head')13 }14 // 向链表末尾添加结点15 append(newElement) {16 const newNode = new Node(newElement)17 let currentNode = this.head18 while (currentNode.next) {19 currentNode = currentNode.next20 }21 currentNode.next = newNode22 }23 // 根据元素值value查找结点24 findByValue(item) {25 let currentNode = this.head.next26 while (currentNode !== null && currentNode.element !== item) {27 currentNode = currentNode.next28 }29 console.log(currentNode)30 return currentNode === null ? -1 : currentNode31 }32 // 根据index查找结点,下标从0开始33 findByIndex (index) {34 let currentNode = this.head.next35 let pos = 036 while (currentNode !== null && pos !== index) {37 currentNode = currentNode.next38 pos++39 }40 console.log(currentNode)41 return currentNode === null ? -1 : currentNode42 }43 // 在指定元素后面插入44 insert(element, newElement) {45 const currentNode = this.findByValue(element)46 if (currentNode === -1) {47 console.log('未找到插入的位置')48 return49 }50 const newNode = new Node(newElement)51 newNode.next = currentNode.next52 currentNode.next = newNode53 }54 // 查找前一个结点55 findPrev (item) {56 let currentNode = this.head 57 while (currentNode.next !== null && currentNode.next.element !== item) {58 currentNode = currentNode.next59 }60 if (currentNode.next === null) 61 return -162 return currentNode63 }64 // 根据值删除65 remove (item) {66 const prevNode = this.findPrev(item)67 if (prevNode === -1) {68 console.log('未找到该元素')69 return 70 }71 prevNode.next = prevNode.next.next72 }73 // 遍历显示所有的结点74 display () {75 let currentNode = this.head.next // 忽略头指针的值76 while (currentNode !== null) {77 console.log(currentNode.element)78 currentNode = currentNode.next79 }80 }81}82const linkList = new LinkList()83console.log(linkList)84// LinkList { head: Node { element: 'head', next: null } }85linkList.append('apple')86linkList.append('banana')87linkList.append('pear')88linkList.append('potato')89linkList.display()90// apple91// banana92// pear93// potato94linkList.findByValue('banana')95// Node {96// element: 'banana',97// next: Node {98// element: 'pear',99// next: Node { element: 'potato', next: null }100// }101// }102linkList.insert('banana', 'tomato')103linkList.display()104// apple105// banana106// tomato107// pear108// potato109linkList.findByIndex(2)110// Node {111// element: 'tomato',112// next: Node {113// element: 'pear',114// next: Node { element: 'potato', next: null }115// }116// }117linkList.remove('pear')118linkList.display()119// apple120// banana121// tomato122// potato123linkList.remove('egg')124linkList.display()125// 未找到该元素126// apple127// banana128// tomato...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var Cucumber = require('cucumber');2var fs = require('fs');3var gherkin = Cucumber.getGherkin();4var parser = new gherkin.Parser();5var lexer = new gherkin.Lexer('en');6var featureSource = fs.readFileSync('feature1.feature', 'utf8');7var feature = parser.parse(lexer.lex(featureSource));8var featureNode = feature.getFeature();9var scenarioNodes = featureNode.getScenarios();10var scenarioNode = scenarioNodes[0];11var stepNodes = scenarioNode.getSteps();12var stepNode = stepNodes[0];13console.log(stepNode.getName());14var express = require('express');15var app = express();16var bodyParser = require('body-parser');17var jsonParser = bodyParser.json();18var urlencodedParser = bodyParser.urlencoded({ extended: false });19var fs = require('fs');20var path = require('path');21var multer = require('multer');22var upload = multer({ dest: 'uploads/' });23var http = require('http');24var server = http.createServer(app);25var io = require('socket.io').listen(server);26app.use(express.static(path.join(__dirname, 'public')));27app.get('/', function(req, res) {28 res.sendFile(__dirname + '/index.html');29});30app.post('/upload', upload.single('file'), function(req, res) {31 var file = req.file;32 var fileName = file.originalname;33 var filePath = file.path;34 var fileSize = file.size;35 var fileType = file.mimetype;36 var fileExtension = fileName.split('.').pop();37 var fileBaseName = fileName.split('.').shift();38 var fileNewName = fileBaseName + '_' + Date.now() + '.' + fileExtension;39 fs.rename(filePath, 'uploads/' + fileNewName, function(err) {40 if (err) {41 throw err;42 }43 });

Full Screen

Using AI Code Generation

copy

Full Screen

1var Cucumber = require('cucumber');2var parser = new Cucumber.Parser();3var fs = require('fs');4var gherkinSource = fs.readFileSync('test.feature', 'utf8');5var feature = parser.parse(gherkinSource);6var featureNode = feature.getFeature();7var scenarioNode = featureNode.getScenarioDefinitions()[0];8console.log(scenarioNode.getTitle());

Full Screen

Using AI Code Generation

copy

Full Screen

1module.exports = function() {2 this.When(/^I search for "([^"]*)"$/, function (arg1, callback) {3 callback(null, 'pending');4 });5};61 scenario (1 undefined)73 steps (1 undefined, 2 skipped)8var {defineSupportCode} = require('cucumber');9defineSupportCode(function({Given, When, Then}) {10 Given(/^I am on the home page$/, function (callback) {11 callback(null, 'pending');12 });13 When(/^I search for "([^"]*)"$/, function (arg1, callback) {14 callback(null, 'pending');15 });16 Then(/^I should see the search results$/, function (callback) {17 callback(null, 'pending');18 });19});

Full Screen

Using AI Code Generation

copy

Full Screen

1var gherkin = require('gherkin');2var parser = new gherkin.Parser();3var lexer = new gherkin.Lexer('en');4var fs = require('fs');5var data = fs.readFileSync('./features/feature1.feature');6var tokens = lexer.lex(data.toString());7var feature = parser.parse(tokens);8var currentNode = feature;9while (currentNode) {10 console.log(currentNode.type);11 currentNode = currentNode.next();12}

Full Screen

Using AI Code Generation

copy

Full Screen

1var Cucumber = require('cucumber');2var gherkin = Cucumber.Gherkin.Parser();3var fs = require('fs');4fs.readFile('test.feature', 'utf8', function(err, data) {5 if (err) throw err;6 var feature = gherkin.parse(data);7 console.log(feature);8});

Full Screen

Using AI Code Generation

copy

Full Screen

1var Cucumber = require('cucumber');2var fs = require('fs');3var featureSource = fs.readFileSync('./test.feature', 'utf8');4var parser = new Cucumber.Parser(featureSource);5var feature = parser.parse();6var astNode = feature.getAstNode();7var children = astNode.getChildren();8var scenarios = children.filter(function(child) {9 return child instanceof Cucumber.Ast.Scenario;10});11console.log(scenarios[0].getScenario().getAstNode().getChildren().getChildren().getChildren().get

Full Screen

Using AI Code Generation

copy

Full Screen

1var gherkin = require('gherkin');2var parser = new gherkin.Parser();3var lexer = new gherkin.Lexer('en');4var fs = require('fs');5var ast = parser.parse(lexer.lex(fs.readFileSync('test.feature', 'utf-8')));6ast.currentNode().print();

Full Screen

Cucumber Tutorial:

LambdaTest offers a detailed Cucumber testing tutorial, explaining its features, importance, best practices, and more to help you get started with running your automation testing scripts.

Cucumber Tutorial Chapters:

Here are the detailed Cucumber testing chapters to help you get started:

  • Importance of Cucumber - Learn why Cucumber is important in Selenium automation testing during the development phase to identify bugs and errors.
  • Setting Up Cucumber in Eclipse and IntelliJ - Learn how to set up Cucumber in Eclipse and IntelliJ.
  • Running First Cucumber.js Test Script - After successfully setting up your Cucumber in Eclipse or IntelliJ, this chapter will help you get started with Selenium Cucumber testing in no time.
  • Annotations in Cucumber - To handle multiple feature files and the multiple scenarios in each file, you need to use functionality to execute these scenarios. This chapter will help you learn about a handful of Cucumber annotations ranging from tags, Cucumber hooks, and more to ease the maintenance of the framework.
  • Automation Testing With Cucumber And Nightwatch JS - Learn how to build a robust BDD framework setup for performing Selenium automation testing by integrating Cucumber into the Nightwatch.js framework.
  • Automation Testing With Selenium, Cucumber & TestNG - Learn how to perform Selenium automation testing by integrating Cucumber with the TestNG framework.
  • Integrate Cucumber With Jenkins - By using Cucumber with Jenkins integration, you can schedule test case executions remotely and take advantage of the benefits of Jenkins. Learn how to integrate Cucumber with Jenkins with this detailed chapter.
  • Cucumber Best Practices For Selenium Automation - Take a deep dive into the advanced use cases, such as creating a feature file, separating feature files, and more for Cucumber testing.

Run Cucumber-gherkin automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful