File: linkedlist__define.pro

package info (click to toggle)
coyote 2019.01.29-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 6,316 kB
  • sloc: python: 184; makefile: 14; sh: 13
file content (1205 lines) | stat: -rw-r--r-- 39,058 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
;+
; NAME:
;   LINKEDLIST
;
; FILENAME:
;   linkedlist__define.pro
;
; PURPOSE:
; 
;   The purpose of this program is to implement a list that
;   is linked in both the forward and backward directions. There
;   is no restriction as to what can be stored in a linked list
;   node. The linked list is implemented as an object.
;
; AUTHOR:
; 
;   FANNING SOFTWARE CONSULTING
;   David Fanning, Ph.D.
;   1645 Sheely Drive
;   Fort Collins, CO 80526 USA
;   Phone: 970-221-0438
;   E-mail: david@idlcoyote.com
;   Coyote's Guide to IDL Programming: http://www.idlcoyote.com/
;
; CATEGORY:
; 
;   General programming.
;
; CALLING SEQUENCE:
; 
;   mylist = Obj_New('LINKEDLIST', item)
;
; OPTIONAL INPUTS:
; 
;   item: The first item added to the list. Items can be any
;     valid IDL variable type.
;
; COMMON BLOCKS:
; 
;   Are you kidding?!
;
; RESTRICTIONS:
; 
;   Be sure to destroy the LINKEDLIST object when you are finished
;   with it: Obj_Destroy, mylist
;
;   Node index numbers start at 0 and go to n-1, where n is the
;   number of items in the list.
;
; PUBLIC METHODS:
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; PRO LINKEDLIST::ADD, item, index, $
;     AFTER=after, $
;     BEFORE=before, $
;     ERROR=error, $
;     NO_COPY=no_copy, $
;     REPLACE=replace
;     
;
;   The ADD method adds a data item to the list.
;
;   Parameters:
;
;   item: The data item to be added to the list. Required.
;
;   index: The location in the list where the data item is
;     to be added. If neither the AFTER or BEFORE keyword is
;     set, the item is added AFTER the item at the index location.
;     If index is missing, the index points to the last item in
;     the list. Optional.
;
;   Keywords:
;
;   AFTER: If this keyword is set, the item is added after the
;     item at the current index.
;
;   BEFORE: If this keyword is set, the item is added before the
;     item at the current index.
;     
;   ERROR: On return, if this is not a null string, an error occurred
;      and this value is set equal to the error message.
;      
;   NO_COPY: If set, the item is transferred to the internal pointer using
;      a no copy method. This will cause the item variable to become undefined.
;      
;   REPLACE: If this keyword is set, the item will replace the current item at
;      the index location.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; PRO LINKEDLIST::DELETE, index, ALL=all, DESTROY=destroy, ERROR=error   
;      
;   The DELETE method deletes an item from the list.
;
;   Parameters:
;
;   index: The location in the list where the data item is
;     to be delete. If index is missing, the index points to
;     the last item in the list. Optional.
;
;   Keywords:
;
;   ALL: If this keyword is set, all items in the list are deleted.
;
;   DESTROY: If the item at the node is an object or pointer, the
;     item will be destroyed before the node is deleted. This keyword
;     is turned on (set to 1) by default. Set to 0 to prevent destruction.
;
;    ERROR: On return, if this is not a null string, an error occurred
;      and this value is set equal to the error message.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; FUNCTION LINKEDLIST::GET_COUNT
;
;   The GET_COUNT method returns the number of items in the list.
;
;   Return Value: The number of items stored in the linked list.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
; FUNCTION LINKEDLIST::GET_ITEM, index, $
;    ALL=all, $                 ; This ASSUMES all items stored are the same type!!!
;    Dereference=dereference, $ ; Obsolete. Ignored. Always returns item.
;    ItemPtr=itemPtr, $         ; The pointer to the item, if needed. Output.
;    NO_COPY=no_copy, $         ; Copy from location with NO_COPY.
;    ERROR=errorMsg             ; The error message. Null string if no error.

;
;   Parameters:
;
;   index: The location in the list from which the data item is
;     to be retrieved. If not present, the last item in the list
;     is retrieved. Optional.
;
;   Keywords:
;
;   DEREFERENCE: This keyword obsolete and only provided for backward compatibility.
;
;   ALL: Set this keyword to return an n-element array containing all the list
;      items.  This requires that all list items be of the same type, and
;      if they are arrays, they have 7 dimensions or fewer. If index is passed, 
;      it is ignored.
;     
;   ITEMPTR: The pointer to the data item.
;   
;   NO_COPY: If this keyword is set, the item is transferred from the data
;      pointer using a NO_COPY method. This will undefine the item at that
;      indexed locaton.
;      
;    ERROR: On return, if this is not a null string, an error occurred
;      and this value is set equal to the error message.
;
;   Return Value: The data item at this index on the list.
;     If ALL is set, then an array containing all the data items is returned.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; FUNCTION LINKEDLIST::GET_NODE, index, ERROR=error
;
;   The GET_NODE method returns a pointer to the specified node
;   from the list.
;
;   Parameters:
;
;   index: The location in the list from which the data node is
;     to be retrieved. If not present, the last node in the list
;     is retrieved. The node is a structure with three fields:
;     Previous is a pointer to the previous node in the list.
;     Next is a pointer to the next node in the list. A null pointer
;     in the previous field indicates the first node on the list. A
;     null pointer in the next field indicates the last node on the
;     list. The item field is a pointer to the item stored in the
;     node. Optional.
;
;   ERROR: On return, if this is not a null string, an error occurred
;      and this value is set equal to the error message.
;      
;   Return Value: A pointer to the specified node structure in
;     the linked list.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; PRO LINKEDLIST::HELP, PRINT=print
;
; The HELP method performs a HELP command on each item
; in the linked list.
;
;   Keywords:
;
;    PRINT: If this keyword is set, the PRINT command is used
;      instead of the HELP command on the items in the list.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; PRO LINKEDLIST::MOVE_NODE, nodeIndex, location, BEFORE=before, ERROR=error
;
;   The MOVE_NODE method moves a list node from one location to another.
;
;   Parameters:
;
;   nodeIndex: The location in the list of the node you are moving.
;     Required.
;
;   location: The location (index) you are moving the node to. If
;     location is missing, the location points to the node at the
;     end of the list.
;
;   Keywords:
;
;    BEFORE: If this keyword is set, the node is added to the
;      list before the location node. Otherwise, it is added after
;      the location node.
;
;    ERROR: On return, if this is not a null string, an error occurred
;      and this value is set equal to the error message.
;      
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; PRO LINKEDLIST::REPLACE_ITEM, newItem, index, ERROR=error
;
;  Use this method to replace any item in the list with any other value.
;  This allows the caller to change an item without stepping through the
;  process of deleting an item then adding a new one.
;
;  Parameters:
;     index:  The location of the node you are replacing
;
;     newItem:  Any value of any data type.
;
;    ERROR: On return, if this is not a null string, an error occurred
;      and this value is set equal to the error message.
;      
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; FUNCTION LINKEDLIST::HAVE_ITEM, index, ERROR=error
;
;  Use this method to check to see if an item exits at a particular location
;  on the list. Returns a 1 if the item is there, otherwise a 0.
;
;  Parameters:
;     index:  The location of the node you are replacing
;      
;    ERROR: On return, if this is not a null string, an error occurred
;      and this value is set equal to the error message.
;      
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
; EXAMPLE:
;
;   mylist = Obj_New("LINKEDLIST", 5)
;   mylist->Add, 10
;   mylist->Add, 7, 1, /Before
;   mylist->Add, 12
;   print, mylist->Get_Item(/All)
;   mylist->Add, 'Bob', 2, /Replace
;   mylist->Help
;   mylist->Delete, 0
;   mylist->Help, /Print
;
; MODIFICATION HISTORY:
;   Written by: David Fanning, 25 August 98.
;   25 August 99. Fixed several errors in various methods dealing with
;       moving nodes from one place to another. DWF.
;   13 June 2001. DWF. Added DEREFERENCE to the GET_ITEM method to
;       return the item itself, instead of the pointer to the item.
;   27 June 2001 Added REPLACE_ITEM method.  Ben Tupper.
;   7 April 2003. Added DESTROY keyword to DELETE method so that objects
;      and pointers could be cleaned up properly when they are deleted
;      from the linked list. DWF.
;   9 April 2003. Fixed a problem that occurs when deleting the last node. DWF.
;   3 Feb 2004. Make sure loop index vars are long.  Jeff Guerber
;   30 Jun 2004.  Added /ALL to GET_ITEM function.  Henry Throop, SWRI.
;   23 Nov 2004.  Fixed GET_ITEM, /ALL to accomodate structures and empty
;      lists.  Henry Throop.
;   21 February 2011. A complete refurbishing to incorporate changes and to fix bugs
;      I found in the SolarSoft version of this code. I've tried to make this compatible
;      with the version distributed with SolarSoft to reduce problems caused by two versions
;      of the software with the same name.
;    9 December 2011. Fixed a problem with the ALL keyword on the Get_Item method. DWF.
;-
;******************************************************************************************;
;  Copyright (c) 2008, by Fanning Software Consulting, Inc.                                ;
;  All rights reserved.                                                                    ;
;                                                                                          ;
;  Redistribution and use in source and binary forms, with or without                      ;
;  modification, are permitted provided that the following conditions are met:             ;
;                                                                                          ;
;      * Redistributions of source code must retain the above copyright                    ;
;        notice, this list of conditions and the following disclaimer.                     ;
;      * Redistributions in binary form must reproduce the above copyright                 ;
;        notice, this list of conditions and the following disclaimer in the               ;
;        documentation and/or other materials provided with the distribution.              ;
;      * Neither the name of Fanning Software Consulting, Inc. nor the names of its        ;
;        contributors may be used to endorse or promote products derived from this         ;
;        software without specific prior written permission.                               ;
;                                                                                          ;
;  THIS SOFTWARE IS PROVIDED BY FANNING SOFTWARE CONSULTING, INC. ''AS IS'' AND ANY        ;
;  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES    ;
;  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT     ;
;  SHALL FANNING SOFTWARE CONSULTING, INC. BE LIABLE FOR ANY DIRECT, INDIRECT,             ;
;  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED    ;
;  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;         ;
;  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND             ;
;  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT              ;
;  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS           ;
;  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                            ;
;******************************************************************************************;
PRO LINKEDLIST::ADD, item, index, $
    After=after, $
    Before=before, $
    Error=errorMsg, $
    No_Copy=no_copy, $
    Replace=replace


    ; This method is the public interface to the private ADD_+ methods.
    ; If INDEX is not specified, the item is always added to the end
    ; of the list. If INDEX is specified, but neither the BEFORE or
    ; AFTER keywords are used, the item is added AFTER the INDEX specified.
    
    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, errorMsg, /Continue
       RETURN
    ENDIF

    ; Assume no error.
    errorMsg = ""
    
    ; Must supply an item to add to the list.
    IF N_Elements(item) EQ 0 THEN BEGIN
       errorMsg = 'Must supply an item to add to the list.'
       Message, errorMsg, /Continue
       RETURN
    END
    
    ; Check for index. If there is none, add to end of list.
    IF N_Elements(index) EQ 0 THEN BEGIN
       self->Add_To_End, item, NO_COPY=no_copy, ERROR=errorMsg
       RETURN
    ENDIF
    
    ; If this is the last index, add it to the end of list.
    IF index GT (self.count-1) THEN BEGIN
       self->Add_To_End, item, NO_COPY=no_copy, ERROR=errorMsg
       RETURN
    ENDIF
    
    ; Are we replacing an item?
    IF Keyword_Set(replace) THEN BEGIN
        self -> Replace_Item, item, index, NO_COPY=no_copy, ERROR=errorMsg
        RETURN
    ENDIF
    
    ; BEFORE keyword set.
    IF Keyword_Set(before) THEN BEGIN
       self->Add_Before, item, index, NO_COPY=no_copy, ERROR=errorMsg
       RETURN
    ENDIF
    
    ; AFTER keyword set.
    IF Keyword_Set(after) THEN BEGIN
       self->Add_After, item, index, NO_COPY=no_copy, ERROR=errorMsg
       RETURN
    ENDIF
    
    ; No BEFORE or AFTER keywords. Add to location AFTER index.
    self->Add_After, item, index, NO_COPY=no_copy, ERROR=errorMsg
    
END
;------------------------------------------------------------------------



PRO LINKEDLIST::ADD_AFTER, item, index, NO_COPY=no_copy, ERROR=errorMsg

    ; This method adds an item node AFTER the item specified by
    ; the index number. This is a private method. Use the public
    ; ADD method to add an item to the list.

    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, errorMsg, /Continue
       RETURN
    ENDIF

    ; Assume no error.
    errorMsg = ""
    
    ; Must supply an item to add to the list.
    IF N_Elements(item) EQ 0 THEN BEGIN
       errorMsg = 'Must supply an item to add to the list.'
       Message, errorMsg, /Continue
       RETURN
    END
    
    ; Check for index. If there is none, add to end of list.
    IF N_Elements(index) EQ 0 THEN BEGIN
       self->Add_To_End, item, NO_COPY=no_copy, ERROR=errorMsg
       RETURN
    ENDIF
    
    ; If this is the last index, add it to the end of list.
    IF index GE (self.count-1) THEN BEGIN
       self->Add_To_End, item, NO_COPY=no_copy, ERROR=errorMsg
       RETURN
    ENDIF

    ; Create a new node and store the item in it.
    currentNode = Ptr_New( {LINKEDLIST_NODE} )
    (*currentNode).item = Ptr_New(item, NO_COPY=no_copy)
    self.count = self.count + 1
    
    ; Get the node currently located at the index.
    indexNode = self->Get_Node(index)
    
    ; Get the node that follows the indexNode.
    nextNode = (*indexNode).next
    
    ; Update pointers.
    (*indexNode).next = currentNode
    (*currentNode).previous = indexNode
    (*nextNode).previous = currentNode
    (*currentNode).next = nextNode

END
;------------------------------------------------------------------------



PRO LINKEDLIST::ADD_BEFORE, item, index, NO_COPY=no_copy, ERROR=errorMsg

    ; This method adds an item node BEFORE the item specified by
    ; the index number. This is a private method. Use the public
    ; ADD method to add an item to the list.

    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, errorMsg, /Continue
       RETURN
    ENDIF

    ; Assume no error.
    errorMsg = ""
    
    ; Must supply an item to add to the list.
    IF N_Elements(item) EQ 0 THEN BEGIN
       errorMsg = 'Must supply an item to add to the list.'
       Message, errorMsg, /Continue
       RETURN
    END
    
    ; Check for index. If there is none, add to head of list.
    IF N_Elements(index) EQ 0 THEN index = 0

    ; Create a new node and store the item in it.
    currentNode = Ptr_New( {LINKEDLIST_NODE} )
    (*currentNode).item = Ptr_New(item, NO_COPY=no_copy)
    self.count = self.count + 1
    
    ; Get the node currently located at the index.
    indexNode = self->Get_Node(index)
    
    ; Get the node that is before the indexNode.
    previousNode = (*indexNode).previous
    
    ; Update pointers.
    (*indexNode).previous = currentNode
    (*currentNode).previous = previousNode
    (*currentNode).next = indexNode
    IF Ptr_Valid(previousNode) THEN $
       (*previousNode).next = currentNode ELSE $
       self.head = currentNode

END
;------------------------------------------------------------------------



PRO LINKEDLIST::ADD_TO_END, item, NO_COPY=no_copy, ERROR=errorMsg

    ; This method adds an item to the tail of the list. This is 
    ; a private method. Use the public ADD method to add an 
    ; item to the list.

    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, errorMsg, /Continue
       RETURN
    ENDIF

    ; Assume no error.
    errorMsg = ""
    
    ; Must supply an item to add to the list.
    IF N_Elements(item) EQ 0 THEN BEGIN
       errorMsg = 'Must supply an item to add to the list.'
       Message, errorMsg, /Continue
       RETURN
    END
    
    ; Is this the first item we are adding to the list?
    IF self.count EQ 0 THEN BEGIN
    
        ; Create a new node.
        currentNode = Ptr_New({ LINKEDLIST_NODE })
    
        ; Add the item to the node.
        (*currentNode).item = Ptr_New(item, NO_COPY=no_copy)
    
         ; The head and tail point to current node.
        self.head = currentNode
        self.tail = currentNode
     
        ; Update the node count.
        self.count = self.count + 1
     
    ENDIF ELSE BEGIN ; Not first, but must add a node.
     
        ; Create a new node.
        currentNode = Ptr_New({ LINKEDLIST_NODE })
    
        ; Set the next field of the previous node.
        (*self.tail).next = currentNode
     
        ; Add the item to the current node.
        (*currentNode).item = Ptr_New(item, NO_COPY=no_copy)
    
        ; Set the previous field to point to previous node.
        (*currentNode).previous = self.tail
    
        ; Update the tail field to point to current node.
        self.tail = currentNode
    
        ; Update the node count.
        self.count = self.count + 1
        
    ENDELSE
    
END
;------------------------------------------------------------------------



PRO LINKEDLIST::DELETE, index, All=all, DESTROY=destroy, ERROR=errorMsg

    ; This method is the public interface to the private DELETE_+ methods.
    ; If INDEX is not specified, the last item on the list is always deleted.
    ; The ALL keyword will delete all the items on the list.
    ; The DESTROY keyword will destroy any object or pointer item stored in
    ; the node before the node is deleted.

    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, errorMsg, /Continue
       RETURN
    ENDIF

    ; Assume no error.
    errorMsg = ""
    
    ; Assume you want to destroy the items in the node.
    IF (N_Elements(destroy) EQ 0) $
        THEN destroy = 1 $
        ELSE destroy = Keyword_Set(destroy)
    
    ; Delete all the nodes?
    IF Keyword_Set(all) THEN BEGIN
       self->Delete_Nodes, DESTROY=destroy, ERROR=errorMsg
       RETURN
    ENDIF

    ; Check for index. If there is none, delete last node on list.
    IF N_Elements(index) EQ 0 THEN BEGIN
       self->Delete_Last_Node, DESTROY=destroy, ERROR=errorMsg
       RETURN
    ENDIF
    
    ; Delete specified node.
    IF index EQ (self.count - 1) THEN $
       self->Delete_Last_Node, DESTROY=destroy, ERROR=errorMsg ELSE $
       self->Delete_Node, index, DESTROY=destroy, ERROR=errorMsg
       
