Stack Implementations using Array
Hi,
I tried to configure the stack and queue structure using a array.Do you think?
Is that the correct configuration?
Thank you all.
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
|
class STACK{
int *p;
int size;
public:
STACK()
{
p = 0;
size = 0;
}
void push(int val)
{
if (size >= 1){
size++;
p = (int *)realloc(p, sizeof(int) * size);
for (int k = 0; k < size -1; k++)
p[size - 1 -k] = p[size -2 -k];
p[0] = val;
}
else {
p = (int *)realloc(p, sizeof(int) * (size + 1));
size++;
p[0] = val;
}
}
bool empty()const
{
if (size == 0)
return true;
return false;
}
int SIZE()const
{
return size;
}
int front()const
{
return p[0];
}
void pop()
{
int temp;
for(int i = 0; i < size; i++)
p[i] = p[i + 1];
size--;
p = (int *)realloc(p, sizeof(int)* size);
}
void Display()
{
for (int k = 0;k < size; k++)
cout << p[k] << " ";
cout << endl;
}
};
|
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
|
class Oueue{
int *p;
int count;
int size;
public:
Oueue()
{
p = 0;
count = 0;
size = 0;
}
void push(int val)
{
size++;
p = (int *)realloc(p, sizeof(int) * size);
p[count++] = val;
}
bool empty()const
{
if (size == 0)
return true;
return false;
}
int SIZE()const
{
return size;
}
int front()const
{
return p[0];
}
void pop()
{
int temp;
for(int i = 0; i < size; i++)
p[i] = p[i + 1];
size--;
p = (int *)realloc(p, sizeof(int)* size);
}
};
|
Topic archived. No new replies allowed.