i remembered there was something about double pointers but couldn't make them work with a simple program so gave up and decided to use the return values. This is what i came up with
Code:
typedef struct Numbers
{
int Num;
struct Numbers *Next;
struct Numbers *Previous;
} Number;
Number * GetMemory();
void CreateHead( Number *, int );
Number * CreateNode( Number *, Number *, int );
Number * AppendtoList( Number *, Number *, int );
void ReadList( Number * );
Number * SearchList( Number *, Number *, int, int );
void DisplayNode( Number * );
void DeleteNode( Number * ); //untested as search doesnt work
void DistroyList( Number * );
int main()
{
int RecNum = 0;
Number *Head = NULL, *Tail = NULL;
for ( int i = 1; i < 11; i++ )
{
if ( Head == NULL )
{
Head = AppendtoList( Head, Tail, i );
Tail = Head;
}
else
{
Tail = AppendtoList( Head, Tail, i );
if ( Tail == NULL )
{
break;
}
}
RecNum++;
}
ReadList( Head );
DisplayNode( SearchList( Head, Tail, 8, RecNum ) );
DistroyList( Tail );
return 0;
}
Number * GetMemory()
{
return ( Number * ) malloc ( sizeof( Number ) );
}
void CreateHead( Number *Tmpptr, int x )
{
Tmpptr->Num = x;
Tmpptr->Next = NULL;
Tmpptr->Previous = NULL;
}
Number * CreateNode( Number *TmpPtr, Number *TPtr, int x )
{
TmpPtr->Previous = TPtr;
TPtr->Next = TmpPtr;
TmpPtr->Next = NULL;
TmpPtr->Num = x;
return TmpPtr;
}
Number * AppendtoList( Number *HPtr, Number *TPtr, int x )
{
if ( HPtr == NULL )
{
HPtr = GetMemory();
if ( HPtr != NULL )
{
CreateHead( HPtr, x );
TPtr = HPtr;
return HPtr;
}
else
{
printf("error getting memory!\n");
exit(0);//will improve this
}
}
else
{
Number *tmpNode = NULL;
tmpNode = GetMemory();
if ( tmpNode != NULL )
{
TPtr = CreateNode(tmpNode, TPtr, x);
return TPtr;
}
else
{
printf("error getting memory! Record not created \n");
return NULL;
}
}
}
void ReadList( Number * CurrentRecord) //passed head pointer
{
int countrec = 0;
while ( CurrentRecord != NULL )
{
printf("%d\n", CurrentRecord->Num);
CurrentRecord = CurrentRecord->Next;
countrec++;
}
printf("%d records displayed\n", countrec);
}
Number * SearchList( Number *FirstRec, Number *LastRec, int Goal, int NumRec )
{
if ( FirstRec == NULL )
{
return NULL;
}
int NumRecNew = 0;
Number *MidRec = NULL;
MidRec = FirstRec;
while ( NumRec / 2 - NumRecNew > 0 )
{
MidRec = MidRec->Next;
NumRecNew += 1;
}
if ( MidRec->Num == Goal )
{
return MidRec;
}
return MidRec->Num < Goal
? SearchList( FirstRec, MidRec , Goal, NumRecNew )
: SearchList( MidRec->Next, LastRec, Goal, NumRecNew );
}
void DisplayNode( Number *CurrentRecord )
{
if ( CurrentRecord != NULL )
{
printf("%d\n", CurrentRecord->Num);
}
else
{
printf("Record not found!!\n");
}
}
void DeleteNode( Number *CurrentRecord )
{
if ( CurrentRecord != NULL )
{
Number *NextNode = NULL, *PrevNode = NULL;
NextNode = CurrentRecord->Next;
PrevNode = CurrentRecord->Previous;
CurrentRecord->Next->Previous = PrevNode;
CurrentRecord->Previous->Next = NextNode;
free ( CurrentRecord );
}
}
void DistroyList( Number *CurrentTail ) //passed tail pointer
{
int countrec = 0;
while ( CurrentTail != NULL )
{
Number *tmpTail =NULL;
tmpTail = CurrentTail;
CurrentTail = CurrentTail->Previous;
free (tmpTail);
countrec++;
}
printf("%d records distroyed\n", countrec);
}
However the search function doesn't work as in it remembers the addresses of head and tail rather than one of them and mid then after a few goes it crashes so i assume my first if statement doesn't work