【資料結構-隊列】鏈式隊列

來源:互聯網
上載者:User

標籤:隊列   鏈式隊列   

關於鏈式隊列

鏈式隊列又稱為鏈隊,是使用單鏈表實現的,需要一個頭指標一個尾指標
結構圖:

鏈隊需要的元素組成
/*鏈式隊列的每一個節點*/struct node{    int data;//儲存資料    struct node *next;//指向下一個節點的指標};
/*鏈式隊列*/typedef struct{    struct node *head;//頭指標    struct node *tail;//尾指標}LinkedQueue;
建立一個帶前端節點的空隊列
  1. 建立一個節點p
  2. 將p節點的next指向null
  3. 建立一個隊列
  4. 將隊列的head指標指向節點p
  5. 將隊列的tail指標指向節點p

建立出空節點的結構圖(實際建立出來沒有p這個指標)

/*初始化一個空的鏈式隊列*/LinkedQueue *initLinkedQueue(){    LinkedQueue *linkedQueue = (LinkedQueue *)malloc(sizeof(LinkedQueue));//建立隊列    struct node *p = (struct node *)malloc(sizeof(struct node));//建立第一個空節點    p->next = NULL;//讓第一個節點的next指向null    linkedQueue->head = linkedQueue->tail = p;//將隊列的head和tail指標指向第一個節點    return linkedQueue;}
入隊操作

對於鏈式隊列不必考慮隊列滿不滿,因為永遠不會滿
1. 建立一個新的節點q
2. 將傳入的資料賦值給q的data
3. q的next指標指向null
4. 將隊列的tail指標指向的節點的next指標指向q節點
5. 隊列的tail指標指向新插入的那個q節點,tail永遠指向最後一個節點

/*入隊操作*/void insert(LinkedQueue *lq, int num){    struct node *q = (struct node *)malloc(sizeof(struct node));//在記憶體中申請一個新的節點    q->data = num;//給新節點傳入資料    q->next = NULL;//新節點的next指標為空白    lq->tail->next = q;//將尾節點所指的那個節點的下一個節點指向新節點    lq->tail = q;//尾指標指向新插入的節點}
出隊操作
  1. 聲明一個節點q
  2. 如果隊列不為空白,讓q指向前端節點後面的那個節點,也就是要刪除的那個節點
  3. 讓隊列的前端節點的next指向q所指節點 的後面的那個節點
  4. 將q所指節點的data儲存起來
  5. 銷毀q所指的那個節點(free(q))
  6. 注意如果只有最後一個節點出隊了,tail指標所指的節點記憶體已被回收,所以需要對tail指標做處理,讓隊列的head指標=tail指標


/*刪除隊列元素*/int delete(LinkedQueue *lq, int *temp){    struct node *q;    if (isEmpty(lq)) {        printf("隊列為空白!\n");        return 0;    }    else {        q = lq->head->next;//讓q指向前端節點後面的那個節點        lq->head->next = q->next;//讓隊列的前端節點的next指向q所指節點 的後面的那個節點        *temp = q->data;//將q所指節點的data儲存起來        free(q);// 銷毀q所指的那個節點,回收記憶體        //當只有一個元素的時候,出隊後隊列為空白,此時要修改尾指標        if (lq->head->next == NULL) {            lq->head = lq->tail;        }        return 1;    }}
使用C語言實現鏈式隊列
#include<stdio.h>#include<stdlib.h>/*鏈式隊列的每一個節點*/struct node{    int data;    struct node *next;};/*鏈式隊列*/typedef struct{    struct node *head;    struct node *tail;}LinkedQueue;/*初始化一個空的鏈式隊列*/LinkedQueue *initLinkedQueue(){    LinkedQueue *linkedQueue = (LinkedQueue *)malloc(sizeof(LinkedQueue));    struct node *p = (struct node *)malloc(sizeof(struct node));    p->next = NULL;    linkedQueue->head = linkedQueue->tail = p;    return linkedQueue;}/*入隊操作*/void insert(LinkedQueue *lq, int num){    struct node *q = (struct node *)malloc(sizeof(struct node));//在記憶體中申請一個新的節點    q->data = num;//給新節點傳入資料    q->next = NULL;//新節點的next指標為空白    lq->tail->next = q;//將尾節點所指的那個節點的下一個節點指向新節點    lq->tail = q;//尾指標指向新插入的節點}/*判斷隊列是否為空白*/int isEmpty(LinkedQueue *lq){    if (lq->head == lq->tail){//如果頭尾指標指向一個,隊列就是為空白的        return 1;    }    else {        return 0;    }}/*刪除隊列元素*/int delete(LinkedQueue *lq, int *temp){    struct node *q;    if (isEmpty(lq)) {        printf("隊列為空白!\n");        return 0;    }    else {        q = lq->head->next;        lq->head->next = q->next;        *temp = q->data;        free(q);        //當只有一個元素的時候,出隊後隊列為空白,此時要修改尾指標        if (lq->head->next == NULL) {            lq->head = lq->tail;        }        return 1;    }}/*遍曆隊列*/void display(LinkedQueue *lq){    struct node *q = lq->head->next;    while (q != NULL) {        printf("隊列元素為%d\n", q->data);        q = q->next;    }}int main(){    LinkedQueue *lq = initLinkedQueue();    insert(lq, 1);    insert(lq, 2);    insert(lq, 3);    insert(lq, 4);    insert(lq, 5);    int temp = 0;    delete(lq, &temp);    printf("%d\n", temp);    display(lq);    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.