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

Commit3441c6d

Browse files
committed
Circular Linked List 👻
1 parentbede2a5 commit3441c6d

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
classNode{
2+
constructor(data){
3+
this.data=data;
4+
this.next=null;
5+
}
6+
}
7+
8+
classLinkedList{
9+
constructor(){
10+
this.head=newNode("head");
11+
this.head.next=this.head;
12+
}
13+
14+
find(data){
15+
lettemp=this.head;
16+
while(temp.data!=data)
17+
temp=temp.next;
18+
returntemp;
19+
}
20+
21+
findPrev(data){
22+
lettemp=this.head;
23+
while(temp.next!=null&&temp.next.data!=data)
24+
temp=temp.next;
25+
returntemp;
26+
}
27+
28+
printList(){
29+
lettemp=this.head;
30+
if(temp.next==temp)
31+
return"The Linked List is Empty";
32+
letstr="";
33+
while(temp.next!=this.head){
34+
str+=temp.next.data+" -> ";
35+
temp=temp.next;
36+
}
37+
str+=this.head.next.data+"\n";
38+
returnstr;
39+
}
40+
41+
insertAfter(data,currData){
42+
constnewNode=newNode(data);
43+
letcurr=this.find(currData);
44+
curr.next=newNode;
45+
newNode.next=this.head;
46+
}
47+
48+
remove(data){
49+
constprev=this.findPrev(data);
50+
if(prev.next!=null)
51+
prev.next=prev.next.next;
52+
}
53+
}
54+
55+
module.exports=LinkedList;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
constexpect=require('chai').expect;
2+
constCircularLinkedList=require('../../examples/datastructures/circular-linked-list');
3+
4+
describe('=> CIRCULAR LINKED LIST',function(){
5+
letnames;
6+
before(function(){
7+
names=newCircularLinkedList();
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 -> 1\n");
26+
done();
27+
});
28+
29+
it('should remove element from a Linked List',function(done){
30+
names.remove(1);
31+
expect(names.printList()).to.equal("2 -> 3 -> 4 -> 2\n");
32+
done();
33+
});
34+
35+
describe('=> clear',function(){
36+
it('should clear the Linked List',function(done){
37+
names.remove(2);
38+
names.remove(3);
39+
names.remove(4);
40+
expect(names.printList()).to.equal("The Linked List is Empty");
41+
done();
42+
});
43+
});
44+
45+
});
46+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp