source: mainline/uspace/dist/sysel/list.sy@ 37f527b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 37f527b was 37f527b, checked in by Jiri Svoboda <jiri@…>, 16 years ago

Update SBI to rev. 144.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1--
2-- Copyright (c) 2010 Jiri Svoboda
3-- All rights reserved.
4--
5-- Redistribution and use in source and binary forms, with or without
6-- modification, are permitted provided that the following conditions
7-- are met:
8--
9-- o Redistributions of source code must retain the above copyright
10-- notice, this list of conditions and the following disclaimer.
11-- o Redistributions in binary form must reproduce the above copyright
12-- notice, this list of conditions and the following disclaimer in the
13-- documentation and/or other materials provided with the distribution.
14-- o The name of the author may not be used to endorse or promote products
15-- derived from this software without specific prior written permission.
16--
17-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18-- IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19-- OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20-- IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21-- INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22-- NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23-- DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24-- THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25-- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26-- THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27--
28
29-- Doubly-linked list implementation.
30class List is
31 var head : ListNode;
32
33 -- Initialize list.
34 fun Init() is
35 head = new ListNode();
36 head.prev = head;
37 head.next = head;
38 end
39
40 -- Append new entry at the end of the list.
41 fun Append(data : int) is
42 var n : ListNode;
43 var ntl : ListNode;
44
45 ntl = head.prev;
46
47 n = new ListNode();
48 n.value = data;
49
50 n.prev = ntl;
51 n.next = head;
52 n.head = head;
53
54 ntl.next = n;
55 head.prev = n;
56 end
57
58 -- Return first node in the list or @c nil if there is none.
59 prop First : ListNode is
60 get is
61 return get_first();
62 end
63 end
64
65 -- Return first node in the list or @c nil if there is none.
66 fun get_first() : ListNode is
67 if head.next == head then
68 return nil;
69 else
70 return head.next;
71 end
72 end
73end
74
75class ListNode is
76 var value : int;
77
78 var prev : ListNode;
79 var next : ListNode;
80 var head : ListNode;
81
82 -- Value stored in this node.
83 prop Value : int is
84 get is
85 return value;
86 end
87 end
88
89 -- Previous node in list.
90 prop Prev : ListNode is
91 get is
92 return get_prev();
93 end
94 end
95
96 -- Next node in list.
97 prop Next : ListNode is
98 get is
99 return get_next();
100 end
101 end
102
103 -- Get next node.
104 fun get_next() : ListNode is
105 if next != head then
106 return next;
107 else
108 return nil;
109 end
110 end
111
112 -- Get previous node.
113 fun get_prev() : ListNode is
114 if prev != head then
115 return next;
116 else
117 return nil;
118 end
119 end
120
121end
122
123class ListDemo is
124 fun Main() is
125 var list : List;
126
127 list = new List();
128 list.Init();
129
130 list.Append(5);
131 list.Append(6);
132 list.Append(7);
133 list.Append(8);
134
135 var n : ListNode;
136
137 n = list.First;
138 while n != nil do
139 Builtin.WriteLine(n.value);
140 n = n.Next;
141 end
142 end
143end
Note: See TracBrowser for help on using the repository browser.