END
;------------------------------------------------------------------------



PRO LINKEDLIST::DELETE_LAST_NODE, DESTROY=destroy, ERROR=errorMsg

    ; This method deletes the last node in the list. This is a 
    ; private method. Use the public DELETE method to delete an 
    ; item from the list.

    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, errorMsg, /Continue
       RETURN
    ENDIF

    ; Assume no error.
    errorMsg = ""
    
    ; Assume you want to destroy the items in the node.
    IF (N_Elements(destroy) EQ 0) $
        THEN destroy = 1 $
        ELSE destroy = Keyword_Set(destroy)
    
    ; If there is nothing here, return.
    IF self.count EQ 0 THEN RETURN
    
    ; This should be the last node.
    currentNode = self.tail
    
    ; Free the item pointer and everything it points to.
    IF Keyword_Set(destroy) THEN BEGIN
        IF Ptr_Valid((*currentNode).item) THEN Heap_Free, (*currentNode).item
    ENDIF    
    
    ; Is this the last node in the list?
    IF NOT Ptr_Valid((*currentNode).previous) THEN BEGIN
        self.head = Ptr_New()
        self.tail = Ptr_New()
        self.count = 0
        Ptr_Free, (*currentNode).next
    ENDIF ELSE BEGIN
        previousNode = (*currentNode).previous
        self.tail = previousNode
        Ptr_Free, (*self.tail).next
        (*self.tail).next = Ptr_New()
        self.count = self.count - 1
    ENDELSE
    
    ; Release the currentNode pointer.
    Ptr_Free, currentNode
END
;------------------------------------------------------------------------



PRO LINKEDLIST::DELETE_NODE, index, DESTROY=destroy, ERROR=errorMsg

    ; This method deletes the indicated node from the list. This is a 
    ; private method. Use the public DELETE method to delete an 
    ; item from the list.
    
    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, errorMsg, /Continue
       RETURN
    ENDIF

    ; Assume no error.
    errorMsg = ""
    
    ; Assume you want to destroy the items in the node.
    IF (N_Elements(destroy) EQ 0) $
        THEN destroy = 1 $
        ELSE destroy = Keyword_Set(destroy)
    
    IF self.count EQ 0 THEN BEGIN
       errorMsg = 'No nodes to delete.'
       Message, errorMsg, /Continue
       RETURN
    ENDIF
    
    IF index GT (self.count - 1) THEN BEGIN
       errorMsg = 'No node with the requested index number.'
       Message, errorMsg, /Continue
       RETURN
    ENDIF
      
    ; Get the current node and free the item pointer.
    currentNode = self->Get_Node(index)
    IF ~Ptr_Valid(currentNode) THEN BEGIN
        errorMsg = 'The current node is not a valid pointer.'
        Message, errorMsg, /Continue
        RETURN
    ENDIF
    
    ; Free the item pointer and everything it points to.
    IF Keyword_Set(destroy) THEN $
    BEGIN
      theItem = *(*currentNode).item
      CASE Size(theItem, /TNAME) OF
         'OBJREF': Obj_Destroy, theItem
         'POINTER': Ptr_Free, theItem
         ELSE:
      ENDCASE
    ENDIF
    Ptr_Free, (*currentNode).item
    
    ; Is this the last node?
    IF index EQ (self.count - 1) THEN BEGIN
        self->Delete_Last_Node, DESTROY=destroy, ERROR=errorMsg
        RETURN
    ENDIF
    
    ; Is this the first node in the list?
    IF NOT Ptr_Valid((*currentNode).previous) THEN BEGIN
            nextNode = (*currentNode).next
            Ptr_Free, (*nextNode).previous
            (*nextNode).previous = Ptr_New()
            self.head = nextNode
    ENDIF ELSE BEGIN
            previousNode = (*currentNode).previous
            nextNode = (*currentNode).next
            (*nextNode).previous = previousNode
            (*previousNode).next = nextNode
    ENDELSE

    ; Release the currentNode pointer. 
    Ptr_Free, currentNode
    self.count = self.count - 1
END
;------------------------------------------------------------------------




PRO LINKEDLIST::DELETE_NODES, DESTROY=destroy, ERROR=errorMsg

    ; This method deletes all of the nodes. This is a private method. 
    ; Use the public DELETE method to delete all items from the list.

    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, errorMsg, /Continue
       RETURN
    ENDIF

    ; Assume no error.
    errorMsg = ""
    
    ; Assume you want to destroy the items in the node.
    IF (N_Elements(destroy) EQ 0) $
        THEN destroy = 1 $
        ELSE destroy = Keyword_Set(destroy)
    
    ; Cycle through the list, deleting as you go.
    WHILE Ptr_Valid(self.head) DO BEGIN
        currentNode = *self.head
        IF Keyword_Set(destroy) THEN $
        BEGIN
          theItem = *(currentNode.item)
          CASE Size(theItem, /TNAME) OF
             'OBJREF': Obj_Destroy, theItem
             'POINTER': Ptr_Free, theItem
             ELSE:
          ENDCASE
        ENDIF
        Ptr_Free, currentNode.previous
        Ptr_Free, currentNode.item
        self.head = currentNode.next
    ENDWHILE

    ; Free up that last pointer.
    Ptr_Free, self.tail

    ; Update the count.
    self.count = 0

END
;------------------------------------------------------------------------



FUNCTION LINKEDLIST::GET_COUNT

    ; This method returns the number of items in the list.
    RETURN, self.count
END
;------------------------------------------------------------------------



FUNCTION LINKEDLIST::GET_ITEM, index, $
    ALL=all, $         ; This ASSUMES all items stored are the same type!!!
    Dereference=dereference, $ ; Obsolete. Ignored. Always returns item.
    ItemPtr=itemPtr, $ ; The pointer to the item, if needed. Output.
    NO_COPY=no_copy, $
    ERROR=errorMsg

    ; This method returns the item stored at the index location.
    ; The DEREFERENCE keyword is obsolete and is provide only
    ; to allow backward compatibility. It is always set to 1.
    ; If you wish to obtain the pointer to the item, use the
    ; ItemPtr keyword. If an INDEX is not provided, the
    ; last item is returned.
    
    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, errorMsg, /Continue
       RETURN, Ptr_New()
    ENDIF

    ; Assume no error.
    errorMsg = ""
    dereference = 1
    
    ; Last item, if index is unspecified.
    IF N_Elements(index) EQ 0 THEN index = self.count - 1
    
    ; Make sure there are items in the list.
    IF self.count EQ 0 THEN BEGIN
       errorMsg = 'Nothing is currently stored in the list.'
       Message, errorMsg, /CONTINUE
       RETURN, Ptr_New()
    ENDIF
    
    ; The index has to be in range.
    IF index GT (self.count-1) OR index LT 0 THEN BEGIN
       errorMsg = 'Requested node index is out of range.'
       Message, errorMsg, /CONTINUE
       RETURN, Ptr_New()
    ENDIF
    
    ; Start at the head of the list.
    currentNode = self.head
    
    ; Is this a valid pointer?
    IF ~Ptr_Valid(currentNode) THEN BEGIN
        errorMsg = 'The current node pointer is invalid.'
        Message, errorMsg, /CONTINUE
        RETURN, Ptr_New()
    ENDIF
    
    ; Is the ALL keyword set. ; We are going *assume* the items are all the same type.
    IF Keyword_Set(ALL) THEN BEGIN 
    
        item  = self->Get_Item()
        num   = self.count
        
        ; If items in list are x * y arrays, then for n items, we return a n * x * y ... array
        ; Since IDL's maximum arrays are 8D, this means that this particular routine works only up to 7D arrays.
        ;
        ; There is an unadvertised limitation in IDL's MAKE_ARRAY() function, in that it does not allow
        ; the automatic creation of structures by passing TYPE=8.  Instead, we use the VALUE= keyword
        ; keyword to MAKE_ARRAY.  This takes a scalar and not a vector.
          dim_item      = Size(item, /N_DIMENSIONS) 
          IF (dim_item gt 0) THEN BEGIN
            size_item   = Size(item, /DIMENSIONS)
            type_item   = Size(item, /TYPE)
            arr         =  Make_Array(dimension=[num,size_item], val=item[0])
        
          ENDIF ELSE BEGIN
            type_item   = Size(item, /TYPE)
            arr         = Make_Array(dimension=[num], val=item[0])
          ENDELSE
        
          dim_item_save = dim_item
          type_item_save= type_item
        
          ; Rather than call Get_Item for each element, it is much faster to march through the list
          ; sequentially and extract every item as we get to it.
          ; After we extract each item, we check to make sure that its type and size are the same as for the
          ; first item; if they're not, we generate an error and return.  This is a bit conservative -- we
          ; could promote ints to floats, for instance -- but it's safe.
          currentNode = self.head
          FOR i = 0L, num-1 DO BEGIN
        
                item        =  *((*currentNode).item)
                dim_item    = Size(item, /N_DIMENSIONS)            ; number of dimensions
                type_item    = Size(item, /TYPE)
                IF ((type_item NE type_item_save) OR (dim_item NE dim_item_save)) THEN BEGIN
                    errorMsg = 'Inconsistent type or size for Get_Item(/ALL).  Use Get_Item(index) instead.'
                    Message, errorMsg, /CONTINUE
                    RETURN, Ptr_New()
                ENDIF
            
                ; How many dimensions are there?
                CASE dim_item OF
                  0 : arr[i]               = item
                  1 : arr[i,*]             = item
                  2 : arr[i,*,*]           = item
                  3 : arr[i,*,*,*]         = item
                  4 : arr[i,*,*,*,*]       = item
                  5 : arr[i,*,*,*,*,*]     = item
                  6 : arr[i,*,*,*,*,*,*]   = item
                  7 : arr[i,*,*,*,*,*,*,*] = item
                  ELSE : BEGIN
                    errorMsg = 'Maximum array size for Get_Item(/ALL) exceeded.  Use Get_Item(index) instead.'
                    Message, errorMgs, /CONTINUE
                    RETURN, Ptr_New()
                  END
                ENDCASE
                currentNode = (*currentNode).next
          ENDFOR
        
          RETURN, arr
    
    ENDIF ; if keyword ALL is set.

    ; Find the item asked for by traversing the list.
    FOR j=0L, index-1 DO currentNode = (*currentNode).next
    
    ; Get the item pointer.
    itemPtr = (*currentNode).item
    
    ; Return the item.
    IF Ptr_Valid(itemPtr) THEN BEGIN
        RETURN, *itemPtr 
    ENDIF ELSE BEGIN
        errorMsg = 'The item pointer is invalid.'
        Message, errorMsg, /CONTINUE
        RETURN, Ptr_New()
   ENDELSE
    
