Project Rosalynde for KAIST student
/***************************
* Programming Methodology *
* Practice Session #9 *
* Polymorphism *
* Skeleton Code *
***************************/
#include <iostream>
using namespace std;
#define MAX_SIZE 10
#define MIN_SIZE 0
#define ERROR -1
class ADT
{
public:
ADT(){
items=new int[MAX_SIZE];
}
~ADT(){
delete[] items;
}
virtual void push(int)=0;
virtual int pop()=0;
virtual void print()=0;
/* ±∏«ˆ«œººø‰ */
protected:
int *items;
};
class Stack : public ADT
{
public:
Stack()
{
current=0;
}
void push(int x)
{
if(current==MAX_SIZE) return;
items[current++]=x;
}
int pop(){
if(current==MIN_SIZE) return ERROR;
int pop=items[--current];
return pop;
}
void print(){
cout << "Stack: ";
for(int i=current-1;i>=0;i--) cout << items[i] << " ";
cout << endl;
}
/* ±∏«ˆ«œººø‰ */
private:
int current;
};
class Queue : public ADT
{
public:
Queue(){
front=rear=0;
full=false;
}
void push(int x)
{
if(full==true) return;
items[rear]=x;
rear=(rear+1)%MAX_SIZE;
if(rear==front) full=true;
}
int pop()
{
if(full==false && front==rear) return ERROR;
int pop=items[front];
front=(front+1)%MAX_SIZE;
full=false;
return pop;
}
void print()
{
if(full==false && rear==front) return;
if(rear<=front)
{
cout << "Queue: ";
for(int i=front;i<rear+MAX_SIZE;i++)
{
cout << items[i%MAX_SIZE] << " ";
}
}
else {
cout << "Queue: ";
for(int i=front; i<rear; i++)
cout << items[i] << " ";
}
cout << endl;
};
private:
int front,rear;
bool full;
/* ±∏«ˆ«œººø‰ */
};
/* main «‘ºˆ∏¶ πŸ≤Ÿ¡ˆ ∏ª∞Õ! */
int main()
{
ADT *adt[2];
adt[0] = new Queue;
adt[1] = new Stack;
int input[] = {3,5,4,8,6,1,3,9,7,2,
3,2,8,5,2,7,6,4,1,3};
/******* overflow/underflow test **********/
for (int i = 0; i < 11; i++)
{
adt[0]->push(input[i]);
adt[1]->push(input[i]);
}
for (int i = 0; i < 11; i++)
{
adt[0]->pop();
adt[1]->pop();
}
/******* overflow/underflow test end ******/
for (int i = 0; i < 10; i++)
{
adt[0]->push(input[i]);
adt[1]->push(input[i]);
}
for (int i = 0; i < 5; i++)
{
adt[0]->pop();
adt[1]->pop();
}
for (int i = 10; i < 20; i++)
{
adt[0]->push(input[i]);
adt[1]->push(input[i]);
}
adt[0]->print();
adt[1]->print();
return 0;
}