Data structure - queues (sequential queues, cyclic sequential queues, chain queues, double-ended queues)

Posted by chuckwgn on Sat, 10 Aug 2019 15:24:06 +0200

Queue:

Team, in essence, is a kind of linear table with limited operation, which is limited to allow insertion only at one end of the table and deletion at the other end of the table. The end that can be inserted is the end of the team, and the end that can be deleted is the head of the team.

Operation:

Entry and exit.

The characteristics of the queue:

First in first out (FIFO) is like a train passing through a tunnel.

The storage structure of the queue:

Queues are divided into sequential queues and chain queues by storage structure

2. Sequential teams:

Using array simulation, the team has two markers, one marking the head of the team and one marking the tail of the team. When the head of a team is equal to the tail, it means the team is empty.

When an element enters the team, the tag of the end of the team moves backward and the position of the end of the team stores the element. If the first element enters the team, the head of the team points to the previous position of the element.

When an element is out of the team, the head tag moves backward, points to the position of the element just out of the team, and takes out the element referred to by the head of the team.

#include<stdio.h>
#include<malloc.h>
# define MaxSize 8

//Sequential Team
typedef struct{
	int data[MaxSize]; //8
	int front;
	int rear;
}SqQueue; 

//Sequential queue emptying 
void sqEmpty(SqQueue Sq)
{
	if(Sq.front == Sq.rear)
	{
		printf("The queue was empty\n");
	}
}

//A sequential team is included in the team.
void pushQueue(SqQueue &Sq,int data){
	if(Sq.rear!=MaxSize-1)//Team dissatisfaction 
	{
		Sq.data[++Sq.rear]=data;
		printf("%d Successful entry\n",data);
	}
	else
	{
		printf("%d Failure to join the team\n",data);
	}
} 

//Sequential queue
void popQueue(SqQueue &Sq)
{
	if(Sq.rear==Sq.front)
	{
		printf("Team Air, Element Out of Team Failure\n");
	}
	else
	{
		printf("%d Success in team formation\n",Sq.data[++Sq.front]);//front + + is to assign the value first, then calculate it. After the first element is out of the queue, it needs to move to the next element. 
	}
}

void initSqQueue()
{
	SqQueue Sq;
	Sq.front=Sq.rear=-1;//Initialize the first and last pointers of the team to - 1; 
	sqEmpty(Sq); 
	int elem[5]={5,4,6,4,2};//Setting initialization queue elements 
	for(int i=0;i<5;i++)
		pushQueue(Sq,elem[i]);//Join the team 
	for(int i=0;i<5;i++)
		popQueue(Sq);//Team out 
} 

int main()
{
	initSqQueue();//Sequential queue 
 } 

Disadvantages:

When the elements enter the queue, after all the elements leave the queue, front= rear, the queue is empty, but front and rear are always moving to the back of the array, causing waste of front space and false spillovers (front= rear= MaxSize-1). So there's a circular queue.

3. Cyclic queue:

Make the queue a loop, and let both front and rear go around the loop. The circular queue is an improved sequential queue.

Let the front tag execute the statement front=(front+1)%MaxSize. If the initial value of the front is 0, in an infinite loop, the front tag is 0, 1, 2, 3, 4,..., MaxSize-1. That is, the infinite number of cycles from 0 to MaxSize-1. rear is the same.

However, the circular queue must leave a blank space, because if there is no blank space (full queue, right-to-back: rear = front), it is difficult to distinguish full queue from empty queue state markers.

Status:

Team full: (queue.rear+1)% MaxSize = queue. front

Team air: queue. rear = queue.front.

Operation: When an element enters or leaves the queue, move the pointer first, and then take out the element.

Entry: queue.rear= (queue.rear + 1)% MaxSize; queue. data [queue.rear] = x;

Queue.front= (queue.front+1)% MaxSize; x = queue.data [queue.front];
 

  

#include<stdio.h>
#include<malloc.h>
# define MaxSize 8

//Cyclic queue 
typedef struct{
	int data[MaxSize]; //8
	int front;
	int rear;
}SqQueue; 

//Cyclic queue emptying 
void sqEmpty(SqQueue Sq)
{
	if(Sq.rear == Sq.front)
	{
		printf("The queue was empty\n");
	}
}

//A sequential team is included in the team.
void pushQueue(SqQueue &Sq,int data){
	if((Sq.rear + 1)%MaxSize != Sq.front)//Team dissatisfied before joining the team 
	{
		Sq.rear = (Sq.rear + 1)%MaxSize; 
		Sq.data[Sq.rear]=data;
		printf("%d Successful entry\n",data);
	}
	else
	{
		printf("The team is full.%d Failure to join the team\n",data);
	}
} 

//Sequential queue
void popQueue(SqQueue &Sq)
{
	if(Sq.rear==Sq.front)
	{
		printf("Team Air, Element Out of Team Failure\n");
	}
	else
	{
		printf("%d Success in team formation\n",Sq.data[++Sq.front]);//front + + is to assign the value first, then calculate it. After the first element is out of the queue, it needs to move to the next element. 
	}
}

//Create a circular queue 
void initSqQueue()
{
	SqQueue Sq;
	Sq.front=Sq.rear=-1;//Initialize the first and last pointers of the team to - 1; 
	sqEmpty(Sq); 
	int elem[5]={5,4,6,4,2};//Setting initialization queue elements 
	for(int i=0;i<5;i++)
		pushQueue(Sq,elem[i]);//Join the team 
	printf("\n"); 
		
	for(int i=0;i<2;i++) //Two out of the team 
		popQueue(Sq);//Team out 
	printf("\n"); 
		
	for(int i=0;i<5;i++)
		pushQueue(Sq,elem[i]);
} 

int main()
{
	initSqQueue();//Cyclic queue 
 }