#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Albuquerque, NM
    Posts
    22
    Rep Power
    0

    Unhappy HELP! Simple Linked List Prog that has some pieces i dont know how to write in C


    Hi there... I am brand new to this site and am attempting a cource in C. I took C++ but this seems to be more chalanging. I had some help from a C++ major but we could not figure out how to get the malloc and free functions to work so we just put it a new and delete for now. I desperately need help to get this working. We were not able to compile this yet so if you see any syntax erors please let me know as well. Thank you soooo much to who ever helps me out. The code is attatched and the data file is here:
    Data File

    PS this is suposed to be in C not C++ if I was confusing...sorry.

    Carlos
    Attached Files
  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    28
    Try using * instead of & in your function declarations.

    Have a look at this code for a double linked list written in WIN32 API based C.

    It uses the WIN32 API memory functions instead of malloc()

    Code:
    typedef struct RPT_NODE
    	{
    	struct RPT_NODE		*pNext;
    	struct RPT_NODE		*pPrev;
    	REPORT_DATA			ReportData;//the data this node contains
    	}RPT_NODE;
    
    extern     RPT_NODE       *pHead, *pTail;//I used these as globals, init them to NULL
    //**************************************************************************************
    //	AllocNode
    //**************************************************************************************
    RPT_NODE*	AllocNode(WINDOW_DATA *WD,REPORT_DATA *pRpt)
    {
    	HGLOBAL		hMem=NULL;
    	DWORD		dwSize=0;
    	RPT_NODE		*pNode=NULL;
    
    	
    //my own alloc me function that checks that the mem is alloced and displays errors. Use malloc()
    hMem=AllocateMem(WD->hWnd,hMem,1,sizeof(RPT_NODE),TRUE);
    	if(hMem==NULL)
    		return FALSE;
    //lock the alloced mem to a pointer
    pNode=(RPT_NODE*)GlobalLock(hMem);
    	
    //init to zero, or use calloc()
    ZeroMemory(pNode,sizeof(RPT_NODE));
    	
    //filll node with data
    CopyMemory(&pNode->ReportData,pRpt,sizeof(REPORT_DATA));
    	AppendNode(pNode);
    	return pNode;
    }
    //**************************************************************************************
    //	AppendNode
    // Appends a node to the end of the list
    //**************************************************************************************
    void AppendNode(RPT_NODE *pNode)
    {
       if (pHead == NULL) 
       {
          pHead = pNode;
          pNode->pPrev = NULL;
       }
       else 
       {
          pTail->pNext = pNode;
          pNode->pPrev = pTail;
       }
       pTail = pNode;
       pNode->pNext = NULL;
    }
    //**************************************************************************************
    //	InsertNode
    // Inserts a node into the list after pAfter
    //**************************************************************************************
    void InsertNode(RPT_NODE *pNode, RPT_NODE *pAfter)
    {
       pNode->pNext = pAfter->pNext;
       pNode->pPrev = pAfter;
       if (pAfter->pNext != NULL)
          pAfter->pNext->pPrev = pNode;
       else
          pTail = pNode;
       pAfter->pNext = pNode;
    }
    //**************************************************************************************
    //	RemoveNode
    // Removes the specified node from the list
    //**************************************************************************************
    int RemoveNode(RPT_NODE *pNode)
    {
    	HGLOBAL		hMem=NULL;
    
    	if(pNode->pPrev == NULL)
    		pHead = pNode->pNext;
    	else
    		pNode->pPrev->pNext = pNode->pNext;
    	if(pNode->pNext == NULL)
    		pTail = pNode->pPrev;
    	else
    		pNode->pNext->pPrev = pNode->pPrev;
    	hMem=GlobalHandle(pNode);
    	hMem=FreeMem(Window_Data[MAIN].hWnd,hMem);//my own free mem includes a messagebox if error
    	if(hMem==NULL)
    	{
    		pNode=NULL;
    		return TRUE;
    	}
    	return FALSE;
    }
    //**************************************************************************************
    //	DeleteAllNodes
    // Deletes the entire list
    //**************************************************************************************
    void DeleteAllNodes()
    {
    	while (pHead != NULL)
    		RemoveNode(pHead);
    }
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Albuquerque, NM
    Posts
    22
    Rep Power
    0
    I'll try the suggestion w/ using * but I need help with malloc so I can learn how it is used before I do anything else.
  6. #4
  7. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    Malloc (http://www.opengroup.org/onlinepubs/...ns/malloc.html) asks the OS for a block of memory and returns a pointer to the start of that block or NULL if the allocation fails. It is easy to use, but you must remember to couple it with a free() or you get memory leaks (however, when your program exits/crashes, the OS will recover the memory). It is fairly straitforward to use, though the memory supplied is unitialized (though, depending on the OS, it may have been sanitized so it can't be used to probe memory for interesting bits of information). If you can't remember to avoid using the memory until you have put something meaningful in it, use calloc() instead, as it zeros out the block of memory before returning the pointer. You can also use realloc() to make a contigous block of memory larger (or smaller), though it can get very expensive when used over and over. Using the Win32 API is fine and presents lower overhead, but is not portable (even between some versions of Windows), so unless you are trying to squeeze the last bit of performance from your code (and you wouldn't be here if you were), just use the ANSI version.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

IMN logo majestic logo threadwatch logo seochat tools logo