END
;------------------------------------------------------------------------



FUNCTION LINKEDLIST::GET_NODE, index, ERROR=errorMsg

    ; This method returns a pointer to the asked-for node.
    ; Ask for the node by number or order in the list
    ; (node numbers start at 0).
    
    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, errorMsg, /Continue
       RETURN, Ptr_New()
    ENDIF

    ; Assume no error.
    errorMsg = ""
    
    ; Gets last node by default.
    IF N_Elements(index) EQ 0 THEN index = self.count - 1
    
    ; Make sure there are items in the list.
    IF self.count EQ 0 THEN BEGIN
       errorMsg = 'Nothing is currently stored in the list.'
       Message, errorMsg, /Continue
       RETURN, Ptr_New()
    ENDIF
    
    IF index GT (self.count - 1) OR index LT 0 THEN BEGIN
       errorMsg = 'Sorry. Requested node is not in list.'
       Message, errorMsg, /Continue
       RETURN, Ptr_New()
    ENDIF  
    
    ; Start at the head of the list.
    currentNode = self.head
   
    ; Find the item asked for by traversing the list.
    FOR j=0L, index-1 DO currentNode = (*currentNode).next
    
    ; Return the pointer to the node.
    RETURN, currentNode
    
END
;------------------------------------------------------------------------



FUNCTION LINKEDLIST::GET_VALUE, index, NO_COPY=no_copy, ERROR=errorMsg

    ; This method returns the item stored at index in the list.
    ; It is a synonym for Get_Item. Added here to be compatible with
    ; SolarSoft changes to LinkImage.
    
    RETURN, self -> Get_Item(index, NO_COPY=no_copy, ERROR=errorMsg)

END
;------------------------------------------------------------------------


FUNCTION LINKEDLIST::HAVE_ITEM, index, ERROR=errorMsg

    ; This method returns a 1 if there is a valid item stored at the
    ; index location. Otherwise, it returns a 0.
    
    ; Try to get the item. See if you were successful.
    item = self -> Get_Item(ERROR=errorMsg, ITEMPTR=itemPtr)
    IF errorMsg NE "" THEN RETURN, 0
    IF ~Ptr_Valid(itemPtr) THEN RETURN, 0
    
    RETURN, 1
    
END
;------------------------------------------------------------------------


PRO LINKEDLIST::HELP, Print=print

    ; This method performs a HELP command on the items
    ; in the linked list. If the PRINT keyword is set, the
    ; data items are printed instead.
    
    ; Are there nodes to work with?
    IF NOT Ptr_Valid(self.head) THEN BEGIN
        ok = Widget_Message('No nodes in Linked List.')
        RETURN
    ENDIF
    
    ; First node.
    currentNode = *self.head
    IF Keyword_Set(print) THEN Print, *currentNode.item ELSE $
        Help, *currentNode.item
    
    ; The rest of the nodes. End of list indicated by null pointer.
    WHILE currentNode.next NE Ptr_New() DO BEGIN
        nextNode = *currentNode.next
        IF Keyword_Set(print) THEN Print, *nextNode.item ELSE $
            Help, *nextNode.item
        currentNode = nextNode
    ENDWHILE

END
;------------------------------------------------------------------------



