Basic queue operation implementation

Source: Internet
Author: User

Queue)

A queue is a typeFirst in first out (FIFO)Linear table. It only allows element insertion at one end of the table (team end/rear), while element deletion at the other end (team head/Front. The insert operation is called "join" or "enter", and the delete operation is called "exit" or "quit. The queue is as follows:


1. ordered Team

The ordered storage structure of the queue must be implemented using an array and two integer variables. The array is used to store all elements in the queue. The two integer variables are used to store the subscript positions of the header and tail elements, respectively, they are called the head pointer and tail pointer respectively.

Assuming that the number of elements in the queue does not exceed maxsize and all element data types are elemtype, the ordered queue type sqqueue is defined as follows:

Typedefstruct

{

Elemtype data [maxsize];

Int front, rear;

} Sqqueue;

In the ordered queue * Q, the empty queue condition is Q-> front = Q-> rear; the full queue condition is Q-> rear = MaxSize-1; element E is used to add element e to the end of the team first, and then add element e to the end of the team. The departure operation is to first add the first pointer to the team, and then take out the element at the head of the team; the team tail pointer always points to the team tail element in the current queue, while the team head pointer always points to the previous position of the current queue squadron Header element.

The basic operations of the sequence queue are as follows:

# Include <stdio. h> # include <stdlib. h> # define maxsize 20 // maximum number of queue elements typedef char elemtype; // queue element type typedef struct // order team {elemtype data [maxsize]; // data element int front; // head pointer int rear; // team end pointer} sqqueue; void initqueue (sqqueue * & Q) // initialize the queue {q = (sqqueue *) malloc (sizeof (sqqueue); q-> front = Q-> rear =-1;} void destoryqueue (sqqueue * & Q) // destroy the queue {free (Q);} bool queueempty (sqqueue * q) // determine whether the queue is empty {return (Q-> front = Q-> rear);} bool queuefull (sqqueue * q) // determine whether the queue is full {return (Q-> rear = MaxSize-1);} bool enqueue (sqqueue * & Q, elemtype e) // Queue (insert element) {If (Q-> rear = MaxSize-1) // The queue is full and cannot be inserted return false; q-> rear ++; q-> data [q-> rear] = E; return true;} bool dequeue (sqqueue * & Q, elemtype & E) // release (delete element) {If (Q-> front = Q-> rear) // If the queue is empty, return false cannot be deleted; q-> front ++; E = Q-> data [q-> front]; return true;} int main (){...; return 0 ;}

2. Annular queue

Connect the front-end and back-end of the array to form a circular sequence table-circular queue.

The basic operations of the ring queue are as follows:

# Include <stdio. h> # include <stdlib. h> # define maxsize 10 // maximum number of queue elements typedef char elemtype; // queue element type typedef struct // annular queue {elemtype data [maxsize]; // data element int front; // head pointer int rear; // team end pointer} qutype; void initqueue (qutype * & qu) // initialize the queue {Qu = (qutype *) malloc (sizeof (qutype); qu-> front = qu-> rear = 0;} void destoryqueue (qutype * & qu) // destroy queue {free (qu );} bool queueempty (qutype * & qu) // determines whether the queue is empty {return (qu-> front = qu-> rear);} bool queue Full (qutype * & qu) // determine whether the queue is full {return (qu-> front ==( qu-> rear + 1) % maxsize );} bool enqueue (qutype * & Qu, elemtype e) // enter the team {If (qu-> front = (qu-> rear + 1) % maxsize) // note! Return false; qu-> rear = (qu-> rear + 1) % maxsize; qu-> data [Qu-> rear] = E; return true ;} bool dequeue (qutype * & Qu, elemtype & E) // departure {If (qu-> front = qu-> rear) return false; qu-> front = (qu-> front + 1) % maxsize; E = qu-> data [Qu-> front]; return true;} int main () {...; return 0 ;}

3. blockchain

The queue chain storage structure is implemented through a single-linked table composed of nodes. At this time, you can only delete the header of the single-linked table and insert the table at the end of the single-linked table, therefore, two pointers are required: front and rear. Use front to point to the first node of the team, and use rear to point to the End Node of the team. A single-chain table used to store queues becomes a chain.

The data node type qnode in a chain (lead node) is defined as follows:

Typedefstruct qnode

{

Elemtype data;

Struct qnode * next;

} Qnode; // defines the data node type of the Chain team.

The linkqueue type of a chain node is defined as follows:

Typedefstruct

{

Qnode * front;

Qnode * rear;

} Linkqueue; // the definition of the team type.

The basic operations of the blockchain are as follows:

# Include <stdio. h> # include <stdlib. h> typedef char elemtype; typedef struct qnode {elemtype data; struct qnode * Next;} qnode; // data node typedef struct {qnode * front; // queue head pointer qnode * rear; // team end pointer} linkqueue; // queue node void initqueue (linkqueue * & Q) // initialize the Chain {qnode * P = (qnode *) malloc (sizeof (qnode); // create the header node if (! P) {printf ("memory allocation failed \ n"); exit (1);} p-> next = NULL; q = (linkqueue *) malloc (sizeof (linkqueue); // allocate the Node space of the blockchain if (! Q) {printf ("memory allocation failed \ n"); exit (1) ;}q-> front = Q-> rear = P ;} void destoryqueue (linkqueue * & Q) // destroy the Chain Team {qnode * P; while (Q-> front! = NULL) {P = Q-> front; q-> front = p-> next;} Free (p);} bool queueempty (linkqueue * q) // determine whether the queue is empty {return (Q-> front-> next = NULL);} void enqueue (linkqueue * & Q, elemtype E) // queue {qnode * P; P = (qnode *) malloc (sizeof (qnode); // create a new node if (! P) {printf ("memory allocation failed! \ N "); exit (1);} p-> DATA = E; P-> next = NULL; q-> rear-> next = P; q-> rear = P;} bool dequeue (linkqueue * & Q, elemtype & E) // team out {qnode * P; if (Q-> front-> next = NULL) // If the queue is empty, return false is invalid; P = Q-> front-> next; E = p-> data; q-> front-> next = p-> next; If (p-> next = NULL) // After deleting the last node, point the tail pointer to the header node Q-> rear = Q-> front; free (p); Return true;} int main (){...; return 0 ;}

Notes:

1. Q-> front = Q-> rear are empty conditions of the ordered queue and the circle queue; the full condition of the ordered queue is P-> rear = MaxSize-1, q-> front = (Q-> rear + 1) % maxsize;

2. Different sequential queues and ring queues lead to different elements in the last bucket. Initialization of the sequence queue: Q-> front = Q-> rear =-1, so the elements are stored from Q-> data [0], while the initialization of the ring queue: q-> front = Q-> rear = 0, so the elements are stored from Q-> data [1], and Q-> data [0] is idle;

3. When the ordered queue meets the full team condition, it may be a false overflow (such as P-> rear = MaxSize-1. At this time, even if several data in the team still meet the full team condition, however, a number of spaces have been made up, which is a false overflow), and the ring queue does not have any extra storage space to satisfy the team's full conditions;

4. The number of elements in the ordered queue is rear-front, and the number of elements in the circular queue is (rear-front + maxsize) % maxsize;

5. initialize the blockchain. When the first node is created, allocate memory space to the node;

6. When leaving the queue, when the last node is deleted, the tail pointer is also released. Therefore, the tail pointer should be assigned a new value to point to the header node.

Tuesday, July 29, 2014




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.