xref: /openbsd/games/atc/list.c (revision 898184e3)
1 /*	$OpenBSD: list.c,v 1.7 2009/10/27 23:59:23 deraadt Exp $	*/
2 /*	$NetBSD: list.c,v 1.3 1995/03/21 15:04:18 cgd Exp $	*/
3 
4 /*-
5  * Copyright (c) 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Ed James.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 /*
37  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
38  *
39  * Copy permission is hereby granted provided that this notice is
40  * retained on all partial or complete copies.
41  *
42  * For more info on this and all of my stuff, mail edjames@berkeley.edu.
43  */
44 
45 #include "include.h"
46 
47 PLANE	*
48 newplane(void)
49 {
50 	PLANE *p;
51 
52 	if ((p = (PLANE *) calloc(1, sizeof (PLANE))) == NULL)
53 		loser(NULL, "Out of memory");
54 	return (p);
55 }
56 
57 void
58 append(LIST *l, PLANE *p)
59 {
60 	PLANE 	*q = NULL, *r = NULL;
61 
62 	if (l->head == NULL) {
63 		p->next = p->prev = NULL;
64 		l->head = l->tail = p;
65 	} else {
66 		q = l -> head;
67 
68 		while (q != NULL && q->plane_no < p->plane_no) {
69 			r = q;
70 			q = q -> next;
71 		}
72 
73 		if (q) {
74 			if (r) {
75 				p->prev = r;
76 				r->next = p;
77 				p->next = q;
78 				q->prev = p;
79 			} else {
80 				p->next = q;
81 				p->prev = NULL;
82 				q->prev = p;
83 				l->head = p;
84 			}
85 		} else {
86 			l->tail->next = p;
87 			p->next = NULL;
88 			p->prev = l->tail;
89 			l->tail = p;
90 		}
91 	}
92 }
93 
94 void
95 delete(LIST *l, PLANE *p)
96 {
97 	if (l->head == NULL)
98 		loser(p, "deleted a non-existent plane! Get help!");
99 
100 	if (l->head == p && l->tail == p)
101 		l->head = l->tail = NULL;
102 	else if (l->head == p) {
103 		l->head = p->next;
104 		l->head->prev = NULL;
105 	} else if (l->tail == p) {
106 		l->tail = p->prev;
107 		l->tail->next = NULL;
108 	} else {
109 		p->prev->next = p->next;
110 		p->next->prev = p->prev;
111 	}
112 }
113