#include<stdio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}*node,pnode;
typedef struct queue{
node fount;
node real;
}*queue,squeu;
queue init(){
queue s =(queue)malloc(sizeof (squeu));
if(s)
{
s->fount=NULL;
s->real=NULL;
}
return s;
}
int is_empty(queue s){
if(s&&s->fount==NULL&&s->real==NULL)
return 1;
else
return 0;
}
//入队
void insert(queue s,int x)
{
node q=(node)malloc(sizeof (pnode));
if(!q){
printf("内存溢出");
}
q->data=x;
q->next=NULL;
if(is_empty(s)){
s->fount=q;
s->real=q;
}else {
s->real->next=q;
s->real=q;
}
}
//出队
void push(queue s,int *x)
{
// node p;
// if(is_empty(s)){
// printf("队空");
// }
// p=s->fount;
// *x=s->fount->data;
// printf("**>%d",*x);
// s->fount=s->fount->next;
// free(p);
// if(!s->fount){
// s->real=NULL;
// }
node p;
if(is_empty(s)){
printf("队空");
}else {
p=s->fount;
*x=s->fount->data;
printf("**>%d",*x);
s->fount=s->fount->next;
free(p);
}
}
void pri(queue s)
{
while (is_empty(s)!=1) {
printf("-->%d",s->fount->data);
s->fount=s->fount->next;
}
}
int main()
{
queue s=init();
int x;
for(int i=1;i<=5;i++)
{
insert(s,i);
}
push(s,&x);
push(s,&x);
push(s,&x);
printf("\n");
pri(s);
return 0;
}
链式队列(基本功能)
最新推荐文章于 2024-07-25 15:58:50 发布