Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8431739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:56:12+00:00 2026-06-10T05:56:12+00:00

I am reading about FIFO queue array implementeation in Algorithms in C++ by Robert

  • 0

I am reading about FIFO queue array implementeation in Algorithms in C++ by Robert Sedwick.. The contents of the queue are all the elements in the array between “head” and “tail”, taking into account the wraparound back to 0 when the end of the array is encountered. If “head” and “tail” are equal, then we consider the queue to be empty; but if “put” would make then equal, then we consider it to be full. We are making the size of the array 1 greater than the maximum number of elements that the client expects to see in the queue so that we could augument this program to make error checks.

template <class Item>
class QUEUE
  {
    private:
      Item *q; int N, head, tail;
    public:
      QUEUE(int maxN)
        { q = new Item[maxN+1]; 
          N = maxN+1; head = N; tail = 0; }
      int empty() const
        { return head % N == tail; }
      void put(Item item)
        { q[tail++] = item; tail = tail % N; }
      Item get()
        { head = head % N; return q[head++]; }
  };

My question why author as mentioned in text allocating array 1 greater than user specified for making error checks. I am not getting how allocating 1 greater than user request will help us in error checking? Please help me with sample code.

Thanks for your time and help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:56:13+00:00Added an answer on June 10, 2026 at 5:56 am

    I think that, without the extra cell, there would be no way to distinguish between a queue with maxN elements and an empty queue, since head and tail would be in the same equivalence class modulo maxN in both cases.

    So, I guess error handling could be done right after a put, to check that the empty criterion is not fulfilled (which means capacity was exceeded):

    void put(Item item)
    {
      q[tail++] = item;
      tail = tail % N;
      // check that capacity is not exceeded.
      if (head % N == tail)
        throw;
    }
    

    and likewise at the beginning of get, to check that the queue is not empty.

    Does it make sense?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading about queues in robert sedwick book on algorithms When the items
After reading about the problem of passing empty std::string objects between DLLs and EXEs,
After reading about the differences between appSettings and applicationSettings in a web.config file, I'd
I'm reading about the initialized values by default of an array/struct and have this
Reading about both separatedly, looks like the same, html+xml+javascript. What's the difference between then?
Reading about the Dispose pattern , I see the documentation repeatedly refer to cleaning
Reading about Kohana templates and saw something I've never seen before: $this->template->title = __('Welcome
Reading about Django, I saw this: http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#ref-contrib-admin - the fancy simple to use admin
Reading about Delphi's Exit statement (see here for instance), I cannot ignore that writing
While reading about exception, I will always come across checked exceptions and unchecked exceptions,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.