I am currently trying to create a doubly linked list that sorts the inserted char value alphabetically, however it is not updating the previous nodes properly because of the loop used to compare data values.
This is my current insert code:
void insert(ListNodePtr *sPtr, char value)
{
ListNodePtr newPtr; /* pointer to new node */
ListNodePtr previousPtr; /* pointer to previous node in list */
ListNodePtr currentPtr; /* pointer to current node in list */
newPtr = (ListNode*)malloc(sizeof(ListNode)); /* create node */
if (newPtr != NULL) { /* is space available */
newPtr->data = value; /* place value in node */
newPtr->nextPtr = NULL; /* node does not link to another node */
newPtr->prevPtr = NULL;
previousPtr = NULL;
currentPtr = *sPtr;
/* loop to find the correct location in the list */
while (currentPtr != NULL && value > currentPtr->data) {
previousPtr = currentPtr; /* walk to ... */
currentPtr = currentPtr->nextPtr; /* ... next node */
} /* end while */
/* insert new node at beginning of list */
if (previousPtr == NULL) {
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
} /* end if */
else { /* insert new node between previousPtr and currentPtr */
previousPtr->nextPtr = newPtr;
newPtr->prevPtr = previousPtr;
newPtr->nextPtr = currentPtr;
} /* end else */
} /* end if */
else {
printf("%c not inserted. No memory available.\n", value);
} /* end else */
} /* end function insert */
here is my node structure:
/* self-referential structure */
struct listNode {
char data; /* each listNode contains a character */
struct listNode *nextPtr; /* pointer to next node*/
struct listNode *prevPtr; /* pointer to previous node*/
}; /* end structure listNode */
typedef struct listNode ListNode; /* synonym for struct listNode */
typedef ListNode *ListNodePtr; /* synonym for ListNode* */
and here is my print function:
void printReverse(ListNodePtr currentPtr)
{
/* if list is empty */
if (currentPtr == NULL) {
printf("List is empty.\n\n");
} /* end if */
else {
printf("The list in reverse is:\n");
while (currentPtr->nextPtr != NULL)
currentPtr = currentPtr->nextPtr;
/* while not the beginning of the list */
while (currentPtr != NULL) {
printf("%c --> ", currentPtr->data);
currentPtr = currentPtr->prevPtr;
} /* end while */
printf("NULL\n\n");
} /* end else */
} /* end function printList */
and here is the output that I get:
output when entering chars in ascending order of ASCII value it works properly
but when I add a larger value and then a value smaller than that one it does not print the correct previous value.
I have been working on this for the last few days trying to figure out what to do, but I cannot find any related situations that others have been in so I would appreciate any help that I can get. Thanks!
P.S. This is a hw assignment and I normally wouldn't ask for help with it, but the book says nothing about doubly-linked lists and even less on how to insert data alphabetically with them so I am just really desperate for help right now and I will gladly take any info I can get to point me in the right direction!