Displaying Contents of a Queue in C

Having difficulty displaying the contents of my queue in C

Page 1 of 1

2 Replies - 1400 Views - Last Post: 29 March 2010 - 04:46 AM Rate Topic: -----

#1 TonyShaker  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 28-March 10

Displaying Contents of a Queue in C

Posted 28 March 2010 - 07:11 PM

I believe that my program is converting the correct result. and storing it inside of the dynamic queue; I cannot display its result. I think it is due to the data being stored inside of the queue are both int and char types.

I am using a char *cPtr to read each character from the string ar[ ] at a time. The numbers( '0' through '9' ) are being converted into an int, and then pushed onto the queue. Can you help me with displaying the correct result? Thank you.

char ar[ ] = "(	47 + 5	) / (385 - 42 )	";//Postfix conversion answer:47 5 + 385 42 - /

typedef struct stackNode {
	int top;
	struct stackNode *next; 
}stackNode;
 
 
void PrintQueStack( QueueNodePtr ptr, QueueNodePtr tailPtr )
{
 	int count = 0;	
 	while ( ptr != NULL ) 
	{
		if(!isdigit(ptr->top) ) 
		{
			printf("\nNODE number %d with stored data number is %d", ++count, ptr->top);
		}
		else
		{
			printf("\nNODE number %d with stored data number is %c", ++count, ptr->top);
		}
 
		ptr = ptr->next;//sets the pointer ptr to the next node of type NODE
	}
}
 





Is This A Good Question/Topic? 0
  • +

Replies To: Displaying Contents of a Queue in C

#2 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Displaying Contents of a Queue in C

Posted 28 March 2010 - 07:19 PM

Quote

I think it is due to the data being stored inside of the queue are both int and char types.


Yep, that's not going to work. You could use a union to store the data but that could get really nasty.

Are you coding an infix to postfix converter? If so, use two queues/stacks. One for the numbers and one for the operators.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5937
  • View blog
  • Posts: 12,862
  • Joined: 16-October 07

Re: Displaying Contents of a Queue in C

Posted 29 March 2010 - 04:46 AM

Can't really tell from the code. However, obfuscating the pointer (QueueNodePtr) is always a poor idea.

I'd make another struct to ease you pain. e.g.

typedef struct QueueNodeStruct {
	int value;
	struct QueueNodeStruct *next; 
} QueueNode;

typedef struct {
	QueueNode *head, *tail;
} Queue;


 
void PrintQueStack( Queue *q ) {
	QueueNode *ptr = q->head;
 	int count = 0;
 	while ( ptr != NULL ) {
		printf("\nNODE number %d with stored data ", ++count);
		if(!isdigit(ptr->data) ) {
			printf("number is %d", ptr->data);
		} else {
			printf("char is %c", ptr->data);
		}
 		ptr = ptr->next;
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1