Sunday 16 October 2011

this is the demonstration of queue

#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();
}

No comments:

Post a Comment