Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <title>TEST</title>
- </head>
- <body>
- <script>
- class LinkedList{
- constructor(){
- this.list = null;
- }
- push(item){
- this.list = {value: item, next: this.list};
- }
- pop(){
- var {value: item} = this.list;
- this.list = this.list.next != null ? { value: this.list.next.value, next: this.list.next.next} : {value : null};
- return item;
- }
- unshift(x) {
- let current = this.list;
- if (current) {
- while(current.next){
- current = current.next;
- }
- current.next = {value : x, next : null};
- }else{
- this.list = {value: x, next: this.list};
- }
- }
- shift() {
- let current = this.list;
- let previous = {};
- while(current.next){
- previous = current;
- current = current.next;
- }
- let {value : lastEle} = current;
- previous.next = null;
- current = previous;
- return lastEle;
- }
- count() {
- let count = 0;
- var elements = this.list;
- while(elements){
- count++;
- elements = elements.next;
- }
- return count;
- }
- }
- var list1 = new LinkedList();
- list1.push(31);
- list1.unshift(43);
- list1.shift();
- list1.pop();
- console.log(list1.list);
- console.log(list1.count());
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement