SHARE
TWEET

Untitled

a guest Jul 22nd, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function LinkedList() {
  2.     this.Node = null;
  3.     this.count = 0;
  4.     this.head = null;
  5. };
  6.  
  7. LinkedList.prototype.append = function (value) {
  8.     // create new node
  9.     var node = this.createNode(value);
  10.     console.log(value);
  11.     if (this.head == null) {
  12.         this.Node = node;
  13.         this.Node.next = null;
  14.         this.head = this.Node;
  15.     } else {
  16.         var ptr = this.Node;
  17.         while (ptr.next != null) {
  18.             ptr = ptr.next;
  19.         }
  20.         ptr.next = node;
  21.     }
  22.     this.count++;
  23. };
  24.  
  25. LinkedList.prototype.getSize = function () {
  26.     console.log(this);
  27. };
  28.  
  29. LinkedList.prototype.close = function () {
  30.     var ptr = this.head;
  31.     while (ptr.next != null) {
  32.         ptr = ptr.next;
  33.     }
  34.     ptr.next = this.head;
  35. };
  36.  
  37. LinkedList.prototype.createNode = function (value) {
  38.     var node = {};
  39.     node.value = value;
  40.     node.next = null;
  41.     return node;
  42. };
  43.  
  44. var li = new LinkedList();
  45. li.append(1);
  46. li.append(2);
  47. li.append(3);
  48. li.append(4);
  49. li.close();
  50. li.getSize();
  51.      
  52. LinkedList.prototype.append = function(value) {
  53.   var node = {
  54.         value: value,
  55.         next: this.head
  56.     };//cricular node
  57.   if (this.count === 0) {
  58.     this.head = {};
  59.     this.head.value = value;
  60.     this.head.next = this.head;
  61.     this.tail = this.head;
  62.   } else {
  63.     this.tail.next = node;
  64.     this.tail = node;
  65.   }
  66.   this.count++;
  67. };
  68.      
  69. getCircular(start){
  70.         let i=0,j=0;
  71.         let secondHead = this.head;
  72.         let pointer = this.head;
  73.         let pointer2 = secondHead;
  74.         while(i<start){
  75.             let temp1 = pointer.next;
  76.             pointer = temp1;
  77.             i++;
  78.         }
  79.         this.head = pointer;
  80.         this.tail.next = pointer2;
  81.         while(j<start-1){
  82.             let temp2 = pointer2.next;
  83.             pointer2 = temp2;
  84.             j++;
  85.         }
  86.         this.tail = pointer2;
  87.         this.tail.next = null;
  88.         return this;
  89.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top