1 /*
2 * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Adam de Boor.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the University of California, Berkeley. The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 */
20
21 #ifndef lint
22 static char sccsid[] = "@(#)lstIndex.c 5.2 (Berkeley) 03/11/90";
23 #endif /* not lint */
24
25 /*-
26 * lstIndex.c --
27 * Function to return the index of a datum in a Lst.
28 */
29
30 #include "lstInt.h"
31
32 /*-
33 *-----------------------------------------------------------------------
34 * Lst_Index --
35 * Return the index of a datum in a Lst. Indices start at 0.
36 *
37 * Results:
38 * Returns -1 if the datum isn't in the Lst, or the index of
39 * the datum if it is.
40 *
41 * Side Effects:
42 * None.
43 *
44 *-----------------------------------------------------------------------
45 */
46 int
Lst_Index(l,d)47 Lst_Index(l, d)
48 Lst l;
49 ClientData d;
50 {
51 List list = (List)l;
52 register ListNode lNode;
53 register int index;
54
55 lNode = list->firstPtr;
56
57 if (lNode == NilListNode) {
58 return(-1);
59 }
60
61 index = 0;
62
63 do {
64 if (lNode->datum == d) {
65 return(index);
66 } else {
67 lNode = lNode->nextPtr;
68 index += 1;
69 }
70 } while (lNode != NilListNode && lNode != list->firstPtr);
71 return(-1);
72 }
73