1   /****************************************************************************
2    **
3    ** This file is part of yFiles-2.6. 
4    ** 
5    ** yWorks proprietary/confidential. Use is subject to license terms.
6    **
7    ** Redistribution of this file or of an unauthorized byte-code version
8    ** of this file is strictly forbidden.
9    **
10   ** Copyright (c) 2000-2008 by yWorks GmbH, Vor dem Kreuzberg 28, 
11   ** 72070 Tuebingen, Germany. All rights reserved.
12   **
13   ***************************************************************************/
14  package demo.base;
15  
16  import y.base.ListCell;
17  import y.base.YCursor;
18  import y.base.YList;
19  
20  import java.util.Comparator;
21  
22  /**
23   * Demonstrates how to use the linked list datatype YList and the YCursor interface.
24   * <p>
25   * <b>usage:</b> java demo.base.ListDemo
26   * </p>
27   */
28  public class ListDemo 
29  {
30    public ListDemo()
31    {
32      //create new YList instance
33      YList list = new YList();
34      
35      //add numbered String elements to list 
36      for(int i = 0; i < 20; i++)
37        list.addLast(""+i);
38      
39      //iterate over list from first to last
40      System.out.println("List elements from front to back");
41      for(YCursor c = list.cursor(); c.ok(); c.next())
42      {
43        //output element at cursor position
44        System.out.println(c.current());
45      }
46      
47      //iterate over list from last to first
48      System.out.println("List elements from back to front");
49      YCursor rc = list.cursor();
50      for(rc.toLast(); rc.ok(); rc.prev())
51      {
52        //output element at cursor position
53        System.out.println(rc.current());
54      }
55      
56      //sort list lexicografically
57      list.sort(new Comparator() 
58                {
59                  public int compare(Object a, Object b)
60                    {
61                      return ((String)a).compareTo((String)b);
62                    }
63                });
64      
65      
66      //iterate over list from first to last
67      System.out.println("Lexicographically sorted list");
68      for(YCursor c = list.cursor(); c.ok(); c.next())
69      {
70        //output element at cursor position
71        System.out.println(c.current());
72      }
73      
74      //low level iteration on list cells (non-const iteration)
75      for(ListCell cell = list.firstCell(); cell != null; cell = cell.succ())
76      {
77        String s = (String)cell.getInfo();
78        //remove all Strings from list that have length == 1 
79        if(s.length() == 1)
80        {
81          list.removeCell(cell); 
82          //note that cell is still half-valid, i.e it's succ() and pred() 
83          //pointers are still unchanged. therefore cell = cell.succ() is
84          //valid in the for-statement
85        }
86      }
87      
88      System.out.println("list after element removal");
89      System.out.println(list);
90      
91      //initialize list2 with the elements from list
92      YList list2 = new YList(list.cursor());
93      System.out.println("list2 after creation");
94      System.out.println(list2);
95      
96      //reverse element order in list2
97      list2.reverse();
98      System.out.println("list2 after reversal");
99      System.out.println(list2);
100     
101     //move all elements of list2 to the end of list
102     list.splice(list2);
103     
104     System.out.println("list after splicing");
105     System.out.println(list);
106     System.out.println("list2 after splicing");
107     System.out.println(list2);
108     
109   }
110   
111   public static void main(String args[])
112   {
113     new ListDemo();
114   }
115   
116 }