Monday, March 5, 2007

Linked Lists Confusion

A. Figure out linked lists
B. Snippet of Code from a program I wrote in the summer, for my class
The structs that accompany this code are missing. There are two structs that are need in this linked list. One is Patient and the other Clinic. The next pointer is in the Clinic and the head (listPtr) is in the Clinic.
void insertNode (struct Clinic* clinicPtr, struct Patient* newPatientPtr)
{
struct Patient *current = NULL; struct Patient *previous = NULL;
if (clinicPtr->listPtr == NULL) {

clinicPtr->listPtr = newPatientPtr; clinicPtr->listPtr->nextPatPtr = NULL;
}
else {

previous = current = clinicPtr->listPtr;
while ((current != NULL) && (current->ID <>ID)) {
previous = current; current = current->nextPatPtr;
}
if (current == clinicPtr->listPtr) {
clinicPtr->listPtr = newPatientPtr; clinicPtr->listPtr->nextPatPtr = current;
}

else {
previous->nextPatPtr = newPatientPtr; newPatientPtr->nextPatPtr = current;
}
}
}


There is no output specifically for this code, there was a display function that showed output the screen for the whole program.

C. As somone who has a very hard time with the subject, I am trying to understand my previous code to help me with new code. This linked list inserted nodes by the ID number of the patient. So it sorted it for you.

No comments: