FIFO-Data Structures

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akenato
    New Member
    • Feb 2008
    • 3

    FIFO-Data Structures

    Hi for me this is th first time that I use c++. I have to do this exercise. Somebody can help me?
    Thanx.

    Implement a FIFO-queue ( first in, first out ) as a ring buffer. Use a static array as the data structure. The program needs to be able to add elements to the end of the queue, tell the value of the oldest element and remove from the queue. The capacity n of the ring buffer is 10000 elements.
    The program must handle the following commands:

    L k Read in an integer k, push it to the end of the queue and print "L". If the queue is already full, just print an error message "V".
    T Print the value of the oldest element in the queue. If the queue is empty, print "V".
    P Print the value of the oldest element in the queue and remove it from the queue. If the queue is empty, print "V".
    Q End the program execution. Print "the end".

    You can assume the following when implementing the program:

    1. All the integers are between -1000000 - +1000000
    2. The input lines are at best 75 characters long
    3. No undefined commands are given. All commands and their elements are separated with one or more white space character (space, tab stop, newline).

    Thanx for the help
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by akenato
    Hi for me this is th first time that I use c++. I have to do this exercise. Somebody can help me?
    Thanx.

    Implement a FIFO-queue ( first in, first out ) as a ring buffer. Use a static array as the data structure. The program needs to be able to add elements to the end of the queue, tell the value of the oldest element and remove from the queue. The capacity n of the ring buffer is 10000 elements.
    The program must handle the following commands:

    L k Read in an integer k, push it to the end of the queue and print "L". If the queue is already full, just print an error message "V".
    T Print the value of the oldest element in the queue. If the queue is empty, print "V".
    P Print the value of the oldest element in the queue and remove it from the queue. If the queue is empty, print "V".
    Q End the program execution. Print "the end".

    You can assume the following when implementing the program:

    1. All the integers are between -1000000 - +1000000
    2. The input lines are at best 75 characters long
    3. No undefined commands are given. All commands and their elements are separated with one or more white space character (space, tab stop, newline).

    Thanx for the help
    Please don't just explain your requirements, also attach your efforts. Here we cannot give you readymade code. Kindly have a look to posting guidelines.

    Regards

    Comment

    • akenato
      New Member
      • Feb 2008
      • 3

      #3
      I have made two file:
      MAIN:

      [CODE=cpp]// Implement a FIFO-queue ( first in, first out ) as a ring buffer using
      // a static array. The program must be able to add elements into the
      // end of the queue, tell the value of the oldest element and remove it
      // from the queue. The capacity of the ring buffer n is 10000 elements.

      #include <iostream>
      #include <cstdlib>


      #include "datastructure. hh"

      // the standard namespace for convenience
      using namespace std;

      int main() {

      // initialization of variables
      int number = 0;
      char command = ' ';
      bool success = true;
      int FifoQue[10000];
      int front=0;
      int rear=0;

      Datastructure ringbuffer(Fifo Que,front,rear) ;

      while(true) {

      cin >> command;
      switch(command) {
      case 'Q':
      // the quit command
      cout << endl << "the end" << endl;
      return EXIT_SUCCESS;
      break;
      case 'L':
      // addition
      cin >> number;
      success = ringbuffer.add( number);
      if(success) {
      cout << "L ";
      }
      else {
      cout << "V" << endl;
      }
      break;
      case 'P':
      // removal
      success = ringbuffer.remo ve();
      if(!success) {
      cout << "V" << endl;
      }
      break;
      case 'T':
      // peek
      success = ringbuffer.prin t();
      if(!success) {
      cout << "V" << endl;
      }
      break;
      default:
      return EXIT_FAILURE;
      }
      }
      return EXIT_FAILURE;
      }

      datastructure.h h

      // definition of the interface

      #ifndef DATASTRUCTURE_H
      #define DATASTRUCTURE_H

      class Datastructure {

      public:

      Datastructure() ;
      Datastructure(i nt FifoQ[],int first,int last);
      ~Datastructure( );

      bool add(int number){
      if (last == 10000)
      return (0);
      else{
      last = last + 1;
      FifoQ[last] = number;
      //if(last == 1)first ++;
      return (1);
      }
      };

      bool remove(){
      if (first == last + 1)
      return (0);
      else{
      first = first +1;
      return (1);
      }
      };

      bool print(){
      if (first == last + 1)
      return (0);
      else{
      cout << FifoQ[last] << endl;
      return (1);
      }
      };

      private:
      };

      #endif[/CODE]

      it doesn't work.
      Last edited by Ganon11; Feb 21 '08, 04:05 PM. Reason: Please use the [CODE] tags provided.

      Comment

      • RedSon
        Recognized Expert Expert
        • Jan 2007
        • 4980

        #4
        Have you learned about pointers yet?

        Usually FIFOs are implemented using pointers, you can read more about them on wikipedia.

        Comment

        • akenato
          New Member
          • Feb 2008
          • 3

          #5
          not yet, but you can help me?
          do you know what is my problems?

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Well, you haven't implemented the functionality of a circular queue correctly. By your method, I can add 10000 elements, remove 10000 elements, and try to add an element. When I try to do so, I will get an error (because last == 10000), even though there are no elements in the queue any longer. Rethink how to implement this circular queue before continuing to write functions.

            Comment

            Working...