1.链表实现的简单算法
# include <stdio.h>
# include <stdlib.h>
typedef struct queue_node
{
int data;
struct queue_node * next;
}QUEUE;
QUEUE * rear = NULL;
QUEUE * front = NULL;
int InQueue(int value)
{
QUEUE * q = (QUEUE *)malloc(sizeof(QUEUE));
if(q == NULL)
return 0;
q->data = value;
q->next = NULL;
if(front == NULL)
front = q;
else
rear->next = q;
rear = q;
return 1;
}
int OutQueue(int * value)
{
QUEUE * p = NULL;
if(NULL == front)
return 0;
p = front;
front = front->next;
*value = p->data;
free(p);
return 1;
}
void main()
{
int temp;
while(1)
{
printf("1.Store 2.Read -->");
scanf("%d", &temp);
fflush(stdin);
if(temp == 1)
{
printf("Please input the value:");
scanf("%d", &temp);
fflush(stdin);
if(InQueue(temp) == 1)
printf("Insert success\n");
else
printf("The queue is full\n");
}
else if(temp == 2)
{
if(OutQueue(&temp))
{
printf("The out queue value is %d\n",temp);
}
else
printf("The queue is empty\n");
}
else
break;
}
}