队列:
栈先进后出,队列先进先出
队列:
#include<stdio.h>
#include<stdlib.h>
typedef char ElementType;
typedef struct node
{
ElementType data;
struct node *next;
}Node;
typedef struct list
{
Node *first;
Node *last;
int count;
}List;
//创建一个空队列
List * CreateQueue(void)
{
List *l = (List*)malloc(sizeof(List));
l->first = l->last = NULL;
l->count = 0;
return l;
}
//入对
void EnQueue(List *l,ElementType ch)
{
if(l == NULL)
return ;
Node *p = (Node *)malloc(sizeof(Node));
p->data = ch;
p->next = NULL;
if(l->first == NULL)
{
l->first = l->last = p;
}
else
{
l->last->next = p;
l->last = p;
}
l->count++;
}
//出队
Node *DeQueue(List *l)
{
if(l == NULL)
return NULL;
Node *p = l->first;
if(l->first == l->last)
{
l->first = l->last = NULL;
}
else
{
l->first = p->next;
p->next = NULL;
}
l->count--;
return p;
}
//判断队列空
int QueueEmpty(List *l)
{
if(l)
{
if(l->first == NULL)
return 1;
else
return 0;
}
}
int main(int argc,char *argv[])
{
List *l = CreateQueue();
ElementType x;
while(1)
{
scanf("%c",&x);
if(x == '#')
break;
EnQueue(l,x);
}
Node *p = NULL;
while(QueueEmpty(l) == 0)
{
p = DeQueue(l);
printf("%c\t",p->data);
free(p);
}
p = NULL;
printf("\n");
return 0;
}