银行排队系统 循环队列实现
时间: 2024-04-14 22:21:27 浏览: 21
银行排队系统是指在银行等候服务的客户按照先来先服务的原则排成一条队列,等待银行工作人员为其提供服务。循环队列是一种特殊的队列,它可以在队列的两端进行插入和删除操作,当队列的尾部到达数组的末尾时,可以将其连接到数组的开头,形成一个循环。在银行排队系统中,循环队列可以用来实现客户的排队和服务。
具体实现方法如下:
1. 定义一个循环队列的结构体,包括队列的最大长度、队头和队尾指针以及队列元素数组。
2. 初始化队列,将队头和队尾指针都指向数组的第一个元素。
3. 当有客户到来时,将其加入队列的队尾,并将队尾指针向后移动一位。
4. 当有客户需要服务时,将队头指向的客户出队,并将队头指针向后移动一位。
5. 当队列已满时,将队尾指针连接到数组的开头,形成一个循环。
6. 当队列为空时,队头和队尾指针指向同一个元素。
相关问题
c语言银行排队管理系统链队列实现
下面是基于链队列实现银行排队管理系统的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100 // 队列的最大容量
// 队列节点结构体
typedef struct Node {
int id; // 客户编号
struct Node *next;
} Node;
// 队列结构体
typedef struct {
Node *front; // 队头指针
Node *rear; // 队尾指针
int length; // 队列长度
} Queue;
// 初始化队列
void InitQueue(Queue *queue) {
queue->front = queue->rear = (Node *) malloc(sizeof(Node));
queue->front->next = NULL;
queue->length = 0;
}
// 判断队列是否为空
int IsEmpty(Queue *queue) {
return queue->front == queue->rear;
}
// 入队
void EnQueue(Queue *queue, int id) {
Node *node = (Node *) malloc(sizeof(Node));
node->id = id;
node->next = NULL;
queue->rear->next = node;
queue->rear = node;
queue->length++;
}
// 出队
int DeQueue(Queue *queue) {
if (IsEmpty(queue)) {
printf("队列为空,无法出队!\n");
exit(1);
}
Node *node = queue->front->next;
int id = node->id;
queue->front->next = node->next;
if (queue->rear == node) {
queue->rear = queue->front;
}
free(node);
queue->length--;
return id;
}
// 输出队列中的所有元素
void PrintQueue(Queue *queue) {
if (IsEmpty(queue)) {
printf("队列为空!\n");
return;
}
printf("队列中的元素为:");
Node *node = queue->front->next;
while (node != NULL) {
printf("%d ", node->id);
node = node->next;
}
printf("\n");
}
int main() {
Queue queue;
InitQueue(&queue);
int choice, id;
while (1) {
printf("请输入操作代码:\n");
printf("1. 客户进入队列\n");
printf("2. 客户出队\n");
printf("3. 输出队列中的所有元素\n");
printf("4. 退出程序\n");
scanf("%d", &choice);
switch (choice) {
case 1:
if (queue.length >= MAX_SIZE) {
printf("队列已满,无法进入!\n");
break;
}
printf("请输入客户编号:");
scanf("%d", &id);
EnQueue(&queue, id);
printf("客户 %d 进入队列!\n", id);
break;
case 2:
id = DeQueue(&queue);
printf("客户 %d 出队!\n", id);
break;
case 3:
PrintQueue(&queue);
break;
case 4:
printf("程序已退出!\n");
exit(0);
default:
printf("无效的操作代码!\n");
break;
}
}
return 0;
}
```
该程序中,我们使用了链表来实现队列,每个节点表示一个客户,包含客户的编号和指向下一个节点的指针。队列结构体中包含队头指针、队尾指针和队列长度三个成员变量。程序中实现了入队、出队、判断队列是否为空、输出队列中的所有元素等操作。在主函数中,我们通过一个循环来不断接受用户输入的操作代码,根据不同的操作代码执行相应的操作。
运用循环队列和链队列银行排队叫号系统
银行排队叫号系统是一个典型的队列应用场景,可以使用循环队列或者链队列来实现。
1. 循环队列实现
循环队列的特点是可以利用数组首尾相连的特性来实现循环利用队列空间,同时可以通过取余运算计算队头和队尾指针的值,避免队列满时浪费空间。
下面是使用循环队列实现银行排队叫号系统的示例代码。
```python
class CircularQueue:
def __init__(self, capacity):
self.queue = [None] * capacity
self.capacity = capacity
self.head = 0
self.tail = 0
def enqueue(self, item):
if (self.tail + 1) % self.capacity == self.head:
return False # 队列满了
self.queue[self.tail] = item
self.tail = (self.tail + 1) % self.capacity
return True
def dequeue(self):
if self.tail == self.head:
return None # 队列为空
item = self.queue[self.head]
self.head = (self.head + 1) % self.capacity
return item
def is_empty(self):
return self.head == self.tail
def is_full(self):
return (self.tail + 1) % self.capacity == self.head
class BankQueue:
def __init__(self):
self.queue = CircularQueue(100)
self.current_number = 0
def get_number(self):
self.current_number += 1
return self.current_number
def enqueue(self):
number = self.get_number()
result = self.queue.enqueue(number)
if result:
print("顾客" + str(number) + "排队成功!")
else:
print("队列已满,无法排队!")
def dequeue(self):
number = self.queue.dequeue()
if number is None:
print("队列为空,无法出队!")
else:
print("当前叫号:" + str(number))
def display(self):
if self.queue.is_empty():
print("队列为空!")
else:
print("当前排队情况:", end="")
for i in range(self.queue.head, self.queue.tail):
print(str(self.queue.queue[i]) + " ", end="")
print()
```
2. 链队列实现
链队列的特点是利用链表来存储队列元素,因此可以动态地分配内存空间,避免了循环队列可能出现的空间浪费问题。
下面是使用链队列实现银行排队叫号系统的示例代码。
```python
class Node:
def __init__(self, data):
self.data = data
self.next_node = None
class LinkedQueue:
def __init__(self):
self.head = None
self.tail = None
def enqueue(self, item):
new_node = Node(item)
if self.tail is None:
self.head = new_node
else:
self.tail.next_node = new_node
self.tail = new_node
def dequeue(self):
if self.head is None:
return None # 队列为空
item = self.head.data
self.head = self.head.next_node
if self.head is None:
self.tail = None
return item
def is_empty(self):
return self.head is None
def is_full(self):
return False
class BankQueue:
def __init__(self):
self.queue = LinkedQueue()
self.current_number = 0
def get_number(self):
self.current_number += 1
return self.current_number
def enqueue(self):
number = self.get_number()
self.queue.enqueue(number)
print("顾客" + str(number) + "排队成功!")
def dequeue(self):
number = self.queue.dequeue()
if number is None:
print("队列为空,无法出队!")
else:
print("当前叫号:" + str(number))
def display(self):
if self.queue.is_empty():
print("队列为空!")
else:
print("当前排队情况:", end="")
node = self.queue.head
while node is not None:
print(str(node.data) + " ", end="")
node = node.next_node
print()
```