Thread: Queue Help

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    11

    Queue Help

    I am having some trouble with my program. I am trying to print out my queue but its just printing garbage values. Hopefully can maybe take a few minutes to go over the code and see what is going wrong. I know the actual program really doesn't make sense but this is the way I need to do it. Thanks
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    typedef struct {
        char *name;   
    	int number;
        } SomeSeq;
    
    typedef struct QueueNodeTag{
    	SomeSeq *item;
    	struct QueueNodeTag *link;
    }QueueNode;
    
    typedef struct{
    	QueueNode *front;
    	QueueNode *rear;
    }Queue;
    
    //function prototypes
    void getbuf(Queue **Q,QueueNode **Temp);
    int GetNumberOfSomeSeqsToGenerate();
    SomeSeq * GetNextSomeSeq(void);
    void Print(void);
    
    int main()
    {
    	/* initialize random seed: */
    	srand ( time(NULL) );
    
    	Print();
    	      
    }
    
    void  getbuf(Queue **Q,QueueNode **Temp)
    {
    		
    		int n = GetNumberOfSomeSeqsToGenerate();
    		//creates as many SomeSeqs as user requests and 
    		//adds them to the list
    		for(int i =0;i<n;i++)
    		{
    			(*Temp) = (QueueNode*)malloc(sizeof(QueueNode));
    
    			if ((*Temp) == NULL)
    			{
    			return;// (char*) buff;
    		 
    			}else{
    			(*Temp)->item = GetNextSomeSeq();
    			(*Temp)->link = NULL;
    			if ((*Q)->rear == NULL){
    				(*Q)->front = (*Temp);
    				(*Q)->rear = (*Temp);
    			}else{
    				(*Q)->rear->link = (*Temp);
    				(*Q)->rear = (*Temp);
    			}
    		}
    		
    			
    	}
    	 
    	
    }
    
    //
    int GetNumberOfSomeSeqsToGenerate()
    {
    	 int n = 0;
    	printf("GetNumberOfSomeSeqsToGenerate():");
    	scanf("%d",&n);
    	 
    	return n;
    }
    
    
    
     // Return the next structure (to fill the 
         //                          abovementioned list with).
          
        SomeSeq * GetNextSomeSeq()
    	  {
    		SomeSeq p;
    		int names;
    		 
    		 
    		p.number = rand() % 100 + 1;
    	    names  =   rand() % 10 + 1 ;
    
    		  switch(names)
    		  {
    		  case 1:
    			  p.name="John";
    			  break;
    		  case 2:
    			  p.name="Mike";
    			  break;
    		  case 3:
    			  p.name="Sally";
    			  break;
    		  case 4:
    			  p.name="Samus";
    			  break;
    		  case 5:
    			  p.name ="Epona";
    			  break;
    		  case 6:
    			  p.name = "JJ";
    			  break;
    		  case 7:
    			  p.name = "Matt";
    			  break;
    		  case 8:
    			  p.name = "Sandy";
    			  break;
    		  case 9:
    			  p.name="Ken";
    			  break;
    		  case 10:
    			  p.name="Todd";
    			  break;
    		  }
    		  return &p ;
    	  }
      
    	  	 
    void Print()
    {
    		Queue *Q;
    		Q =(Queue*)malloc(sizeof(Queue));
     		Q->front=NULL;
    		Q->rear=NULL;
    		QueueNode *Temp;
    		Temp = NULL;
    		getbuf(&Q,&Temp);
    	
    	   while(Q->front->link != NULL)
    	  {
    	  printf("%d  %s\n",Q->front->item->number,Q->front->item->name);
    	 //Its printing garbage values here
    	  Q->front = Q->front->link;
    	  }
    		  free(Temp);
    		  free(Q);
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your function GetNextSomeSeq() is returning the address of a local variable. When the function returns, the local variable is destroyed, yet you are returning the address of that destroyed variable. You have a couple options to fix this:

    1. Pass a pointer to a SomeSeq object to the GetNextSomeSeq() function, and fill that passed variable with the values (best solution).

    2. Instead of creating a local variable, call malloc() in the GetNextSomeSeq() function to create the variable on the heap. Make sure that you free() this memory at some point though.

    3. Declare the SomeSeq variable in GetNextSomeSeq() as static. (Not that great of a solution).

    4. Declare the SomeSeq variable as global. (Bad solution).
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    If I used your option #2, would the variable have to be a pointer?

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Yes, it would.
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    Great thanks for your help. I just used your option#2 and it seems to be working. I appreciate your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM