Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbede2a5

Browse files
committed
Doubly Linked List 😉
1 parentf8ae8ee commitbede2a5

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
classNode{
2+
constructor(data){
3+
this.data=data;
4+
this.prev=null;
5+
this.next=null;
6+
}
7+
}
8+
9+
classLinkedList{
10+
constructor(){
11+
this.head=newNode("head");
12+
}
13+
14+
find(data){
15+
lettemp=this.head;
16+
while(temp.data!=data)
17+
temp=temp.next;
18+
returntemp;
19+
}
20+
21+
findLast(){
22+
lettemp=this.head;
23+
while(temp.next!=null)
24+
temp=temp.next;
25+
returntemp;
26+
}
27+
28+
printList(){
29+
lettemp=this.head;
30+
if(temp.next==null)
31+
return"The Linked List is Empty";
32+
letstr="";
33+
while(temp.next!=null){
34+
str+=temp.next.data+" -> ";
35+
temp=temp.next;
36+
}
37+
str+="NULL\n";
38+
returnstr;
39+
}
40+
41+
printListReverse(){
42+
lettemp=this.findLast();
43+
if(temp.prev==null)
44+
return"The Linked List is Empty";
45+
letstr="";
46+
while(temp.prev!=null){
47+
str+=temp.data+" -> ";
48+
temp=temp.prev;
49+
}
50+
str+="NULL\n";
51+
returnstr;
52+
}
53+
54+
insertAfter(data,currData){
55+
constnewNode=newNode(data);
56+
letcurr=this.find(currData);
57+
newNode.next=curr.next;
58+
newNode.prev=curr;
59+
curr.next=newNode;
60+
}
61+
62+
remove(data){
63+
constcurr=this.find(data);
64+
if(curr.next!=null){
65+
curr.prev.next=curr.next;
66+
curr.next.prev=curr.prev;
67+
curr.prev=null;
68+
curr.next=null;
69+
}
70+
elsethis.head.next=null;
71+
}
72+
}
73+
74+
module.exports=LinkedList;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
constexpect=require('chai').expect;
2+
constDoublyLinkedList=require('../../examples/datastructures/doubly-linked-list');
3+
4+
describe('=> DOUBLY LINKED LIST',function(){
5+
letnames;
6+
before(function(){
7+
names=newDoublyLinkedList();
8+
});
9+
10+
it('should create an Empty Linked List',function(done){
11+
expect(names.head.data).to.equal("head");
12+
done();
13+
});
14+
15+
it('should print an Empty Linked List',function(done){
16+
expect(names.printList()).to.equal("The Linked List is Empty");
17+
done();
18+
});
19+
20+
it('should insert into a Linked List',function(done){
21+
names.insertAfter(1,"head");
22+
names.insertAfter(2,1);
23+
names.insertAfter(3,2);
24+
names.insertAfter(4,3);
25+
expect(names.printList()).to.equal("1 -> 2 -> 3 -> 4 -> NULL\n");
26+
expect(names.printListReverse()).to.equal("4 -> 3 -> 2 -> 1 -> NULL\n");
27+
done();
28+
});
29+
30+
it('should remove element from a Linked List',function(done){
31+
names.remove(1);
32+
expect(names.printList()).to.equal("2 -> 3 -> 4 -> NULL\n");
33+
expect(names.printListReverse()).to.equal("4 -> 3 -> 2 -> NULL\n");
34+
done();
35+
});
36+
37+
describe('=> clear',function(){
38+
it('should clear the Linked List',function(done){
39+
names.remove(2);
40+
names.remove(3);
41+
names.remove(4);
42+
expect(names.printList()).to.equal("The Linked List is Empty");
43+
done();
44+
});
45+
});
46+
47+
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp