循环队列—数组实现
2012-08-02 20:27
471 查看
/* circular queue: 牺牲一个存储节点,对头front指示的节点不用于存储数据,起标志性作用 * front: 队头元素的前一个位置 * rear: 队尾元素 */ #include<stdio.h> #include<stdlib.h> #define MAX 10 //size of queue int insertCircularQueue(int queue[], int front,int *rear, int value); int delCircularQueue(int queue[], int *front, int rear, int *value); void printQueue(int queue[], int front, int rear); void item(); int queue[MAX]; int front =0;//与初始值无关 0也也可以 int rear = 0; main(void) { char item_choice; int n; item(); while(1) { item_choice = getchar(); switch (item_choice) { case '1': printf("Insert a element to the queue: "); scanf("%d", &n); insertCircularQueue(queue, front,&rear, n); printf("\nQueue: "); printQueue(queue, front, rear); putchar('\n'); item(); break; case '2': printf("Get an element from the queue: "); delCircularQueue(queue, &front, rear, &n); printf("Element: %d\n", n); printf("Queue: "); printQueue(queue, front, rear); putchar('\n'); item(); break; case '0': printf("Good luck to you!\n"); return 0; } } return; } /* insert a element into the end of queue */ /* 插入队尾,只有队尾值会改变,所以队尾要传入指针,而队头只需传入值即可 */ int insertCircularQueue(int queue[], int front, int *rear, int value) { *rear = (*rear + 1)%MAX; if(*rear == front) { printf("Queue overflow\n"); exit(-1); } queue[*rear] = value; return 0; } /* delete an element at the head of queue */ /* 删除在对头,只有队头值会改变,所以队头要传入指针,而队尾只需传入值即可 */ int delCircularQueue(int queue[], int *front, int rear, int *value) { if(*front == rear) { printf("Queue underflow\n"); exit(-1); } *front = (*front+1)%MAX; *value = queue[*front]; return 0; } /* print content of the queue, we use {} to contaion queue elements*/ void printQueue(int queue[], int front, int rear) { int i; if(front == -1) printf("{"); for(i=0; i<MAX; i++) { printf("%d ", queue[i]); if(i == front) printf("{ "); if(i == rear) printf("} "); } putchar('\n'); } void item() { printf("*************** Welcome to the Queue *************\n"); printf("1: Insert one element;\n"); printf("2: Delete one element;\n"); printf("0: Exit.\n"); printf("*************************************************\n"); printf("Select your operation: "); }
相关文章推荐
- 队列----循环数组实现队列
- Java实现 数组实现循环队列
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- 循环数组实现一个队列
- 数组实现循环队列
- 队列(queue)的链表(list)实现及循环数组(circular array)实现 C++实现
- 队列的C语言实现(循环数组结构)
- 循环队列的数组实现
- java队列的循环数组实现
- c语言用简单数组实现循环队列
- 循环队列的数组表示函数的实现
- 无锁队列的实现-循环数组
- 8.用定长数组 实现 循环队列
- 循环队列_数组实现
- 循环队列数组实现
- 循环队列拾遗-使用数组实现循环队列
- Java数组实现循环队列、Java链表实现队列
- 简单队列和循环队列的实现--数组实现
- 数组实现循环队列(Java)
- 用数组实现循环队列