使用C語言來解決循環(huán)隊列問題的方法
題目描述:
大家都知道數(shù)據(jù)結(jié)構(gòu)里面有一個結(jié)構(gòu)叫做循環(huán)隊列。顧名思義,這是一個隊列,并且是循環(huán)的。但是現(xiàn)在,淘氣的囧哥給這個循環(huán)隊列加上了一些規(guī)矩,其中有5條指令:
(1) Push K, 讓元素K進隊列。
(2) Pop,對頭元素出隊列。
(3) Query K,查找隊列中第K個元素,注意K的合法性。
(4) Isempty,判斷隊列是否為空。
(5) Isfull,判斷隊列是否已滿。
現(xiàn)在有N行指令,并且告訴你隊列大小是M。
輸入:
第一行包含兩個整數(shù)N和M。1<=N,M<=100000。
接下來有N行,表示指令,指令格式見題目描述。
其中元素均在int范圍。
輸出:
對于指令(1),若隊列已滿,輸出failed,否則不做輸出。
對于指令(2),若隊列已空,輸出failed,否則不做輸出。
對于指令(3),輸出隊列中第K個元素,若不存在,輸出failed。
對于指令(4)和(5),則用yes或者no回答。
詳情見樣例。
樣例輸入:
12 2Push 1Push 2Push 3Query 2Query 3IsemptyIsfullPopPopPopIsemptyIsfull
樣例輸出:
failed2failednoyesfailedyesno
AC代碼:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define queuesize 100001 //最大隊列長度
struct queue
{
int front;
int rear;
int data[queuesize];
int count; //記錄隊列中的元素
};
void InitQueue(struct queue *Q);
void EnQueue(struct queue *Q, int element, int m);
void Dequeue(struct queue *Q, int m);
void QueueSearch(struct queue *Q, int k, int m);
int main()
{
int n, m, i, element, k, flag;
char command[10];
while(scanf("%d%d",&n, &m) != EOF)
{
if(n < 1 || m > 100000)
return 0;
struct queue *Q;
Q = malloc(sizeof(struct queue));
InitQueue(Q);
for(i = 0; i < n; i ++)
{
scanf("%s",command);
if (strcmp(command,"Push") == 0)
{
scanf("%d",&element);
EnQueue(Q, element, m);
}else if (strcmp(command,"Pop") == 0)
{
Dequeue(Q, m);
}else if (strcmp(command,"Query") == 0)
{
scanf("%d",&k);
QueueSearch(Q, k, m);
}else if (strcmp(command,"Isempty") == 0)
{
flag = (Q -> count == 0)? 1 : 0;
if(flag)
{
printf("yes\n");
}else
{
printf("no\n");
}
}else if (strcmp(command,"Isfull") == 0)
{
flag = (Q -> count == m)? 1 : 0;
if(flag)
{
printf("yes\n");
}else
{
printf("no\n");
}
}
}
}
return 0;
}
/**
* Description:隊列初始化
*/
void InitQueue(struct queue *Q)
{
Q -> front = Q -> rear = 0;
Q -> count = 0;
}
/**
* Description:入隊操作
*/
void EnQueue(struct queue *Q, int element, int m)
{
int flag;
flag = (Q -> count == m)? 1 : 0;
if(!flag)
{
Q -> data[Q -> rear] = element;
Q -> count ++;
Q -> rear = (Q -> rear + 1) % m;
}else
{
printf("failed\n");
}
}
/**
* Description:出隊操作
*/
void Dequeue(struct queue *Q, int m)
{
int flag;
int element;
flag = (Q -> count == 0)? 1 : 0;
if(!flag)
{
element = Q -> data[Q -> front];
Q -> front = (Q -> front + 1) % m;
Q -> count --;
}else
{
printf("failed\n");
}
}
/**
* Description:查找隊列中的指定元素
*/
void QueueSearch(struct queue *Q, int k, int m)
{
int flag, temp;
flag = (Q -> count == 0)? 1: 0;
temp = Q -> front + k - 1;
if((!flag) && (k <= m && k >= 1))
{
if((Q -> front < Q -> rear) && ( Q-> front <= temp && Q -> rear > temp))
printf("%d\n",Q -> data[temp]);
else if((Q -> front > Q -> rear) && (temp >= Q -> front || temp < Q->rear))
printf("%d\n",Q -> data[temp]);
else if(Q -> front == Q -> rear)
printf("%d\n",Q -> data[temp]);
else
printf("failed\n");
}else
{
printf("failed\n");
}
}
相關(guān)文章
適合初學(xué)者的C語言數(shù)據(jù)類型的講解
在 C 語言中,數(shù)據(jù)類型指的是用于聲明不同類型的變量或函數(shù)的一個廣泛的系統(tǒng)。變量的類型決定了變量存儲占用的空間,以及如何解釋存儲的位模式。2022-04-04
從txt中讀入數(shù)據(jù)到數(shù)組中(fscanf)的實現(xiàn)代碼
下面小編就為大家?guī)硪黄獜膖xt中讀入數(shù)據(jù)到數(shù)組中(fscanf)的實現(xiàn)代碼。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12
C++經(jīng)典例題之字符串特定規(guī)則反轉(zhuǎn)問題的解法
這篇文章主要介紹了如何解決字符串反轉(zhuǎn)問題,通過將字符串按每2k個字符為一個區(qū)間進行劃分,并使用雙指針方法來確定實際反轉(zhuǎn)的邊界,最終實現(xiàn)字符串按特定規(guī)則進行反轉(zhuǎn),文中通過代碼介紹的非常詳細,需要的朋友可以參考下2025-03-03
C++中實現(xiàn)fibonacci數(shù)列的幾種方法
本文主要介紹了C++中實現(xiàn)fibonacci數(shù)列的幾種方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01

