Problem on Queues in C

March 27, 2009 at 22:04:40
Specs: Windows XP, 1.73MHz
This program takes integers as input from the user. It has to enter these integers into a queue and then delete the negative integers entered if any and print out the queue this time without the negative integers.

Program does compile but It doesnt terminate once I enter EOF to stop entering numbers.What should i do??

CODE:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "queues.h"

int main(void)
{
int *number;
int count;

//create two queues
QUEUE* Q1;
QUEUE* Q2;

Q1 = createQueue();
Q2 = createQueue();

while((int) number!= EOF)
{
number = (int*)malloc(sizeof(int));
printf("Enter a number or EOF to exit: ");
scanf("%d", number);

enqueue(Q1, number); //enqueue into Q1.

}

count= queueCount(Q1); //hold the count of the queue

while (count > 0)
{
dequeue(Q1, (void**)& number); //dequeue each integer from the queue

if(number >= 0) //checks if it a +ve int
{
enqueue(Q2, number); //if number is +ve then enqueue it in Q2
}
count = count - 1; //decrement the count

printf("count of Q1 is %d\n", count);

}
printf("Integers in Q2 are %d\n", * number); // print only +ve integers from the new queue

return 0;
}



See More: Problem on Queues in C

Report •


#1
March 28, 2009 at 03:28:09
You can't assign a string to a number.

You can, however, use gets(), check for the EOF, then call atoi()


Report •

#2
March 28, 2009 at 12:30:40
I modified the code. It now enters integers in a queue, and the while loop terminates when I use Ctrl D to print the queue.

The problem is that this program has a if loop that doesnt seem to operate. If loop checks for each number that is dequeued from the old queue. If the number is a +ve integer it is entered in d new queue if not then it is just dequeued from the old queue.

Can you please show me how to solve this issue??

MODIFIED CODE
-----------------------------
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
#include<string.h>
#include "queues.h"

int main(void)
{
bool done = false;
int *number;
int count;

//create two queues
QUEUE* Q1;
QUEUE* Q2;

Q1 = createQueue();
Q2 = createQueue();

emptyQueue(Q1);
emptyQueue(Q2);

while(!done)
{
number= (int*)malloc(sizeof(int));
printf("Enter a number or <EOF> to exit: ");

if((scanf("%d", number)) == EOF )
done = true;

else
enqueue(Q1, number); //enqueue into Q1.

}

printf("\n\nContents of queue Q2 are now:\n");
count= queueCount(Q1); //will hold the count of the queue

while (count > 0)
{
dequeue(Q1, (void**)&number); //dequeue each integer from the queue

if(number >= 0 ) //checks if it a positive integer
{
enqueue(Q2, number); //if number is +ve then enqueue it in the new queue
}

count = count - 1; //decrement the count

printf("%d\t", *number);
free(number);
}

printf("\n");

destroyQueue(Q1);
destroyQueue(Q2);
return 0;
}


Report •

#3
March 29, 2009 at 05:32:35
if(number >= 0) //checks if it a +ve int

Should be
if(*number >= 0) //checks if it a +ve int


Report •

Related Solutions

#4
March 29, 2009 at 14:26:54
I changed my code, but it still makes no difference. If negative integers are entered by the user, they should be deleted but that doesnt happen.cant figure out why?

Report •

#5
March 30, 2009 at 10:58:18
Question: When do you ever go though Q2? You go though Q1, sure, but not Q2. Also, you probably shouldn't be free()ing number.

Report •


Ask Question