This is dlist.c in view mode; [Download] [Up]
/* dlist v1.0.0 Dynamic list library
* Copyright (c) 1994 Bill Bereza
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*
* To reach the author
*
* email:
* berezaw@river.it.gvsu.edu ac368@leo.nmc.edu
*
* $Log: dlist.c,v $
* Revision 1.4 94/12/19 21:17:12 berezaw
* finished quick sort function
*
* Revision 1.3 94/12/19 13:15:51 berezaw
* made daddel functions macros for a more general function
*
* Revision 1.2 94/12/18 01:29:33 berezaw
* fixed a sizeof in a malloc call
*
* Revision 1.1 94/12/17 21:54:05 berezaw
* Initial revision
*
*
* $Header: /Users/berezaw/src/dlist/RCS/dlist.c,v 1.4 94/12/19 21:17:12 berezaw Exp $
*/
#include "dlist.h"
DLIST *_initdlist(size_t els,int lnk)
{
DLIST *newstr;
newstr=(DLIST *)malloc(sizeof(DLIST));
if(newstr != NULL) {
newstr->_first=NULL;
newstr->_last=NULL;
newstr->_bytes=0;
newstr->_elems=0;
newstr->_elsize=els;
newstr->_flags.is_linked=lnk ? 1 : 0;
}
return newstr;
}
void freeels(DLIST *oldstr)
{
void *tchar, *pchar;
if(oldstr==NULL)
return;
pchar=oldstr->_first;
if(oldstr->_flags.is_linked)
while(pchar) {
tchar=pchar;
free(((_DEL *)pchar)->_element);
pchar=((_DEL *)pchar)->_next;
free(tchar);
}
else
while(pchar) {
tchar=pchar;
pchar=((_DEL *)pchar)->_next;
free(tchar);
}
oldstr->_first=oldstr->_last=NULL;
oldstr->_bytes=oldstr->_elems=oldstr->_elsize=0;
}
void freedlist(DLIST *oldstr)
{
if(oldstr==NULL)
return;
freeels(oldstr);
free(oldstr);
}
void *__daddel(DLIST *dstr, void *addel, size_t els)
{
_DEL *newchar;
_DELV *newvel;
int lnk=dstr->_flags.is_linked;
if(!dstr || (!delsize(dstr) && !els) )
return NULL;
if(els) {
if(!(newvel=(struct _delv *)malloc(sizeof(struct _delv))))
return NULL;;
newvel->_elvsize=els;
if(!lnk)
newvel->_element=malloc(els);
else
newvel->_element=addel;
newchar=(_DEL *)newvel;
}
else {
if(!(newchar=(struct _del *)malloc(sizeof(struct _del))))
return NULL;
if(!lnk)
newchar->_element=malloc(delsize(dstr));
else
newchar->_element=addel;
}
if(!(newchar->_element))
return NULL;
if(!lnk)
(void)memcpy(newchar->_element, addel, delsize(dstr) ? delsize(dstr) : els);
newchar->_next=NULL;
if(dstr->_last==NULL) {
dstr->_last=newchar;
dstr->_first=newchar;
newchar->_prev=NULL;
}
else {
newchar->_prev=dstr->_last;
((struct _del *)dstr->_last)->_next=newchar;
dstr->_last=newchar;
}
dstr->_bytes+=delsize(dstr)?delsize(dstr):els;
dstr->_elems++;
return addel;
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.