Doubly-Linked List and structures

This is a discussion on Doubly-Linked List and structures within the C Programming forums, part of the General Programming Boards category; Hi, any help would be greatly appreciated. I apologize that it's a bit sloppy. I'm having trouble understanding exactly why ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    2

    Doubly-Linked List and structures

    Hi, any help would be greatly appreciated. I apologize that it's a bit sloppy. I'm having trouble understanding exactly why PS35, the last struct jobQueue processName is continually repeating when attempting to output entire queue. Is anyone able to understand what's going on?

    Code:
    File Input:
    
    PS1    90001    100    90500    100    
    PS2    90010     50    90400    0     
    PS3    90011     40    90520    0    
    PS4    90050    300    91000    100    
    PS5    90065    150    90765    100    
    PS6    90070    500    90170    200    
    PS7    90080    100    91100    100    
    PS8    90090    150    90690    200    
    PS9    90167    130    91500    0    
    PS10    90170    500    93500    100    
    PS11    90172    50    90900    0    
    PS12    90180    105    92190    0    
    PS13    90190    99    93200    0    
    PS14    90195    475    91300    100    
    PS15    90200    100    91900    0    
    PS16    91000    130    93100    100    
    PS17    91001    150    93200    0    
    PS18    91002    350    93800    200    
    PS19    91050    399    93999    200    
    PS20    91100    300    94000    100    
    PS21    91110    100    91900    200    
    PS22    91215    500    92125    200    
    PS23    91235    150    92500    200    
    PS24    91240    300    92600    0    
    PS25    91320    500    93700    0    
    PS26    91350    700    94500    100    
    PS27    91630    170    92630    200    
    PS28    91999    119    93999    100    
    PS29    92001    240    94001    0    
    PS30    92019    191    94900    0    
    PS31    92201    110    94950    0        
    PS32    92299    299    95000    100    
    PS33    92301    199    95010    0    
    PS34    92319    100    95700    200    
    PS35    93001    200    96000    100
    Code:
    File Output:
    Name:  Screenshot-Neal@localhost_~-a_semProject.jpg
    Views: 180
    Size:  109.1 KB
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
        struct dllist{    
        char *processName;            // Length = 10
        unsigned int arrivalTime;    // When it processor in Ready Queue
        unsigned int jobLength;        // Burst Time
        unsigned int deadline;        // Processing Limit
        unsigned int ioTime;        // Time for IO = 200 x I/O Request Time
        struct dllist *next;
        struct dllist *prev;
    };
        struct dllist *head, *tail;
        void append_node(struct dllist *lnode);
        void insert_node(struct dllist *lnode, struct dllist *after);
        void remove_node(struct dllist *lnode);
        void import_file(char *);
    
    int main()
    {
    	import_file("testFile.txt");
    	return 0;
    }
    
    
    void import_file(char *fileName){
        struct dllist *jobQueue;
        FILE *fp;
        char buf[100];
        
        
        fp = fopen(fileName,"r");
        if(!fp)
        {
            printf("%s\n","Input_2012.txt: File not found");
        }
    
    
        while(fgets(buf,100,fp)!=NULL)        // Gets from file each 100 bit line.
        {                
            char *pch;                        // Stores current token
            char *temp_pch;
            unsigned int temp_data;
            jobQueue = (struct dllist *)malloc(sizeof(struct dllist));
            
            pch = strtok(buf," ,.-\t");
            temp_pch = pch;
            jobQueue->processName = temp_pch;
    
    
            pch = strtok(NULL," ,.-\t");
            temp_pch = pch;
            temp_data = (unsigned int) atoi(temp_pch);
            jobQueue->arrivalTime = temp_data;
             
            
            pch = strtok(NULL," ,.-\t");
            temp_pch = pch;
            temp_data = (unsigned int) atoi(temp_pch);
            jobQueue->jobLength = temp_data;
    
            pch = strtok(NULL," ,.-\t");
            temp_pch = pch;
            temp_data = (unsigned int) atoi(temp_pch);
            jobQueue->deadline = temp_data;
        
            pch = strtok(NULL," ,.-\t");
            temp_pch = pch;
            temp_data = (unsigned int) atoi(temp_pch);
            jobQueue->ioTime = temp_data;
            append_node(jobQueue);
        }
        
        fclose(fp);
            struct dllist *jq;    
         for(jq = head; jq != NULL; jq = jq->next) {
            printf("%s ", jq->processName);
            printf("%d ", jq->arrivalTime);
            printf("%d ", jq->jobLength);
            printf("%d ", jq->deadline);
            printf("%d\n", jq->ioTime);
         }
    }
    
    
    
    
    
    
    void append_node(struct dllist *lnode) 
    {
         if(head == NULL)
         {
              head = lnode;
             lnode->prev = NULL;
         } 
         else 
         {
              tail->next = lnode;
            lnode->prev = tail;
         }
             tail = lnode;
             lnode->next = NULL;
    }
         
    void insert_node(struct dllist *lnode, struct dllist *after) 
    {
         lnode->next = after->next;
         lnode->prev = after;
         
         if(after->next != NULL)
              after->next->prev = lnode;
         else
              tail = lnode;
         
         after->next = lnode;
    }
         
    void remove_node(struct dllist *lnode) 
    {
         if(lnode->prev == NULL)
              head = lnode->next;
         else
              lnode->prev->next = lnode->next;
         
         if(lnode->next == NULL)
              tail = lnode->prev;
         else
              lnode->next->prev = lnode->prev;
    }
    Last edited by Socrates271; 04-30-2012 at 09:16 PM.

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    strtok() does not return copies of strings to you. Rather, it returns pointers into the buffer that you passed to it. Since you're using the same buffer each iteration, your first call to strtok() each iteration will wind up returning a pointer to the same location (that is, to the beginning of that buffer). As a result, all processName entries point to the same location, which, after the last iteration, contains "PS35".

    You can either malloc() space for processName and copy the token into it, or make processName an array and copy the token into it.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    2
    Quote Originally Posted by cas View Post
    strtok() does not return copies of strings to you. Rather, it returns pointers into the buffer that you passed to it. Since you're using the same buffer each iteration, your first call to strtok() each iteration will wind up returning a pointer to the same location (that is, to the beginning of that buffer). As a result, all processName entries point to the same location, which, after the last iteration, contains "PS35".

    You can either malloc() space for processName and copy the token into it, or make processName an array and copy the token into it.
    Thank you, that answered it perfectly!

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    #1
    Code:
    void import_file(char *);
    A nit but this should probably be const char * given how you are using it.





    #2
    Code:
    int main()
    {
        import_file("testFile.txt");
        return 0;
    }
    
    void import_file(char *fileName){
        struct dllist *jobQueue;
        FILE *fp;
        char buf[100];
         
         
        fp = fopen(fileName,"r");
        if(!fp)
        {
            printf("%s\n","Input_2012.txt: File not found");
        }
        ...
    File name you are passing in is "testFile.txt" so why would you output "Input_2012.txt: File not found"? I'd probably make that:
    Code:
    printf("%s: File not found\n",fileName);
    So that whatever file you passed would get correctly reported as unable to be opened.





    #3 Also, if you fail to open the file your current code keeps on trying to process data.




    #4 You should avoid the use of globals as much as possible. Your code works now because as globals, the head pointer is initialized to 0 for you. If you were to make some of these variables local instead - as you should - you'd have to remember to explicitly initialize them.





    #5
    Code:
    jobQueue = (struct dllist *)malloc(sizeof(struct dllist));
    You should avoid the typecasting of the return result from a malloc call (at least in C, in C++ you'd have to typecast or otherwise you'd be using new instead). The void* result returned by the malloc call can be safely assigned without the need for a typecast. Also (another nit) from a maintenance perspective it's usually better to write sizeof(*jobQueue) instead of sizeof(struct dllist) as if you change the struct name for whatever reason you then do not have to hunt down such lines of code throughout the program and remember to use the new name.





    #6 You don't appear to have any cleanup code to take care of all that memory you've allocated... where are the calls to free?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doubly linked list
    By msky in forum C Programming
    Replies: 0
    Last Post: 11-11-2011, 04:25 PM
  2. doubly linked list
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 07-21-2009, 10:32 AM
  3. doubly linked list
    By bazzano in forum C Programming
    Replies: 5
    Last Post: 04-26-2007, 04:41 AM
  4. doubly linked list
    By bahareh in forum C++ Programming
    Replies: 7
    Last Post: 03-28-2007, 02:31 PM
  5. singly linked list to doubly linked list
    By t48j in forum C Programming
    Replies: 3
    Last Post: 03-23-2005, 06:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21