【题目】利用链表实现一先进先出的队列结构
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:链表实现先进先出的队列结构
Funcion List:
*****************************************************/
<strong>/*******************方法1***************************/</strong>
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#include <time.h>
#define MAX 5
struct stack_data
{
int top;
int bottom;
int quece[MAX];
};
typedef struct stack_data Quece;
typedef struct stack_data* link;
enum return_result{PUSH_NO, PUSH_OK, POP_NO, POP_OK, EXIT_FALL};
void is_malloc_ok(link *quece)
{
if(*quece == NULL)
{
exit (EXIT_FALL);
}
}
void create_quece( link *quece)
{
*quece = (link)malloc(sizeof(Quece));
is_malloc_ok(quece);
}
void init_quece(link *quece)
{
(*quece)->top = -1;
(*quece)->bottom = -1;
}
int push_quece(link *quece, int num)
{
int i;
if((*quece)->bottom == -1)
{
if((*quece)->top >= MAX - 1)
{
printf("quece is full!\n");
return PUSH_NO;
}
}
else
{
if((*quece)->top - (*quece)->bottom >= MAX - 1)
{
printf("quece is full!\n");
return PUSH_NO;
}
}
if((*quece)->top == MAX - 1)
{
for(i = (*quece)-> bottom; i <= (*quece)->top; i++)
{
(*quece)->quece[i - (*quece)->bottom] = (*quece)->quece[i];
}
(*quece)->top = (*quece)->top - (*quece)->bottom + 1;
(*quece)->bottom = 0;
((*quece)->top)++;
((*quece)->quece[(*quece)->top]) = num;
return PUSH_OK;
}
else
{
((*quece)->top)++;
((*quece)->quece[(*quece)->top]) = num;
return PUSH_OK;
}
}
int pop_quece(link *quece, int *num)
{
if((*quece)->bottom >= (*quece)->top)
{
printf("quece is empty!\n");
return POP_NO;
}
else
{
((*quece)->bottom)++;
*num = ((*quece)->quece[(*quece)->bottom]);
return POP_OK;
}
}
int main()
{
link quece;;
int i, num1, num;
create_quece(&quece);
init_quece(&quece);
srand(time(NULL));
while(1)
{
num = rand() % 100;
if(num > 50)
{
if(push_quece(&quece, num) == PUSH_OK)
{
printf("push done! %d\n", num);
}
}
else
{
if(pop_quece(&quece,&num1) == POP_OK)
{
printf("pop done! %d\n",num1);
}
}
sleep(2);
}
return 0;
}
/*******************************方法2*************************/
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Desciption:用双向链表实现队列的进出
Funcion List:
*****************************************************/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int num;
struct node *prior;
struct node *next;
};
typedef struct node Node;
typedef struct node * link;
void is_malloc_ok(link *head)
{
if(*head == NULL)
{
printf("malloc is error!\n");
exit(-1);
}
}
void create_link(link *head)
{
*head = (link)malloc(sizeof(Node));
is_malloc_ok(head);
(*head)->prior = *head;
(*head)->next = *head;
}
void create_new_node(link *new_node, int i)
{
*new_node = (link)malloc(sizeof(Node));
is_malloc_ok(new_node);
(*new_node)->num = i;
}
void push_quece(link *head, link *new_node)
{
(*new_node)->next = (*head)->next;
(*new_node)->prior = (*head);
(*head)->next->prior = (*new_node);
(*head)->next = (*new_node);
printf("push number:%d\n", (*new_node)->num);
}
void pop_quece(link *head)
{
link tmp;
tmp = (*head)->prior;
if(tmp == *head)
{
printf("quece is empty!\n");
return;
}
(*head)->prior = tmp->prior;
tmp->prior->next = *head;
printf("pop number:%d\n", tmp->num);
free(tmp);
tmp = (*head)->prior;
}
int main()
{
int i,num;
link head = NULL;
link new_node = NULL;
create_link(&head);
srand(time(NULL));
while(1)
{
i = rand() % 100;
if(i > 50)
{
create_new_node(&new_node,i);
push_quece(&head, &new_node);
}
else
{
pop_quece(&head);
}
sleep(1);
}
return 0;
}