List and array implementations of FIFO queues

Source: Internet
Author: User

FIFO (First-in, first-out, FIFO) queue: Deletes those elements that remain in the queue for the longest time when a delete operation is performed.

The FIFO queue is an ADT that contains two basic operations: inserting (Put) a new item, deleting (GET) an item that was first inserted.

First, the list of FIFO queue implementationThe difference between a FIFO queue and the lower stack is that the insertion of the new item is at the tail, not the head. So the implementation program saves a tail pointer tail to the last node in the list, so when the put operation, the tail pointer to next points to the new node, and then updates the tail pointer to point to the new node.
Fifo_linklist.cpp
/************************************************************* function: First in-line list implementation: Init, Put, get operation Description: From the head get, Put time from tail: 2015/02/03 quinn**************************************************************/#include <stdio.h> #include <malloc.h> #include <stdlib.h>typedef int item;typedef struct Node node;typedef node* queue;struct Node//Nodes structure {item item; Node* next;}; static int maxn = 10;static Queue q = null;static node* tail = null;//new node node* NewNode (item item, node* next)//next plug-in Enter the latter node {node* x = (node*) malloc (sizeof (*X));//the inserted node X->item = Item;x->next = Next;return x;} Queue initialization of void queueinit (int maxn) {q = NULL;} Determines whether the queue is empty int queueisempty () {return (q = = NULL);} Put operation void Queueput (item item) {if (Queueisempty ()) {q = (tail = NewNode (item, q));} Else{tail->next = NewNode (item, tail->next); tail = Tail->next;} printf ("Put:%d\n", item);} Get operation item Queueget () {if (q = = NULL) {printf ("sequence is empty! \ n "); return-1;} Item FirstItem = header element of q->item;//sequence node* Tmpnode = Q;q = Q->next;freE (Tmpnode); return firstitem;} Test program int Main () {queueinit (MAXN); Queueput (2); Queueput (3); Queueput (4);p rintf ("\ n");p rintf ("Get:%d\n", Queueget ());p rintf ("Get:%d\n", Queueget ());p rintf ("Get:%d\n", Queueget ());p rintf ("Get:%d\n", Queueget ()); System ("pause"); return 0;}


Operation Result:

Second, the array implementation of FIFO queueThe contents of the queue are all elements from head to tail in the array, and when tail reaches the end of the array back to 0, the implementation of the program takes into account the queue full and the queue empty two special states, This article is implemented in 2 ways:
1) Set the array size of the FIFO queue larger than the maximum number of elements the customer will place in the queue 1 (from: "Algorithm: C language Implementation" P93) NULL when head and tail coincide, full when tail+1 and head overlap Fifo_array_1.cpp
/*************************************************************from: "algorithm: C language Implementation" P93 function: An array of FIFO queue implementations: Init, Put, Get operation Description: 1) The contents of the queue are all elements from head to tail in the array, tail back to 02 when the end of the array is reached) sets the array size of the FIFO queue to 1 larger than the maximum number of elements that the customer will place in the queue, which is empty when head and tail overlap When head and tail+1 coincide: 2015/02/03 quinn**************************************************************/#include <stdio.h> #include <stdlib.h>const int maxn = 10; FIFO sizetypedef int item;static Item *q; Queue static int head, tail, n;//queue initialization, initialize array, tail index void queueinit (int maxn) {q = (item*) malloc (sizeof (*Q) * (maxn+1)); N = maxn + 1;head = n;tail = 0;}  Check if the queue is empty int queueisempty () {return (head% N) = = tail; The get operation is head++ after the% operation, and the put operation is tail++ before the% operation}int Queueisfull () {return (tail + 1)%N = = head% N;} Push-in queue, put operation void Queueput (item item) {if (Queueisfull ()) {printf ("queue full, put:%d operation failed \ n", Item); return;} q[tail++] = Item;tail%= n;printf ("Put:%d\n", item);} Out of queue, get operation item Queueget () {if (Queueisempty ()) {printf ("This queue is now empty, get operation failed, return -1\n"); return-1;} Head%= N;return q[head++];} Test program inT Main () {queueinit (MAXN); for (int i = 0; I <=; i++) {queueput (i);} printf ("\ n");p rintf ("Get:%d\n\n", Queueget ()), for (int i = 0; i <; i++) {printf ("Get:%d\n", Queueget ());} System ("pause"); return 0;}
Run results
2) Set the array size of the FIFO queue and the maximum number of elements the customer will place in the queue equal when the put operation causes tail and head to coincide, set the full flag flag_full = 1; (initialization setting flag_full= 0) when the get operation causes tail and head to coincide, set the NULL flag flag_empty = 1; (Initialize setting flag_empty = 1)
Fifo_array_2.cpp
/************************************************************* function: An array of FIFO queue implementations: Init, Put, get operation Description: 1) Set the size of the FIFO queue, The contents of the queue are all elements from head to tail in the array, and back to tail when the end of the array reaches 0 time: 2015/02/03 quinn************************************************** /#include <stdio.h> #include <stdlib.h>const int maxn = 10; FIFO sizetypedef int item;static Item *q;static int head, tail;static int flag_full = 0, flag_empty = 1; Queue full empty flag//queue initialization, initialize array, tail index void queueinit (int maxn) {printf ("Initialize FIFO size is%d\n", MAXN); q = (item*) malloc (sizeof (*Q) * MAXN ); head = 0;tail = 0;} Push-in queue, Put operation void Queueput (item item) {if (flag_full) {printf ("queue full, put:%d operation failed \ n", Item); return;} q[tail++] = Item;tail%= maxn;printf ("Put:%d\n", item); flag_empty = 0; The queue is full {flag_full = 1 when the head and tail coincide with a non-empty if (tail = = head)//put operation;}} Out of queue, get operation item Queueget () {if (flag_empty) {printf ("queue is empty, get operation failed, return -1\n"); return-1;} Head%= maxn;flag_full = 0; Get operation succeeded, full flag set to 0if ((head+1)% MAXN = = tail)//get cause head+1 and tail coincident, queue empty {flag_empty = 1;} return q[head++];} //test program int main () {queueinit (MAXN); for (int i = 0; I <=; i++) {queueput (i);} printf ("\ n");p rintf ("Get:%d\n\n", Queueget ()), for (int i = 0; I <=; i++) {printf ("Get:%d\n", Queueget ());} System ("pause"); return 0;}

Operation Result:


the get and put operations of the FIFO queue ADT can be implemented in constant time regardless of whether arrays or lists are used.
reference: "algorithm: C language Implementation" P93



List and array implementations of FIFO queues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.