| 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: (1) source distributions retain this entire copyright |
| 10 | * notice and comment, and (2) distributions including binaries display |
| 11 | * the following acknowledgement: ``This product includes software |
| 12 | * developed by the University of California, Berkeley and its contributors'' |
| 13 | * in the documentation or other materials provided with the distribution |
| 14 | * and in all advertising materials mentioning features or use of this |
| 15 | * software. Neither the name of the University nor the names of its |
| 16 | * contributors may be used to endorse or promote products derived |
| 17 | * from this software without specific prior written permission. |
| 18 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 19 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 21 | */ |
| 22 | |
| 23 | #ifndef lint |
| 24 | static char sccsid[] = "@(#)lstInsert.c 5.3 (Berkeley) 6/1/90"; |
| 25 | #endif /* not lint */ |
| 26 | |
| 27 | /*- |
| 28 | * LstInsert.c -- |
| 29 | * Insert a new datum before an old one |
| 30 | */ |
| 31 | |
| 32 | #include "lstInt.h" |
| 33 | |
| 34 | /*- |
| 35 | *----------------------------------------------------------------------- |
| 36 | * Lst_Insert -- |
| 37 | * Insert a new node with the given piece of data before the given |
| 38 | * node in the given list. |
| 39 | * |
| 40 | * Results: |
| 41 | * SUCCESS or FAILURE. |
| 42 | * |
| 43 | * Side Effects: |
| 44 | * the firstPtr field will be changed if ln is the first node in the |
| 45 | * list. |
| 46 | * |
| 47 | *----------------------------------------------------------------------- |
| 48 | */ |
| 49 | ReturnStatus |
| 50 | Lst_Insert (l, ln, d) |
| 51 | Lst l; /* list to manipulate */ |
| 52 | LstNode ln; /* node before which to insert d */ |
| 53 | ClientData d; /* datum to be inserted */ |
| 54 | { |
| 55 | register ListNode nLNode; /* new lnode for d */ |
| 56 | register ListNode lNode = (ListNode)ln; |
| 57 | register List list = (List)l; |
| 58 | |
| 59 | |
| 60 | /* |
| 61 | * check validity of arguments |
| 62 | */ |
| 63 | if (LstValid (l) && (LstIsEmpty (l) && ln == NILLNODE)) |
| 64 | goto ok; |
| 65 | |
| 66 | if (!LstValid (l) || LstIsEmpty (l) || !LstNodeValid (ln, l)) { |
| 67 | return (FAILURE); |
| 68 | } |
| 69 | |
| 70 | ok: |
| 71 | PAlloc (nLNode, ListNode); |
| 72 | |
| 73 | nLNode->datum = d; |
| 74 | nLNode->useCount = nLNode->flags = 0; |
| 75 | |
| 76 | if (ln == NILLNODE) { |
| 77 | if (list->isCirc) { |
| 78 | nLNode->prevPtr = nLNode->nextPtr = nLNode; |
| 79 | } else { |
| 80 | nLNode->prevPtr = nLNode->nextPtr = NilListNode; |
| 81 | } |
| 82 | list->firstPtr = list->lastPtr = nLNode; |
| 83 | } else { |
| 84 | nLNode->prevPtr = lNode->prevPtr; |
| 85 | nLNode->nextPtr = lNode; |
| 86 | |
| 87 | if (nLNode->prevPtr != NilListNode) { |
| 88 | nLNode->prevPtr->nextPtr = nLNode; |
| 89 | } |
| 90 | lNode->prevPtr = nLNode; |
| 91 | |
| 92 | if (lNode == list->firstPtr) { |
| 93 | list->firstPtr = nLNode; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | return (SUCCESS); |
| 98 | } |
| 99 | |