Data Structures-Queue implementations (sequential queues and chain queues) and C + + templates
One, sequential queue
The sequential storage structure of a queue is called a sequential queue, and the sequential queue is actually a sequential table with limited operations.
① and sequential tables, sequential queues use a vector space to hold elements in the current queue.
② because the position of the queue's head and tail is changed, the two pointers front and rear respectively indicate the position of the team head element and the tail element in the vector space, and their initial value should be set to 0 when the queue is initialized.
Attention:
① the queue is empty when the head-tail pointer is equal.
② in a non-empty queue, the team head pointer always points to the team head element, and the tail pointer always points to the next position in the tail element. (So the last one in the following loop order queue when the end of the team pointer points to the team head pointer indicates that the queue is full)
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define OK 1#define FAIL 0//Maximum Queue Length cycle queue size minus 1 In order to differentiate between team space and team full judgment #define QUEUE_MAX_SIZE (10+1) typedef int QELEMTYPE;//based on array typedef struct{qelemtype *QUEUE; int front; Represents the head int rear of the current queue; Represents the end of the queue where data is inserted (no data)}my_queue_t;int My_atoi (char *str, int m) {int I;int num = 0;if (!STR) return Fail;while (str[i]! = {if (!isdigit (Str[i])) return fail;num = num*10 + str[i]-' 0 '; i++;} return OK; }//Initialize queue void Queue_init (my_queue_t *q) {q->queue = (Qelemtype *) malloc (queue_max_size * sizeof (Qelemtype)); q-> Front = 0;q->rear = 0;if (!q->queue) exit ( -1); elseprintf ("Queue Init ok!\n");} Queue null judgment int queue_empty (my_queue_t *q) {if (Q->front = = q->rear) {printf ("The queue is empty!\n"); return OK;} Elsereturn FAIL;} Queue full judgment int queue_full (my_queue_t *q) {if ((Q->rear + 1)%queue_max_size = = Q->front) {printf ("The queue is full!\n"); return OK;} else return FAIL;} Take the team head element and set the team head element out of the team int queue_pop (my_queue_t *q, int *e) {if (Queue_empty (q)) return fail;*e = Q->queue[q->front];q->front = (q->front + 1)% Queue_max_size;return OK;} The end of the insertion element element returns whether the insert succeeded int Queue_push (my_queue_t *q,int <span style= "font-family:arial, Helvetica, Sans-serif;" >e) </span>{if (Queue_full (q))//Queue full returns INSERT Failure return Fail;q->queue[q->rear] = E;q->rear = (q->rear + 1)% queue_max_size;printf ("Insert%d at the QUEUE position%d!\n", e,q->rear); return OK;} print queue element void Queue_print (my_queue_t *q) {int i;if (! Queue_empty (q)) {printf ("The queue is:"); for (i=q->front; i!=q->rear; i++) {printf ("%d", q->queue[i]); if ((i+1)% Queue_max_size = = 0) i =-1; For loop auto +1}printf ("\ n");}} int main (int argc, char **argv) {my_queue_t my_queue;char *str;int num; Queue_init (&my_queue); printf ("Please input action, ' P ' or ' Q ' \ n"), while (scanf ("%s", str)!=eof) {if (* (str) = = ' P ' | | * (str) = = ' P ') {printf ("please Input Insert num: "); scanf ("%d ", &num); Queue_push (&my_queue,num)) {printf ("queue Push fail!\n");}} else if (* (str) = = ' Q ' | | * (str) = = ' Q ') {if (Queue_pop (&my_queue, &num)) {printf ("Get the Queue head:%d\n", num);}} Queue_print (&my_queue);p rintf ("Please input action, ' P ' or ' Q ' \ n");} return 0;}
second, chain queue
The chain storage structure of a queue is abbreviated as a chain queue. It is a single linked list that restricts only the header deletions and footer insertions.
Attention:
Adds a tail pointer to the last node on the list, making it easy to insert at the end of the table.
See the chain queue, the figure Q is a listnode type of pointer.
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #define OK 1#define FAIL 0//with linked list queue Length unrestricted Typede f int qelemtype;//defines the list typedef struct LISTNODE{QELEMTYPE data;struct ListNode *next;} Listnode;typedef ListNode *ptrlist; ListNode head;//A queue model based on linked list struct{ptrlist front; Ptrlist Rear; }my_queue_t; Initialize queue void Queue_init (my_queue_t *q) {Q->front = Q->rear = (ptrlist) malloc (sizeof (ListNode));q->front-> Next = null;if (!q->front) exit ( -1); elseprintf ("Queue Init ok!\n");} Queue null judgment int queue_empty (my_queue_t *q) {if (Q->front = = q->rear) {printf ("The queue is empty!\n"); return OK;} Elsereturn FAIL;} Take the team head element and put the team head element out of the team int queue_pop (my_queue_t *q, Qelemtype *e) {ptrlist p;if (queue_empty (q)) return fail;p = q->front; Get Queue header node p to release the node space after the team *e = p->data; node Data Q->front = p->next; Move the queue head pointer to the next node free (p); return OK;} The tail insert element element returns whether the insert succeeded int Queue_push (my_queue_t *q, Qelemtype e) {ptrlist New_node = (ptrlist) malloc (sizeof (Listnode)); new_node->next = NULL; if (!new_node) return FAIL; Application memory failed Insert unsuccessful q->rear->data = e; Inserting data into the current node q->rear->next = New_node; The current node next points to the node of the new application q->rear = New_node; The queue tail pointer points to the new requested node printf ("Insert%d at the queue!\n", e); return OK;} print queue element void Queue_print (my_queue_t *q) {ptrlist p;p = q->front;if (! Queue_empty (q)) {printf ("The queue is:") and while (P! = q->rear) {printf ("%d", p->data);p = P->next;} printf ("\ n");}} int main (int argc, char **argv) {my_queue_t my_queue;char *str;int num; Queue_init (&my_queue); printf ("Please input action, ' P ' or ' Q ' \ n"), while (scanf ("%s", str)!=eof) {if (* (str) = = ' P ' | | * (str) = = ' P ') {printf ("please Input Insert num: "); scanf ("%d ", &num); Queue_push (&my_queue,num)) {printf ("queue Push fail!\n");}} else if (* (str) = = ' Q ' | | * (str) = = ' Q ') {if (Queue_pop (&my_queue, &num)) {printf ("Get the Queue head:%d\n", num);}} Queue_print (&my_queue);p rintf ("Please input action, ' P ' or ' Q ' \ n");} return 0;}
Iii. C + + queue templates
Header file #include <queue>
Data Structures-Queue implementations (sequential queues and chain queues) and C + + templates