Problem with Deleting Nodes/Inserting Nodes

Okay. So heres my problem. I believe that my Delete Node and Insert Node Functions are correct, but I am getting runtime errors. My code compiles OKAY, but I think it's just what I am inputting into the Paramaters that are making it explode while I run it.

Here is my code, it's properly commented in case you have no idea what I'm doing at times.

[code]typedef int DATA;

typedef struct node {
struct node * next;
DATA digit; /* a digit of a number range 0-9*/
} NODE;

typedef struct {
struct node *head; /* the head of the list */
int size; /* number of elements in the list */
} LIST_HEAD;

NODE *insertAfter(LIST_HEAD *list, NODE *p, DATA digit) {

/* Allocate Memory */
NODE *prev, *temp = (NODE *) malloc( sizeof(NODE) );

/* Check Memory Availability */
temp->digit = digit;

/* Insert into List */
temp->next = p->next;
prev = temp;

while(temp -> next != p) {
/* Prev points to the previous container */
prev = temp;
temp = temp -> next;
}

temp->next=p;

/* Increase the list size by 1 */
list -> size++;

return (p);
}

int deleteNode(LIST_HEAD *list, NODE *p, DATA *digit) {
/* Set containers */
NODE *temp, *prev;
/* Allocate Memory */
temp = (NODE *) malloc( sizeof(NODE) );

/* Delete the Pth container in the list */
temp = list->head;
prev = temp;

if (temp!=NULL) {

while(temp -> next != p) {
/* Prev points to the previous container */
prev = temp;
temp = temp -> next;
}
/* Set the prev container to next */
prev->next = temp->next;

/* Free temp's memory */
free(temp);

/* Decrease the list size by 1 */
list -> size --;

return 0;
}
else return 1;

}

int main() {
LIST_HEAD *newList = createList();
NODE digitTest;

NODE *sixthNode = insertAfter(newList, digitTest,5);
int del = deleteNode(newList, digitTest, 5)

}
[/code]

Please Note: That I have truncated the function list, and main functions just down to the guts of my problem.

As you can see from the code above, I have no idea if what I am inserting into the parameters of the functions are correct, and if my functions are correct, as I'm getting runtime errors as previously stated.

I hope to get answers, and thanks.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories