Linking Lists work!

Posted By: FixxeR

Linking Lists work! - 01/31/07 01:38

Huzzah! Expanding memory abound:

Code:

#include <acknex.c>
#include <litec.h>

typedef struct LList
{
int data;

struct LList *pNext, *pPrev;
} LList;

LList *pStart, *pPtr;

// add a ptr to the linking list
LList *AddPtr(LList *pPrevNode, int data)
{
pPtr = (LList *)malloc(sizeof(LList));

pPtr->data = data;
pPtr->pNext = pPtr->pPrev = NULL;

// does the given node exist
if (pPrevNode == NULL) // no
{
// do we have a starting node
if (pStart == NULL) //no
pStart = pPtr;

} else {
pPrevNode->pNext = pPtr;
pPtr->pPrev = pPrevNode;
}

return pPtr;
}

// find an item in the linking list
LList *FindPtr(LList *pStartNode, int finddata)
{
LList *pCurrentNode;

pCurrentNode = pStartNode;

while (pCurrentNode != NULL)
{
if (pCurrentNode->data == finddata)
return pCurrentNode;

pCurrentNode = pCurrentNode->pNext;
}

return NULL;
}
int main()
{
LList *pFoundNode;

AddPtr(NULL, 10);
AddPtr(pStart, 20);
AddPtr(pStart->pNext, 30);

if((pFoundNode = FindPtr(pStart, 20)) == NULL)
MessageBox(NULL, "Node not found!", "Linking List Test", MB_OK);
else
MessageBox(NULL, "Item Found!", "Linking List Test", MB_OK);

}



I'm a happy programmer now, .

FixxeR
Posted By: JibbSmart

Re: Linking Lists work! - 01/31/07 05:17

nice stuff!
ive set this as favourite thread coz i think its a great example of some clever programming you couldn't do with c-script. i understand it mostly, though i couldn't program that myself without having some references.

again, good stuff!

julz
Posted By: testDummy

Re: Linking Lists work! - 02/03/07 06:33

Don't forget to 'free' the memory when you are done.

This might be evidence that 'standard' C code works with Lite C without modification, but I don't think that, when taken alone, is really 'impressive'.

Many references for C data structures are freely available.
Posted By: FixxeR

Re: Linking Lists work! - 02/03/07 18:19

Yeah, at that point I was just looking for proof of concept with lite-C.

FixxeR
© 2024 lite-C Forums