Thread: Doubly Linked Lists ???????

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    6

    Question Doubly Linked Lists ???????

    Hi everyone, could u guys help me figure out, what a possible error for this Thread Stop could be? I am writing a function for Insertion Sort in Doubly Linked Lists. I am posting my code. I know that it is not the clearest code ever , but at least it makes sense to me. I use *trailing and *current to traverse through the list. After a place for the new node (*student) is found , it is inserted and then *trailing and *current are set to the beginning of the list again. Dbase-students is the master database pointer. I think the function UnarchiveStudent works properly. Most likely the problem is created from reassigning the pointers. The Thread Stops at the strcmp(.....) line right after while (1).......I've been stucked on this function for too long and I just can't see it......Appreciate any kind of help. THANKS


    int LoadDatabase(Database *dBase)
    {
    FILE *f;
    StudentRecord *student, *trailing=NULL, *current=NULL;
    int i=0;

    f = fopen("Database.txt","r"); // check if the file has open properly
    if(f == NULL) {
    printf("Cannot open that file");
    return -1;
    }

    /* unarchive all the students and put them in the list */
    while ( (student = UnarchiveStudent(f) )!= NULL)
    {
    if(current == NULL) //if the list is empty
    {
    dBase->students = student;
    student->prev = student->next = NULL;
    trailing = current = student;
    }
    else if ( (current != NULL) && (current->next == NULL) ) /*if there is only one node in the list*/
    {
    if (strcmp(student->sLastName, current->sLastName) > 0) /*insert after that single node*/
    {
    student->next = NULL;
    student->prev = current;
    current->next = student;
    }
    else /*insert before that single node, right after the head*/
    {
    student->next = current;
    student->prev = NULL;
    dBase->students = student;
    current->prev = student;
    trailing = current = student;
    }
    }
    else /*if the list already consists of many nodes*/
    {
    while(1)
    {
    if (strcmp(student->sLastName, current->sLastName) > 0) //if the name is still >, keep going along the list
    {
    trailing = current;
    current = current->next;
    }
    //the node has to be inserted now !!!
    else if (trailing == current ) //if we have to insert in the beginning, right after the head
    {
    student->next = current;
    student->prev = NULL;
    current->prev = student;
    dBase->students = student;
    break;
    }
    else if ( (current->next) != NULL)//if the end of the list hasn't been reached yet,insert before the current position and after trailing */
    {
    student->next = current;
    student->prev = trailing;
    trailing->next = student;
    current->prev = student;
    break;
    }
    else /*if the insertion has to be performed at the end of the list*/
    {
    student->next = NULL;
    student->prev = current;
    current->next = student;
    break;
    }
    }
    //move back trailing and current to the beginning of the list in the right order
    current = trailing = dBase->students;
    trailing->prev = current->prev = NULL;
    }
    i++;
    }

    (dBase->studentCount) = i;
    fclose(f);

    return 0;
    }
    if (strcmp(student->sLastName, current->sLastName) > 0)

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    15

    Angry

    use CODE tags
    I'm with
    <----------- newb!

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    6

    addition

    on the line where I have:
    trailing = current = student;
    does it mean that prev and next of trailing and current will be automatically set to prev and next of student?

    I am pasting the code more clearly...sorry

    Code:
    int LoadDatabase(Database *dBase)
    {
    	FILE *f;
    	StudentRecord *student, *trailing=NULL, *current=NULL;
    	int i=0;
    
       f = fopen("Database.txt","r");     // check if the file has open properly
      if(f == NULL) {
    	   printf("Cannot open that file");
       	return -1;
     	}
    
       /* unarchive all the students and put them in the list */
    	while	( (student = UnarchiveStudent(f) )!= NULL)
       {
    		if(current == NULL)          //if the list is empty
          {
             dBase->students = student;
             student->prev = student->next = NULL;
             trailing = current = student;
          }
          else  if ( (current != NULL) && ( (current->next) == NULL) ) /*if there is only one node in the list*/
          {
          	if (strcmp(student->sLastName, current->sLastName) > 0) /*insert after that single node*/
             	{
                	student->next = NULL;
                   student->prev = current;
                   current->next = student;
                }
             else  /*insert before that single node, right after the head*/
             {
             	student->next = current;
                student->prev = NULL;
                dBase->students = student;
                current->prev = student;
                trailing = current = student;
             }
          }
          else                 /*if the list already consists of many nodes*/
          {
          	while(1)
             {
                if (strcmp(student->sLastName, current->sLastName) > 0) //if the name is still >, keep going along the list
                {
                	trailing = current;
                   current = current->next;
                }
                //the node has to be inserted now !!!
                else if (trailing == current ) //if we have to insert in the beginning, right after the head
                {
                	student->next = current;
                   student->prev = NULL;
                   current->prev = student;
                   dBase->students = student;
                   break;
                }
                else  if ( (current->next) != NULL)//if the end of the list hasn't been reached yet,insert before the current position and after trailing */
                {
                	student->next = current;
                   student->prev = trailing;
                   trailing->next = student;
                   current->prev = student;
                   break;
                }
                else          /*if the insertion has to be performed at the end of the list*/
                {
                	student->next = NULL;
             		student->prev = current;
             		current->next = student;
    					break;
                }
             }
             //move back trailing and current to the beginning of the list in the right order
             current = trailing = dBase->students;
    			trailing->prev = current->prev = NULL;
          }
          i++;
       }
    
     	(dBase->studentCount) = i;
       fclose(f);
    
     	return 0;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    does it mean that prev and next of trailing and current will be automatically set to prev and next of student?
    No. It means "this pointer points to this pointer which points to this pointer".

    [edit]
    On a side note, your code is far to hard to read. You should be breaking this down into seperate functions.

    Create an insertion function.

    void insert( Node **list, Node *toinsert );

    Make it do your sorting automaticly. (ie: Define your sort parameter (alphabeticly, whatever) and simply use it to automaticly insert after or before whatever it needs to.
    [/edit]

    Quzah.
    Last edited by quzah; 05-02-2003 at 07:22 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    15
    yeah it's like saying all 3 now point to what student was pointing too!
    I'm with
    <----------- newb!

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    6

    Lightbulb yap

    ok 10x that looks a lot neater
    i'll try

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked lists
    By mohanlon in forum C Programming
    Replies: 8
    Last Post: 12-08-2010, 01:01 AM
  2. Doubly Linked Lists
    By Swerve in forum C++ Programming
    Replies: 6
    Last Post: 03-23-2009, 12:51 PM
  3. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM