1 /* =============================================================================
4 * -- Sorted singly linked list
5 * -- Options: duplicate allowed
6 * (DLIST_NO_DUPLICATES) is no implemented yet (default: allow duplicates)
8 * =============================================================================
10 * Copyright (C) Stanford University, 2006. All Rights Reserved.
11 * Author: Chi Cao Minh
13 * =============================================================================
15 * For the license of bayes/sort.h and bayes/sort.c, please see the header
18 * ------------------------------------------------------------------------
20 * For the license of kmeans, please see kmeans/LICENSE.kmeans
22 * ------------------------------------------------------------------------
24 * For the license of ssca2, please see ssca2/COPYRIGHT
26 * ------------------------------------------------------------------------
28 * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
29 * header of the files.
31 * ------------------------------------------------------------------------
33 * For the license of lib/rbtree.h and lib/rbtree.c, please see
34 * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
36 * ------------------------------------------------------------------------
38 * Unless otherwise noted, the following license applies to STAMP files:
40 * Copyright (c) 2007, Stanford University
41 * All rights reserved.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions are
47 * * Redistributions of source code must retain the above copyright
48 * notice, this list of conditions and the following disclaimer.
50 * * Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in
52 * the documentation and/or other materials provided with the
55 * * Neither the name of Stanford University nor the names of its
56 * contributors may be used to endorse or promote products derived
57 * from this software without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
60 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
62 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
64 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
65 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
66 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
67 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
68 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
69 * THE POSSIBILITY OF SUCH DAMAGE.
71 * =============================================================================
77 public List_Node head;
81 head = new List_Node();
85 /* =======================================================================
87 * -- Returns null on failure
88 * =======================================================================
90 private List_Node allocNode(Object dataPtr)
92 List_Node nodePtr = new List_Node();
98 nodePtr.dataPtr = dataPtr;
99 nodePtr.nextPtr = null;
105 /* =============================================================================
107 * -- If NULL passed for 'compare' function, will compare data pointer addresses
108 * -- Returns NULL on failure
109 * =============================================================================
110 * list_t* list_alloc (long (*compare)(const void*, const void*));
115 public static List_t alloc()
117 List_t listPtr = new List_t();
119 if(listPtr == null) {
123 listPtr.head.dataPtr = null;
124 listPtr.head.nextPtr = null;
130 /* =============================================================================
132 * -- If NULL passed for 'compare' function, will compare data pointer addresses
133 * -- Returns NULL on failure
134 * =============================================================================
135 * void list_free (list_t* listPtr);
137 public static void free(List_t listPtr)
144 /* =============================================================================
146 * -- Return TRUE if list is empty, else FALSE
147 * =============================================================================
148 * bool_t list_isEmpty (list_t* listPtr);
150 public boolean isEmpty()
152 return (head.nextPtr == null);
155 /* =============================================================================
157 * -- Returns size of list
158 * =============================================================================
159 * long list_getSize (list_t* listPtr);
161 public int getSize() {
165 /* =============================================================================
167 * =============================================================================
168 * void* list_find (list_t* listPtr, void* dataPtr);
170 private List_Node findPrevious(Object dataPtr)
172 List_Node prevPtr = head;
173 List_Node nodePtr = prevPtr.nextPtr;
175 for(; nodePtr != null; nodePtr = nodePtr.nextPtr) {
176 if (compare(nodePtr.dataPtr,dataPtr) >= 0) {
185 /* =============================================================================
187 * -- Returns NULL if not found, else returns pointer to data
188 * =============================================================================
189 * void* list_find (list_t* listPtr, void* dataPtr);
191 public Object find(Object dataPtr) {
193 List_Node prevPtr = findPrevious(dataPtr);
195 nodePtr = prevPtr.nextPtr;
197 if((nodePtr == null) ||
198 (compare(nodePtr.dataPtr,dataPtr) != 0)) {
202 return (nodePtr.dataPtr);
205 public int compare(Object obj1,Object obj2)
207 Reservation_Info aPtr=(Reservation_Info)obj1;
208 Reservation_Info bPtr=(Reservation_Info)obj2;
211 typeDiff = aPtr.type - bPtr.type;
213 return ((typeDiff != 0) ? (typeDiff) : (aPtr.id - bPtr.id));
216 /* =============================================================================
218 * -- Return TRUE on success, else FALSE
219 * =============================================================================
220 * bool_t list_insert (list_t* listPtr, void* dataPtr);
222 public boolean insert(Object dataPtr) {
227 prevPtr = findPrevious(dataPtr);
228 currPtr = prevPtr.nextPtr;
230 nodePtr = allocNode(dataPtr);
231 if (nodePtr == null) {
235 nodePtr.nextPtr = currPtr;
236 prevPtr.nextPtr = nodePtr;
243 /* =============================================================================
245 * -- Returns TRUE if successful, else FALSE
246 * =============================================================================
247 * bool_t list_remove (list_t* listPtr, void* dataPtr);
249 public boolean remove(Object dataPtr)
254 prevPtr = findPrevious(dataPtr);
256 nodePtr = prevPtr.nextPtr;
258 if((nodePtr != null) &&
259 (compare(nodePtr.dataPtr,dataPtr) == 0))
261 prevPtr.nextPtr = nodePtr.nextPtr;
262 nodePtr.nextPtr = null;
272 int compareObject(Object obj1,Object obj2) {
277 /* =============================================================================
279 * -- Removes all elements
280 * =============================================================================
281 * void list_clear (list_t* listPtr);
283 public void clear() {
284 head = new List_Node();
288 /* =============================================================================
292 * =============================================================================
297 public static void main(String[] argv) {
299 int[] data1 = new int[5];
300 int[] data2 = new int[6];
304 System.out.println("Starting...");