Sunday, July 20, 2014

Implementing a Linear Queue using Array - Data Structures - C++ Program (Object Oriented)

Problem Question


To implement Linear Queue using array

Explanation of Problem


In this problem, the basic need is to have an array as the basic data structure. We would be making logics and attaching them to this basic array of ours that would make it a linear queue. So basically, this is the enhancement of the basic data structure array, to evolve it into a linear queue. A linear queue is identified by two main pointers, one is the head or the front of queue, and the other is the tail or the rear of the queue. The most important thing to understand about a linear queue is that, all the new data gets inserted at the rear, and any deletions are done at the front, just like a real life queue. Any new person joins a queue at the rear and the person to leave the queue is the person at the front. Just keeping these two restrictions on the addition and deletion to an array, we get the linear queue in place.

NOTE: To keep the program simple, we have kept the size of the queue as 1000. You can alter that as per your needs.

Code


/**@Title: ooArray_Queue.cpp*
*@Programming Paradigm: Object Oriented*
*@Language: C++*
*@Compiler: GNU GCC*
*@IDE: Code::Blocks 13.12*
*@Author: Rogue Coder*
*@URL: http://letsplaycoding.blogspot.com/*
*@Date: 20-07-2014*
*@Updated: 28-07-2014*
*/
#include <iostream>

class myArrayQueue
{
private:
  int queueArray [ 1000 ];
  int frontOfQueue;
  int rearOfQueue;

public:
  myArrayQueue()
  {
    frontOfQueue = 0;
    rearOfQueue = 0;
  }

  void displayQueue()
  {
    int i;
    std::cout << "\nQueue:\n";
    if ( rearOfQueue == frontOfQueue )
      std::cout << "\n\aQUEUE EMPTY!!\n\a";
    else
      for ( i = frontOfQueue; i < rearOfQueue; i++ )
        std::cout << queueArray [ i ] << " ";
  }

  void enterIntoQueue(int element)
  {
    if ( rearOfQueue >= 1000 )
      std::cout << "\n\a!!QUEUE OVERFLOW!!\n";
    else
      queueArray [ rearOfQueue++ ] = element;
  }

  void removeFromQueue()
  {
    int i;
    if ( rearOfQueue <= frontOfQueue )
      std::cout << "\n\a!!QUEUE UNDERFLOW!!\n";
    else
    {
      std::cout << std::endl << queueArray [ frontOfQueue ] << " removed!";
      frontOfQueue++;
    }
  }
};

int main()
{
  myArrayQueue myQueue;
  int choice = 0, elementOfQueue;
  std::cout << "\aWelcome to Queue v2.1\n\aMade by Rogue Coder\n\nThe Queue has a limit of 1000 elements.";
  do
  {
    std::cout << "\n\n\aMenu:\n1. Enter\n2. Remove\n3. Display\n4. Exit\nYour Choice: ";
    std::cin >> choice;
    switch ( choice )
    {
    case 1:
      std::cout << "\nEnter the element to fed into Queue: ";
      std::cin >> elementOfQueue;
      myQueue.enterIntoQueue ( elementOfQueue );
      break;

    case 2:
      myQueue.removeFromQueue();
      break;

    case 3:
      myQueue.displayQueue();
      break;

    case 4:
      std::cout << "\n\n\aThank You for using Queue v2.1\n\aMade by Rogue Coder\nPress any key to exit.";
      std::cin.get();
      break;

    default:
      std::cout << "\n\n\a!!!WRONG CHOICE!!!\a";
      break;
    }
  }
  while ( choice != 4 );
  return 0;
}

Explanation of Code


#include <iostream> -> The compiler calls the Preprocessor to include the IOSTREAM(Standard Input / Output Streams Library) header file into the program, thus letting the use of the Standard Input / Output Streams functions like std::cin and std::cout. As per C++11 specification, including <iostream> automatically includes also <ios>, <streambuf>, <istream>, <ostream> and <iosfwd>.

int main() -> The entry point of the program where the execution starts. This function has to be named main. As per the ANSI specification, the return type has to be int. Since the return type is specified as int in my program, I have to use a return statement at the end of my code. So I use return 0 since zero returned from a function, by convention, implies a correct execution of the program. The return values are used to debug the program.

std::cin (extern istream cin) -> Standard Input Stream, and object of class istream. It is generally used with the extraction operator (>>), though we can use member functions like get (cin.get()), read (cin.read()), etc. for the input. The use of extraction operator is much more popular due to the fact that it aids in getting formatted input.

std::cout (extern ostream cout) -> Standard Output Stream, and object of class ostream. It is generally used with the insertion operator (<<), though we can use member functions like write (cout.write()) for the output. The use of insertions operator is much more popular due to the fact that it aids in giving formatted output.

std::endl (ostream& endl (ostream& os)) -> This is a function which is used to insert a newline character and flush the stream. Because this function is a manipulator, it is designed to be used alone with no arguments in conjunction with the insertion (<<) operations on output streams.

myArrayQueue()
{
frontOfQueue = 0;
rearOfQueue = 0;
}
-> This is the constructor of our myArrayQueue class. It is a special method that is used to initialise the variables when the class is referenced with a new object. Thus, we assign both head & tail of queue to point at the beginning of the queue, since we have newly formed, empty queue.

if ( rearOfQueue == frontOfQueue )
std::cout << "\n\aQUEUE EMPTY!!\n\a";
else
for ( i = frontOfQueue; i < rearOfQueue; i++ )
std::cout << queueArray [ i ] << " ";
}
-> This is our next method, which is used to display the queue to the user. The method first inspects the rearOfQueue, which if found to be pointing to same location as frontOfQueue, the queue is considered as empty. Else, the control is transferred to a for loop, which traverses the queue from front to rear, displaying each element one by one to the user.

else
queueArray [ rearOfQueue++ ] = element;
}
-> The next method is used to insert a new element into the queue. To do this, we first check the rearOfQueue. If it is at the maximum expected value, a message is displayed to the user, QUEUE OVERFLOW, since this means that the queue is full. Else, the new element is added at the rear of the queue, and the rear is incremented by one. The new element is passed from the calling function and is accepted as a parameter to the method.

else
{
std::cout << std::endl << queueArray [ frontOfQueue ] << " removed!";
frontOfQueue++;
}
}
-> In this method the rearOfQueue is inspected first. If it points to a location sa,e as frontOfQueue or before that, that means the queue is empty, and thus a message is displayed to the user, QUEUE UNDERFLOW. Else, the element at the front of queue is displayed to the user(for information) as deleted from queue followed by the frontOfQueue getting incremented by one to point to the new front of the queue. Actually, an element is never deleted in case of linear queues, it is only orphaned. Thus, due to memory constraints, queues are used in lesser situations.

do {…} while() ->The do-while loop is our program loop which is used to make the program interactive and menu-driven. The user enters the choice. Depending on the choice, the switch-case logic calls the method of the object. When the user wishes to enter into the queue, the program accepts the user input and calls the method which does the same, and passes the element to the method when calling it. When the user chooses ‘4’, the program exits.

exit(0); -> This function is used to exit the program with an error code as it's argument. '0' implies normal exit. Other values are used for debugging purposes.

std::cin.get() -> This statement is used to pause our program, until user presses a key. This function is not necessary in your program; I use it to see my outputs at a paused screen. If you use cmd to run your programs, you might not need this. If you use linux/unix you might not need this. Moreover, removing this line of code from this program, doesn't affect the functionality of the program.

Output(s)




Download Source Code


No comments:

Post a Comment

Need help?