当前位置:网站首页>C language queue implementation

C language queue implementation

2022-06-12 07:16:00 Sour plum is not sour

The following explanation is one-sided , But it is easy for beginners to understand the code :
1. Queues are first in first out data structures
2. The queue structure has only two pointers, the head and the tail , Does not contain stored data . It is only responsible for recording the beginning and end of the queue , It is convenient to understand the implementation of queues .
3. The queue member structure contains a variable for storing data , And a pointer to the next member , Pointer to the next queue member .
4. To join the queue, change the end of queue pointer of the queue structure to the next queue member , At the same time, the pointer of the queue member structure should point to the new member .
5. The pointer of the queue structure is used to record that the head and tail of the entire queue point to . The pointer in the queue member structure is used to record where the new member is .

Code :

#include <stdio.h>// fifo 
#include <stdlib.h>
#define TURE 1
#define FALSE 0

struct STU
{
    
	int num;
	struct STU *next;
};
//  queue   yes   from   Tail insert element ,  Delete element from header  
struct QUEUE
{
    
	struct STU * front;
	struct STU * rear;
};

struct QUEUE * create_queue(void);// Create a queue 
int pd_queue(struct QUEUE *p);// Judge the queue 
void insert_queue(struct QUEUE *p);// Insert queue 
void Tra_queue(struct QUEUE *p);// Output 
void delete_queue(struct QUEUE *p);// Delete 

int main(void)
{
    
	struct QUEUE *p;
	p = create_queue();	// Create a queue 
	insert_queue(p);	// Insert 
	insert_queue(p);// Insert 
	insert_queue(p);// Insert 
	Tra_queue(p);// Output 

	delete_queue(p);// Delete 
	Tra_queue(p);// Output 

	insert_queue(p);// Insert 
	insert_queue(p);// Net income 
	Tra_queue(p);// Output 

	delete_queue(p);// Delete 
	delete_queue(p);// Delete 
	delete_queue(p);// Delete 
	delete_queue(p);// Delete 
	Tra_queue(p);// Output 
	return 0;
}

// Create a queue 
struct QUEUE * create_queue(void)
{
    
	struct QUEUE *p;

	p = (struct QUEUE *)malloc(sizeof(struct QUEUE));
	if(p == NULL)
	{
    
		printf(" Space application failed , The system is shutting down ~\n");
		return 0;
	}

	p->front = NULL; //  Header  
	p->rear = NULL; //  Tail 

	printf(" Queue created successfully ~\n");
	return p;
}

// Determines if the queue is empty :
int pd_queue(struct QUEUE *p)
{
    
	if(p->rear == NULL)
	{
    
		printf("\t The queue is empty ~\n");
		return FALSE;
	}
	printf("\t The queue is not empty ~\n");
	return TURE;
}

//  The team 
void insert_queue(struct QUEUE *p)
{
    
	struct STU *New;

	New = (struct STU *)malloc(sizeof(struct STU));
	if(New == NULL)
	{
    
		printf(" Space application identification , The system is exiting !\n");
		return;
	}

	if(pd_queue(p) == FALSE)
	{
    
		p->front = p->rear = New;
	}
	else
	{
    
		p->rear->next = New; //  The team 
		p->rear = New; //  Change the direction of the tail of the team 
	}
	printf("\t The data of joining the team is :");
	scanf("%d", &New->num);
	New->next = NULL;
}

void Tra_queue(struct QUEUE *p)
{
    
	struct STU *r;
	r = p->front;

	if(pd_queue(p) == FALSE)
	{
    
		printf(" Space traversal failed !\n");
		return ;
	}

	do
	{
    
		printf(" The element in the queue is :%d\n", r->num);
		r = r->next;
	}while(r != NULL);
}

void delete_queue(struct QUEUE *p)
{
    
	struct STU *r;

	if(pd_queue(p) == FALSE)
	{
    
		printf(" Unable to complete the delete operation !\n");
		return ;
	}

	r = p->front;
	if(p->front == p->rear)
	{
    	
		p->front = p->rear = NULL;		
	}
	else
	{
    
		p->front = r->next;
	}		
	printf("%d Out of the team !\n", r->num);
	free(r);
}

function :

 Queue created successfully ~
         The queue is empty ~
         The data of joining the team is :1
         The queue is not empty ~
         The data of joining the team is :2
         The queue is not empty ~
         The data of joining the team is :3
         The queue is not empty ~
 The element in the queue is :1
 The element in the queue is :2
 The element in the queue is :3
         The queue is not empty ~
1 Out of the team !
         The queue is not empty ~
 The element in the queue is :2
 The element in the queue is :3
         The queue is not empty ~
         The data of joining the team is :
原网站

版权声明
本文为[Sour plum is not sour]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203010558098842.html