非迴圈鏈隊類C++定義,迴圈定義

來源:互聯網
上載者:User

非迴圈鏈隊類C++定義,迴圈定義

使用鏈表來實現隊列有其得天獨厚的條件,鏈表靈活的節點刪除和增加操作,對於實現隊列來說尤其是小菜一碟。使用順序表來實現隊列還得為了有效使用空間而進行迴圈操作;即就是這樣依然還會發生溢出現象,所以,還是鏈表來的爽快!
啥也不說,上代碼

///////////////////////////////////////////////////  LinkQueue.h#include "stdafx.h"#include <cassert>#include<iomanip>#include<iostream>using namespace std;const int ERROR=-1;const int OK=1;typedef int Status; //表示操作結果的狀態///非迴圈鏈隊資料結構的C++說明template<typename ElemType>class LinkQueue{public:    class LinkNode    {    public:        ElemType data;        LinkNode * next;    };    typedef LinkNode*  NodePointer;    LinkQueue();    ~LinkQueue();    void randLinkQueue();    void display();    void clear();    Status deQueue(ElemType &e);    Status enQueue(ElemType &e);private:    NodePointer front;    NodePointer rear;};/////////////////////////////////////////  自動調用建構函式和解構函式template<typename ElemType>LinkQueue<ElemType>::LinkQueue(){}template <typename ElemType>LinkQueue<ElemType>::~LinkQueue(){}template<typename ElemType>void LinkQueue<ElemType>::randLinkQueue(){    srand(unsigned(time(NULL)));    int n;    ElemType Elem[11];    NodePointer p,s;    n=rand()%10+1;    cout<<"產生的隨機數組為:"<<endl;    for (int i = 0; i < n; i++)    {        Elem[i]=rand()%100+1;        cout<<setw(6)<<Elem[i];    }    front=NULL;    for (int i = 0; i < n; i++)    {        s=new(LinkNode);        assert(s!=0);        s->data=Elem[i];        s->next=NULL;        if (front==NULL)            front=rear=s;        else        {            rear->next=s;            rear=rear->next;        }    }    display();}template<typename ElemType>void LinkQueue<ElemType>::display(){    NodePointer p;    int n=0;    p=front;    cout<<endl<<"產生的非迴圈鏈隊為:"<<endl;    while (p!=NULL)    {        cout<<setw(6)<<p->data;        p=p->next;        n++;    }    cout<<endl;    cout<<setw(6)<<"↑";    for (int i = 0; i < n-2; i++)    {            cout<<setw(6)<<" ";    }    cout<<setw(6)<<"↑"<<endl;    cout<<endl;    cout<<setw(6)<<"front";    for (int i = 0; i < n-2; i++)    {            cout<<setw(6)<<" ";    }    cout<<setw(6)<<"rear"<<endl;}template<typename ElemType>Status LinkQueue<ElemType>::deQueue(ElemType &e){    NodePointer p;    p=front;    if (p==NULL)        return ERROR;    e=p->data;    front=p->next;    delete p;    return OK;}template<typename ElemType>Status LinkQueue<ElemType>::enQueue(ElemType &e){    NodePointer s;    s=new(LinkNode);    assert(s!=0);    s->data=e;    s->next=NULL;    if(front==NULL)        front=rear=s;    rear->next=s;    return OK;}
// LinkQueueTest.cpp : 定義控制台應用程式的進入點。//#include "stdafx.h"#include"LinkQueue.h"#include<iostream>using namespace std;int _tmain(int argc, _TCHAR* argv[]){    LinkQueue<int> LQ;    int a;    LQ.randLinkQueue();    LQ.deQueue(a);    LQ.display();    cout<<"輸入要進入隊列的元素:"<<endl;    cin>>a;    LQ.enQueue(a);    LQ.display();    system("pause");    return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.