What is a queue structure
The queue structure is classified from data operations, that is, the queue structure has special operation rules. According to the logical structure of the data, the queue structure is actually a linear structure. If the data storage structure is further divided, the queue structure can be divided into two categories.
sequential Queue structure: the data in the queue is saved sequentially even with a set of contiguous memory units. In a program, you can define an array of structures of a specified size as a queue.
chained queue structure: The value of each element in the queue is saved using a linked list.
Both ends are allowed to operate in the queue structure, but the operations at both ends are different. Only deletes can be done at one end of the table, called the team header, and only inserts can be made at the other end of the table, called the team tail. If there is no data element in the queue, it is called an empty queue. From the point of view of data operation, the queue is based on the principle of "advanced first out" to deal with the node.
The queue structure can be understood as: The supermarket line to checkout the crowd, the queue first person to pay the bill, and then there will be people in line at the end of the queue waiting for the checkout, this is a first-served queues in the reality of the example.
There are only two basic operations for a general queue structure:
into queues: adding an element to the end of the queue (equivalent to queuing at the end)
out queue: takes the element out of the right, deletes the element, and makes the latter element the team head.
For the first time, there are simple operations that initialize the queue and get the queue length. Below, we use C + + to establish sequential queue structure, and complete the basic operation of sequential queue structure.
Preparing data
Preparing data is defined as the variables and data structures that need to be used in queue operations.
Copy Code code as follows:
struct data{
String name;
int age;
};
struct Sqtype
{
DATA Data[queuelen]; Array of queues
int head; Team Head
int tail; Team Tail
};
This defines the maximum length queuelen of the queue structure, the type of data elements of the queue structure, and the data structure Sqtype of the queue structure. In the data structure Sqtype, the date is the element, the head is the serial number of the first team, and the tail is the serial number of the tail. When head=0 indicates that the queue is empty and the queue is full when tail=queuelen.
Initializing queue data
The initialization procedure for the sequential queue is as follows:
(1) Queuelen the specified size by symbol constant to apply a memory space to hold the data in the queue.
(2) Set head=0 and tail=0 to represent an empty stack.
The sample code is as follows:
Copy Code code as follows:
Sqtype *sqtypeinit ()
{
Sqtype * q;
if (q=new sqtype)//request queue memory space
{
q->head=0; Set up Team head
q->tail=0; Set the tail of a team
return q;
}
Else
{
return NULL; return empty
}
}
First, use new to request memory, set the team head and tail after the request succeeds, then return to the first address of the application memory, and return NULL if the request fails.
Judging empty queues
An empty queue is judged to determine whether a queue structure is empty. If it is an empty queue, the queue structure does not have data in China. At this point, you can enter such as queue operations, you cannot perform queue operations.
The sample code is as follows:
Copy Code code as follows:
int Sqtypeisempty (Sqtype *q)
{
return (Q->head==q->tail);
}
The input parameter q is a pointer to the operation's queue. In the program, whether the queue is empty according to whether the queue head equals tail.
Judge Full queue
A full queue is judged by whether a queue structure is full or not. If it is a full queue, there is no extra space in the queue to hold the extra data. The test can not be queued to perform a queue operation.
The sample code is as follows:
Copy Code code as follows:
int Sqtypeisfull (Sqtype *q)
{
Return (Q->tail==queuelen)
}
The input parameter q is a pointer to the operation's queue. In the program, whether the queue is tail equal to the maximum value of the queue is queuelen to determine whether the queues are full.
Empty queues
Emptying a queue is clear of all the data in a queue. The sample code is as follows:
Copy Code code as follows:
void Sqtypeclear (Sqtype *q)
{
q->head=0;
q->tail=0;
}
Sets the queue top pointer head and tail pointer tail to 0, which indicates that the vacuuming operation is performed.
Free space
Freeing space is the memory unit that frees up the queue structure. As we know from the previous, in initializing the queue structure, the new keyword is used to allocate the internal deposit. Although you can use the Purge queue operation, the empty queue operation does not free up memory space, which requires the use of the DELETE keyword to release the memory unit.
The sample code is as follows:
Copy Code code as follows:
void Sqtypefree (Sqtype *q)
{
if (q!=null) Delete q;
}
In your program, you call Delete to release the memory space for the new request.
into queues
The entry queue is the basic operation of the queue structure, and the primary operation is to save the data elements to the queue structure. The specific steps in the queue operation are as follows:
(1) First judge the tail of the team, if Tail equals Queuelen, then the overflow, error handling. Otherwise, perform the following actions.
(2) Set tail=tail+1 (the top pointer of the queue plus 1, point to the queue address)
(3) To save the entry queue element to the location that tail points to
The sample code is as follows:
Copy Code code as follows:
int Insqtype (Sqtype *q,data DATA)
{
if (Q->tail==queuelen)
{
cout<< the queue is full! Operation failed! "<<endl;
return 0;
}else
{
q-data[q->tail++]=data; To enter an element into a queue
return 1;
}
}
The input parameter q is a pointer to the operation's queue, and the input parameter is data element that needs to be queued. The program first determines whether the queue overflows, if the overflow does not enter the queue operation, or modify the value of the queue top pointer, and then the element into the queue.
Because the value of the tail starts at 0, the current value is the position of the element corresponding to the array to be inserted, so write q->tail++.
Out of the queue
The queue is the basic operation of the queue structure, the main operation in contrast to the queue, actually pops a data element from the top of the queue. The specific steps for the out queue operation are as follows:
(1) First of all, judge the head of the team, if heads are equal to tail, then express as empty queue, error handling. Otherwise, perform the following steps
(2) from the queue header to remove the team head elements (actually return the team head element of the pointer)
(3) Revise the serial number of the team head to point to the latter element.
The sample code is as follows:
Copy Code code as follows:
DATA *outsqtype (Sqtype *q)
{
if (Q->tail==q->head)
{
The cout<< queue is empty! Operation failed! "<<endl;
Exit (0);
}else
{
Return & (q->data[q->head++]);
}
}
The input parameter q is a pointer to the queue for the operation. The return value of the function is a data type, and the return value is a pointer to the data element.
read the node data.
Read the node data is read the queue structure node data, the read operation here is actually read the queue header data. What needs to be helped is that the operation of the read node data is different from the queue operation. The operation of Read node data is only the content that displays the top node data of the queue, while the Out queue operation pops up the top data of the queue, and the data no longer exists.
The sample code is as follows:
Copy Code code as follows:
DATA * Peeksqtype (Sqtype *q)
{
if (Sqtypeisempty (q))
{
cout<< "Empty Queues" <<endl;
return NULL;
}else
{
Return & (Q->data[q->head]);
}
}
The return value of the function is also a pointer to data type, and the return value is a pointer to the data element.
Calculate Queue Length
The calculation queue Length counts the number of data nodes in the queue. The method of calculating queue length is simpler, and the serial number of the tail is subtracted from the first serial number of the team.
The sample code is as follows:
Copy Code code as follows:
int Sqtypelen (Sqtype *q)
{
return (Q->tail-q->head);
}
Queue structure Operation Instance code:
The complete code will be longer, but the function section is basically consistent with the one described above, mainly by looking at the use of these functions in the main function:
Copy Code code as follows:
#include <iostream>
#include <string>
using namespace Std;
#define Queuelen 10
struct data{
String name;
int age;
};
struct Sqtype
{
DATA Data[queuelen]; Array of queues
int head; Team Head
int tail; Team Tail
};
Initialization of the/******************* queue *************************/
Sqtype *sqtypeinit ()
{
Sqtype * q;
if (q=new sqtype)//request queue memory space
{
q->head=0; Set up Team head
q->tail=0; Set the tail of a team
return q;
}
Else
{
return NULL; return empty
}
}
/******************* Judge empty queue *************************/
int Sqtypeisempty (Sqtype *q)
{
return (Q->head==q->tail);
}
/******************* Judge Full Queue *************************/
int Sqtypeisfull (Sqtype *q)
{
return (Q->tail==queuelen);
}
/******************* Empty Queue *************************/
void Sqtypeclear (Sqtype *q)
{
q->head=0;
q->tail=0;
}
/******************* Free Space *************************/
void Sqtypefree (Sqtype *q)
{
if (q!=null) Delete q;
}
/******************* into queue operations *************************/
int Insqtype (Sqtype *q,data DATA)
{
if (Q->tail==queuelen)
{
cout<< the queue is full! Operation failed! "<<endl;
return 0;
}else
{
q->data[q->tail++]=data; To enter an element into a queue
return 1;
}
}
/******************* out queue Operation *************************/
DATA *outsqtype (Sqtype *q)
{
if (Q->tail==q->head)
{
return NULL;
}else
{
Return & (q->data[q->head++]);
}
}
/******************* Read node data *************************/
DATA * Peeksqtype (Sqtype *q)
{
if (Sqtypeisempty (q))
{
cout<< "Empty Queues" <<endl;
return NULL;
}else
{
Return & (Q->data[q->head]);
}
}
/******************* Calculate Queue Length *************************/
int Sqtypelen (Sqtype *q)
{
return (Q->tail-q->head);
}
/********************* Main function ******************************/
int main ()
{
Sqtype *stack;
DATA data,*p;
Stack=sqtypeinit (); Perform an initialization operation
cout<< "execute into queue operation:" <<endl;
cout<< "Enter name, age for Team operation:" <<endl;
while (1)
{
cin>>data.name>>data.age;
if (data.age==0)
{
Break If entered as 0, exit
}
Else
{
Insqtype (Stack,data);
}
}
cout<< "Perform stack operations:" <<endl;
P=outsqtype (stack);
cout<<p->name<< "," <<p->age<<endl;
cout<< "read the first node data:" <<endl;
P=peeksqtype (stack);
cout<<p->name<< "," <<p->age<<endl;
cout<< "Perform a stack operation:" <<endl;
while (1)
{
if (Sqtypeisempty (stack))
{
cout<< "All data out stack, stack is empty!" "<<endl;
Break
}else
{
P=outsqtype (stack);
cout<<p->name<< "," <<p->age<<endl;
}
}
Sqtypefree (stack);
return 0;
}
Program Run Interface: