資料結構學習之迴圈隊列(順序儲存)__隊列

來源:互聯網
上載者:User

【摘要】隊列特性:先進先出(FIFO)——先進隊列的元素先出隊列。來源於我們生活中的隊列(先排隊的先辦完事)。

這樣有個缺陷,空間利用率不高,所以我們直接學習迴圈隊列(基於連續記憶體的)。


(1)設計隊列資料結構

typedef struct _QUEUE_NODE{    int* pData;    int length;//隊列長度    int head ;//隊頭指標    int tail;//隊尾指標    int count;//隊列元素當前個數}QUEUE_NODE;

(2)申請隊列記憶體

QUEUE_NODE* alloca_queue(int number){    QUEUE_NODE* pQueueNode;    if( 0 == number)        return NULL;    pQueueNode = (QUEUE_NODE*)malloc(sizeof(QUEUE_NODE));    assert(NULL != pQueueNode);    memset(pQueueNode, 0, sizeof(QUEUE_NODE));    pQueueNode->pData = (int*)malloc(sizeof(int) * number);    if(NULL == pQueueNode->pData){        free(pQueueNode);        pQueueNode = NULL;        return NULL;    }    pQueueNode->length = number;//隊列長度    return pQueueNode;}

(3)釋放隊列記憶體

STATUS delete_queue(const QUEUE_NODE* pQueueNode){    if(NULL == pQueueNode)         return FALSE;    assert(NULL != pQueueNode->pData);    free(pQueueNode->pData);    free((void*)pQueueNode);    return TRUE;}

(4) 把資料壓入隊列

STATUS insert_queue(QUEUE_NODE* pQueueNode, int value){    if(NULL == pQueueNode)        return FALSE;    if(pQueueNode->length == pQueueNode->count)//判斷是不是溢出        return FALSE;    pQueueNode->pData[pQueueNode->tail] = value;    pQueueNode->tail = (pQueueNode->tail + 1) % pQueueNode->length;  //隊尾指標位置    pQueueNode->count ++;    return TRUE;}

(5)把資料彈出隊列

STATUS get_queue_data(QUEUE_NODE* pQueueNode, int* value){    if(NULL == pQueueNode || NULL == value)        return FALSE;    if(0 == pQueueNode->count)        return FALSE;    *value = pQueueNode->pData[pQueueNode->head];    pQueueNode-> pData[pQueueNode->head] = 0; //被彈出的位置賦值為0    pQueueNode-> count --;    pQueueNode->head = (pQueueNode->head + 1) % pQueueNode->length;//重新置放隊頭指標的位置    return TRUE;}

(6)統計當前隊列中有多少資料

int  get_total_number(const QUEUE_NODE* pQueueNode){    if(NULL == pQueueNode)        return 0;    return pQueueNode->count;}

(7)查看隊列中初始化的時候總長度是多少

int  get_total_number(const QUEUE_NODE* pQueueNode){    if(NULL == pQueueNode)        return 0;    return pQueueNode->length;}
// 迴圈隊列.cpp : 定義控制台應用程式的進入點。////迴圈隊列//Written by ZP1015//2015.10.21#include "stdafx.h"#include <stdlib.h>#include <stdio.h>#include <string.h>struct QUEUE_NODE{    int* pData;    int QueueLenMax;//隊列最大長度    int head ;//隊頭指標    int tail;//隊尾指標    int QueueCurLen;//隊列元素當前個數};struct QUEUE_NODE* alloc_queue(int Queue_Max_Size){    if(Queue_Max_Size <= 0)        return NULL;    struct QUEUE_NODE* pQueueNode  = NULL;    pQueueNode = (QUEUE_NODE*)malloc(sizeof(QUEUE_NODE));    if(NULL == pQueueNode) {        return NULL;    }    memset(pQueueNode, 0, sizeof(struct QUEUE_NODE));    pQueueNode->pData = (int*)malloc(sizeof(int) * Queue_Max_Size);    if(NULL == pQueueNode->pData) {       goto malloc_failed;    }    pQueueNode->QueueLenMax = Queue_Max_Size;//隊列長度    pQueueNode->head = 0;    pQueueNode->tail = 0;    pQueueNode->QueueCurLen = 0;    return pQueueNode;malloc_failed:    free(pQueueNode);    return NULL;}int free_queue(struct QUEUE_NODE* pQueueNode){    if(NULL == pQueueNode)         return -1;    if(NULL == pQueueNode->pData) {        free(pQueueNode);        return -1;     }    free(pQueueNode->pData);    free(pQueueNode);    return 0;}int queue_push(struct QUEUE_NODE* pQueueNode, int value){    if(NULL == pQueueNode)        return -1;    if(pQueueNode->QueueLenMax == pQueueNode->QueueCurLen)//判斷是不是溢出        return -1;    pQueueNode->pData[pQueueNode->tail] = value;    pQueueNode->tail = (pQueueNode->tail + 1) % pQueueNode->QueueLenMax;  //隊尾指標位置    pQueueNode->QueueCurLen ++;    return 0;}int queue_pop(struct QUEUE_NODE* pQueueNode, int* value){    if(NULL == pQueueNode || NULL == value)        return -1;    if(0 == pQueueNode->QueueCurLen)        return -1;    *value = pQueueNode->pData[pQueueNode->head];    pQueueNode->pData[pQueueNode->head] = 0; //被彈出的位置賦值為0    pQueueNode->QueueCurLen --;    pQueueNode->head = (pQueueNode->head + 1) % pQueueNode->QueueLenMax;//重新置放隊頭指標的位置    return 0;}int get_queue_curlen(struct QUEUE_NODE* pQueueNode){    if(NULL == pQueueNode)        return -1;    return pQueueNode->QueueCurLen;}int get_queue_maxlen(struct QUEUE_NODE* pQueueNode){    if(NULL == pQueueNode)        return 0;    return pQueueNode->QueueLenMax;}void print_queue_node(struct QUEUE_NODE *pQueueNode) {    /*1.輸入的參數有誤*/    if(NULL == pQueueNode) {        printf("[%d] pQueueNode is illegal! \n",__LINE__);        return;    }    /*2.輸入的鏈式堆棧為空白*/    if(0 == pQueueNode->QueueCurLen) {        printf("[%d] pQueueNode is empty!\n",__LINE__);        return ;    }    struct QUEUE_NODE *pQueueNodeTemp = pQueueNode;    int count = 0;    while(count < pQueueNode->QueueCurLen) {        printf("%d ",pQueueNode->pData[pQueueNode->head+count]);        count++;;    }    printf("\n");}int main(){    struct QUEUE_NODE *pQueueNode;    pQueueNode = alloc_queue(20);    int i = 0;    for (i = 0;i<10;i++) {        queue_push(pQueueNode,i);    }    print_queue_node(pQueueNode);    int a = 0;    queue_pop(pQueueNode,&a);    print_queue_node(pQueueNode);    free_queue(pQueueNode);    getchar();    getchar();    return 0;}
OJ 系列迴圈隊列的基本操作
/******************************************************************************  Copyright (C), 2001-2011, SCUT. ******************************************************************************  File Name     :   Version       :   Author        :   Created       : 2016/01/21  Last Modified :  Description   :   Function List :  History       :  1.Date        : 2016/01/21    Author      :     Modification: Created file******************************************************************************/#include <stdlib.h>#define MAXSIZE 50struct strqueue {    int queue[MAXSIZE];    int head; /* 隊頭 */    int tail; /* 隊尾 */    int num;  /* 隊元素個數 */};/* 初始化隊列,返回0表示失敗,返回1表示成功 */bool initqueue(struct strqueue *s) {    if(!s)        return 0;    for(int i = 0;i<MAXSIZE;i++) {        s->queue[i] = 0;    }    s->head = -1;    s->tail = -1;    s->num = 0;    return 1;}bool enqueue(struct strqueue *s, int x) /* 進隊列,返回0表示失敗,返回1表示成功 */{    if(!s)        return 0;    if(s->num == MAXSIZE )        return 0;    if(s->head==-1||s->tail==-1) {        s->head = 0;        s->tail = 0;        s->queue[s->tail] = x; /*賦值*/        s->num  = 1;        return 1;    }     s->tail  =  (s->tail + 1) % MAXSIZE;/*從隊尾入隊列*/    s->queue[s->tail] = x; /*賦值*/    s->num ++;    return 1;}bool dequeue(struct strqueue *s, int *x) /* 出隊列,返回0表示失敗,返回1表示成功 */{    if(!s||!x)        return 0;    if(s->num == 0)        return 0;    *x = s->queue[s->head]; /*賦值*/    s->queue[s->head] = 0;    s->head  =  (s->head + 1) % MAXSIZE;/*從對頭出隊列*/    s->num --;    if(s->num==0) {        s->head = -1;        s->tail = -1;    }    return 1;}int gethead(struct strqueue *s)  /* 獲得隊列頭數值 */{       if(!s)        return -1;    if(s->num==0)        return -1;    int head = 0;    head = s->queue[s->head];    return head;}int gettail(struct strqueue *s)  /* 獲得隊列尾數值 */{    if(!s)        return 0;    int tail = 0;    if(s->num==0)        return -1;    tail=s->queue[s->tail];    return tail;}int getqueuelenth(struct strqueue *s)  /* 獲得隊列長度 */{    if(!s)        return 0;    int lenth = 0;    lenth = s->num;    return lenth;}bool search(struct strqueue *s, int x)  /* 在隊列中尋找x是否存在,如果存在返回1,否則返回0 */{    if(!s)        return 0;    int headTemp = s->head;    int tailTemp = s->tail;    /*1.隊列為滿的情況*/    if(s->num == MAXSIZE) {        for(int i=0;i<MAXSIZE;i++) {            if(x==s->queue[i])                return 1;        }    } else {    /*2.隊列不滿,從head到tail處是存在元素的*/        while(headTemp!=tailTemp) {            if(x==s->queue[headTemp])                return 1;            headTemp=(headTemp + 1) % MAXSIZE;        }    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.