Computing.Net > Forums > Programming > Problem on Queues in C

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Problem on Queues in C

Reply to Message Icon

Name: gunzem
Date: March 27, 2009 at 22:04:40 Pacific
OS: Windows XP
CPU/Ram: 1.73MHz
Subcategory: C/C++
Comment:

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;
}




Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: March 28, 2009 at 03:28:09 Pacific
Reply:

You can't assign a string to a number.

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


0

Response Number 2
Name: gunzem
Date: March 28, 2009 at 12:30:40 Pacific
Reply:

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;
}


0

Response Number 3
Name: Razor2.3
Date: March 29, 2009 at 05:32:35 Pacific
Reply:

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

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


0

Response Number 4
Name: gunzem
Date: March 29, 2009 at 14:26:54 Pacific
Reply:

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?


0

Response Number 5
Name: Razor2.3
Date: March 30, 2009 at 10:58:18 Pacific
Reply:

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.


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Problem on Queues in C

Problem reading file in c++ www.computing.net/answers/programming/problem-reading-file-in-c/7826.html

Classes in C++ www.computing.net/answers/programming/classes-in-c/11619.html

Problem creating graphics in C++ www.computing.net/answers/programming/problem-creating-graphics-in-c/7169.html