home *** CD-ROM | disk | FTP | other *** search
- /**************************************************************
- // Linked.c
- //26jul92-bm
- **************************************************************/
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <conio.h>
- #include "astar.h"
-
- /******************
- global functions .. see astar.h
- NPTR DropLinked( NPTR Head )
- NPTR MakeLink( char *DataPtr, unsigned Info )
- NPTR PopLink( NPTR *Head )
- NPTR PushLink( NPTR Head, NPTR New )
- void ReleaseNodes( void )
- *******************************/
-
- /* local functions */
- static int AppendLink( NPTR head, NPTR New );
- static NPTR DeleteLinkNum( NPTR *Head, unsigned Number );
- static NPTR GetFreeLink( void );
- static NPTR GetLastLink( NPTR First );
- static NPTR GetLinkName( NPTR Tmp, char *Sp );
- static NPTR GetLinkNum( NPTR Start, unsigned Number );
- static int InsertLink( NPTR Head, NPTR Prev, NPTR New );
- static int PutFreeLink( NPTR Spare );
- static int ShowLinkedNames( NPTR Start );
- NPTR FreeHead; /* this is external to astar.c */
-
- /**************************************************************
- AppendLink()
- // Add a new node to the end of a non-empty linked list.
- // Return success flag
- **************************************************************/
- static int AppendLink( Head, New ) NPTR Head; NPTR New;
- {
- int Success = NO;
- NPTR Tmp = NULL;
- if( New != NULL )
- Tmp = Head;
- while ( Tmp != NULL && Success == NO )
- {
- if ( Tmp->Next == NULL ) /* if at end of list */
- { /* append */
- Tmp->Next = New;
- New->Next = NULL;
- Success = YES;
- }
- else
- Tmp = Tmp->Next;
- }
- return( Success );
- }
- /**************************************************************
- **************************************************************/
- static NPTR DeleteLinkNum( Head, Number ) NPTR *Head; unsigned Number;
- {
- NPTR Prev = *Head;
- NPTR Tmp = Prev->Next;
- NPTR NewFree = NULL;
- if( (UINT)Prev->Num == Number )
- { /* delete first node */
- NewFree = Prev;
- Prev = Prev->Next;
- *Head = Prev; /* adjust head of list */
- NewFree->Next = NULL; /* terminate loose node */
- }
- while( Tmp != NULL && NewFree==NULL )
- {
- if ( (UINT)Tmp->Num == Number )
- {
- NewFree = Tmp;
- Prev->Next = Tmp->Next;
- NewFree->Next = NULL;
- }
- else
- {
- Prev = Tmp;
- Tmp = Tmp->Next;
- }
- }
-
- return( NewFree ); /* NULL indicates not found */
- }
- /**************************************************************
- // Delete entire linked list and return free nodes to
- // the global list of free nodes.
- // GLOBALS: FreeHead
- **************************************************************/
- NPTR DropLinked( Head ) NPTR Head;
- {
- if( Head != NULL )
- {
- NPTR Next = Head->Next;
- PutFreeLink( Head );
- while( Next != NULL )
- {
- Head = Next;
- Next = Head->Next;
- PutFreeLink( Head );
- }
- }
- return( NULL );
- }
- /**************************************************************
- // To minimize memory fragmentation, new nodes are removed
- // from a list of free nodes if possible
- // GLOBALS: FreeHead
- **************************************************************/
- static NPTR GetFreeLink( void )
- {
- NPTR Tmp = FreeHead ;
- if ( Tmp != NULL )
- {
- FreeHead = Tmp->Next; /* remove from head of list */
- Tmp->Next = NULL; /* terminate loose node */
- }
- return( Tmp );
- }
- /**************************************************************
- GetLastLink()
- // Return pointer to last link in a linked list
- **************************************************************/
- static NPTR GetLastLink( First ) NPTR First;
- {
- NPTR Last = NULL;
- while( First != NULL )
- {
- Last = First;
- First = First->Next;
- }
- return( Last );
- }
- /**************************************************************
- // Return a pointer to the node in a linked list which
- // contains the specified number.
- // GLOBALS: struct lnode
- **************************************************************/
- static NPTR GetLinkNum( Start, Number ) NPTR Start; unsigned Number;
- {
- int Found = NO;
- while ( Start != NULL && !Found )
- {
- if( (UINT)Start->Num == Number )
- Found = YES;
- else
- Start = Start->Next;
- }
- return( Start );
- }
- /**************************************************************
- // Return a pointer to the node in a linked list which points
- // to the specified string.
- // GLOBALS: struct lnode
- **************************************************************/
- static NPTR GetLinkName( Tmp, Sp ) NPTR Tmp; char *Sp;
- {
- int Found = NO;
- while ( Tmp != NULL && !Found )
- {
- if( strcmp(Tmp->Dptr, Sp)==0 )
- Found = YES;
- else
- Tmp = Tmp->Next;
- }
- return( Tmp );
- }
- /**************************************************************
- // Insert a new node within a linked list in front of node pointed
- // to by Prev.
- // Return success flag
- **************************************************************/
- static int InsertLink( Head, Prev, New ) NPTR Head; NPTR Prev; NPTR New;
- {
- int Success = NO;
- if ( Prev != NULL && Head != NULL && New != NULL )
- {
- Success = YES;
- if ( Head == Prev )
- {
- New->Next = Head;
- Head = New;
- }
- else
- {
- New->Next = Prev->Next;
- Prev->Next = New;
- }
- }
- return( Success );
- }
- /**************************************************************
- // Take a node from the global pool of free nodes or
- // make a new list link & store a data pointer & node number
- // Return pointer to new link which is null terminated
- // GLOBALS: NodeNum;
- **************************************************************/
- NPTR MakeLink( DataPtr, Info ) char *DataPtr; unsigned Info;
- {
- NPTR New = GetFreeLink();
- if ( New == NULL )
- {
- New = (NPTR) malloc(sizeof(struct lnode) );
- }
- if ( New != NULL )
- {
- New->Num = Info; /* optional field */
- New->Dptr = DataPtr;
- New->Next = NULL;
- }
- else
- puts("\n **no linked list space**");
-
- return( New );
- }
- /**************************************************************
- // Given pointer-pointer to a linked list, remove the first one
- // and return pointer to it:
- // GLOBALS: None
- **************************************************************/
- NPTR PopLink( Head ) NPTR *Head;
- {
- NPTR Tmp = *Head;
- if ( Tmp != NULL )
- {
- *Head = Tmp->Next; /* remove from head of list */
- Tmp->Next = NULL; /* terminate loose node */
- }
- return( Tmp );
- }
- /**************************************************************
- // Add a node to the head of given list. Return pointer
- // to new head.
- // GLOBALS: None
- **************************************************************/
- NPTR PushLink( Head, New ) NPTR Head; NPTR New;
- {
- NPTR Tmp = Head;
- if( New != NULL )
- {
- if ( Head == NULL )
- {
- Head = New; /* start a new list */
- Head->Next = NULL; /* ground 1st node */
- }
- else
- {
- Head = New; /* insert at front */
- Head->Next = Tmp; /* & connect old to tail */
- }
- }
- return( Head );
- }
- /**************************************************************
- // To minimize memory fragmentation, dropped nodes are added to
- // a list of free nodes.
- // GLOBALS: FreeHead
- **************************************************************/
- static int PutFreeLink( Spare ) NPTR Spare;
- {
- NPTR Tmp = FreeHead ;
- if ( FreeHead == NULL )
- {
- FreeHead = Spare;
- FreeHead->Next = NULL;
- }
- else
- {
- FreeHead = Spare;
- FreeHead->Next = Tmp;
- }
- return( 1 );
- }
- /**************************************************************
- // de-allocate space used for free list of nodes
- **************************************************************/
- void ReleaseNodes( void )
- {
- NPTR Tmp = FreeHead;
- while( Tmp != NULL )
- {
- FreeHead = FreeHead->Next;
- free( Tmp );
- Tmp = FreeHead;
- }
- }
- /**************************************************************
- // display linked list headers (if pointing to strings)
- // and return count
- **************************************************************/
- static int ShowLinkedNames( Start ) NPTR Start;
- {
- int q1 = 0;
- while ( Start != NULL )
- {
- printf("\nNode: %d %s",Start->Num, Start->Dptr);
- Start = Start->Next;
- q1++;
- }
- printf("\n >>");getch();
- return( q1 );
- }
-
-