基礎知識
//循環(huán)隊列數(shù)據(jù)結構
typedef struct
{
QElemType data[MaxQSize];//數(shù)據(jù)域
int front,rear; //隊頭隊尾指針
}SqQueue;
//鏈隊結點數(shù)據(jù)結構
typedef struct QNode
{
int data;//數(shù)據(jù)域
struct QNode* next;//指針域
}QNode, * QueuePtr;
typedef struct
{
struct QNode* front, * rear;//rear指針指向隊尾 用于入隊 front指針指向隊頭 用于出隊
}LinkQueue;文章來源:http://www.zghlxwxcb.cn/news/detail-615149.html
隊列是一種常見的數(shù)據(jù)結構,它遵循==先進先出(FIFO)==的原則。以下是隊列的基本操作:
1、入隊(Enqueue):將元素添加到隊列的末尾。
2、出隊(Dequeue):從隊列的頭部移除一個元素,并返回其值。
3、隊列長度(Size):獲取隊列中元素的個數(shù)。
4、隊列是否為空(IsEmpty):檢查隊列是否為空。
5、獲取隊首元素(Front):獲取隊列頭部的元素值,但不移除該元素。文章來源地址http://www.zghlxwxcb.cn/news/detail-615149.html
C++ 實現(xiàn)隊列的基本操作的示例代碼:
#include <iostream>
#include <queue>
int main() {
std::queue<int> myQueue;
// 入隊
myQueue.push(10);
myQueue.push(20);
myQueue.push(30);
// 隊列長度
std::cout << "隊列長度:" << myQueue.size() << std::endl;
// 出隊
int frontElement = myQueue.front();
myQueue.pop();
std::cout << "出隊元素:" << frontElement << std::endl;
// 獲取隊首元素
std::cout << "隊首元素:" << myQueue.front() << std::endl;
// 隊列是否為空
std::cout << "隊列是否為空:" << (myQueue.empty() ? "是" : "否") << std::endl;
return 0;
}
在C語言中,隊列的基本操作可以通過結構體和指針來實現(xiàn)。
#include <stdio.h>
#include <stdlib.h>
// 定義隊列結構體
typedef struct {
int *array; // 存儲隊列元素的數(shù)組
int front; // 隊首索引
int rear; // 隊尾索引
int size; // 隊列的容量
} Queue;
// 初始化隊列
void initQueue(Queue *queue, int capacity) {
queue->array = (int*)malloc(capacity * sizeof(int));
queue->front = 0;
queue->rear = -1;
queue->size = 0;
}
// 銷毀隊列
void destroyQueue(Queue *queue) {
free(queue->array);
}
// 入隊
void enqueue(Queue *queue, int element) {
queue->rear = (queue->rear + 1) % queue->size;
queue->array[queue->rear] = element;
queue->size++;
}
// 出隊
int dequeue(Queue *queue) {
int dequeuedElement = queue->array[queue->front];
queue->front = (queue->front + 1) % queue->size;
queue->size--;
return dequeuedElement;
}
// 獲取隊列長度
int getSize(Queue *queue) {
return queue->size;
}
// 檢查隊列是否為空
int isEmpty(Queue *queue) {
return queue->size == 0;
}
// 獲取隊首元素
int getFront(Queue *queue) {
return queue->array[queue->front];
}
int main() {
Queue myQueue;
initQueue(&myQueue, 5);
// 入隊
enqueue(&myQueue, 10);
enqueue(&myQueue, 20);
enqueue(&myQueue, 30);
// 隊列長度
printf("隊列長度:%d\n", getSize(&myQueue));
// 出隊
int frontElement = dequeue(&myQueue);
printf("出隊元素:%d\n", frontElement);
// 獲取隊首元素
printf("隊首元素:%d\n", getFront(&myQueue));
// 隊列是否為空
printf("隊列是否為空:%s\n", isEmpty(&myQueue) ? "是" : "否");
destroyQueue(&myQueue);
return 0;
}
循環(huán)隊列
#define MaxSize 100
typedef struct { int data[MaxSize]; int front, rear; }SqQueue;
//*********************************** 基本操作函數(shù) *******************************************
int InitQueue(SqQueue &Q)
{
Q.front = Q.rear = 0;
return 1;
}
bool QueueEmpty(SqQueue& Q) {
if (Q.front != Q.rear) return true;
else return false;
}
//入隊:尾部加1; 出隊:頭部加1
bool EnQueue(SqQueue& Q, int& e) {
if (Q.front ==Q.rear) return false;
e = Q.data[Q.rear];
Q.rear = (Q.rear + 1) % MaxSize; //指針加1 取模
return true;
}
bool DeQueue(SqQueue& Q, int &e) {
if (Q.front == Q.rear) return false;
e = Q.data[Q.front];
Q.front = (Q.front + 1) % MaxSize; //指針加1 取模
return true;
}
bool GetHead(SqQueue& Q, int &e)
{
if (Q.front == Q.rear) return false;//隊空
e = Q.data[Q.front];
return true;
}
//********************************功能實現(xiàn)函數(shù)**************************************//
void EnterToQueue(SqQueue& Q)
{
int n; int e; int flag;
printf("請輸入入隊元素個數(shù)(>=1):\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("請輸入第%d個元素的值:", i + 1);
scanf("%d", &e);
flag = EnQueue(Q, e);
if (flag)printf("%d已入隊\n", e);
else { printf("隊已滿?。?!\n"); break; }
}
}
void DeleteFromQueue(SqQueue& Q)
{
int n; int e; int flag;
printf("請輸入出隊元素個數(shù)(>=1):\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
flag = DeQueue(Q, e);
if (flag)printf("%d已出隊\n", e);
else { printf("隊已空?。?!\n"); break; }
}
}
void GetHeadOfQueue(SqQueue Q)
{
int e; bool flag;
flag = GetHead(Q, e);
if (flag)printf("隊頭元素為:%d\n", e);
else printf("隊已空?。?!\n");
}
void menu() {
printf("********1.入隊 2.出隊*********\n");
printf("********3.取隊頭元素 4.退出*********\n");
}
int main() {
SqQueue Q;
int choice = 0;;
InitQueue(Q);
while (1)
{
menu();
printf("請輸入菜單序號:\n");
scanf("%d", &choice);
if (choice == 4) break;
switch (choice)
{
case 1:EnterToQueue(Q); break;
case 2:DeleteFromQueue(Q); break;
case 3:GetHeadOfQueue(Q); break;
default:printf("輸入錯誤?。?!\n");
}
system("pause");
system("cls");
}
return 0;
}
鏈隊
#define MaxSize 100
//鏈隊結點數(shù)據(jù)結構
typedef struct QNode
{
int data;//數(shù)據(jù)域
struct QNode* next;//指針域
}QNode, * QueuePtr;
typedef struct
{
struct QNode* front, * rear;//rear指針指向隊尾 用于入隊 front指針指向隊頭 用于出隊
}LinkQueue;
//*********************************** 基本操作函數(shù) *******************************************
int InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = new QNode;
Q.front->next = NULL;
return 1;
}
int EnQueue(LinkQueue& Q, int& e) {
QNode* p;
p = new QNode;//生成新節(jié)點
p->data = e; //賦值
p->next = NULL;
Q.rear->next = p;//加入隊尾
Q.rear = p; //尾指針后移
return 1;
}
bool DeQueue(LinkQueue &Q, int &e) {
QueuePtr p;
if (Q.front == Q.rear)return false;//隊空
e = Q.front->next->data; //e返回值 之前寫的Q.front->data 炸了,頭結點沒數(shù)據(jù)的,一定要注意頭結點
p = Q.front->next; //保留,一會兒釋放空間
Q.front->next = p->next; //出隊,注意Q.front->next 不是Q.front 還有頭結點
if (Q.rear == p)Q.rear = Q.front; //最后一個元素出隊,rear指向頭結點
free(p);
return true;
}
//取隊頂函數(shù) 用e返回
bool GetHead(LinkQueue &Q, int &e)
{
if (Q.front == Q.rear) return false;//隊空
e = Q.front->next->data;
return true;
}
//********************************功能實現(xiàn)函數(shù)**************************************//
void EnterToQueue(LinkQueue& Q)
{
int n; int e; int flag;
printf("請輸入入隊元素個數(shù)(>=1):\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("請輸入第%d個元素的值:", i + 1);
scanf("%d", &e);
flag = EnQueue(Q, e);
if (flag)printf("%d已入隊\n", e);
}
}
void DeleteFromQueue(LinkQueue& Q)
{
int n; int e; int flag;
printf("請輸入出隊元素個數(shù)(>=1):\n");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
flag = DeQueue(Q, e);
if (flag)printf("%d已出隊\n", e);
else { printf("隊已空?。?!\n"); break; }
}
}
void GetHeadOfQueue(LinkQueue Q)
{
int e; bool flag;
flag = GetHead(Q, e);
if (flag)printf("隊頭元素為:%d\n", e);
else printf("隊已空?。?!\n");
}
void menu() {
printf("********1.入隊 2.出隊*********\n");
printf("********3.取隊頭元素 4.退出*********\n");
}
int main() {
LinkQueue Q;
int choice = 0;;
InitQueue(Q);
while (1)
{
menu();
printf("請輸入菜單序號:\n");
scanf("%d", &choice);
if (choice == 4) break;
switch (choice)
{
case 1:EnterToQueue(Q); break;
case 2:DeleteFromQueue(Q); break;
case 3:GetHeadOfQueue(Q); break;
default:printf("輸入錯誤?。。n");
}
system("pause");
system("cls");
}
return 0;
}
到了這里,關于數(shù)據(jù)結構--循環(huán)隊列、鏈隊的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!