動態數組實現的隊列

來源:互聯網
上載者:User
/*_############################################################################
  _##
  _##  動態數組實現的隊列
  _##  Author: xwlee                        
  _##  Time: 2006.12.31 
  _##  Chang'an University
  _##  Development condition: win2003 Server+VC6.0
  _##
  _##  dynamic_array.cpp 檔案
  _##########################################################################*/
#include "stack.h"
#include <stdio.h>

// 用於儲存隊列元素的指標和指向隊列頭和尾的指標
static QUEUE_TYPE  *queue;
static size_t  queue_size;  // 隊列的大小
static size_t  front = 1;
static size_t  rear = 0;
static size_t  qnumber = 0; // 引入新變數,記錄隊列中的元素數量(方法1)

// create_queue函數
int create_queue( size_t size )
{
    if( queue_size != 0)
    {
        printf("create queue is false,try again./n");
        exit(0);
    }
    queue_size = size;
    //queue = ( QUEUE_TYPE * )malloc( (queue_size+1) * sizeof( QUEUE_TYPE ) ); // 方法2
    queue = ( QUEUE_TYPE * )malloc( queue_size * sizeof( QUEUE_TYPE ) ); // 方法1
    if( queue == NULL )
    {
        printf("malloc is false,try again./n");
        return 0;
    }

    return 1;
}

// destroy_queue函數
int destroy_queue( void )
{
    if( queue_size == 0)
    {
        printf("destroy queue is false./n");
        exit(0);
    }
    queue_size = 0;
    free( queue );
    queue = NULL;

    return 1;
}

// insert函數
void myinsert( QUEUE_TYPE value )
{
    if( is_full() )
    {
        printf("queue is already full, insert is false./n");
        exit(0);
    }
    //rear = ( rear + 1 ) % (queue_size+1); // 方法2
    rear = ( rear + 1 ) % (queue_size); // 方法1
    queue[ rear ] = value;
    printf("rear=%d", rear);
    qnumber++;   // 引入新變數使用.(方法1)
}

// delete函數
void mydelete( void )
{
    if( is_empty() ) // 若隊列已空,條件成立.
    {
        printf("queue already empty, delete is false./n");
        exit(0);
    }
    //front = ( front + 1 ) % (queue_size+1); // 方法2
    front = ( front + 1 ) % (queue_size); // 方法1
    printf("front=%d", front);
    qnumber--;  // 引入新變數使用.(方法1)
}

// first函數
QUEUE_TYPE first( void )
{
    if( is_empty() ) // 若隊列已空,條件成立.
    {
        printf("queue already empty./n");
        exit(0);
    }
    return queue[ front ];
}

// is_empty函數
int is_empty( void )
{
    //return ( rear +1 ) % (queue_size+1) == front; // 方法2
    return  qnumber == 0;  // 引入新變數使用. 方法1
}

// is_full函數
int is_full( void )
{
    //return ( rear +2 ) % (queue_size+1) == front;  // 方法2
    return  qnumber == (queue_size);  // 引入新變數使用. 方法1
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.