#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node* next;
}node;
typedef struct
{
node *front,*rear;
}queue;
void initstack(queue *s)
{
s->front=s->rear=(node*)malloc(sizeof(node));
s->front->next=NULL;
}
int stackempty(queue s)
{
if(s.front->next==NULL)
return 1;
else
return 0;
}
int enterstack(queue *s,int e)
{
node *p;
p=(node*)malloc(sizeof(node));
if(!p)
{
printf("申请内存失败/n");
return 0;
}
p->data=e;
p->next=NULL;
s->rear->next=p;
s->rear=p;
return 1;
}
int popstack(queue *s)
{
node *p;
int e;
if(s->front==s->rear)
return 0;
else
{
p=s->front->next;
e=p->data;
printf("%d ",e);
s->front->next=p->next;
free(p);
return 1;
}
}
void main()
{
queue s;
int e,i,count=0;
char ch;
initstack(&s);
printf("请输入元素的值/n");
scanf("%d",&e);
count++;
enterstack(&s,e);
while(ch=getchar()!=EOF)
{
printf("请继续输入/n");
scanf("%d",&e);
count++;
enterstack(&s,e);
printf("是否继续,继续请按任意键,停止请按ctrl+z/n");
ch=getchar();
}
printf("队列中现在共有%d个元素/n",count);
printf("队列中元素按先进先出的顺序出栈为/n");
for(i=0;i<count;i++)
popstack(&s);
printf("/n");
}
链式队列
最新推荐文章于 2025-03-24 22:34:05 发布