20 Kasım 2012 Salı

kuyruk

Dizi tabanlı bir dairesel kuyruk oluşturan menü tabanlı c++ kodlu program.




#include<iostream>
#include<cstdlib>
#define MAX_SIZE 10
using namespace std;
class Queue{
    private:
        int item[MAX_SIZE];
        int head;
        int tail;
    public:
        Queue();
        void enqueue(int);
        int dequeue();
        int size();
        void display();
        bool isEmpty();
        bool isFull();
};
Queue::Queue(){
    head = 0;
    tail = 0;
}
void Queue::enqueue(int data){
        item[tail] = data;
        tail = (tail+1)%MAX_SIZE;
}
int Queue::dequeue(){
    int temp;
    temp = item[head];
    head = (head+1)%MAX_SIZE;
    return temp;
}
int Queue::size(){
    return (tail - head);
}
void Queue::display(){
    int i;
    if(!this->isEmpty()){
        for(i=head; i!=tail; i=(i+1)%MAX_SIZE)
        {
            cout<<item[i]<<endl;
        }
    }else{
        cout<<"kuyruk boşaldı!"<<endl;
    }
}
bool Queue::isEmpty(){
    if(abs(head == tail)){
        return true;
    }else{
        return false;
    }
}
bool Queue::isFull(){
    if(head==(tail+1)%MAX_SIZE){
        return true;
    }else{
        return false;
    }
}
int main(){
    Queue queue;
    int data;
char choice;
    while(1){
        cout<<"\ne- Ekleme\ns- silme\ny- say\nl- listele\nq- cikis";
        cout<<"\nseciminiz: ";
        cin>>choice;
        switch(choice){
            case 'e':
            if(!queue.isFull()){
                    cout<<"\ndata girin: ";
                    cin>>data;
                    queue.enqueue(data);
            }else{
                cout<<"kuyruk dolu!"<<endl;
            }
            break;
            case 's':
            if(!queue.isEmpty()){
                cout<<"silinen eleman :"<<queue.dequeue();
            }else{
                cout<<"kuyruk bos!"<<endl;
            }
                break;
            case 'y':
                cout<<"eleman sayisi: "<<queue.size();
                break;
            case 'c':
                queue.display();
                break;
            case 'q':
                exit(0);
                break;
        }
    }
    return 0;
}

Hiç yorum yok:

Yorum Gönder