【指針+模擬】隊列的運用

 

   普通隊列運用

數組裝數據,用兩個int來模擬頭尾指針

#include<iostream>
using namespace std;
int main()
{
int q[100] = { 0, 6, 3, 1, 7, 5, 8, 9, 2, 4 },head=0,tail=9;
    //模擬並初始化隊列
while (head < tail)
{
cout<<q[head];
head++;
q[tail] = q[head];
tail++; head++;
}

return 0;
}

可以試試結構體封裝一下

#include<iostream>
using namespace std;
struct queue
{
int data[100];//隊列主體,用於儲存
int head;//用於存放隊首
int tail;//用於存放隊尾
};

int main()
{
struct queue q;
int i;//用於初始化
q.head = 1;//成員訪問符
q.tail = 1;
//數組初始化
for (i = 1; i <= 9; i++)
{
cin>>q.data[q.tail];
q.tail++;
}

//遍歷輸出
while (q.head < q.tail)
{
    {
    cout<<q.data[q.head];
    q.head++;
    q.data[q.tail] = q.data[q.head];
    q.tail++;
    q.head++;
    }
return 0;
}

 

使用頭尾指針

==指針不熟練,有時間再補充

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章