Data Structures (Weiss) Chapter 3: The Queue ADT, Array

本文深入探讨了使用C++实现队列ADT(抽象数据类型)的方法,特别关注了FIFO(先进先出)原则。通过模板类Queue的构建,详细介绍了如何利用数组构造队列,并阐述了关键成员函数如empty、size、front、back、push和pop的使用。最后,通过实例展示了队列的操作,包括初始化、添加元素、删除元素和访问队首队尾元素的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//

//  main.cpp

//  Data Structure TRY1

//

//  Created by zr9558 on 6/7/13.

//  Copyright (c) 2013 zr9558. All rights reserved.

//



// Data Structure C++, Weiss, P.104 Section 3.7 The Queue ADT

// make use of array to construct the class queue (FIFO)


/*

 http://www.cplusplus.com/reference/queue/queue/

 member functions:

 empty   Test whether container is empty (public member function)

 size    Return size (public member function)

 front   Access next element (public member function)

 back    Access last element (public member function)

 push    Insert element (public member function)

 pop     Delete next element (public member function)

 

 */


#include<iostream>

using namespace std;


template<typename Comparable>

class Queue

{

public:

    Queue(){ theSize=0; theCapacity=10; Array= new Comparable[theCapacity];}

    Queue(const Queue &rhs) { operator=(rhs);}


    const Queue &operator=( const Queue &rhs)

    {

        if( this!=&rhs)

        {

            theSize=rhs.theSize;

            theCapacity=rhs.theCapacity;

            

            Array=new Comparable[theCapacity];

            for( int i=0; i!=theSize; ++i)

                Array[i]=rhs.Array[(rhs.front+i)%theCapacity];

                

            front=0; back=theSize-1;

            

        }

        return *this;

    }

    

    ~Queue() { delete []Array;}

    

    bool Empty() const { return theSize==0;}

    int Size() { return theSize;}

    Comparable Front() { return Array[front];}

    Comparable Back() { return Array[back];}

    

    void Push( const Comparable &x)

    {

        if( theSize==theCapacity) reserve(2*theCapacity+1);

        

        if( theSize==0)

        {

            front=back=0;

            Array[0]=x;

            ++theSize;

        }

        else

        {

            back=(back+1)%theCapacity;

            Array[back]=x;

            ++theSize;

        }

        

    }

    

    void Pop()

    {

        front=(front+1)%theCapacity;

        --theSize;

    }

    

private:

    Comparable * Array;

    int theSize;

    int theCapacity;

    

    int front, back;

    

    void reserve( int newCapacity)

    {

        if( newCapacity<theSize) return;

        

        Comparable *oldArray=Array;

        

        Array=new Comparable[newCapacity];

        for( int i=0; i<theSize; ++i)

            Array[i]=oldArray[(front+i)%theSize];

        

        theCapacity=newCapacity;

        front=0; back=theSize-1;

        

        delete []oldArray;

        

    }


};








int main()

{

    Queue<int> Q;

    for( int i=0; i<20; ++i)

    {

        Q.Push(i);

    }

    

    Queue<int> Q2(Q);

    

    cout<<Q.Size()<<endl;

    while( !Q.Empty())

    {

        cout<<Q.Front()<<" "<<Q.Back()<<endl;

        Q.Pop();

    }

    cout<<endl;

    

    cout<<Q.Size()<<endl;

    


    cout<<Q2.Front()<<" "<<Q2.Back()<<endl;

    cout<<Q2.Size()<<endl;

    return 0;

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值