鏈表實現隊列操作


使用鏈表實現隊列,需要一個對頭指向對列頭部管理數據出對,一個隊尾管理數據入隊;還需要隊列的數據區域

那么就需要用兩個結構管理隊列,一個是數據節點,一個隊列

隊列節點結構,專門管理數據的

typedef struct queueNode{

  int  data;   //數據域,存放的是有效數據

  struct queueNode  * next; //指向隊列的下一個節點

}queueNode;

 

隊列管理結構:

typedef struct linkqueue{

   struct queueNode  *front; // 指向隊列頭部 

   struct queueNode  *rear; // 指向隊列尾部

}linkqueue;

1. front 只指向隊列的頭節點,通過頭節點的next指針去訪問數據節點,實現出對操作,

2. 鏈式隊列沒有滿的情況,當隊列為空時,頭和尾都指向頭節點(頭節點只是用來管理這個鏈式對列,並不存放有效數據)

3. 隊尾用來插入隊列,對頭用來出入操作

 

創建一個空的隊列:

 

插 入隊列一個數據

 

這樣通過隊尾rear 一直指向鏈表的尾部管理的數據插入隊列操作

 舉例說明: 隊列 linkqueue *qe;

(1) 插入一個新節點 queueNode *pnew 

(2)qe->rear->next 是當前節點的next指針,用來連接新節點的 qe->rear->next = pnew

(3)新節點的next指針指向空NULL , pnew->next = NULL;

(4)最后是把尾指針,移動指向尾部節點 qe->rear = qe->rear->next;

 linkqueue.c文件:

#include "linkqueue.h"

linkqueue *create_linkqueue(void)
{
    //創建隊列
    linkqueue *qe=NULL;
    qe = (linkqueue*)malloc(sizeof(linkqueue));
    if(qe == NULL)
    {
        printf("create queue malloc error \n");
        return NULL;
    }

    //創建隊列節點
    qe->front = (queueNode*)malloc(sizeof(queueNode));
    if(qe->front == NULL)
    {
        free(qe);
        printf("create node malloc error\n");
        return NULL;
    }
    qe->front->next = NULL;//隊列頭的next指向實際的數據節點
    qe->front->data = 0;
    qe->rear = qe->front; //隊列空時,對頭和對尾指向同一個位置
    return qe;
}

//插入數據,入隊列,對尾入對
int in_linkqueue(linkqueue *qe, u16 value)
{
    if(qe == NULL)
    {
        printf("in lingkqueue is null\n");
        return -1;
    }
    queueNode *pnew = NULL;//入對的新節點
    pnew = (queueNode*)malloc(sizeof(queueNode));
    if(pnew == NULL)
    {
        printf("in pnew malloc is fail\n");
        return -1;
    }
    pnew->data = value;//入對的數據
    pnew->next = NULL;
    qe->rear->next = pnew;//把入對的節點鏈接到隊列上
    qe->rear = qe->rear->next;//把指向對尾的指針,繼續移動到隊尾,即指向新插入的節點位置
    return 1;
}

//判斷隊列是否空,空返回1,非空返回0, 其他返回-1
int is_empty_linkqueue(linkqueue *qe)//判空
{
    if(qe == NULL)
    {
        printf("is empty lingkqueue is null\n");
        return -1;
    }
    return ((qe->front == qe->rear) ? 1 : 0);
}

int out_linkqueue(linkqueue *qe, u16 *dat)//出隊列
{
    if(qe == NULL)
    {
        printf("out lingkqueue is null\n");
        return -1;
    }
    if(is_empty_linkqueue(qe) == 1)//隊列為空
    {
        printf("out lingkqueue is empty\n");
        return 0;
    }
    queueNode *pdel = NULL;//出對的節點
    if(qe->front->next == NULL) //出對列,到對尾時
    {
        qe->rear = qe->front;
        return 0;
    }
    pdel = qe->front->next;//對頭front永遠頭節點,出對時是頭節點的下一個節點
    qe->front->next = pdel->next;//把要刪除的節點的下一個節點地址鏈接到對列頭上
    *dat = pdel->data; //對頭的數據
    free(pdel);
    pdel = NULL;
    return 1; 
}

//顯示隊列內容,從對頭開始顯示
void show_linkqueue(linkqueue *qe)//顯示隊列內容
{
    if(qe == NULL)
    {
        printf("show lingkqueue is null\n");
        return;
    }
    if(is_empty_linkqueue(qe) == 1)//隊列為空
    {
        printf("show lingkqueue is empty\n");
        return;
    }
    queueNode *pcur = qe->front->next;//找到數據節點開始
    while(pcur != NULL)
    {
        printf("%d\n",pcur->data);
        pcur = pcur->next;
    }
}

linkqueue.h文件:

#ifndef __LINKQUEUE_H
#define __LINKQUEUE_H

#include <stdio.h>
#include <stdlib.h>

typedef int u16;

//數據節點
typedef struct queueNode{
    u16 data;
    struct queueNode *next;
}queueNode;

//隊列結構
typedef struct linkqueue{
    queueNode *front; //對列頭節點
    queueNode *rear;  //隊列尾節點
}linkqueue, *linkqueue_p;

linkqueue *create_linkqueue(void);
int in_linkqueue(linkqueue *qe, u16 value);//插入數據,入對列
int is_empty_linkqueue(linkqueue *qe);//判空
int out_linkqueue(linkqueue *qe, u16 *dat);//出隊列
void show_linkqueue(linkqueue *qe);//顯示隊列內容

#endif

測試文件main.c:

#include "linkqueue.h"

int main(int argc, const char *argv[])
{
    linkqueue *s = NULL;

    s=create_linkqueue();
    in_linkqueue(s,1);
    show_linkqueue(s);

    putchar(10);

    in_linkqueue(s,2);
    in_linkqueue(s,3);
    in_linkqueue(s,4);
    in_linkqueue(s,5);
    show_linkqueue(s);

    putchar(10);
    int a=0;
    out_linkqueue(s,&a);
    printf("-------test------!\n");
    out_linkqueue(s,&a);
    show_linkqueue(s);


    return 0;
}

測試結果

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM