473,473 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

FIFO-Data Structures

3 New Member
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
Feb 19 '08 #1
5 4374
Meetee
931 Recognized Expert Moderator Contributor
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
Feb 19 '08 #2
akenato
3 New Member
I have made two file:
MAIN:

Expand|Select|Wrap|Line Numbers
  1. // Implement a FIFO-queue ( first in, first out ) as a ring buffer using 
  2. // a static array. The program must be able to add elements into the 
  3. // end of the queue, tell the value of the oldest element and remove it 
  4. // from the queue. The capacity of the ring buffer  n is 10000 elements.
  5.  
  6. #include <iostream>
  7. #include <cstdlib>
  8.  
  9.  
  10. #include "datastructure.hh"
  11.  
  12. // the standard namespace for convenience
  13. using namespace std;
  14.  
  15. int main() {
  16.  
  17.   // initialization of variables
  18.   int number = 0;
  19.   char command = ' ';
  20.   bool success = true;
  21.   int FifoQue[10000];
  22.   int front=0;
  23.   int rear=0;
  24.  
  25.   Datastructure ringbuffer(FifoQue,front,rear);
  26.  
  27.   while(true) {
  28.  
  29.     cin >> command;
  30.     switch(command) {
  31.        case 'Q':
  32.           // the quit command
  33.       cout << endl << "the end" << endl;
  34.       return EXIT_SUCCESS;
  35.       break;
  36.        case 'L':
  37.           // addition
  38.       cin >> number;
  39.       success = ringbuffer.add(number);
  40.       if(success) {
  41.          cout << "L ";
  42.       }
  43.       else {
  44.          cout << "V" << endl;
  45.       }
  46.       break;
  47.        case 'P':
  48.           // removal
  49.       success = ringbuffer.remove();
  50.       if(!success) {
  51.          cout << "V" << endl;
  52.       }
  53.       break;
  54.        case 'T':
  55.           // peek
  56.       success = ringbuffer.print();
  57.       if(!success) {
  58.          cout << "V" << endl;
  59.       }
  60.       break;
  61.        default:
  62.       return EXIT_FAILURE;
  63.     }
  64.   }
  65.   return EXIT_FAILURE;
  66. }
  67.  
  68. datastructure.hh
  69.  
  70. // definition of the interface
  71.  
  72. #ifndef DATASTRUCTURE_H
  73. #define DATASTRUCTURE_H
  74.  
  75. class Datastructure {
  76.  
  77. public:
  78.  
  79.   Datastructure();
  80.   Datastructure(int FifoQ[],int first,int last);
  81.   ~Datastructure();
  82.  
  83.   bool add(int number){
  84.          if (last == 10000)
  85.                 return (0);
  86.          else{
  87.               last = last + 1;
  88.               FifoQ[last] = number;
  89.               //if(last == 1)first ++;
  90.               return (1);
  91.          }
  92.   };
  93.  
  94.   bool remove(){
  95.        if (first == last + 1)
  96.           return (0);
  97.        else{
  98.           first = first +1;
  99.           return (1);
  100.           }
  101.    };
  102.  
  103.   bool print(){
  104.         if (first == last + 1)
  105.           return (0);
  106.           else{
  107.           cout << FifoQ[last] << endl;
  108.        return (1);
  109.        }
  110.   };
  111.  
  112. private:
  113. };
  114.  
  115. #endif
it doesn't work.
Feb 21 '08 #3
RedSon
5,000 Recognized Expert Expert
Have you learned about pointers yet?

Usually FIFOs are implemented using pointers, you can read more about them on wikipedia.
Feb 21 '08 #4
akenato
3 New Member
not yet, but you can help me?
do you know what is my problems?
Feb 21 '08 #5
Ganon11
3,652 Recognized Expert Specialist
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.
Feb 21 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Tobias Pfeiffer | last post by:
Hi! I want to write a "client-server-application" (only running on the same machine) or actually I've already begun with and have problems with the interprocess communication. The server, when...
4
by: Luca | last post by:
I have the need of a container of integers showing both the characteristics of an associative container (all integer elements different from each other) and the FIFO behaviour. Do you know if...
2
by: Michele Moccia | last post by:
How can I implement a "time critical" fifo in c++ ? I just have to manage sequences of raw bytes, no user defined types. An std::queue<unsigned char> or std::deque<unsigned char> seems to be slow....
8
by: Jack | last post by:
I want to implement a fixed-size FIFO queue for characters. I only want to use array not linked list. For example, const int N = 10; char c_array; The question is when the queue is full,...
4
by: David Bear | last post by:
I'm looking to see if there are any examples or prewritting fifo queue classes. I know this is a broad topic. I'm looking to implement a simple application where a web server enqueue and pickle...
1
by: mai | last post by:
Hi everyone, i'm trying to exhibit FIFO anomaly(page replacement algorithm),, I searched over 2000 random strings but i couldnt find any anomaly,, am i I doing it right?,, Please help,,,The...
1
by: gulvaiz | last post by:
Dear Members, Hi I'm Shahzad form PAKISTAN. I want stock in FIFO system. let suppose i have two tables, pur, and sale, now i want results like that Date Transction Description ...
3
by: kvivek002 | last post by:
hi how to write a code in C which implements FIFO policy with using only arrays? (by using HIT AND MISS)...
1
by: Arepi | last post by:
Hi! Wath's sense of this line? open ( FILE, "+>fifo"); Thanks!
40
by: sazd1 | last post by:
Hi Student2 I am working on similar kind of thing for stock calculation but could not find any solution to my problem even after putting my problem to different forums. I saw your post that you...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.