Data structure - stacks and queues 03

The basic sequence of operations of the queue

As mentioned previously represents the state of the queue with two pointers, as the difference between the team and the team is full empty state, so that front pointer moves forward one position.

Sequence structure definition:

typedef strct
{
	datatype data[QUEUE_SIZE];
	int front, rear;
}SeqQueue;

1. The blank stack
on an empty initialization condition team front = rear = QUEUE_SIZE-1 provided the head and tail pointer.

Program:

/*======================================
函数功能:顺序队列置队空
函数输入:队列起始地址
函数输出:无
========================================*/
void initialize_SqQueue(SeqQueue *sq)
{
    sq->front = QUEUE_SIZE-1;
    sq->rear  = QUEUE_SIZE-1;
}

2. Empty Team judged
according to the condition of empty rear = front, determines whether the queue is empty: Empty Team returns a non-null return force ,; 0.

Program:

/*======================================
函数功能:顺序队列判队空
函数输入:队列起始地址
函数输出:1——队空;0——队非空
========================================*/
int  Empty_SqQueue(SeqQueue *sq)
  {  if ( sq->rear == sq->front )  return TRUE;
     else return FALSE;
  } 

3. Take the head elements
if the queue is not empty, return to a position where the head elements subscript; -1 otherwise.

Program:

/*======================================
函数功能:顺序队列取队头元素
函数输入:队列起始地址
函数输出:-1——队空标志;其他值——队头元素位置
========================================*/
int  get_SqQueue(SeqQueue *sq )
{
  if ( ! Empty_SqQueue(sq) )//队非空 
      return (sq->front+1) % QUEUE_SIZE;
  return -1;//队空 
} 

4. enqueue
if the team is full, return FALSE; otherwise, the tail pointer is moved back a rear, inserted into the team at the rear pointing to the element value.

/*======================================
函数功能:顺序队列元素入队
函数输入:队列起始地址,入队元素值
函数输出:0——队满,操作不成功;1——队非满,操作成功
========================================*/
int Insert_SqQueue(SeqQueue*sq, datatype x)
{
	if(sq->front==(sq->rear+1)%QUEUE_SIZE)//判队满
	    return FALSE;
    else
	{
		sq->rear=(sq->rear+1)%QUEUE_SIZE;//队尾指针向后移一位
		sq->data[sq->rear]=x;    //元素x入队
		return TRUE; 
	 } 
}

5. dequeue
if the team is not empty, a head pointer front moves back, front returned, otherwise empty tag team -1.
Note that the difference between the operating force and take the head elements.

/*======================================
函数功能:顺序队列出队
函数输入:队列起始地址
函数输出:-1——队空标志;其他值——队头元素位置
========================================*/
int Delete_SqQueue(SeqQueue *sq)
{
	if(!Empty_SqQueue(sq))//队非空
	{
		sq->front=(sq->front+1)%QUEUE_SIZE;
		return sq->front;
	 } 
	 return -1;//队空 
}

Chain queue - the problem is introduced
when a circular queue achieve Triangle, if there is no team full of restrictions, will face an array overflow problem, which makes the results of the scale accordingly limited.

Chain queue definition:
queue (queue chain storage structure) represented by the list, and is limited only to delete the inserted end of the table in a single linked list header

Storage Structure queue

Design chain queue data structure
list consists of a single column list, the head pointer pointing to the first node front, rear tail pointer pointing to the tail pointer, the head and tail pointers combined in one structure, lq queue pointer pointing to this structure.
Here Insert Picture Description
1) description of the data type hinge point

typedef struct node
{
	datatype data;
	struct node *next;
 } LinkListNode;

2) the head and tail pointers in the chain Data Type Description

 typedef struct 
 {
 	LinkListNode *front,*rear;
 }LinkQueue;

3) chain type data queue pointer lq

LinkQueue *lq;

Lq queue pointer chain set purposes:

Discussion: Set lq queue head and tail pointer to a pointer chain structure, and complete delivery queue information so easy.

The basic operation of the queue chain

1. Initialize blank team

Establishing a first link queue only the head node:
applying a node;
pointer field blanking node;
queue head and tail pointers point to this node.
Here Insert Picture Description
Malloc () function to allocate a size LinkListNode bytes of space, and the start address of the pointer variable lq-> front of.

/*======================================
函数功能:链队列初始化
函数输入:队列起始地址
函数输出:无
========================================*/
void initialize_LkQueue(LinkQueue *lq)
{
	lq->front=(LinkListNode *) malloc(sizeof(LinkListNode));
	lq->front->next=NULL;
	lq->rear=lq->front;
}

2. Air Force sentence
Here Insert Picture Description
if the head and tail pointers are equal teams returned empty mark TRUE; otherwise, returns a non-empty tag FALSE.

/*======================================
函数功能:链队列判队空
函数输入:队列起始地址
函数输出:1——队空;0——队非空
========================================*/
int Empty_LkQueue( LinkQueue *lq )
{    
if  ( lq->front  ==  lq->rear)  return TRUE;
     else   return FALSE;
} 

3. Take the head node team

Returns whether successful logo, node value.

If the queue is not empty, queue head pointer to take the node value;
returning operation success flag TRUE;
otherwise returns FALSE.

Program:

/*======================================
函数功能:链队列取队头结点
函数输入:队列起始地址,(队列结点值)
函数输出:0——队空;1——队非空
========================================*/
int Get_LkQueue(LinkQueue *lq, datatype *x)
{   
 	if ( Empty_LkQueue(lq))  return FALSE;  	//队空
 	x = &(lq->front->next->data);			//取队头结点值
 	return TRUE;
}

4. into the team

Application new node into the queue tail chains; modification queue tail pointer; new node assignment x.
Here Insert Picture Description
Program:

/*======================================
函数功能:链队列入队
函数输入:队列起始地址,入队列结点值
函数输出:无
========================================*/
void Insert_LkQueue(LinkQueue *lq, datatype x)
{  
  lq->rear->next=(LinkListNode *)malloc(sizeof( LinkListNode )); 
 //新结点链入队尾
 lq->rear=lq->rear->next;	//修改队列尾指针
 lq->rear->data=x; 		//新结点赋值
 lq->rear->next=NULL; 	//尾结点指针域置结束标志NULL
}  

5. dequeue
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
pseudocode description:

If the queue is not empty, the HOL find node s;
if only one node queue, the queue empty set;
off s team first node, the first node to modify the target domain;
return address s;
return NULL;

/*======================================
函数功能:链队列出队
函数输入:队列起始地址
函数输出:队头结点地址
========================================*/
LinkListNode *Delete_LkQueue(LinkQueue *lq) {
	LinkListNode *s;
	if(!Empty_LkQueue(lq)) { //队非空
		s=lq->front->next;//s指向队头结点
		if(s->next==NULL) //队中只有一个结点
			lq->rear=lq->front;//队列置空
		else lq->front->next=s->next;//摘下队头结点
		return(s);           //返回摘下的队头结点地址
	}
	return NULL;             //队空时,返回NULL
}

Question: memory leaks.

6. Destruction chain queues
in order to prevent memory leaks, do destruction queue.

/*======================================
函数功能:链队列的销毁
函数输入:队列起始地址
函数输出:无
========================================*/
void Destory_LkQueue(LinkQueue *lq)
{
	LinkListNode *s;
	
	while(!Empty_LkQueue(lq))
	{
		s=Delete_LkQueue(lq);
		free(s;)
	}
	free(lq->front);
	lq->front=NULL;
	lq->rear=NULL;
 } 
Published 26 original articles · won praise 3 · Views 1476

Guess you like

Origin blog.csdn.net/herui7322/article/details/104083427