1+ function CircularLinkedList ( ) {
2+
3+ var Node = function ( element ) {
4+
5+ this . element = element ;
6+ this . next = null ;
7+ } ;
8+
9+ var length = 0 ;
10+ var head = null ;
11+
12+ this . append = function ( element ) {
13+
14+ var node = new Node ( element ) ,
15+ current ;
16+
17+ if ( head === null ) { //first node on list
18+ head = node ;
19+ } else {
20+
21+ current = head ;
22+
23+ //loop the list until find last item
24+ while ( current . next !== head ) { //last element will be head instead of NULL
25+ current = current . next ;
26+ }
27+
28+ //get last item and assign next to added item to make the link
29+ current . next = node ;
30+ }
31+
32+ //set node.next to head - to have circular list
33+ node . next = head ;
34+
35+ length ++ ; //update size of list
36+ } ;
37+
38+ this . insert = function ( position , element ) {
39+
40+ //check for out-of-bounds values
41+ if ( position >= 0 && position <= length ) {
42+
43+ var node = new Node ( element ) ,
44+ current = head ,
45+ previous ,
46+ index = 0 ;
47+
48+ if ( position === 0 ) { //add on first position
49+
50+ node . next = current ;
51+
52+ //update last element
53+ while ( current . next !== head ) { //last element will be head instead of NULL
54+ current = current . next ;
55+ }
56+
57+ head = node ;
58+ current . next = head ;
59+
60+ } else {
61+ while ( index ++ < position ) {
62+ previous = current ;
63+ current = current . next ;
64+ }
65+ node . next = current ;
66+ previous . next = node ;
67+
68+ if ( node . next === null ) { //update in case last element
69+ node . next = head ;
70+ }
71+ }
72+
73+ length ++ ; //update size of list
74+
75+ return true ;
76+
77+ } else {
78+ return false ;
79+ }
80+ } ;
81+
82+ this . removeAt = function ( position ) {
83+
84+ //check for out-of-bounds values
85+ if ( position > - 1 && position < length ) {
86+
87+ var current = head ,
88+ previous ,
89+ index = 0 ;
90+
91+ //removing first item
92+ if ( position === 0 ) {
93+
94+ while ( current . next !== head ) { //needs to update last element first
95+ current = current . next ;
96+ }
97+
98+ head = head . next ;
99+ current . next = head ;
100+
101+ } else { //no need to update last element for circular list
102+
103+ while ( index ++ < position ) {
104+
105+ previous = current ;
106+ current = current . next ;
107+ }
108+
109+ //link previous with current's next - skip it to remove
110+ previous . next = current . next ;
111+ }
112+
113+ length -- ;
114+
115+ return current . element ;
116+
117+ } else {
118+ return null ;
119+ }
120+ } ;
121+
122+ this . remove = function ( element ) {
123+
124+ var index = this . indexOf ( element ) ;
125+ return this . removeAt ( index ) ;
126+ } ;
127+
128+ this . indexOf = function ( element ) {
129+
130+ var current = head ,
131+ index = - 1 ;
132+
133+ //check first item
134+ if ( element == current . element ) {
135+ return 0 ;
136+ }
137+
138+ index ++ ;
139+
140+ //check in the middle of the list
141+ while ( current . next !== head ) {
142+
143+ if ( element == current . element ) {
144+ return index ;
145+ }
146+
147+ current = current . next ;
148+ index ++ ;
149+ }
150+
151+ //check last item
152+ if ( element == current . element ) {
153+ return index ;
154+ }
155+
156+ return - 1 ;
157+ } ;
158+
159+ this . isEmpty = function ( ) {
160+ return length === 0 ;
161+ } ;
162+
163+ this . size = function ( ) {
164+ return length ;
165+ } ;
166+
167+ this . getHead = function ( ) {
168+ return head ;
169+ } ;
170+
171+ this . toString = function ( ) {
172+
173+ var current = head ,
174+ s = current . element ;
175+
176+ while ( current . next !== head ) {
177+ current = current . next ;
178+ s += ', ' + current . element ;
179+ }
180+
181+ return s . toString ( ) ;
182+ } ;
183+
184+ this . print = function ( ) {
185+ console . log ( this . toString ( ) ) ;
186+ } ;
187+ }
0 commit comments