隊列的基本操做-隊列的鏈式存儲結構(帶圖詳細)

什麼是隊列?        

        隊列是一種特殊的線性表,特殊之處在於它只容許在表的前端(front)進行刪除操做,而在表的後端(rear)進行插入操做,和棧同樣,隊列是一種操做受限制的線性表。進行插入操做的端稱爲隊尾,進行刪除操做的端稱爲隊頭。隊列中沒有元素時,稱爲空隊列。前端

        鏈式隊列是用單鏈表的形式來表示隊列,可是要符合隊列「尾進頭出」的規則後端

鏈式隊列的構建:

鏈式隊列=單鏈表+隊列函數

以下代碼是對一個隊列的鏈式存儲的定義:首先定義一個構成單鏈表基本單元的結點,而後定義由指向結點的頭指針、指向結點的尾指針和表示隊列長度的變量組成的隊列spa

//鏈式隊列    鏈式結構+隊列
//鏈式結構體 =單鏈表的基本單元:結點 
struct Node{
    int data;//數據域 
    struct Node* next;     //指針域 
}; 
//隊列結構體=頭指針+尾指針+隊列大小 
 struct Queue{
    struct Node* front;//指向結點的頭指針 
    struct Node* rear;//指向結點的尾指針 
    int queueSize; //隊列大小/長度 
}; 

建立結點:

隊列是由一個一個結點經過指針連接起來的指針

//建立結點
struct Node* createNode(int data){
    struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->next=NULL;
    newNode->data=data;
    return newNode;    
}; 

隊列的初始化:

首先使用malloc函數爲隊列分配一塊內存,初始狀態隊列的頭指針和尾指針在一塊兒都爲空(不帶頭結點),而後設置隊列的大小爲0。這樣隊列的初始化操做就完成了。code

//隊列初始化
struct Queue* createQueue(){
    struct Queue* queue=(struct Queue*)malloc(sizeof(struct Queue));//分配內存空間 
    queue->front=queue->rear=NULL;//頭指針和尾指針在一塊兒爲空 
    queue->queueSize=0;//隊列大小爲0 
    return queue;
} 

入隊操做:

準備工做:首先建立入隊函數push()傳入兩個參數,一個是須要插入那個隊列另外一個是須要插入的數據。而後調用createNode()函數建立一個新的結點,保存插入的數據。blog

準備工做完成後:先判斷隊列是不是空隊列,若是爲空隊列直接將front和rear指針指向這個新建結點便可,若不爲空,則將尾指針的下一個位置指向新建結點,而後再將尾指針修改成這個新建結點。(這個地方我我的感受比較難懂,個人理解是 先告訴這個新建結點在哪裏,而後在移動rear指針到新建結點把他設爲隊尾)。隊列

最後別忘記queueSize自增,表示隊列長度的增長。內存

 

void push(struct Queue* queue,int data){
    struct Node* newNode=createNode(data);
    if(queue->queueSize==0)
        queue->front=newNode;
        else
        queue->rear->next=newNode;
        queue->rear=newNode;
        queue->queueSize++;
    
} 

 獲取對頭元素:

首先判斷隊列queueSize是否爲0若是爲0說明隊列爲空,若是不爲空直接返回隊列頭指針的data值。it

//獲取對頭元素
int queryFront(struct Queue* queue) {
    if(queue->queueSize==0){
        printf("隊列爲空沒法獲取對頭元素");
        printf("\n"); 
        return -1; 
    }
    return queue->front->data;
}

判斷隊列是否爲空:

 代碼很簡單就不一一解釋了。

//判斷隊列是否爲空
int empty(struct Queue* queue){
    if(queue->queueSize==0)
    return 0;
    else
    return 1;
} 

出隊操做:

新建頭結點指針指向隊列front指針所指結點的下一個位置(由於出隊操做是在對頭進行的),而後釋放原來的隊頭結點,最後設置這個新的頭結點爲隊頭。最後隊列的大小減1.

 

 

//出隊操做
void pop (struct Queue* queue){
    if(queue->queueSize==0){

    printf("隊列爲空不能出隊");
    exit(0);
     }else{
         struct Node* newFrontNode=queue->front->next;
         free(queue->front); 
         queue->front=newFrontNode;
         queue->queueSize--;
     }
}

 最後附上完整代碼:

#include <stdio.h>
#include <stdlib.h>
//鏈式隊列    鏈式結構+隊列
//鏈式結構體 =單鏈表的基本單元:結點 
struct Node{
    int data;//數據域 
    struct Node* next;     //指針域 
}; 
//隊列結構體=頭指針+尾指針+隊列大小 
 struct Queue{
    struct Node* front;//指向結點的頭指針 
    struct Node* rear;//指向結點的尾指針 
    int queueSize; //隊列大小/長度 
}; 
//建立結點
struct Node* createNode(int data){
    struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));
    newNode->next=NULL;
    newNode->data=data;
    return newNode;    
}; 
//隊列初始化
struct Queue* createQueue(){
    struct Queue* queue=(struct Queue*)malloc(sizeof(struct Queue));//分配內存空間 
    queue->front=queue->rear=NULL;//頭指針和尾指針在一塊兒爲空 
    queue->queueSize=0;//隊列大小爲0 
    return queue;
} 
//入隊
void push(struct Queue* queue,int data){
    struct Node* newNode=createNode(data);
    if(queue->queueSize==0)
        queue->front=newNode;
        else
        queue->rear->next=newNode;
        queue->rear=newNode;
        queue->queueSize++;
    
} 
//獲取對頭元素
int queryFront(struct Queue* queue) {
    if(queue->queueSize==0){
        printf("隊列爲空沒法獲取對頭元素");
        printf("\n"); 
        return -1; 
    }
    return queue->front->data;
}
//判斷隊列是否爲空
int empty(struct Queue* queue){
    if(queue->queueSize==0)
    return 0;
    else
    return 1;
} 
//出隊操做
void pop (struct Queue* queue){
    if(queue->queueSize==0){

    printf("隊列爲空不能出隊");
    exit(0);
     }else{
         struct Node* newFrontNode=queue->front->next;
         free(queue->front); 
         queue->front=newFrontNode;
         queue->queueSize--;
     }
}
int main(){
    struct Queue* myQueue=createQueue();
    push(myQueue,1);
    push(myQueue,2);
    push(myQueue,3);
    while(empty(myQueue)){
        printf("%d\t",queryFront(myQueue));
        pop(myQueue);
    }
    printf("\n");
    system("pause");
return 0;
}
 
相關文章
相關標籤/搜索