데이터 구조 | 순환 대기 열 | 조세 프 링

16318 단어 데이터 구조
#include
#include
#include
#include
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int QElemType;
typedef int Status;
using namespace std;
typedef struct{
    QElemType *base;
    int front;
    int rear;
    int MAXQSIZE;
}SqQueue;

Status InitQueue(SqQueue &Q, int n){
    Q.MAXQSIZE = 100;
    Q.base = (QElemType *)malloc(Q.MAXQSIZE*sizeof(QElemType));
    if(!Q.base)
        exit(OVERFLOW);
    Q.front = Q.rear = 0;
    Q.MAXQSIZE = n+1;//             
    return OK;
}
int QueueLength(SqQueue Q){
    return (Q.rear-Q.front+Q.MAXQSIZE)%Q.MAXQSIZE;
}
Status EnQueue(SqQueue &Q, QElemType e){
    if((Q.rear+1)%Q.MAXQSIZE == Q.front)
        return ERROR;
    Q.base[Q.rear] = e;
    Q.rear = (Q.rear+1)%Q.MAXQSIZE;
    return OK;
}
Status DeQueue(SqQueue &Q, QElemType &e){
    if(Q.front == Q.rear)
        return ERROR;
    e = Q.base[Q.front];
    Q.base[Q.front] = 0;
    Q.front = (Q.front+1)%Q.MAXQSIZE;
    return OK;
}
Status Visit(SqQueue Q){
    if(Q.rear == Q.front)
        return ERROR;
    int i = Q.front;
    while(i != Q.rear){
        cout << Q.base[i] << " ";
        i = (i+1)%Q.MAXQSIZE;
    }
    cout << endl;
    return OK;
}
int main(){
    int n, m;
    QElemType e;
    SqQueue Q;
    cout << "   n  :";
    cin >> n;
    InitQueue(Q, n);
    for(int i = 1; i <= n; i++)
        EnQueue(Q, i);
    cout << endl;
    cout << "      :";
    Visit(Q);
    cout << "
m :"
; cin >> m; cout << endl; int cnt = n; while(cnt != 1){ int i = 1; while(i != m){ Q.front = (Q.front+1)%Q.MAXQSIZE; if(Q.base[Q.front] != 0) i++; } DeQueue(Q, e); while(Q.base[Q.front] == 0) Q.front = (Q.front+1)%Q.MAXQSIZE; cout << " :" << e << " !
"
; cnt--; } DeQueue(Q, e); cout << "
:"
<< e << endl; return 0; }

좋은 웹페이지 즐겨찾기