双队列的两种实现方式
2011-12-07 22:51
393 查看
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 InQueueRear(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 InQueueFront(int value)
{
QUEUE * q = (QUEUE *)malloc(sizeof(QUEUE));
if(q == NULL)
return 0;
q->data = value;
q->next = front;
front = q;
if(rear == NULL)
rear = q;
return 1;
}
int OutQueueFront(int * value)
{
QUEUE * p = NULL;
if(NULL == front)
return 0;
p = front;
front = front->next;
*value = p->data;
free(p);
return 1;
}
void PrintQueue()
{
QUEUE * p = front;
while(p)
{
printf("%5d", p->data);
p = p->next;
}
printf("\n");
}
void main()
{
int res;
while(1)
{
{
printf("1.Store from front queue 2.Store from rear queue 3.quit -->");
scanf("%d", &res);
fflush(stdin);
}
if(res == 1)
{
printf("Please input the queue value:");
scanf("%d", &res);
fflush(stdin);
if(InQueueFront(res))
PrintQueue();
else
printf("Failure\n");
}
else if(res == 2)
{
printf("Please input the queue value:");
scanf("%d", &res);
fflush(stdin);
if(InQueueRear(res))
PrintQueue();
else
printf("Failure\n");
}
else if(res == 3)
break;
}
}
2.输出限制性双队列代码实现
# include <stdio.h>
# include <stdlib.h>
typedef struct queue_node
{
int data;
struct queue_node * next;
}QUEUE;
QUEUE * rear = NULL;
QUEUE * front = NULL;
int InQueueRear(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 OutQueueFront(int * value)
{
QUEUE * p = NULL;
if(NULL == front)
return 0;
p = front;
front = front->next;
*value = p->data;
free(p);
return 1;
}
int OutQueueRear(int * value)
{
QUEUE * p = NULL;
if(NULL == rear)
return 0;
if(rear == front)
{
* value = rear->data;
free(rear);
rear = NULL;
front = NULL;
}else
{
p = front;
while(p->next != rear)
p = p->next;
*value = rear->data;
free(rear);
rear = p;
rear->next = NULL;
}
return 1;
}
void main()
{
int i, res, arr[5] = {1, 2, 3, 4, 5};
for(i=0; i<5; ++i)
InQueue(arr[i]);
while(1)
{
printf("1.Out queue from front 2.Out queue from rear 3.quit -->");
scanf("%d", &res);
fflush(stdin);
if(res == 1)
{
if(OutQueueFront(&res) == 1)
printf("The value of out queue is %d\n", res);
else
printf("The queue is empty\n");
}
else if(res == 2)
{
if(OutQueueRear(&res) == 1)
printf("The value of out queue is %d\n", res);
else
printf("The queue is empty\n");
}
else if(res == 3)
break;
}
}
# include <stdio.h>
# include <stdlib.h>
typedef struct queue_node
{
int data;
struct queue_node * next;
}QUEUE;
QUEUE * rear = NULL;
QUEUE * front = NULL;
int InQueueRear(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 InQueueFront(int value)
{
QUEUE * q = (QUEUE *)malloc(sizeof(QUEUE));
if(q == NULL)
return 0;
q->data = value;
q->next = front;
front = q;
if(rear == NULL)
rear = q;
return 1;
}
int OutQueueFront(int * value)
{
QUEUE * p = NULL;
if(NULL == front)
return 0;
p = front;
front = front->next;
*value = p->data;
free(p);
return 1;
}
void PrintQueue()
{
QUEUE * p = front;
while(p)
{
printf("%5d", p->data);
p = p->next;
}
printf("\n");
}
void main()
{
int res;
while(1)
{
{
printf("1.Store from front queue 2.Store from rear queue 3.quit -->");
scanf("%d", &res);
fflush(stdin);
}
if(res == 1)
{
printf("Please input the queue value:");
scanf("%d", &res);
fflush(stdin);
if(InQueueFront(res))
PrintQueue();
else
printf("Failure\n");
}
else if(res == 2)
{
printf("Please input the queue value:");
scanf("%d", &res);
fflush(stdin);
if(InQueueRear(res))
PrintQueue();
else
printf("Failure\n");
}
else if(res == 3)
break;
}
}
2.输出限制性双队列代码实现
# include <stdio.h>
# include <stdlib.h>
typedef struct queue_node
{
int data;
struct queue_node * next;
}QUEUE;
QUEUE * rear = NULL;
QUEUE * front = NULL;
int InQueueRear(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 OutQueueFront(int * value)
{
QUEUE * p = NULL;
if(NULL == front)
return 0;
p = front;
front = front->next;
*value = p->data;
free(p);
return 1;
}
int OutQueueRear(int * value)
{
QUEUE * p = NULL;
if(NULL == rear)
return 0;
if(rear == front)
{
* value = rear->data;
free(rear);
rear = NULL;
front = NULL;
}else
{
p = front;
while(p->next != rear)
p = p->next;
*value = rear->data;
free(rear);
rear = p;
rear->next = NULL;
}
return 1;
}
void main()
{
int i, res, arr[5] = {1, 2, 3, 4, 5};
for(i=0; i<5; ++i)
InQueue(arr[i]);
while(1)
{
printf("1.Out queue from front 2.Out queue from rear 3.quit -->");
scanf("%d", &res);
fflush(stdin);
if(res == 1)
{
if(OutQueueFront(&res) == 1)
printf("The value of out queue is %d\n", res);
else
printf("The queue is empty\n");
}
else if(res == 2)
{
if(OutQueueRear(&res) == 1)
printf("The value of out queue is %d\n", res);
else
printf("The queue is empty\n");
}
else if(res == 3)
break;
}
}
相关文章推荐
- java实现消息队列的两种方式
- Java实现阻塞队列的两种方式
- Java队列的两种实现方式
- [置顶] rabbitmq 实现延迟队列的两种方式
- 队列的C++实现(两种方式)
- Java可阻塞队列的两种实现方式 (传统wait/notify和jdk1.5以后的lock)
- 大话数据结构(六)——队列的两种java实现方式
- rabbitmq 实现延迟队列的两种方式
- 数据结构队列的java实现,包括线性和链式两种方式
- Java可阻塞队列的两种实现方式
- 队列的两种存储方式的介绍与实现(后续)
- [LeetCode] Binary Tree Level Order Traversal 与 Binary Tree Zigzag Level Order Traversal,两种按层次遍历树的方式,分别两个队列,两个栈实现
- 队列的两种存储方式的介绍与实现
- 队列的两种实现方式
- Java可阻塞队列的两种实现方式
- 自定义类型数组排序的两种实现方式
- 创建子线程的两种方式:类实现runnable接口或是类继承Thread类
- 转: Redis消息队列的若干实现方式
- android------引导页两种实现方式(原生和WebView网页实现)