ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.5 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://cplusplus.com/forum/beginner/30696/ |
| Last Crawled | 2026-03-23 14:11:38 (14 days ago) |
| First Indexed | 2024-11-18 03:00:35 (1 year ago) |
| HTTP Status Code | 200 |
| Meta Title | Queues, adding and deleting elements - C++ Forum |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | Forum
Beginners
Queues, adding and deleting elements
Queues, adding and deleting elements
Hello all,
I have a quiz regarding queues tomorrow and am still questions the notation for a given addElement & deleteElement function. I understand the concept behind queues but am stuck on how to turn it into code. For example, in order to add an element to a given queue would you write a function something like this?:
addElement (int & rear, int jeffsQ[], int newEl)
{
if (!fullStack())
{ rear ++; //this is where confusion arises
Q[rear] = newEl;
}
else
cout << "The stack is full" << endl;
}
I know the syntax might not be correct but I am confused on whether or not I just need to increase the rear by 1 or to use the modular formula (because of the specific problem of the front being the same as the rear in some cases) which looks like this..
rear = (rear+1) % maxQueueSize;
The computer science-y definition of a queue is a FIFO. Implemented as an array, you need two "pointers": one
that "points" to the first used index and one that "points" to the last used index. This, with just a little bit
of thought, should give you the answer.
If not, consider a simple example. Suppose the array has 5 elements. What should the queue look like after
each of the following steps:
add element (value = 42)
add element (value = 999)
add element (value = 36)
remove element [value returned is 42]
add element (value = 64)
add element (value = 100)
add element (value = 123456789)
That makes much more sense. Thank you. However, what should I code in order to make a boolean function that returns whether the queue is full. Would it be?:
bool fullQueue(int &front, int &front)
{
return rear == front - 1;
}
What is rear and front set to when the queue is empty?
Starting with those values, if you run the above steps I gave you with pencil and paper, you end up with the queue being
full. What are the values of rear and front at that time?
Oh I see, my teacher had us initialize the front and rear to -1 for some reason. Is that a problem or should I set them to 0 each?
It's not a problem.
So, if rear == front == -1 (not C++ expression!), then the queue is empty.
After the first line above (add element), what will rear and front be set to?
Topic archived. No new replies allowed. |
| Markdown | [cplusplus .com](https://cplusplus.com/)
- [TUTORIALS](https://cplusplus.com/doc/)
- [REFERENCE](https://cplusplus.com/reference/)
- [ARTICLES](https://cplusplus.com/articles/)
- [FORUM](https://cplusplus.com/forum/)
### **[C++](https://cplusplus.com/)**
- [Tutorials](https://cplusplus.com/doc/)
- [Reference](https://cplusplus.com/reference/)
- [Articles](https://cplusplus.com/articles/)
- [Forum](https://cplusplus.com/forum/)
### **[Forum](https://cplusplus.com/forum/)**
- [**Beginners**](https://cplusplus.com/forum/beginner/)
- [**Windows Programming**](https://cplusplus.com/forum/windows/)
- [**UNIX/Linux Programming**](https://cplusplus.com/forum/unices/)
- [**General C++ Programming**](https://cplusplus.com/forum/general/)
- [**Lounge**](https://cplusplus.com/forum/lounge/)
- [**Jobs**](https://cplusplus.com/forum/jobs/)
- [Forum](https://cplusplus.com/forum/)
- [Beginners](https://cplusplus.com/forum/beginner/)
- Queues, adding and deleting elements
### Queues, adding and deleting elements
[](https://cplusplus.com/forum/beginner/30696/#msg166334 "Link to this post")
Oct 28, 2010 at 9:14pm UTC
[**jheffer44** (4)](https://cplusplus.com/user/jheffer44/)
Hello all,
I have a quiz regarding queues tomorrow and am still questions the notation for a given addElement & deleteElement function. I understand the concept behind queues but am stuck on how to turn it into code. For example, in order to add an element to a given queue would you write a function something like this?:
addElement (int & rear, int jeffsQ\[\], int newEl)
{
if (!fullStack())
{ rear ++; //this is where confusion arises
Q\[rear\] = newEl;
}
else
cout \<\< "The stack is full" \<\< endl;
}
I know the syntax might not be correct but I am confused on whether or not I just need to increase the rear by 1 or to use the modular formula (because of the specific problem of the front being the same as the rear in some cases) which looks like this..
rear = (rear+1) % maxQueueSize;
[](https://cplusplus.com/forum/beginner/30696/#msg166359 "Link to this post")
Oct 28, 2010 at 10:17pm UTC
[**jsmith** (5804)](https://cplusplus.com/user/jsmith/)
The computer science-y definition of a queue is a FIFO. Implemented as an array, you need two "pointers": one
that "points" to the first used index and one that "points" to the last used index. This, with just a little bit
of thought, should give you the answer.
If not, consider a simple example. Suppose the array has 5 elements. What should the queue look like after
each of the following steps:
add element (value = 42)
add element (value = 999)
add element (value = 36)
remove element \[value returned is 42\]
add element (value = 64)
add element (value = 100)
add element (value = 123456789)
[](https://cplusplus.com/forum/beginner/30696/#msg166370 "Link to this post")
Oct 28, 2010 at 10:55pm UTC
[**jheffer44** (4)](https://cplusplus.com/user/jheffer44/)
That makes much more sense. Thank you. However, what should I code in order to make a boolean function that returns whether the queue is full. Would it be?:
bool fullQueue(int \&front, int \&front)
{
return rear == front - 1;
}
[](https://cplusplus.com/forum/beginner/30696/#msg166374 "Link to this post")
Oct 28, 2010 at 11:19pm UTC
[**jsmith** (5804)](https://cplusplus.com/user/jsmith/)
What is rear and front set to when the queue is empty?
Starting with those values, if you run the above steps I gave you with pencil and paper, you end up with the queue being
full. What are the values of rear and front at that time?
[](https://cplusplus.com/forum/beginner/30696/#msg166384 "Link to this post")
Oct 29, 2010 at 12:30am UTC
[**jheffer44** (4)](https://cplusplus.com/user/jheffer44/)
Oh I see, my teacher had us initialize the front and rear to -1 for some reason. Is that a problem or should I set them to 0 each?
[](https://cplusplus.com/forum/beginner/30696/#msg166493 "Link to this post")
Oct 29, 2010 at 1:48pm UTC
[**jsmith** (5804)](https://cplusplus.com/user/jsmith/)
It's not a problem.
So, if rear == front == -1 (not C++ expression!), then the queue is empty.
After the first line above (add element), what will rear and front be set to?
Topic archived. No new replies allowed.
[Home page](https://cplusplus.com/) \| [Privacy policy](https://cplusplus.com/privacy.do)
© cplusplus.com, 2000-2025 - All rights reserved - *v3.3.3*
[Spotted an error? contact us](https://cplusplus.com/contact.do?referrer=cplusplus.com%2Fforum%2Fbeginner%2F30696%2F) |
| Readable Markdown | null |
| Shard | 14 (laksa) |
| Root Hash | 7671136614093794214 |
| Unparsed URL | com,cplusplus!/forum/beginner/30696/ s443 |