Senin, 24 Januari 2011

CONTOH PROGRAM ANTRIAN

#include <cstdlib>
#include <iostream>
#include "queue"

using namespace std;
class Queue {
public:
       Queue(int MaxQueueSize= 5);
       ~Queue(){delete [] queue;}
       bool IsEmpty()const {return front == rear ;}
       bool IsFull () const{
            return( ((rear+1)% MaxSize == front ) ? 1:0);}
       int First() const;
       int Last() const;
       Queue& Add(const int x);
       Queue& Delete(int x);
private:
         int front;
         int rear;
         int MaxSize;
         int*queue;
         };
       
Queue::Queue(int MaxQueueSize){
       MaxSize = MaxQueueSize +1;
       queue = new int[MaxSize];
       front = rear = 0;
       }

int Queue::First()const
{      return queue[(front+1)% MaxSize];
       }

int Queue::Last()const
{      return queue [rear];
}

Queue& Queue::Add(const int x){
       rear =(rear + 1)%MaxSize;
       queue[rear] = x;
       return*this;
       }
     
Queue& Queue::Delete(int x){
      front =(front + 1)%MaxSize;
      x = queue[front];
       return*this;
       }
     

int main(int argc, char *argv[])
{
  
    Queue Q(3);
    int x;
    try{Q.Add(1).Add(2).Add(3).Add(4);
       cout<<"No queue add failed"<<endl;}
    catch(...)
       {cout<<"\nA queue add failed"<<endl;}
    cout<<"\nQueue is now 123"<<endl;
    Q.Delete(x);
    cout<<" Deleted "<<x<<endl;
    cout<<Q.First()<<" == is at front "<<endl;
    cout<<Q.Last()<<" == is at end "<<endl;
    try{
        Q.Delete(x);
        cout<<"Deleted"<<x<<endl;
        Q.Delete(x);
        cout<<"Deleted"<<x<<endl;
        Q.Delete(x);
        cout<<"Deleted"<<x<<endl;
        cout<<"No queue delete failed"<<endl;
        }
        catch(...)
        {cout<<"A delete has failed"<<endl;}
      
    system("PAUSE");
    return EXIT_SUCCESS;
}

Tidak ada komentar:

Posting Komentar