法一:
#include #include #define maxsize 100
//為了節省空間,採用迴圈陣列,用到迴圈加1
//非空時,front和rear指向端點元素
//所以rear初始化為0,front為1
//滿的時候,一種情況是,front為1,rear為maxsize-1,滿足(queue->rear+2)%queue->capacity==queue->front
//滿的時候,也可能rear,front均在中間,但均滿足(queue->rear+2)%queue->capacity==queue->front
//滿的時候,陣列也有乙個空沒元素
//空的時候,(queue->rear+1)%queue->capacity==queue->front
struct queuerecord;
struct queuerecord* createandinit(struct queuerecord* queue)
void enqueue(struct queuerecord * queue,int number)
//queue->size++;
queue->rear=(queue->rear+1)%queue->capacity;
queue->data[queue->rear]=number;
return;
}void dequeue(struct queuerecord* queue)
queue->front=(queue->front+1)%queue->capacity;
return;
}int main()
法二:
#include #include #define maxsize 100
//為了節省空間,採用迴圈陣列,用到迴圈加1
//非空時,front指向端點元素,rear指向端點的下乙個位置,rear處無元素
//所以rear初始化為0,front為0
//滿的時候,一種情況是,front為0,rear為maxsize-1,滿足(queue->rear+1)%queue->capacity==queue->front
//滿的時候,也可能rear,front均在中間,但均滿足(queue->rear+1)%queue->capacity==queue->front
//滿的時候,陣列也有乙個空沒元素
//空的時候,queue->rear==queue->front
struct queuerecord;
struct queuerecord* createandinit(struct queuerecord* queue)
void enqueue(struct queuerecord * queue,int number)
//queue->size++;
queue->data[queue->rear]=number;//先進新元素再更改rear
queue->rear=(queue->rear+1)%queue->capacity;
return;
}void dequeue(struct queuerecord* queue)
queue->front=(queue->front+1)%queue->capacity;
return;
}int main()
陣列實現的佇列
myqueue.h 說明 ntai的下乙個位置就是nhead的話,表示隊滿了,犧牲乙個空間 ntail nhead,表示隊為空 入隊的時候要判斷隊是否為滿,出隊的時候,要判斷是否為空。define size 1000 陣列大小 class cmyqueue myqueue.cpp include s...
佇列的陣列實現
typedef struct quene 初始化 void initquene quene q 判空 bool isfull quene q 入隊 void enquene quene q,int e 出隊 void dequene quene q 讀隊頭元素 intgettop quene q r...
佇列的陣列實現
queue.h 1 佇列的型別宣告 2 3 typedef int elementtype 4 start fig3 57.txt 5 ifndef queue h 6 define queue h78 struct queuerecord 9 typedef struct queuerecord ...