顺序队列判断队满或者对空有3种方式
- (Q.rear + 1) % MaxSize == Q.front,不过会浪费1个存储单元
- 加1个字段length表示顺序队列元素个数
- tag标志最近是入队还是出队
下面实现是用第1种方式
#include三、链式队列#include using namespace std; #define MaxSize 10 typedef struct{ int data[MaxSize]; int front, rear; }Queue; void init_data(Queue &Q){ Q.front = Q.rear = 0; } bool set_data(Queue &Q, int value){ if ((Q.rear + 1) % MaxSize == Q.front) { return false; } Q.data[Q.rear] = value; Q.rear = (Q.rear + 1) % MaxSize; return true; } bool erase_data(Queue &Q, int &value){ if (Q.front == Q.rear) { return false; } value = Q.data[Q.front++]; return true; } void print_data(Queue Q){ while (Q.front!=Q.rear) { cout << "element:" << Q.front << " " << Q.data[Q.front] << endl; Q.front = (Q.front + 1) % MaxSize; } } int main(){ std::cout << "welcome, to my world!" << std::endl; Queue Q; init_data(Q); cout << "size of:" << sizeof(Q) << endl; for (int i = 1; i<= MaxSize; i++) { set_data(Q, i*10); } print_data(Q); int value; erase_data(Q, value); set_data(Q, 1); print_data(Q); cout << "size of:" << value << endl; print_data(Q); return 0; }
带头结点代码实现
#include#include using namespace std; typedef struct linkNode{ int data; struct linkNode *next; }linkNode; typedef struct{ linkNode *front, *rear; } Queue; // 带头结点初始化 void init_data(Queue &Q){ Q.front = Q.rear = (linkNode *)malloc(sizeof(linkNode)); Q.front->next = NULL; } // 入队 bool set_data(Queue &Q, int value){ linkNode *p = (linkNode *)malloc(sizeof(linkNode)); // 内存不够,分配失败 if (p == NULL) { return false; } // 构建好p结点数据 p->data = value; p->next = NULL; // 入队 Q.rear->next = p; // 队尾指针永远指向最后一个元素 Q.rear = p; return true; } bool erase_data(Queue &Q, int &value){ if (Q.front == Q.rear) { return false; } linkNode *p = Q.front->next; value = p->data; Q.front->next = p->next; // 如果是最后一个数据元素,修改尾指针 if (p == Q.rear) { Q.rear = Q.front; } free(p); return true; } void print_data(Queue Q){ while (Q.front!=NULL) { cout << "element:" << Q.front << " " << Q.front->data << endl; Q.front = Q.front->next; } } int main(){ std::cout << "welcome, to my world!" << std::endl; Queue Q; init_data(Q); cout << "size of:" << sizeof(Q) << endl; for (int i = 1; i<= 10; i++) { cout << "size of i:" << i << endl; set_data(Q, i*100); } print_data(Q); int value; erase_data(Q, value); set_data(Q, 1); print_data(Q); print_data(Q); return 0; }
不带头结点实现
#include四、双端队列using namespace std; typedef struct linkNode{ int data; struct linkNode *next; }linkNode; typedef struct{ linkNode *front, *rear; } Queue; // 不带头结点初始化 void init_data(Queue &Q){ Q.front = Q.rear = NULL; } // 入队 bool set_data(Queue &Q, int value){ linkNode *p = (linkNode *)malloc(sizeof(linkNode)); // 内存不够,分配失败 if (p == NULL) { return false; } // 构建好p结点数据 p->data = value; p->next = NULL; // 第一个元素入队 if (Q.front == NULL) { Q.front = p; }else{ Q.rear->next = p; } // 队尾指针永远指向最后一个元素 Q.rear = p; return true; } bool erase_data(Queue &Q, int &value){ if (Q.front == NULL) { return false; } linkNode *p = Q.front; value = p->data; Q.front = p->next; // 如果是最后一个数据元素,修改尾指针 if (p == Q.rear) { Q.rear = Q.front = NULL; } free(p); return true; } void print_data(Queue Q){ while (Q.front!=NULL) { cout << "element:" << Q.front << " " << Q.front->data << endl; Q.front = Q.front->next; } } int main(){ std::cout << "welcome, to my world!" << std::endl; Queue Q; init_data(Q); cout << "size of:" << sizeof(Q) << endl; for (int i = 1; i<= 10; i++) { cout << "size of i:" << i << endl; set_data(Q, i*100); } print_data(Q); int value; erase_data(Q, value); set_data(Q, 1); print_data(Q); print_data(Q); return 0; }



