Queue

How would I alter this code to store ints or doubles.
Here is the output I would like:

Using bigQ to store the integers.
Contents of bigQ: 1234567891011121314151617181920212223242526

Using smallQ to generate errors.
Attempting to store 26
Attempting to store 25
Attempting to store 24
Attempting to store 23
Attempting to store 22 – Queue is full.

Contents of smallQ: 26252423 – Queue is empty.


Here's the code I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream> 
using namespace std; 
 
const int maxQsize = 100; 
 
class Queue { 
  char q[maxQsize]; // this array holds the queue 
  int size; // the maximun number of elements that the queue can store 
  int putloc, getloc; // the put and get indices 
public: 
 
  // Construct a queue of a specific length. 
  Queue(int len) { 
    // Queue must be less than max and positive. 
    if(len > maxQsize) len = maxQsize; 
    else if(len <= 0) len = 1;  
 
    size = len; 
    putloc = getloc = 0; 
  } 
 
  // Put a character into the queue. 
  void put(char ch) { 
    if(putloc == size) { 
      cout << " -- Queue is full.\n"; 
      return; 
    } 
     
    putloc++; 
    q[putloc] = ch; 
  } 
 
  // Get a character from the queue. 
  char get() { 
    if(getloc == putloc) { 
      cout << " -- Queue is empty.\n"; 
      return 0;  
    } 
   
    getloc++; 
    return q[getloc]; 
  } 
}; 
 
// Demonstrate the Queue class. 
int main() { 
  Queue bigQ(100); 
  Queue smallQ(4); 
  char ch; 
  int i; 
 
  cout << "Using bigQ to store the alphabet.\n"; 
  // put some numbers into bigQ 
  for(i=0; i < 26; i++) 
    bigQ.put('A' + i); 
 
  // retrieve and display elements from bigQ 
  cout << "Contents of bigQ: "; 
  for(i=0; i < 26; i++) {  
    ch = bigQ.get(); 
    if(ch != 0) cout << ch; 
  } 
 
  cout << "\n\n"; 
 
 
  cout << "Using smallQ to generate errors.\n"; 
 
  // Now, use smallQ to generate some errors 
  for(i=0; i < 5; i++) { 
    cout << "Attempting to store " << 
                  (char) ('Z' - i); 
 
    smallQ.put('Z' - i); 
 
    cout << "\n"; 
  } 
  cout << "\n"; 
 
  // more errors on smallQ 
  cout << "Contents of smallQ: "; 
  for(i=0; i < 5; i++) {  
    ch = smallQ.get(); 
 
    if(ch != 0) cout << ch; 
  } 
 
  cout << "\n"; 
}

> How would I alter this code to store ints or doubles

Make it a template?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;

const int maxQsize = 100;

template < typename T > class Queue {
  T q[maxQsize]; // this array holds the queue
  int size; // the maximun number of elements that the queue can store
  int putloc, getloc; // the put and get indices
public:

  // Construct a queue of a specific length.
  Queue(int len) {
    // Queue must be less than max and positive.
    if(len > maxQsize) len = maxQsize;
    else if(len <= 0) len = 1;

    size = len;
    putloc = getloc = 0;
  }

  // Put a 'T' into the queue.
  void put( const T& v ) {
    if(putloc == size) {
      cout << " -- Queue is full.\n";
      return;
    }

    putloc++;
    q[putloc] = v ;
  }

  // Get a 'T' from the queue.
  T get() {
    if(getloc == putloc) {
      cout << " -- Queue is empty.\n";
      return 0;
    }

    getloc++;
    return q[getloc];
  }

  bool empty() const { return getloc == putloc ; }
};

// Demonstrate the Queue class.
int main() {

  Queue<char> charq (100);
  const char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
  for( char c : alphabet ) charq.put(c) ;
  while( !charq.empty() ) cout << charq.get() ;
  std::cout << '\n' ;

  Queue<int> intQ(4);
  for( int i = 0 ; i < 6 ; ++i ) intQ.put(i) ;
  while( !intQ.empty() ) cout << intQ.get() << ' ' ;
  std::cout << '\n' ;

  Queue<double> dblQ(50);
  for( int i = 0 ; i < 10 ; ++i ) dblQ.put( i * 1.57 ) ;
  while( !dblQ.empty() ) cout << dblQ.get() << ' ' ;
  std::cout << '\n' ;
}

http://ideone.com/xy2aIa
Topic archived. No new replies allowed.