一. 队列相关概念
队列是只允许在一段进行插入元素, 在另一端进行删除元素的线性表,即只允许对队列进行尾插,头删的操作.队列具有先进先出, 后进后出的特性.
1.初始化
void SeqQueInit(SeqQue* q)
{if(q == NULL){return;//非法输入}q -> head = 0;q -> tail = 0;q -> size = 0;
}
2. 打印队列信息
void TestPrintChar(SeqQue* q, char* msg)
{printf("[ %s ]\n", msg);printf("size = %d\n", q -> size);printf("head = %d\n", q -> head);printf("tail = %d\n", q -> tail);if(q == NULL){return;//非法输入}int i = q -> head;for(; i < q -> tail; i++){printf("[%c]", q -> data[i]);}printf("\n");
}
3. 扩容
void SeqQueReSize(SeqQue* q)
{if(q == NULL){return;//非法输入}if(q -> size < SEQQUEMAX){return;//队列未满}int size = 2 * SEQQUEMAX + 1;SeqQueType* new_ptr = (SeqQueType*)malloc(size);int i = 0;for(; i < q -> size; i++){new_ptr[i] = q -> data[i];}free(q -> data);
}
4.入队列
void SeqQuePush(SeqQue* q, SeqQueType value)
{if(q == NULL)//空队列{q -> size++;q -> data[q -> head] = value;q -> data[q -> tail] = value;return;}if(q -> size >= SEQQUEMAX){SeqQueReSize(q);return;//满队列}q -> data[q -> tail++] = value;if(q -> tail > SEQQUEMAX){q -> tail = 0;}++q -> size;
}
5. 出队列
void SeqQuePop(SeqQue* q)
{if(q == NULL){return;//非法输入}if(q -> size == 0){return;//空队列}++q -> head;if(q -> head >= SEQQUEMAX){q -> head = 0;}--q -> size;
}
6. 取队首元素
int SeqQueGetFront(SeqQue* q, SeqQueType *value)
{if(q == NULL || value == NULL){return -1;//非法输入}if(q -> size == 0){return -1;//空队列}*value = q -> data[q -> head];return 0;
}
7. 销毁队列
void SeqQueDestroy(SeqQue* q)
{if(q == NULL){return;//非法输入}q -> size = 0;q -> head = 0;q -> tail = 0;
}