-
Hmm, this isn't simply a case of having a small problems with a linked list, this is a case of not knowing how to write linked list code, specifically removal. That being so, I suggest reading (a) the tutorial just linked to and (b) other tutorials on the net. These will explain what you need to do and most likely show pictures to help you understand. This would be preferable to having someone, specifically try and explain it you you on the spot.
-
Code:
#include <stdio.h>
#include "Prototype.h"
#include "definition.h"
struct EmployeeData* deleteElement(struct EmployeeData *pList)
{
int empCode, found;
struct EmployeeData *pCur, *pPre;
pCur = pPre = NULL;
printf ("Please enter the employee code to delete.\n");
scanf("%d", &empCode);
fflush(stdin);
found = searchList (pList, &pPre, &pCur, empCode);
if (found == TRUE)
deleteNode (pList, pPre, pCur);
else
printf("Employee Not Found!\n");
return pList;
}
int searchList(struct EmployeeData *pList, struct EmployeeData **pPre, struct EmployeeData **pCur, int target)
{
int found = FALSE;
*pPre = NULL;
*pCur = pList;
// Starts search from the beginning
while (*pCur != NULL && ((*pCur)->code != target))
{
*pPre = *pCur;
*pCur = (*pCur)->nextElement;
}
if(*pCur && ((*pCur)->code == target ) )
found = TRUE;
return found;
}
struct EmployeeData* deleteNode (struct EmployeeData *pList, struct EmployeeData *pPre, struct EmployeeData *pCur)
{
if (pPre == NULL)
// Deleting first node
pList = pCur->nextElement; //The first node have garbage allocated
else
// Deleting other nodes
pPre->nextElement = pCur->nextElement;
free (pCur);
return pList;
} // deleteNode
Hi after much failure attempts, i finally mananged to delete without garbage, but when i delete the first node, i get garbage can anyone tell me where is my error???
-
Code:
pList = pCur->nextElement; //The first node have garbage allocated
Code:
struct EmployeeData* deleteNode (struct EmployeeData *pList, struct EmployeeData *pPre, struct EmployeeData *pCur)
Can you spot an error?
-
This is wrong
Code:
if (found == TRUE)
deleteNode (pList, pPre, pCur);
It Should be
Code:
if (found == TRUE)
pList = deleteNode (pList, pPre, pCur);
__________________________________________________ __
This is wrong
Code:
if (pPre == NULL)
pList = pCur->nextElement;
it should be
Code:
if (pPre == NULL)
pList = (*pList).nextElement;//Deleting first node
My program works correctly now Thanks for all your help