用c语言实现队列的算法,C + + 程序实现队列

队列

被实现为FIFO的队列,其中插入是在一端(后部)完成,而删除是在另一端(前部)完成。输入的第一个元素首先被删除。

队列操作是-

EnQueue (int data)-在后端插入

int DeQueue()-删除从前端

这是一个使用数组实现队列的C ++程序。

算法Begin

函数 Enqueue ()插入队列中的元素:

如果队列已满,则打印“ Overflow”。

否则在后面插入元素。

更新后方的价值

End

Begin

函数Dequeue()从队列中删除元素:

如果队列完全为空,则打印“下溢”。

否则从前面插入元素。

更新rear的值。

End

范例程式码#include 

using namespace std;

struct Q {

int f, r, capacity;

int* q;

Q(int c) {

f = r= 0;

capacity = c;

q = new int;

}

~Q() { delete[] q; }

void Enqueue(int d) {

if (capacity == r) { //检查队列是否为空

printf("\nQueue is full\n");

return;

} else {

q[r] = d; //insert data

r++; //update rear

}

return;

}

void Dequeue() {

if (f == r) {

printf("\nQueue is empty\n");

return;

} else {

for (int i = 0; i 

q[i] = q[i + 1];

}

r--; //update rear

}

return;

}

//显示队列

void Display(){

int i;

if (f == r) {

printf("\nQueue is Empty\n");

return;

}

for (i = f; i 

printf(" %d 

}

return;

}

void Front() {

if (f == r) {

printf("\nQueue is Empty\n");

return;

}

printf("\nFront Element is: %d", q[f]); //print front element of queue

return;

}

};

int main(void) {

Q qu(3);

qu.Display();

cout<

qu.Enqueue(10);

qu.Enqueue(20);

qu.Enqueue(30);

qu.Display();

qu.Dequeue();

qu.Dequeue();

printf("\n\nafter two node deletion\n\n");

qu.Display();

qu.Front();

return 0;

}

输出结果Queue is Empty

10 

after two node deletion

30 

Front Element is: 30

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值