PRO LINKEDLIST::MOVE_NODE, nodeIndex, location, Before=before, Error=errormsg

    ; This method moves the requested node to a new location.
    ; The node is added AFTER the target location, unless the BEFORE
    ; keyword is used.

    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, !Error_State.MSG, /Continue
       RETURN
    ENDIF

    ; Assume no error.
    errorMsg = ""
    
    ; A node index is required.
    IF N_Elements(nodeIndex) EQ 0 THEN BEGIN
       errorMsg = 'A node index is required in MOVE_NODE method.'
       Message, errorMsg, /Continue
       RETURN
    ENDIF

    ; If location is not specified the node is moved to the
    ; end of the list.
    IF N_Elements(location) EQ 0 THEN BEGIN
       location = (self->Get_Count()) - 1
    ENDIF

    ; Add the node to the list.
    currentNode = self->Get_Node(nodeIndex)
    
    ; If the current node is not a valid pointer, there is problem.
    IF ~Ptr_Valid(currentNode) THEN BEGIN
        errorMsg = 'The requested node is not a valid pointer.'
        Message, errorMsg, /Continue
        RETURN
    ENDIF

    IF Keyword_Set(before) THEN BEGIN
       self->Add, *(*currentNode).item, location, /Before
    ENDIF ELSE BEGIN
       self->Add, *(*currentNode).item, location, /After
    ENDELSE

    ; Delete the node from its current location.
    IF location LT nodeIndex THEN $
       self->Delete, nodeIndex + 1 ELSE $
       self->Delete, nodeIndex


END
;------------------------------------------------------------------------



PRO LINKEDLIST::REPLACE_ITEM, item, index, NO_COPY=no_copy, ERROR=errorMsg

    ; This method replace an item with another item at a particular
    ; index. If index is not supplied, the the last index is used.
    
    Catch, error
    IF error NE 0 THEN BEGIN
       Catch, /Cancel
       errorMsg = !Error_State.MSG
       Message, !Error_State.MSG, /Continue
       RETURN
    ENDIF

    ; Assume no error.
    errorMsg = ""
    
    ; Must supply an item to add to the list.
    IF N_Elements(item) EQ 0 THEN BEGIN
       errorMsg = 'Must supply an item to add to the list.'
       Message, errorMsg, /Continue
       RETURN
    END
    
    ; Check for index. If there is none, add to end of list.
    IF N_Elements(index) EQ 0 THEN index = self.count - 1
    
    ; Start at the head of the list.
    currentNode = self.head
    
    ; Is this a valid pointer?
    IF ~Ptr_Valid(currentNode) THEN BEGIN
        errorMsg = 'The current node pointer is invalid.'
        Message, errorMsg, /CONTINUE
        RETURN
    ENDIF
    
    ; Find the item asked for by traversing the list.
    FOR j=0L, index-1 DO currentNode = (*currentNode).next
    
    ; Get the item pointer.
    itemPtr = (*currentNode).item
    
    ; Replace the item, if you can.
    IF Ptr_Valid(itemPtr) THEN BEGIN
    
        ; Stuff the new item into the place of the old item.
        IF Keyword_Set(no_copy) THEN BEGIN
            *(*currentNode).item = Temporary(item)
        ENDIF ELSE BEGIN
             *(*currentNode).item = item  
        ENDELSE
        
    ENDIF ELSE BEGIN
    
        errorMsg = 'The item pointer is invalid.'
        Message, errorMsg, /CONTINUE
        RETURN
        
   ENDELSE

END 
;------------------------------------------------------------------------



PRO LINKEDLIST::CLEANUP

    ; This method deletes all of the nodes and cleans up
    ; the objects pointers.
    
    self->Delete_Nodes
    Ptr_Free, self.head
    Ptr_Free, self.tail
    
END
;------------------------------------------------------------------------



FUNCTION LINKEDLIST::INIT, item

    ; Initialize the linked list. Add an item if required.
    
    IF N_Params() EQ 0 THEN RETURN, 1
    self->Add, item
    RETURN, 1
    
END
;------------------------------------------------------------------------



PRO LINKEDLIST__DEFINE

   ; The implementation of a LINKEDLIST object.

   struct = { LINKEDLIST_NODE, $    ; The LINKEDLIST NODE structure.
              previous:Ptr_New(), $ ; A pointer to the previous node.
              item:Ptr_New(), $     ; A pointer to the data item.
              next:Ptr_New()  $     ; A pointer to the next node.
              }

   struct = { LINKEDLIST, $         ; The LINKEDLIST object.
              head:Ptr_New(), $     ; A pointer to the first node.
              tail:Ptr_New(), $     ; A pointer to the last node.
              count:0L $            ; The number of nodes in the list.
              }

END
;------------------------------------------------------------------------