Dec 12, 2015 at 8:44pm UTC
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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#include <iostream>
using namespace std;
template <class T>
class StaQue
{
public :
//constructor
StaQue( int size = 1000)
{
MAX_SIZE = size;
front = -1;
rear = -1;
data = new T[MAX_SIZE];
size = 0;
}
//destructor
~StaQue()
{
delete []data;
}
void Insert(const T &x)
{
if (IsFull())
{
cout << "queue is full " << endl;
return ;
}
if (IsEmpty())
{
front = rear = 0;
size++;
}
else
{
rear = (rear+1) % MAX_SIZE;
size++;
}
data[rear]= x;
}
int Size()
{
return size;
}
T RemoveLast()
{
if (IsEmpty())
{
cout << "staque is empty : " << endl;
}
else if (front == rear)
{
rear = front = -1;
size--;
}
else
{
T val = data[rear];
rear = (rear - 1) % MAX_SIZE;
return val;
size--;
}
}
T Last()
{
if (IsEmpty())
{
cout << "queue is empty : " << endl;
}
else
{
T val = data[rear];
return val;
}
}
T First()
{
if (IsEmpty())
{
cout << "queue is empty : " << endl;
}
else
{
T val = data[front];
return val;
}
}
T RemoveFirst()
{
if (IsEmpty())
{
cout << "queue is empty : " << endl;
}
else if (front == rear)
{
rear = front = -1;
size--;
}
else
{
T val = data[front];
front = (front + 1) % MAX_SIZE;
return val;
size--;
}
}
void clear()
{
delete []data;
front = -1;
rear = -1;
data = new <T>[MAX_SIZE];
}
bool IsEmpty(void )
{
if (front == -1 && rear == -1)
{
return true ;
}
else
{
return false ;
}
}
bool IsFull(void )
{
if ((rear + 1) % MAX_SIZE == front)
{
return true ;
}
else
{
return false ;
}
}
//outputs the data in queue. If the list is empty, outputs “Empty Queue”.
private :
T *data; //array of data items
int front; //front index
int rear; //rear index
int MAX_SIZE;//size of
int size;
};
int main()
{
StaQue <double > queue;
queue.Insert(5.0);
queue.Insert(6.5);
queue.Insert(-3.0);
queue.Insert(-8.0);
queue.RemoveLast();
return 0;
}
Last edited on Jan 6, 2016 at 4:05pm UTC
Dec 12, 2015 at 9:00pm UTC
Looks like a worse std::deque.
Jan 10, 2016 at 2:31am UTC
bad memoy handling, missing copy constructor and assignment operator.
bad optimization, no move or especial swap
no in-place construction of the element. Elements need default constructor.
bad error handling
you've got undefined behaviour because there are paths that do not return anything
you print error messages to the standard output
the client code is not notified of any errors.
fixed size (¿feature?)
immutable elements
no random access
no const-correctness
bug: line 109 would never be reached
If you wanted a review you should have said so from the start