xref: /netbsd-src/games/atc/list.c (revision 1182a44c59cae4d586117d55eca24b4b8b173211)
1*1182a44cSrillig /*	$NetBSD: list.c,v 1.10 2021/05/02 12:50:43 rillig Exp $	*/
2101657d1Scgd 
361f28255Scgd /*-
4101657d1Scgd  * Copyright (c) 1990, 1993
5101657d1Scgd  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * This code is derived from software contributed to Berkeley by
861f28255Scgd  * Ed James.
961f28255Scgd  *
1061f28255Scgd  * Redistribution and use in source and binary forms, with or without
1161f28255Scgd  * modification, are permitted provided that the following conditions
1261f28255Scgd  * are met:
1361f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1561f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1761f28255Scgd  *    documentation and/or other materials provided with the distribution.
18e5aeb4eaSagc  * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd  *    may be used to endorse or promote products derived from this software
2061f28255Scgd  *    without specific prior written permission.
2161f28255Scgd  *
2261f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd  * SUCH DAMAGE.
3361f28255Scgd  */
3461f28255Scgd 
3561f28255Scgd /*
3661f28255Scgd  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
3761f28255Scgd  *
3861f28255Scgd  * Copy permission is hereby granted provided that this notice is
3961f28255Scgd  * retained on all partial or complete copies.
4061f28255Scgd  *
4161f28255Scgd  * For more info on this and all of my stuff, mail edjames@berkeley.edu.
4261f28255Scgd  */
4361f28255Scgd 
44ca57cf90Slukem #include <sys/cdefs.h>
4561f28255Scgd #ifndef lint
46101657d1Scgd #if 0
47101657d1Scgd static char sccsid[] = "@(#)list.c	8.1 (Berkeley) 5/31/93";
48101657d1Scgd #else
49*1182a44cSrillig __RCSID("$NetBSD: list.c,v 1.10 2021/05/02 12:50:43 rillig Exp $");
50101657d1Scgd #endif
5161f28255Scgd #endif /* not lint */
5261f28255Scgd 
5301dcf47eSdholland #include <stdlib.h>
5401dcf47eSdholland 
5501dcf47eSdholland #include "def.h"
5601dcf47eSdholland #include "struct.h"
5701dcf47eSdholland #include "extern.h"
5801dcf47eSdholland #include "tunable.h"
5961f28255Scgd 
6061f28255Scgd PLANE *
newplane(void)614931378aSjmc newplane(void)
6261f28255Scgd {
63860c2027Sdholland 	return calloc(1, sizeof (PLANE));
6461f28255Scgd }
6561f28255Scgd 
66ca57cf90Slukem void
append(LIST * l,PLANE * p)674931378aSjmc append(LIST *l, PLANE *p)
6861f28255Scgd {
6961f28255Scgd 	PLANE 	*q = NULL, *r = NULL;
7061f28255Scgd 
7161f28255Scgd 	if (l->head == NULL) {
7261f28255Scgd 		p->next = p->prev = NULL;
7361f28255Scgd 		l->head = l->tail = p;
7461f28255Scgd 	} else {
7561f28255Scgd 		q = l -> head;
7661f28255Scgd 
7761f28255Scgd 		while (q != NULL && q->plane_no < p->plane_no) {
7861f28255Scgd 			r = q;
7961f28255Scgd 			q = q -> next;
8061f28255Scgd 		}
8161f28255Scgd 
8261f28255Scgd 		if (q) {
8361f28255Scgd 			if (r) {
8461f28255Scgd 				p->prev = r;
8561f28255Scgd 				r->next = p;
8661f28255Scgd 				p->next = q;
8761f28255Scgd 				q->prev = p;
8861f28255Scgd 			} else {
8961f28255Scgd 				p->next = q;
9061f28255Scgd 				p->prev = NULL;
9161f28255Scgd 				q->prev = p;
9261f28255Scgd 				l->head = p;
9361f28255Scgd 			}
9461f28255Scgd 		} else {
9561f28255Scgd 			l->tail->next = p;
9661f28255Scgd 			p->next = NULL;
9761f28255Scgd 			p->prev = l->tail;
9861f28255Scgd 			l->tail = p;
9961f28255Scgd 		}
10061f28255Scgd 	}
10161f28255Scgd }
10261f28255Scgd 
103ca57cf90Slukem void
delete(LIST * l,PLANE * p)1044931378aSjmc delete(LIST *l, PLANE *p)
10561f28255Scgd {
10661f28255Scgd 	if (l->head == NULL)
1073f9984fcSwiz 		loser(p, "deleted a non-existent plane! Get help!");
10861f28255Scgd 
10961f28255Scgd 	if (l->head == p && l->tail == p)
11061f28255Scgd 		l->head = l->tail = NULL;
11161f28255Scgd 	else if (l->head == p) {
11261f28255Scgd 		l->head = p->next;
11361f28255Scgd 		l->head->prev = NULL;
11461f28255Scgd 	} else if (l->tail == p) {
11561f28255Scgd 		l->tail = p->prev;
11661f28255Scgd 		l->tail->next = NULL;
11761f28255Scgd 	} else {
11861f28255Scgd 		p->prev->next = p->next;
11961f28255Scgd 		p->next->prev = p->prev;
12061f28255Scgd 	}
12161f28255Scgd }
122