diff --git a/ds/list/LinkedList.js b/ds/list/LinkedList.js deleted file mode 100644 index 3a02c5b..0000000 --- a/ds/list/LinkedList.js +++ /dev/null @@ -1,92 +0,0 @@ -class LinkedNode{ - constructor(value, next = null){ - this.value = value; - this.next = next; - } -} - -class LinkedList{ - constructor(arr = []){ - this.head = null; - this.tail = null; - this.size = 0; - for(let a of arr){ - this.push(a); - } - } - - push(value){ - - let node = new LinkedNode(value); - this.size++; - - if(!this.head) { - this.head = node; - this.tail = node; - } else { - this.tail.next = node; - this.tail = node; - } - - } - - pop(){ - - if(!this.tail) return null; - - let node = this.tail; - this.size--; - if(this.head == this.tail){ - this.head = null; - this.tail = null; - } else { - let temp = this.head; - while(temp.next.next){ - temp = temp.next; - } - temp.next = null; - this.tail = temp; - } - - return node; - } - - shift(){ - - if(!this.head) return null; - - let node = this.head; - this.size--; - - this.head = this.head.next; - - return node; - - } - - unshift(...values){ - - for(let v of values){ - let node = new LinkedNode(v,this.head); - this.head = node; - } - - } - - print(){ - let temp = this.head; - let output = ''; - while(temp){ - output += temp.value; - temp = temp.next; - if(temp){ - output += '->'; - } - } - - return output - } - -} - -module.exports = LinkedList; \ No newline at end of file diff --git a/ds/list/LinkedList.test.js b/ds/list/LinkedList.test.js deleted file mode 100644 index 637f0be..0000000 --- a/ds/list/LinkedList.test.js +++ /dev/null @@ -1,43 +0,0 @@ -const { it, expect } = require('@jest/globals'); -const LinkedList = require('./LinkedList'); - -it('should work for push and pop', function(){ - - let a = new LinkedList(); - a.push(1); - expect(a.print()).toEqual('1'); - a.push(4); - expect(a.print()).toEqual('1->4'); - a.push(5); - expect(a.print()).toEqual('1->4->5'); - a.push(7); - expect(a.print()).toEqual('1->4->5->7'); - a.pop(); - expect(a.print()).toEqual('1->4->5'); - a.pop(); - expect(a.print()).toEqual('1->4'); - a.push(9); - expect(a.print()).toEqual('1->4->9'); - a.push(10); - expect(a.print()).toEqual('1->4->9->10'); - -}); - -it('should work for empty pop', function(){ - let a = new LinkedList(); - expect(a.pop()).toBeNull(); -}); - -it('should work for shift',function(){ - let a = new LinkedList([1,2,3,4]); - expect(a.shift().value).toBe(1); - expect(a.print()).toEqual('2->3->4'); - expect(a.shift().value).toBe(2); - expect(a.print()).toEqual('3->4'); -}); - -it('should work for unshift',function(){ - let a = new LinkedList([1,2,3,4]); - a.unshift(...[5,6,7]); - expect(a.print()).toBe('7->6->5->1->2->3->4'); -}) \ No newline at end of file diff --git a/ds/list/README.md b/ds/list/README.md deleted file mode 100644 index 6ec7040..0000000 --- a/ds/list/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# List - -A linked list data structure includes a series of connected nodes. \ No newline at end of file diff --git a/ds/list/index.js b/ds/list/index.js deleted file mode 100644 index c293170..0000000 --- a/ds/list/index.js +++ /dev/null @@ -1,5 +0,0 @@ -const LinkedList = require('./LinkedList'); - -module.exports = { - LinkedList -} \ No newline at end of file