队列:只允许在一端进行删除,另一端进行插入的特殊线性表
特点:先进先出
顺序存储
#define max 100
typedef struct node
{
datatype data;
struct node *next;
}Qnode;//链式存储的节点类型
typedef struct
{
Qnode *rear,*front;
}Lqueue;//链队列,将头尾指针装在一起
int empty(Lqueue *s)
{
if(s->front==s->rear) return 0;
else return 1;
}
Lqueue *init_Lqueue()
{
Lqueue *s=(Lqueue*)malloc(sizeof(Lqueue));
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p->next=NULL;
s->front=s->rear=p;
return s;
}
void InLqueue(Lqueue *s,datatype x)
{
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p->data=x;//p->next=NULL;
p->next=s->rear->next;
s->rear->next=p;s->rear=p;
}
datatype OutLqueue(Lqueue *s)
{
datatype x;
if(empty(s)==0) return 0;
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p=s->front->next;x=p->data;
s->front->next=p->next;
if(p->next=NULL) s->rear=s->front;
free(p);
return x;
typedef struct node
{
datatype data;
struct node *next;
}Qnode;//链式存储的节点类型
typedef struct
{
Qnode *rear,*front;
}Lqueue;//链队列,将头尾指针装在一起
int empty(Lqueue *s)
{
if(s->front==s->rear) return 0;
else return 1;
}
Lqueue *init_Lqueue()
{
Lqueue *s=(Lqueue*)malloc(sizeof(Lqueue));
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p->next=NULL;
s->front=s->rear=p;
return s;
}
void InLqueue(Lqueue *s,datatype x)
{
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p->data=x;//p->next=NULL;
p->next=s->rear->next;
s->rear->next=p;s->rear=p;
}
datatype OutLqueue(Lqueue *s)
{
datatype x;
if(empty(s)==0) return 0;
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p=s->front->next;x=p->data;
s->front->next=p->next;
if(p->next=NULL) s->rear=s->front;
free(p);
return x;
}
链式存储
#define max 10
typedef struct node
{
datatype data;
struct node *next;
}Qnode;//链式存储的节点类型
typedef struct
{
Qnode *rear,*front;
}Lqueue;//链队列,将头尾指针装在一起
int empty(Lqueue *s)
{
if(s->front==s->rear) return 0;
else return 1;
}
Lqueue *init_Lqueue()
{
Lqueue *s=(Lqueue*)malloc(sizeof(Lqueue));
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p->next=NULL;
s->front=s->rear=p;
return s;
}
void InLqueue(Lqueue *s,datatype x)
{
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p->data=x;//p->next=NULL;
p->next=s->rear->next;
s->rear->next=p;s->rear=p;
}
datatype OutLqueue(Lqueue *s)
{
datatype x;
if(empty(s)==0) return 0;
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p=s->front->next;x=p->data;
typedef struct node
{
datatype data;
struct node *next;
}Qnode;//链式存储的节点类型
typedef struct
{
Qnode *rear,*front;
}Lqueue;//链队列,将头尾指针装在一起
int empty(Lqueue *s)
{
if(s->front==s->rear) return 0;
else return 1;
}
Lqueue *init_Lqueue()
{
Lqueue *s=(Lqueue*)malloc(sizeof(Lqueue));
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p->next=NULL;
s->front=s->rear=p;
return s;
}
void InLqueue(Lqueue *s,datatype x)
{
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p->data=x;//p->next=NULL;
p->next=s->rear->next;
s->rear->next=p;s->rear=p;
}
datatype OutLqueue(Lqueue *s)
{
datatype x;
if(empty(s)==0) return 0;
Qnode *p=(Qnode*)malloc(sizeof(Qnode));
p=s->front->next;x=p->data;
s->front->next=p->next;
if(p->next=NULL) s->rear=s->front;//记住出队一定要判断当前结点是不是队列最后一个结点,如果是把队列的队头和队尾置为相同,才能继续入队
free(p);
return x;
return x;
}