Hello, dear forum users. The task is to check the "correctness" of the brackets in the string str. The task needs to be solved through a singly-linked list, I listed 1 opening brackets, and 2 to the list are closing. The question is how do I expand list 2 to compare it with the first list. Those. now at withdrawal


([{


}])


Or you can offer another solution idea (maybe more rational).

var list = function (e) { var self = this; var first, last, head; self.insert = function (value) { var node = new Node(value); if (first == null) { first = last = node; } else { var head = first; while (head.next != null) { head = head.next; } head.next = node; last = head.next; } } self.show = function () { var head = first; while (head != null) { console.log(head.value); head = head.next; } } var Node = function (value) { this.value = value; var next = {}; } return self; }; var list_1 = new list(); var list_2 = new list(); var str = "(Я [ хочу {программировать} на ] JavaScript)"; for (var i=0; i<str.length; i++) if(str[i].indexOf('(') + 1 > 0 || str[i].indexOf('[') + 1 > 0 || str[i].indexOf('{') + 1 > 0) list_1.insert(str[i]); else if(str[i].indexOf(')') + 1 > 0 || str[i].indexOf(']') + 1 > 0 || str[i].indexOf('}') + 1 > 0) list_2.insert(str[i]); console.log("____________________________"); list_1.show(); console.log("____________________________"); list_2.show(); console.log("____________________________"); 

  • I did not understand the question - Excess Suslik
  • Just put the brackets on the stack and extract them when passing the reverse bracket. I do not see that your code uses a stack. - Roman C
  • Two lists are definitely superfluous and wrong - Alexey Ten

0