Thread: help with doubly linked lists

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    14

    Post help with doubly linked lists

    Code:
    struct course 
    {
           string30 coursecode;
           int section;
           int units;
           char schedule[3];  
           string30 lecturer;
           int slots; 
           struct getTime *ptrtime;
           struct course *pNext;
           struct course *pPrev;
         
           
    }; 
    
    typedef struct course *ptrcourse; 
    void createclass(ptrcourse *pFirst, int ctr[])
    {   int str, str2, str3, x, a, b, c, d, e = 0, f = 0;
        char temp;
        ptrcourse pNew, pRun, pLast, pTemp;
        
        pNew=malloc(sizeof(struct course));
        
         printf("\tCourse Code     : ");  
         do
         { getchar();       
           gets(pNew->coursecode);
           str = strlen(pNew->coursecode);
        
             if(str != 7)
             printf("\n\tCourse code must be only 7 characters long.  Please try again.\n\t\t\t: ");
             
         }while(str != 7);
         
         printf("\n\tSection(2digits): "); 
          do
        {   
           scanf("%d", &pNew->section);
          
           if(pNew->section < 9 || pNew->section > 99)
           printf("\n\tInvalid section.\n\t\t\t: ");
          
           //e = duplicate(*pFirst,pNew->coursecode,pNew->section);
           
        if(e == 0)
        {   
           if(pNew->section < 9 || pNew->section > 99)
           {
           printf("Enter a valid section with 2 digits");
           scanf("%d",&pNew->section);
           f = 0;
           }
           }  
           
        else if(e == 1)
        {
              do
              {     
                           printf("\t\tSection already exists!.");
                           getchar();     
                           scanf("%d",&pNew->section);
                           if(pNew->section < 9 || pNew->section > 99)
                           printf("Enter another section.");        
                           }while(pNew->section < 9 || pNew->section > 99 && e == 1);
                           
                             
                   
    }
          
          if(pNew->section < 9 || pNew->section > 99)
          f = 1;
       
           }while((pNew->section < 9 || pNew->section > 99) &&( e == 1 && f == 1));
          
         printf("\n\tUnits           : ");
         do
         {
           scanf("%d", &pNew->units);
           x = pNew->units;
             if(x < 0 || x > 3)
             printf("\n\tInvalid input! Please try again.\n\t\t\t: ");
         }while(x < 0 || x > 3);
         
         
         printf("\n\tSchedule(MW/TH) : ");  
         do
         {          
           
           scanf("%s",pNew->schedule);
           getchar();
          a = strcmp(pNew->schedule, "MW");
          b = strcmp(pNew->schedule, "TH");
          c = strcmp(pNew->schedule, "mw");
          d = strcmp(pNew->schedule, "th");
          
          if(a !=0 && b != 0 && c !=0 && d != 0)
             
             printf("\n\tInput should only be MW and TH.\n\t\t\t: ");
             
         }while(a !=0 && b != 0 && c !=0 && d != 0);
         
         printf("\n\tLecturer        : ");
         do
         {
           
           gets(pNew->lecturer);
           str3 = strlen(pNew->lecturer);
         
             if(str3 > 30)
             printf("\tToo many characters.\n\t\t\t: ");
             
         }while(str3 > 30);
    
         printf("\n\tNumber of slots available: ");
         scanf("%d", &pNew->slots);
         
        pTemp = *pFirst;
        *pFirst = pNew;
        
       pNew->pNext=NULL;
          
        
        
         
         if(*pFirst==NULL)
        {              
         *pFirst=pNew;
         
         pNew->pPrev=NULL;
         }
         
         else
         {
         pRun=*pFirst;
         
         while(pRun!=NULL)
                          {
                           
                            
                           pLast=pRun;
                           
                           pRun=pRun->pNext; //error in this part
                           pLast=*pFirst;        
                           }    
                           while(pLast->pNext!=NULL)
                           pLast=pLast->pNext;
                           pLast->pNext=pNew;
                           pNew->pPrev=pLast;
                           
                           if(pRun!=NULL)
                           pNew->pNext=pRun;
             
             
         }
         getchar();
    }
    please help me improve my code
    the program crashes after entering the number of slots
    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    One improvement would be indentation, so people can read it easily (including you).
    Code:
    typedef char string30[30];
    struct course {
        string30 coursecode;
        int section;
        int units;
        char schedule[3];
        string30 lecturer;
        int slots;
        struct getTime *ptrtime;
        struct course *pNext;
        struct course *pPrev;
    };
    
    typedef struct course *ptrcourse;
    void createclass(ptrcourse * pFirst, int ctr[])
    {
        int str, str2, str3, x, a, b, c, d, e = 0, f = 0;
        char temp;
        ptrcourse pNew, pRun, pLast, pTemp;
    
        pNew = malloc(sizeof(struct course));
    
        printf("\tCourse Code     : ");
        do {
            getchar();
            gets(pNew->coursecode);
            str = strlen(pNew->coursecode);
    
            if (str != 7)
                printf("\n\tCourse code must be only 7 characters long.  Please try again.\n\t\t\t: ");
        } while (str != 7);
    
        printf("\n\tSection(2digits): ");
        do {
            scanf("%d", &pNew->section);
            if (pNew->section < 9 || pNew->section > 99)
                printf("\n\tInvalid section.\n\t\t\t: ");
    
            //e = duplicate(*pFirst,pNew->coursecode,pNew->section);
            if (e == 0) {
                if (pNew->section < 9 || pNew->section > 99) {
                    printf("Enter a valid section with 2 digits");
                    scanf("%d", &pNew->section);
                    f = 0;
                }
            }
            else if (e == 1) {
                do {
                    printf("\t\tSection already exists!.");
                    getchar();
                    scanf("%d", &pNew->section);
                    if (pNew->section < 9 || pNew->section > 99)
                        printf("Enter another section.");
                } while (pNew->section < 9 || pNew->section > 99 && e == 1);
            }
    
            if (pNew->section < 9 || pNew->section > 99)
                f = 1;
    
        } while ((pNew->section < 9 || pNew->section > 99)
                 && (e == 1 && f == 1));
    
        printf("\n\tUnits           : ");
        do {
            scanf("%d", &pNew->units);
            x = pNew->units;
            if (x < 0 || x > 3)
                printf("\n\tInvalid input! Please try again.\n\t\t\t: ");
        } while (x < 0 || x > 3);
    
        printf("\n\tSchedule(MW/TH) : ");
        do {
            scanf("%s", pNew->schedule);
            getchar();
            a = strcmp(pNew->schedule, "MW");
            b = strcmp(pNew->schedule, "TH");
            c = strcmp(pNew->schedule, "mw");
            d = strcmp(pNew->schedule, "th");
    
            if (a != 0 && b != 0 && c != 0 && d != 0)
                printf("\n\tInput should only be MW and TH.\n\t\t\t: ");
        } while (a != 0 && b != 0 && c != 0 && d != 0);
    
        printf("\n\tLecturer        : ");
        do {
            gets(pNew->lecturer);
            str3 = strlen(pNew->lecturer);
            if (str3 > 30)
                printf("\tToo many characters.\n\t\t\t: ");
        } while (str3 > 30);
    
        printf("\n\tNumber of slots available: ");
        scanf("%d", &pNew->slots);
    
        pTemp = *pFirst;
        *pFirst = pNew;
    
        pNew->pNext = NULL;
    
        if (*pFirst == NULL) {
            *pFirst = pNew;
            pNew->pPrev = NULL;
        }
        else {
            pRun = *pFirst;
    
            while (pRun != NULL) {
                pLast = pRun;
                pRun = pRun->pNext; //error in this part
                pLast = *pFirst;
            }
    
            while (pLast->pNext != NULL)
                pLast = pLast->pNext;
    
            pLast->pNext = pNew;
            pNew->pPrev = pLast;
    
            if (pRun != NULL)
                pNew->pNext = pRun;
        }
        getchar();
    }
    > pNew = malloc(sizeof(struct course));
    malloc doesn't initialise the memory.
    So chances are, your next/prev pointers are garbage and you never actually find a proper NULL at the end of the list.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If Salem's suggestion doesn't fix it, it would definitely help if we had code that compiled fully, without warnings. It would need a main function and the string30 typedef Salem threw in there.

    Two quick notes about your existing code:
    1. Don't use gets, since it's prone to buffer overflows. Use fgets(buf, sizeof(buf)-1, stdin) instead.
    2. You should watch your order of operations. && has higher precedence, so something like
    Code:
    a || b && c
    evaluates as
    Code:
    a || (b && c)
    when you may want
    Code:
    (a || b) && c
    Use parentheses to be explicit if you need to. You have an ambiguous instance of this in a do-while loop around line 70 of your posted code.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    14
    thanks for helping me
    can someone help me with this code?

    Code:
    void files(ptrcourse *pFirst, ptrstudent *pHead, int ctr[])               
    {
         FILE *pFile;
         ptrcourse pCurrent,pTemp,pNew;
         ptrstudent sCurrent,sTemp;
         string30 temp;
         
         pNew = malloc(sizeof(struct course));
         
         pNew = *pFirst;
         
         pFile = fopen("MP.txt", "rt");
       
         if(pFile == NULL)
          {
                   pFile = fopen("MP.txt", "wt"); 
                   }
         else
         {
           while(!EOF)
           {  
             pCurrent = pNew;
             
              fgets(temp, sizeof(struct course), pFile);
             
              strcpy(pTemp->coursecode,strtok(temp,"\t"));
              strcpy(pTemp->coursecode,strtok(NULL,"\t"));
              pTemp->section = atoi(strtok(temp, "\t"));
              pTemp->section = atoi(strtok(NULL, "\t"));
              pTemp->units = atoi(strtok(temp, "\t"));
              pTemp->units = atoi(strtok(NULL, "\t"));
              strcpy(pTemp->schedule,strtok(temp,"\t"));
              strcpy(pTemp->schedule,strtok(NULL,"\t"));
              pTemp->ptrtime.shr = atoi(strtok(temp, "\t"));
              pTemp->ptrtime.shr = atoi(strtok(NULL, "\t"));
              pTemp->ptrtime.smin = atoi(strtok(temp, "\t"));
              pTemp->ptrtime.smin = atoi(strtok(NULL, "\t"));
              pTemp->ptrtime.ehr = atoi(strtok(temp, "\t"));
              pTemp->ptrtime.ehr = atoi(strtok(NULL, "\t"));
              pTemp->ptrtime.emin = atoi(strtok(temp, "\t"));
              pTemp->ptrtime.emin = atoi(strtok(NULL, "\t"));
              strcpy(pTemp->lecturer,strtok(temp,"\t"));
              strcpy(pTemp->lecturer,strtok(NULL,"\t"));
              pTemp->slots =atoi(strtok(temp, "\t"));
              pTemp->slots =atoi(strtok(NULL, "\t"));
             pTemp->pNext = NULL;
             pCurrent = pNew;
             pCurrent = pCurrent->pNext;
             }; 
             
             }
         
         
         }

    The format of the output text file should be as follows:
    <Course_code_1><space><section1><new line constant ‘\n’>
    <”MW” or “TH”><space><HHMM-HHMM schedule 24-hour format><space><lecturer><new line constant ‘\n’>
    <total slots><space>{<ID_number1>,<ID_number2>,…}<new line constant ‘\n’>
    <Course_code_2><space><section2><new line constant ‘\n’>
    <”MW” or “TH”><space><HHMM-HHMM schedule><space><lecturer><new line constant ‘\n’>
    <total slots><space>{<ID_number1>,<ID_number2>,…}<new line constant ‘\n’>
    . . .
    . . .
    . . .
    <Student_ID_1><space><name_1><space><age1><space>< year level1><new line constant ‘\n’>
    <course_code_1>_<section1><space><course_code2>_<s ection2>…<course_code>_<section><new line constant ‘\n’>
    <Student_ID_2><space><name_2><space><age2><space>< year level2><new line constant ‘\n’>
    <course_code_1>_<section1><space><course_code2>_<s ection2>…<course_code>_<section><new line constant ‘\n’>
    . . .
    . . .
    . . .

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Generally a new question should be in a new thread.

    But nobody can help you until you tell us what's wrong with your code. It would also help us a bunch if we had enough code to make this compile (without errors or warnings). That would mean all struct definitions and typedefs. Attaching a sample data file and providing enough code to actually run this wouldn't hurt either.

    A few things off the bat:
    1. Your while (!EOF) loop is bogus. You're either misusing EOF or, even worse, you redefined it. Besides, never control an loop with EOF: Cprogramming.com FAQ > Why it's bad to use feof() to control a loop.
    2. "rt" and "wt" are not valid parameters to fopen. Perhaps you were thinking of "r+" and "w+" (plus sign).

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

Tags for this Thread