數據結構和算法系列4 隊列

上一篇講了棧,這一篇要總結的是咱們經常使用的隊列,我想從如下幾個方面進行總結。spa

1,什麼是隊列?
2,隊列的存儲結構?
3,隊列的經常使用操做及實現代碼?指針

1,什麼是隊列

1,首先,隊列也是一種特殊的線性表,它是一種操做受限的線性表。它只容許在表的一端進行元素插入,而在另外一端進行元素刪除。容許插入的一端稱爲隊尾(rear),容許刪除的一端稱爲隊頭(font)。code

2,對於隊列,與現實生活中的排隊相似,新加入的成員老是在隊尾,而排在隊列最前面的老是最早離開隊列,即先進先出,所以隊列也稱爲先進先出表(FIFO)。blog

2,隊列的存儲結構

ds16

實現代碼:隊列

/// <summary>
    /// 封裝順序隊列
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SeqQueue<T>
    {
        private const int _maxSize = 100;
        public int MaxSize
        {
            get { return _maxSize; }
        }

        //保存數據
        public T[] data=new T[_maxSize];

        //頭指針
        public int head;

        //尾指針
        public int rear;
    }

3,隊列的常見操做及實現代碼

1,初始化隊列get

思路:構造一個空隊列,即將頭指針head和尾指針rear都設置爲0。string

2,入隊it

思路:若隊列不滿,則將數據x插入到尾指針rear指向的位置,而後再將rear加1。io

3,出隊class

思路:若隊列不空,則將頭指針head加1,並返回被刪除的元素。

4,取隊頭

思路:若隊列不空,則返回隊頭元素。

5,取隊長

思路:即尾指針rear-頭指針head。

6,判隊空

思路:只須要判斷頭指針head與尾指針是否相等便可

7,判隊滿

思路:只需判斷尾指針與MaxSize是否相等便可

注:在一個非空隊列中,頭指針始終指向隊頭元素,而尾指針始終指向隊尾元素的下一個位置

C#版:

namespace DS.Model
{
    /// <summary>
    /// 學生實體
    /// </summary>
    public class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

namespace DS.BLL
{
    /// <summary>
    /// 隊列常見操做邏輯類
    /// </summary>
    public class SeqQueueBLL
    {
        /// <summary>
        /// 初始化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqQueue"></param>
        /// <returns></returns>
        public static SeqQueue<T> Init<T>(SeqQueue<T> seqQueue)
        {
            seqQueue.head = 0;
            seqQueue.rear = 0;
            return seqQueue;
        }

        /// <summary>
        /// 入隊
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqQueue"></param>
        /// <returns></returns>
        public static SeqQueue<T> Enter<T>(SeqQueue<T> seqQueue,T data)
        { 
            //是否隊滿
            if (IsFull(seqQueue)) return null;

            seqQueue.data[seqQueue.rear++] = data; //等價於seqQueue.data[seqQueue.rear] = data; seqQueue.rear++;
            return seqQueue;
        }

        /// <summary>
        /// 出隊
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqQueue"></param>
        /// <returns></returns>
        public static T Dequeue<T>(SeqQueue<T> seqQueue)
        { 
            //是否隊空
            if (IsEmpty(seqQueue)) return default(T);

            var data = seqQueue.data[seqQueue.head];
            seqQueue.head++;
            return data;
        }

        /// <summary>
        /// 取隊頭
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqQueue"></param>
        /// <returns></returns>
        public static T GetHead<T>(SeqQueue<T> seqQueue)
        { 
            //是否隊空
            if(IsEmpty(seqQueue)) return default(T);

            return seqQueue.data[seqQueue.head];
        }

        /// <summary>
        /// 取隊長
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqQueue"></param>
        /// <returns></returns>
        public static int GetLength<T>(SeqQueue<T> seqQueue)
        {
            return seqQueue.rear - seqQueue.head;
        }
        

        /// <summary>
        /// 判斷是否隊滿
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqQueue"></param>
        /// <returns></returns>
        public static bool IsFull<T>(SeqQueue<T> seqQueue)
        {
            if (seqQueue.rear >= seqQueue.MaxSize) return true;
            return false;
        }

        /// <summary>
        /// 判斷是否隊空
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="seqQueue"></param>
        /// <returns></returns>
        public static bool IsEmpty<T>(SeqQueue<T> seqQueue)
        {
            if (seqQueue.head == seqQueue.rear) return true;
            return false;
        }
    }

    /// <summary>
    /// 封裝順序隊列
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class SeqQueue<T>
    {
        private const int _maxSize = 100;
        public int MaxSize
        {
            get { return _maxSize; }
        }

        //保存數據
        public T[] data=new T[_maxSize];

        //頭指針
        public int head;

        //尾指針
        public int rear;
    }
}

namespace SeqQueue.CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            SeqQueue<Student> seqQueue = new SeqQueue<Student>();

            Console.WriteLine("********************初始化*******************\n");
            SeqQueueBLL.Init(seqQueue);
            Console.WriteLine("當前隊列的長度是:{0}", SeqQueueBLL.GetLength(seqQueue));

            Console.WriteLine("\n********************入隊*******************\n");
            Console.WriteLine("插入五條數據到隊列\n");
            SeqQueueBLL.Enter(seqQueue, new Student { ID = 1, Name = "a", Age = 1 });
            SeqQueueBLL.Enter(seqQueue, new Student { ID = 2, Name = "b", Age = 2 });
            SeqQueueBLL.Enter(seqQueue, new Student { ID = 3, Name = "c", Age = 3 });
            SeqQueueBLL.Enter(seqQueue, new Student { ID = 4, Name = "d", Age = 4 });
            SeqQueueBLL.Enter(seqQueue, new Student { ID = 5, Name = "e", Age = 5 });
            Display(seqQueue);

            Console.WriteLine("\n********************出隊*******************\n");
            Console.WriteLine("將隊頭出隊\n");
            Student student= SeqQueueBLL.Dequeue(seqQueue);
            Display(seqQueue);

            Console.WriteLine("\n********************取隊頭*******************\n");
            Student result= SeqQueueBLL.GetHead(seqQueue);
            Console.WriteLine("ID={0},Name={1},Age={2}\n", result.ID, result.Name, result.Age);

            Console.WriteLine("\n********************取隊長*******************\n");
            Console.WriteLine("當前隊列中有{0}個元素\n",SeqQueueBLL.GetLength(seqQueue));

            Console.ReadKey();
        }

        /// <summary>
        /// 展現數據
        /// </summary>
        /// <param name="seqQueue"></param>
        private static void Display(SeqQueue<Student> seqQueue)
        {
            Console.WriteLine("\n*****展現數據*****\n");

            for (int i = seqQueue.head; i < seqQueue.rear; i++)
            {
                Console.WriteLine("ID={0},Name={1},Age={2}\n", seqQueue.data[i].ID, seqQueue.data[i].Name, seqQueue.data[i].Age);
            }
            Console.WriteLine("*****展現完畢*****\n");
        }
    }
}

程序運行結果:

ds17-1

ds17-2

 

C語言版:

#include "stdio.h"    
#include "stdlib.h"   
#include "io.h"  
#include "math.h"  
#include "time.h"

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define MAXSIZE 20

typedef int Status; 
typedef int ElemType;

/* 隊列的順序存儲結構 */
typedef struct
{
    ElemType data[MAXSIZE];
    int head;        /* 頭指針 */
    int rear;        /* 尾指針,若隊列不空,指向隊列尾元素的下一個位置 */
}SeqQueue;

/*初始化*/
Status Init(SeqQueue *seqQueue)
{
    seqQueue->head=0;
    seqQueue->rear=0;
    return OK;
}

/*入隊*/
Status Enter(SeqQueue *seqQueue,ElemType e)
{
    //是否隊滿
    if (IsFull(seqQueue)) return ERROR;

    seqQueue->data[seqQueue->rear]=e;
    seqQueue->rear++;
    return OK;
    
}

/*出隊*/
Status Dequeue(SeqQueue *seqQueue,ElemType *e)
{
    //是否隊空
    if (IsEmpty(seqQueue)) return ERROR;
    
    *e=seqQueue->data[seqQueue->head]; //爲何不能用e=seqQueue->data[seqQueue->head]
    seqQueue->head++;
    return OK;
}

/*取隊頭*/
Status GetHead(SeqQueue *seqQueue,ElemType *e)
{
    //是否隊空
    if (IsEmpty(seqQueue)) return ERROR;
    
    *e=seqQueue->data[seqQueue->head];
    return OK;
}

/*取隊長*/
int GetLength(SeqQueue *seqQueue)
{
    return seqQueue->rear-seqQueue->head;
}

/*是否隊滿*/
Status IsFull(SeqQueue *seqQueue)
{
    if (seqQueue->rear>=MAXSIZE) return TRUE;
    return FALSE;
}

/*是否隊空*/
Status IsEmpty(SeqQueue *seqQueue)
{
    if (seqQueue->rear==seqQueue->head) return TRUE;
    return FALSE;
}

/*展現數據*/
void Display(SeqQueue *seqQueue)
{
    int i;
    printf("*****展現數據*****\n");
    for (i=seqQueue->head;i<seqQueue->rear;i++)
    {
        printf("%d ",seqQueue->data[i]);
    }
    printf("\n*****展現完畢*****\n");
}

void main()
{
    SeqQueue seqQueue;
    ElemType e;
    Status i;
    int j;

    printf("***************初始化***************\n");
    i=Init(&seqQueue);
    if (i==OK) printf("初始化成功!\n");
    else printf("初始化失敗!\n");

    printf("\n***************入隊***************\n");
    printf("插入五條數據到隊列\n");
    for (j=1;j<6;j++)
    {
        i=Enter(&seqQueue,j);
    }
    Display(&seqQueue);

    printf("\n***************出隊***************\n");
    printf("將隊頭出隊\n");
    Dequeue(&seqQueue,&e);
    Display(&seqQueue);

    printf("\n***************取隊頭***************\n");
    GetHead(&seqQueue,&e);
    printf("當前隊頭元素是:%d\n",e);

    printf("\n***************取隊長***************\n");
    printf("當前隊列中有%d個元素\n",GetLength(&seqQueue));
    
    getchar();
}

程序運行結果:

ds18

相關文章
相關標籤/搜索