Hey I am having trouble with this function.
It's arguements are 2 integrs and the beginning of the linked list.
the list begins with one node, which has the value [ 0 | 9999 | / ]
the first integer is where the break should start. and the second integer is for how many "blocks" it should continue till the next node can start.
first call: deallocateNode(1000, 100, &h);
prints two nodes: [ 0 | 999 | ]-----> [ 1100 | 9999 | / ] which is correct.
second call: deallocateNode(500, 100, &h);
prints two nodes instead of three: [ 0 | 499 | ]-----> [ 1100 | 9999 | / ]
but should print: [ 0 | 499 | ]-----> [ 600 | 999 | ]-----> [ 1100 | 9999 | / ]
why is the node [ 600 | 999 | ] not being printed?
thanks.
Code:
/*the list*/
struct node {
int start;
int end;
struct node *nextPtr;
};
/*function deallocateNode*/
void deallocateNode(int p, int q, struct node **h) {
struct node *newPtr, *previousPtr, *currentPtr;
if((newPtr = (struct node *)malloc(sizeof(struct node))) == NULL) {
printf("Node allocation failed. \n");
exit(1);
}
if(newPtr != NULL) { /*create a new node*/
if(p < (*h)->end) {
newPtr->start = (*h)->start;
printf("newPtr->start %d", newPtr->start);
newPtr->end = p-1;
newPtr->nextPtr = NULL;
}
previousPtr = NULL; /*initialize nodes*/
(*h)->start = p + q;
currentPtr = (*h);
while(currentPtr != NULL) {
previousPtr = currentPtr;
currentPtr = currentPtr->nextPtr;
printf("WHILE LOOP \n");
}
newPtr->nextPtr = previousPtr;
previousPtr->nextPtr = currentPtr;
(*h) = newPtr;
}
}
/*just the print function */
void print_list(struct node *h) {
if (h == NULL) {
printf("The list is empty.\n");
}
else {
printf("Values in the list are:\n");
while(h!= NULL) {
printf("%d %d\n", h->start, h->end);
h = h->nextPtr;
}
}
}