数据结构-线性结构-队列常用函数实现
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define Elemtype int
struct Queue{
Elemtype val;
struct Queue* next;
};
typedef struct Queue Queue;
void Init_Queue(Queue*front, Queue*back);
void Clear_Queue(Queue *front);
void Destory_Queue(Queue *front);
bool Queue_isempty(Queue *front);
int Queue_Length(Queue *front);
bool Queue_GetFront(Queue *front,Elemtype *e);
bool Queue_GetBack(Queue *back,Elemtype *e);
void Queue_Push(Queue *front,Queue* back,Elemtype e);
bool Queue_Pop(Queue *back,Elemtype *e);
void Init_Queue(Queue*front, Queue*back)
{
Queue* q = (Queue*)malloc(sizeof(Queue));
q->next = NULL;
q->val = 0;
front = back = q;
return q;
}
void Clear_Queue(Queue* front)
{
Queue* temp = front->next;
while(temp){
front->next = temp->next;
free(temp);
temp = front->next;
}
front->val = 0;
}
void Destory_Queue(Queue **front,Queue **back)
{
Clear_Queue(*front);
free(*front);
free(*back);
*front = NULL;
}
bool Queue_isempty(Queue *front)
{
if(!(front->next))
return true;
else
return false;
}
int Queue_Length(Queue *front)
{
Queue *temp = front->next;
int num = 0;
while(temp){
temp = temp->next;
++num;
}
return num;
}
bool Queue_GetFront(Queue *front,Elemtype *e)
{
if(front->next != NULL){
*e = (front->next)->val;
return true;
}
else
return false;
}
void Queue_Push(Queue *front,Queue *back,Elemtype e)
{
Queue *temp = (Queue*)malloc(sizeof(Queue));
temp->val = e;
temp->next = front->next;
front->next = temp;
if(back->next != NULL){
back = back->next;
back->next = NULL;
}
}
bool Queue_Pop(Queue *front,Queue *back,Elemtype *e)
{
if(Queue_isempty(front)){
return false;
}
else{
Queue* temp = front->next;
while(temp){
if(temp->next == back){
*e = back->val;
temp->next = NULL;
free(back);
back = temp;
break;
}
else{
temp = temp->next;
}
}
}
}
bool Queue_GetBack(Queue *back,Elemtype *e)
{
if(back == NULL)
return false;
else{
*e = back->val;
return true;
}
}