【C言語】【データ構造】ループキューの基本操作(作成、エンキュー、デキュー、キャプテン、キューヘッダ、遍歴、適用)

25062 ワード

空のループキューを作成し、エンキュー、デキュー、リターンキューの長さ、リターンキューヘッダ要素、キューの遍歴などの基本アルゴリズムを実現します.
#include 
#include 
#define ERROR 0
#define OK 1
#define MAXQSIZE 100

typedef int Status;

typedef int QElemType;
typedef struct{
	QElemType *base;
	int front, rear;
}SqQueue;

Status InitQueue(SqQueue & );				//   
Status EnQueue(SqQueue & , QElemType );		//  
Status DeQueue(SqQueue & , QElemType & );	//  
int QueueLength(SqQueue );					//    
Status GetHead(SqQueue , QElemType & );		//  
Status QueueTraverse(SqQueue );				//  

//  
Status bank_simulation();		//          



int main()
{
	SqQueue Q;
	int a;
	QElemType e;
	
	if(!InitQueue(Q)) printf("Create Error!
"
); while(1) { printf("1:Enter
2:Delete
3:Get the front
4:Return the length of the queue
5:Load the queue
0:Exit
Please choose:
"
); scanf("%d",&a); switch(a) { case 1: printf("Enter the element: "); scanf("%d", &e); if(!EnQueue(Q,e)) printf("Enter Error!

"
); else printf("The element is %d successfully entered!

"
, e); break; case 2: if(!DeQueue(Q,e)) printf("Delete Error!

"
); else printf("The element is %d successfully deleted!

"
, e); break; case 3: if(!GetHead(Q,e)) printf("Get Head Error!

"
); else printf("The head of the queue is %d!

"
, e); break; case 4: printf("The length of the queue is %d

"
, QueueLength(Q)); break; case 5: printf("The queue is "); QueueTraverse(Q); printf("
"
); break; case 0: return OK; } } } Status InitQueue(SqQueue &Q) { Q.base = (QElemType *) malloc (MAXQSIZE * sizeof(QElemType)); if(!Q.base) return ERROR; Q.front = Q.rear =0; return OK; } Status EnQueue(SqQueue &Q, QElemType e) { if((Q.rear+1)%MAXQSIZE == Q.front) return ERROR; Q.base[Q.rear] = e; Q.rear = (Q.rear+1) % MAXQSIZE; return OK; } Status DeQueue(SqQueue &Q, QElemType &e) { if(Q.front == Q.rear) return ERROR; e = Q.base[Q.front]; Q.front = (Q.front+1) % MAXQSIZE; return OK; } int QueueLength(SqQueue Q) { return((Q.rear - Q.front + MAXQSIZE) % MAXQSIZE); } Status GetHead(SqQueue Q, QElemType &e) { if(Q.front == Q.rear) return ERROR; e = Q.base[Q.front]; return OK; } Status QueueTraverse(SqQueue Q) { int i; if(Q.front == Q.rear) printf("The queue is empty!
"
); else for(i=Q.front; i!=Q.rear; i=(i+1)%MAXQSIZE) printf("%d ", Q.base[i]); printf("
"
); return OK; }

キューの適用:銀行顧客平均待ち時間(ウィンドウ)到着時間arrivaltime業務時間duration離脱時間departure待ち時間waittime総待ち時間total
入力形式第一行:一日以内の顧客総数n第二行:第一顧客の到着時刻と業務を行う必要がある時間第三行:第二顧客の到着時刻と業務を行う必要がある時間…第n行:第n-1顧客の到着時刻と業務を行う必要がある時間第n+1行:第n顧客の到着時刻と業務を行う必要がある時間時間
出力フォーマットの最初の行:すべての顧客の平均待機時間(小数点以下2桁まで正確)
Status bank_simulation()
{
	SqQueue Q;
	int n, i;
	int arrivaltime, duration, departure, waittime, total=0;
	float agv;
	
	if(!InitQueue(Q)) printf("Create Error!
"
); printf(" : "); scanf("%d", &n); for(i=1; i<=n; i++) { printf(" %d :
"
, i); scanf("%d %d", &arrivaltime, &duration); EnQueue(Q, arrivaltime); EnQueue(Q, duration); } DeQueue(Q, arrivaltime); DeQueue(Q, duration); departure = duration + arrivaltime; while(Q.front != Q.rear) { DeQueue(Q, arrivaltime); waittime = departure - arrivaltime; DeQueue(Q, duration); departure += duration; total += waittime; } agv = total*1.0 / n ; printf(" :%.2f
"
, agv); }