Advertisement
PadmaJS

Untitled

Apr 19th, 2021
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>TEST</title>
  5. </head>
  6. <body>
  7. <script>
  8. class LinkedList{
  9. constructor(){
  10. this.list = null;
  11. }
  12.  
  13. push(item){
  14. this.list = {value: item, next: this.list};
  15. }
  16.  
  17. pop(){
  18. var {value: item} = this.list;
  19. this.list = this.list.next != null ? { value: this.list.next.value, next: this.list.next.next} : {value : null};
  20. return item;
  21. }
  22. unshift(x) {
  23. let current = this.list;
  24. if (current) {
  25. while(current.next){
  26. current = current.next;
  27. }
  28. current.next = {value : x, next : null};
  29. }else{
  30. this.list = {value: x, next: this.list};
  31. }
  32. }
  33. shift() {
  34. let current = this.list;
  35. let previous = {};
  36. while(current.next){
  37. previous = current;
  38. current = current.next;
  39. }
  40. let {value : lastEle} = current;
  41. previous.next = null;
  42. current = previous;
  43. return lastEle;
  44. }
  45. count() {
  46. let count = 0;
  47. var elements = this.list;
  48. while(elements){
  49. count++;
  50. elements = elements.next;
  51. }
  52. return count;
  53. }
  54. }
  55.  
  56. var list1 = new LinkedList();
  57. list1.push(31);
  58. list1.unshift(43);
  59. list1.shift();
  60. list1.pop();
  61. console.log(list1.list);
  62. console.log(list1.count());
  63.  
  64. </script>
  65. </body>
  66. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement