#include <iostream>
using namespace std;
class base{
int value;
base *prev;
base *next;
base *head;
public:
void init();
void add(int data);
int remove();
void isempty();
};
void base::init(){
head = new base [sizeof(base)];
head->prev=NULL;
head->next=NULL;
}
void base::add(int data){
base *New;
base *tail;
for (tail = head ; tail->next; tail=tail->next) {;}
New = new base [sizeof(base)];
New->value = data;
New->next = NULL;
tail->next = New;
}
int base::remove(){
int data;
base *target;
target = head->next;
if (target == NULL){
return -1;
}
data= target->value;
head->next = target->next;
if(head->next) {
head->next->prev = head;
}
free(target);
return data;
}
void base::isempty(){
while ( remove() != -1) {;}
free(head);
head=NULL;
}
void main(){
int cinput;
int vout;
int i = 0;
base b;
b.init();
cout << "How many datas you want to add ? :";
cin >> cinput;
for(int i=0 ; i < cinput ; i++){
cout << "\nInput date (numbers) : ";
cin >> vout;
b.add(vout);
}
for(i=0; i < cinput; i++ ){
cout << "\n" << i + 1 << "th delete : "<<b.remove() << endl;
}
b.isempty();
}
Heonyee's Blog for OOP344
Sunday, 16 October 2011
Thursday, 13 October 2011
this is the demonstration of stack
#include <iostream>
using namespace std;
class base {
int* stack;
int size;
int top;
public:
void init(int _size);
~base();
bool add(int data);
int remove();
bool isempty();
};
void base::init(int _size){
size=_size;
stack = new int [size];
top = -1;
}
bool base::add(int data){
if(top < size-1){
top ++;
stack[top]= data;
return true;
} else {
return false;
}
}
int base::remove(){
if(top >= 0) {
return stack[top--];
} else {
return -1;
}
}
bool base::isempty(){
if (top == -1 ){
cout << "\nNow stack is empty" << endl;
return true;
}else{
return false;
}
}
base::~base(){
delete stack;
}
void main(){
int cpush;
int vpush;
int i = 0;
base b;
b.init(256);
cout << "How many data you want to add ? :";
cin >> cpush;
for(int i=0 ; i < cpush ; i++){
cout << "\nInput data (numbers) : ";
cin >> vpush;
b.add(vpush);
}
while(!b.isempty()){
cout << "\n" << i + 1 << "th pop : "<<b.remove() << endl;
i++;
}
//cout << b.remove() << endl;
//cout << b.remove() << endl;
//cout << b.remove() << endl;
}
using namespace std;
class base {
int* stack;
int size;
int top;
public:
void init(int _size);
~base();
bool add(int data);
int remove();
bool isempty();
};
void base::init(int _size){
size=_size;
stack = new int [size];
top = -1;
}
bool base::add(int data){
if(top < size-1){
top ++;
stack[top]= data;
return true;
} else {
return false;
}
}
int base::remove(){
if(top >= 0) {
return stack[top--];
} else {
return -1;
}
}
bool base::isempty(){
if (top == -1 ){
cout << "\nNow stack is empty" << endl;
return true;
}else{
return false;
}
}
base::~base(){
delete stack;
}
void main(){
int cpush;
int vpush;
int i = 0;
base b;
b.init(256);
cout << "How many data you want to add ? :";
cin >> cpush;
for(int i=0 ; i < cpush ; i++){
cout << "\nInput data (numbers) : ";
cin >> vpush;
b.add(vpush);
}
while(!b.isempty()){
cout << "\n" << i + 1 << "th pop : "<<b.remove() << endl;
i++;
}
//cout << b.remove() << endl;
//cout << b.remove() << endl;
//cout << b.remove() << endl;
}
Friday, 9 September 2011
Subscribe to:
Posts (Atom)