This I've got to see.
Printable View
This I've got to see.
Code:
LIST_ELEM *add(LIST_ELEM *pHead,char type[],char name[], char title[], int year, double price, int size){
LIST_ELEM *new_elem = malloc(sizeof(LIST_ELEM));
strcpy(new_elem->type, type);
strcpy(new_elem->name,name);
strcpy(new_elem->title, title);
new_elem->year = year;
new_elem->price = price;
new_elem->size = size;
new_elem->pNext = pHead;
return new_elem;
}
lol i think you helped me out on this the other day =)
Oh, you're returning the new head instead of changing the parameter.
Well ... why not do that here? Instead of returning void, return a LIST_ELEM* that's the new head of the list.
so I return pHead but that didn't solve my whole removal issue. Going back to the whole *ptr and **ptr deal, would I still have to do that?
HAH HAH finally getting somewhere. So I've got the case were the list is 1 element, and the element is the first element to work. But I can't get the last case to work, where the element is somewhere in the middle of the list. Can you spot where I'm going wrong? i get a seg fault. when i try this.
Code:
else if( i == loc){
LIST_ELEM *ptrB4 = nodeB4(pHead,loc);
tmpPtr = ptr;
LIST_ELEM *ptrAfter = (tmpPtr->pNext);
ptrB4->pNext = ptrAfter;
free(tmpPtr);
break;
}
Not really; you've got two choices:
Option 1: return void, change the parameter
Option 2, return the head of the list (consistent with your add)Code:void removePtr(LIST_ELEM **pHead, int loc) {
LIST_ELEM *prevPtr, *nextPtr, *tempPtr;
int size = sizeOf(*pHead);
if (loc == 0) {
if (size == 0) return; //already empty
prevPtr = (*pHead)->next;
free(*pHead);
*pHead = prevPtr;
return;
}
if (size < loc) return; //can't delete element that doesn't exist
//if we're still here then walk to the element and delete it
tempPtr = *pHead;
for (int i = 0; i < loc; i++) {
tempPtr = tempPtr->next;
}
prevPtr = nodeB4(*pHead, loc); //if we didn't have the function, we'd walk prevPtr with tempPtr
nextPtr = tempPtr->next;
prevPtr->next = nextPtr;
free(tempPtr);
return;
}
I've assumed you still don't want loc==0 to kill the whole list. EDIT: Also I haven't tested it, or even compiled it, so caveat lector.Code:LIST_ELEM *removePtr(LIST_ELEM *pHead, int loc) {
LIST_ELEM *prevPtr, *nextPtr, *tempPtr;
int size = sizeOf(pHead);
if (loc == 0) {
if (size == 0) return NULL; //already empty
prevPtr = pHead->next;
free(pHead);
pHead = prevPtr;
return pHead;
}
if (size < loc) return pHead; //can't delete element that doesn't exist
//if we're still here then walk to the element and delete it
tempPtr = pHead;
for (int i = 0; i < loc; i++) {
tempPtr = tempPtr->next;
}
prevPtr = nodeB4(pHead, loc); //if we didn't have the function, we'd walk prevPtr with tempPtr
nextPtr = tempPtr->next;
prevPtr->next = nextPtr;
free(tempPtr);
return pHead;
}
ah hah hah thnx again tabstop. Its workin now. (sigh) what a work out.