第七週實踐項目1--建立順序環形隊列算法庫

問題及代碼:

/*      
Copyright (c)2015,煙臺大學計算機與控制工程學院      
All rights reserved.      
文件名稱:第7周項目1--建立順序環形隊列算法庫.cpp      
作    者:朱振華      
完成日期:2015年10月19日      
版 本 號:v1.0      
      
問題描述:定義順序環形隊列存儲結構,實現其基本運算
          測試如下:
		  (1)初始化隊列q 
          (2)依次進隊列元素a,b,c 
          (3)判斷隊列是否爲空 
          (4)出隊一個元素 
          (5)輸出隊列中元素個數 
          (6)依次進隊列元素d,e,f 
          (7)輸出隊列中元素個數 
          (8)將隊列中所有元素刪除,並輸出序列 
          (9)釋放隊列
輸入描述:無.
程序輸出:無.
*/
<span style="font-size:14px;">1.頭文件:sqqueue.h,包含定義順序環形隊列數據結構的代碼、宏定義、要實現算法的函數的聲明;</span>
#ifndef SQQUEUE_H_INCLUDED
#define SQQUEUE_H_INCLUDED

#define MaxSize 5
typedef char ElemType;
typedef struct
{
    ElemType data[MaxSize];
    int front,rear;     /*隊首和隊尾指針*/
} SqQueue;


void InitQueue(SqQueue *&q);  //初始化順序環形隊列
void DestroyQueue(SqQueue *&q); //銷燬順序環形隊列
bool QueueEmpty(SqQueue *q);  //判斷順序環形隊列是否爲空
int QueueLength(SqQueue *q);   //返回隊列中元素個數,也稱隊列長度
bool enQueue(SqQueue *&q,ElemType e);   //進隊
bool deQueue(SqQueue *&q,ElemType &e);  //出隊

#endif // SQQUEUE_H_INCLUDED
<span style="font-size:14px;">2.源文件:sqqueue.cpp,包含實現各種算法的函數的定義</span>
<span style="font-size:12px;">#include <stdio.h>
#include <malloc.h>
#include "sqqueue.h"

void InitQueue(SqQueue *&q)  //初始化順序環形隊列
{
    q=(SqQueue *)malloc (sizeof(SqQueue));
    q->front=q->rear=0;
}
void DestroyQueue(SqQueue *&q) //銷燬順序環形隊列
{
    free(q);
}
bool QueueEmpty(SqQueue *q)  //判斷順序環形隊列是否爲空
{
    return(q->front==q->rear);
}


int QueueLength(SqQueue *q)   //返回隊列中元素個數,也稱隊列長度
{
    return (q->rear-q->front+MaxSize)%MaxSize;
}

bool enQueue(SqQueue *&q,ElemType e)   //進隊
{
    if ((q->rear+1)%MaxSize==q->front)  //隊滿上溢出
        return false;
    q->rear=(q->rear+1)%MaxSize;
    q->data[q->rear]=e;
    return true;
}
bool deQueue(SqQueue *&q,ElemType &e)  //出隊
{
    if (q->front==q->rear)      //隊空下溢出
        return false;
    q->front=(q->front+1)%MaxSize;
    e=q->data[q->front];
    return true;
}
</span><span style="font-size:14px;">3.在同一項目(project)中建立一個源文件(如main.cpp),編制main函數,完成相關的測試工作。</span>
#include <stdio.h>
#include "sqqueue.h"

int main()
{
    ElemType e;
    SqQueue *q;
    printf("(1)初始化隊列q\n");
    InitQueue(q);
    printf("(2)依次進隊列元素a,b,c\n");
    if (enQueue(q,'a')==0) printf("隊滿,不能進隊\n");
    if (enQueue(q,'b')==0) printf("隊滿,不能進隊\n");
    if (enQueue(q,'c')==0) printf("隊滿,不能進隊\n");
    printf("(3)隊列爲%s\n",(QueueEmpty(q)?"空":"非空"));
    if (deQueue(q,e)==0)
        printf("隊空,不能出隊\n");
    else
        printf("(4)出隊一個元素%c\n",e);
    printf("(5)隊列q的元素個數:%d\n",QueueLength(q));
    printf("(6)依次進隊列元素d,e,f\n");
    if (enQueue(q,'d')==0) printf("隊滿,不能進隊\n");
    if (enQueue(q,'e')==0) printf("隊滿,不能進隊\n");
    if (enQueue(q,'f')==0) printf("隊滿,不能進隊\n");
    printf("(7)隊列q的元素個數:%d\n",QueueLength(q));
    printf("(8)出隊列序列:");
    while (!QueueEmpty(q))
    {
        deQueue(q,e);
        printf("%c ",e);
    }
    printf("\n");
    printf("(9)釋放隊列\n");
    DestroyQueue(q);
    return 0;
}
<span style="font-size:14px;">運行結果:</span>
<span style="font-size:14px;"><img src="https://img-blog.csdn.net/20151019161802736" alt="" /></span>

 

學習心得:

   建立數據庫,方便以後應用。

<span style="font-size:14px;">
</span>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章