@bintou
2017-11-29T13:42:48.000000Z
字数 2743
阅读 2319
未分类
// C program for array implementation of queue/*Enqueue: Adds an item to the queue. If the queue is full, then it is said to be an Overflow condition.Dequeue: Removes an item from the queue. The items are popped in the same order in which they are pushed. If the queue is empty, then it is said to be an Underflow condition.Front: Get the front item from queue.Rear: Get the last item from queue.*/#include <stdio.h>#include <stdlib.h>#include <limits.h>// A structure to represent a queuestruct Queue{int front, rear, size;unsigned capacity;int* array;};// function to create a queue of given capacity.// It initializes size of queue as 0struct Queue* createQueue(unsigned capacity){struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue));queue->capacity = capacity;queue->front = queue->size = 0;queue->rear = capacity - 1; // This is important, see the enqueuequeue->array = (int*) malloc(queue->capacity * sizeof(int));return queue;}// Queue is full when size becomes equal to the capacityint isFull(struct Queue* queue){ return (queue->size == queue->capacity); }// Queue is empty when size is 0int isEmpty(struct Queue* queue){ return (queue->size == 0); }// Function to add an item to the queue.// It changes rear and sizevoid enqueue(struct Queue* queue, int item){if (isFull(queue))return;queue->rear = (queue->rear + 1)%queue->capacity;queue->array[queue->rear] = item;queue->size = queue->size + 1;printf("%d enqueued to queue\n", item);}// Function to remove an item from queue.// It changes front and sizeint dequeue(struct Queue* queue){if (isEmpty(queue))return INT_MIN;int item = queue->array[queue->front];queue->front = (queue->front + 1)%queue->capacity;queue->size = queue->size - 1;return item;}// Function to get front of queueint front(struct Queue* queue){if (isEmpty(queue))return INT_MIN;return queue->array[queue->front];}// Function to get rear of queueint rear(struct Queue* queue){if (isEmpty(queue))return INT_MIN;return queue->array[queue->rear];}// Driver program to test above functions./int main(){struct Queue* queue = createQueue(1000);enqueue(queue, 10);enqueue(queue, 20);enqueue(queue, 30);enqueue(queue, 40);printf("%d dequeued from queue\n", dequeue(queue));printf("Front item is %d\n", front(queue));printf("Rear item is %d\n", rear(queue));return 0;}
/*The functions supported by queue are :empty() – Returns whether the queue is emptysize() – Returns the size of the queuefront() – Returns a reference to the first element of the queueback() – Returns a reference to the last element of the queuepush(g) – Adds the element ‘g’ at the end of the queuepop() – Deletes the first element of the queue*/#include <iostream>#include <queue>using namespace std;void showq(queue <int> gq){queue <int> g = gq;while (!g.empty()){cout << '\t' << g.front();g.pop();}cout << '\n';}int main(){queue <int> gquiz;gquiz.push(10);gquiz.push(20);cout << "The queue gquiz is : ";showq(gquiz);cout << "\ngquiz.size() : " << gquiz.size();cout << "\ngquiz.front() : " << gquiz.front();cout << "\ngquiz.back() : " << gquiz.back();cout << "\ngquiz.pop() : ";gquiz.pop();showq(gquiz);return 0;}
