C char string help in linked list and queues program

This is a discussion on C char string help in linked list and queues program within the C Programming forums, part of the General Programming Boards category; I have to put a list of jobs from a text file and put them into 3 queues by job ...

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

    C char string help in linked list and queues program

    I have to put a list of jobs from a text file and put them into 3 queues by job type and in ascending order by arrival time. The only issue is that im not to good with char arrays in C and it driving me nuts that there arent any strings like in C++.



    struct Queue{

    Queue *next;
    Queue *previous;
    Queue *head_ptr;
    char job_Name[15];
    unsigned int arrival_Time;
    unsigned int deadLine;
    unsigned int jobLength;
    char job_Type[3];


    };

    void insertAscending(Queue *queue,char jN[10], int aT,int jL, int dL, char joT[3]){

    // Reserve space for new node and fill it with data
    Queue *temp = new Queue;
    Queue *awesome = queue->head_ptr;
    temp->head_ptr = queue->head_ptr;
    temp->job_Name[10] = *jN; -------------------------HERE IS WHAT I NEED TO BE REVIEWED
    temp->arrival_Time = aT;
    temp->jobLength = jL;
    temp->deadLine = dL;
    temp->job_Type[3] = *joT;-------------------------HERE IS WHAT I NEED TO BE REVIEWED
    temp->next = NULL;
    printf("%i %t" , jL, " ");


    Queue *temp1, *temp2;

    if (temp->head_ptr == NULL) { // no intem
    queue->head_ptr = temp;


    } else if (awesome->next == NULL) { // one item
    if (awesome->arrival_Time < temp->arrival_Time) {

    insertLast(temp);

    }else {

    insertFirst(temp);

    }
    } else {
    // two or more
    temp1 = queue->head_ptr;
    temp2 = queue->head_ptr;

    while (temp1->arrival_Time < aT && temp1->next != NULL){
    temp2 = temp1;
    temp1 = temp1->next;
    }

    if (temp1 == queue->head_ptr && temp2 == queue->head_ptr){
    insertFirst(temp); // first node
    } else if (temp1->arrival_Time < aT || temp2->arrival_Time < aT ){
    insertLast(temp); // last node

    } else { // middle node
    insert(temp2, temp2->arrival_Time);
    }

    }

    }

    Queue *printlist(Queue *s){

    Queue *temp = s->head_ptr;

    if (temp == NULL) {

    printf("\nThe List is empty\n");

    } else {

    printf("The items in the Queue:\n");
    do {

    printf("%s %t" , temp->job_Name, " " );

    printf("%i %t" , temp->arrival_Time , " ");

    printf("%i %t" , temp->jobLength , " ");

    printf("%i %t" , temp->deadLine , " ");

    printf("%s %t" , temp->job_Type , " ");
    printf("\n");

    // Move to next node (if present)
    temp = temp->next;

    } while (temp != NULL);

    printf("\n");
    }
    }

    int main(){

    Queue *TXT = new Queue;
    Queue *MM = new Queue;
    Queue *FP = new Queue;


    TXT->head_ptr = NULL;
    MM->head_ptr = NULL;
    FP->head_ptr = NULL;
    FILE *fp;

    fp=fopen("input_2011.txt", "r");

    char jN[5]; -------------------------HERE IS WHAT I NEED TO BE REVIEWED
    int aT = 0;
    int dL;
    int jL;
    char jT[3];-------------------------HERE IS WHAT I NEED TO BE REVIEWED

    for(int eof = 0; eof < 35; eof++){

    fscanf (fp, "%s %i %i %i %s", &jN, &aT, &jL, &dL, &jT);




    if(strcmp(jT , "TXT") == 0 ){

    insertAscending(TXT, jN , aT, jL, dL,jT);

    }
    else if(strcmp(jT , "MM")== 0){
    insertAscending(MM, jN , aT,jL, dL,jT);
    }
    else {
    insertAscending(FP, jN , aT,jL, dL,jT);
    }

    }
    //fclose (fp);

    printlist(TXT);
    printlist(MM);
    printlist(FP);
    return 0;

    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,512
    Read the forum guidelines and this: << !! Posting Code? Read this First !! >>. Make sure to use code tags and proper indentation when you post code, so we can actually read it.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    ok

    Code:
    struct Queue{
    		
    		Queue *next;
    		Queue *previous;
    		Queue *head_ptr;	
    		char job_Name[15];   //all char not sure where the problem is at
    		unsigned int arrival_Time;
    		unsigned int deadLine;
    		unsigned int jobLength;
    		char job_Type[3];    ///this line too
    	
    		
    };	
    
     void insertAscending(Queue *queue,char jN[10], int aT,int jL, int dL, char joT[3]){
    
    	// Reserve space for new node and fill it with data
        Queue *temp = new Queue;
    	Queue *awesome = queue->head_ptr;
    	temp->head_ptr = queue->head_ptr;
    	temp->job_Name[10] = *jN;    //this line 
    	temp->arrival_Time = aT;
    	temp->jobLength = jL;
    	temp->deadLine = dL;
    	temp->job_Type[3] = *joT;    //this line
            temp->next = NULL;
    			printf("%i %t" , jL, " ");
    		
    		
    	Queue *temp1, *temp2;
    
    	if (temp->head_ptr == NULL) {					// no item
    		queue->head_ptr = temp; 
    			
    		
    	} else if (awesome->next == NULL) {	// one item
    		if (awesome->arrival_Time < temp->arrival_Time) {
    			
    			insertLast(temp);
    			
    		}else {
    		
    			insertFirst(temp);
    			
    		}
    	} else {
    											// two or more
    		temp1 = queue->head_ptr;
    		temp2 = queue->head_ptr;
    
    		while (temp1->arrival_Time < aT && temp1->next != NULL){
    			temp2 = temp1;
    			temp1 = temp1->next;
    		}
    
    		if (temp1 == queue->head_ptr && temp2 == queue->head_ptr){ 
    						insertFirst(temp);	// first node
    		} else if (temp1->arrival_Time < aT || temp2->arrival_Time < aT ){ 
    						insertLast(temp);					// last node
    			
    		} else {							// middle node
    			insert(temp2, temp2->arrival_Time);
    		}
    		
    	}
    
    }
    
    Queue *printlist(Queue *s){
    
    	 Queue *temp = s->head_ptr;
    	
    	if (temp == NULL) {
    
    		printf("\nThe List is empty\n");
    
    	} else {
    
    		printf("The items in the Queue:\n");
    		do {	
              
    			printf("%s %t" , temp->job_Name, " " ); //print job name
    			
    			printf("%i %t" , temp->arrival_Time , "  ");
    		
    			printf("%i %t" , temp->jobLength , " ");
    			
    			printf("%i %t" , temp->deadLine , " ");
    			
    			printf("%s %t" , temp->job_Type , " ");  //print job type
    			printf("\n");
    
    			// Move to next node (if present)
    			temp = temp->next;
    
    		} while (temp != NULL);
    
    		printf("\n");
    	}
    }
    
    int main(){
    
    Queue *TXT = new Queue;
    Queue *MM  = new Queue;
    Queue *FP  = new Queue;
    
    
    TXT->head_ptr = NULL;
    MM->head_ptr = NULL;
    FP->head_ptr = NULL;
    FILE *fp;
    //open file
    fp=fopen("input_2011.txt", "r");
    
    char jN[5];  //this line
    int aT = 0;
    int dL;
    int jL;
    char jT[3];  //this line
    
       for(int eof = 0; eof < 35; eof++){
                  //scan text file to get variables
                fscanf (fp, "%s %i %i %i %s", &jN, &aT, &jL,  &dL, &jT);
    
    
    //insert into the proper queue
               if(strcmp(jT , "TXT") == 0 ){
    
                        insertAscending(TXT, jN , aT, jL, dL,jT);
    
    }
                 else if(strcmp(jT , "MM")== 0){
                     
                        insertAscending(MM, jN , aT,jL, dL,jT);
    }
                  else {
                     
                         insertAscending(FP, jN , aT,jL, dL,jT);
    }
    
    }
    //fclose (fp);
    
    printlist(TXT);
    printlist(MM);
    printlist(FP);
    return 0;
    
    }

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,512
    Okay, first, you can't assign strings using = in C. You have to use a library function like strdup. Second, you declare jN as an array of size 5 in main, then in insertAscending, you say it's of size 10, and job_Name is of size 15. Make up your mind, pick one size and stick to it. The way to copy it in is something like:
    Code:
    strcpy(temp->job_Name, jN);
    Now, bust open some text books and find some tutorials on strings in C. We have one here, and Google has a bazillion. Read through them, and work all the example problems until you have it down.

    EDIT: Just noticed you have some funky printf statements in printlist. %t is not a format specifier. If you want a tab character, use \t.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 05-04-2009, 07:54 PM
  2. Fill a linked list char by char
    By w2look in forum C Programming
    Replies: 6
    Last Post: 03-26-2009, 03:07 PM
  3. String overwrites every string in linked list
    By SkidMarkz in forum C Programming
    Replies: 1
    Last Post: 09-11-2008, 08:40 PM
  4. linked list with char array HELP!
    By littlepiggy in forum C++ Programming
    Replies: 4
    Last Post: 10-18-2005, 02:45 PM
  5. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM

Tags for this Thread


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