創建隊列 出隊 入隊 顯示隊列(鏈式)


#include <stdlib.h>
 
typedef  int datatype;
 
/* 定義節點存儲入隊元素和指向下一節點的指針  */
typedef struct node{
    datatype data;
    struct node *next;
} NODE;
 
/* 定義節點存放隊尾和對首指針  */
typedef struct poiner{
    NODE *front;
    NODE *rear;
} Linknode;
 
/* 創建隊列以及初始化函數  */
void created(Linknode *H);
 
/* 入隊函數  */
void enqueue(Linknode *H, int data);
-----------------------------------------------------------------------
 
 
#include <stdio.h>
#include "queue.h"
 
/***********************************************************************
 * 隊列:只允許在一端進行插入,而在另一端進行刪除操作的線性表
 * FIFO(First In First Out)允許插入的一端為隊尾 允許刪除的一端為隊頭
 * 鏈式隊列其實就是單鏈表只能尾進頭出
 ***********************************************************************/ 
 
/* 創建隊列以及初始化函數  */
void created(Linknode *H)
{    
    /* 定義節點並初始化 */
    NODE *M;
     M = (NODE *)malloc(sizeof(NODE));
    if (M == NULL)
    {
        printf("創建隊列中malloc失敗\n");
        return;
    }
     M->next = NULL;
 
    /* 讓隊尾隊首指向頭節點 */
     H->front = M;
    H->rear = M;
 
    return;
}
 
/* 入隊函數  */
void enqueue(Linknode *H, int data)
{
    NODE *P;
     P = (NODE *)malloc(sizeof(NODE));
    if (P == NULL)
    {
        printf("入隊函數申請內存失敗\n");
        return;
    }
 
     P->data = data;
    P->next = NULL;
 
     H->rear->next = P;/* 讓新節點掛在隊尾  */
    H->rear = P;/* 隊尾指向P節點  */
    printf("%d入隊成功\n",data);
    return;
}
 
/* 出隊函數  */
datatype dequeue(Linknode *H)
{    
    NODE *M;
    int x;
    M = (NODE *)malloc(sizeof(NODE));
    if (M == NULL)
    {
        printf("出隊函數malloc失敗\n");
        return -1;
    }
 
     if (H->front == H->rear)
    {
        printf("隊列為空\n");
        x = 0;
    }
    else
    {
         M = H->front->next;/* 保存第一個節點  */
        H->front->next = M->next;/* 指向出隊元素后面的節點 */
        if (M->next == NULL)
        {
             H->rear = H->front;
        }
         x = M->data;
        free(M);
        M = NULL;
    }
    return x;
}
 
/* 顯示隊列中元素  */
void display (Linknode H)
{
    NODE *M;
    M = (NODE *)malloc(sizeof(NODE));
     M = H.front->next; /* 指向第一個數據元素節點 */
 
    while(M != NULL)
    {
         printf("data = %d\n",M->data);
        M = M->next;/* 指向下一個節點 */
    }
    printf("顯示結束\n");
    return;
}
----------------------------------------------------------------------------------
 
 
#include <stdio.h>
#include "queue.h"
int main(int argc, char *argv[])
{
    int comand;
    Linknode *H;
    int num;
    int value;
    int i;
    H =(Linknode *)malloc(sizeof(Linknode));
    if (H == NULL)
    {
        printf("malloc failed\n");
        return -1;
    }
    do{
        printf("\n");
        printf("        1 創建隊列\n");
        printf("        2 向隊列插入元素\n");
        printf("        3 從隊列刪除元素\n");
        printf("        4 顯示隊列中元素\n");
        printf("        5 退出\n");
        printf("-------------------------------\n");
        printf("請輸入對應操作的數字(1 2 3 4 5)\n");
        scanf("%d", &comand);
        switch (comand)
        {
        case 1:
                    created(H);
            printf("請輸入你要創建元素的個數:\n");
            scanf("%d", &num);
            for (i = 1; i <= num; i++ )
            { 
                scanf("%d",&value);
                enqueue(H, value);
            }
 
            break;
        case 2:
            puts("輸入你想要插入的元素:\n");
            scanf("%d", &value);
            enqueue(H, value);
            break;
        case 3:
            printf("outqueue-data:%d\n",dequeue(H));
            break;
        case 4:
            displa y(*H);
            break;
        case 5:
            printf("正在退出....\n");
            return 0;
        default:
            printf("請檢查你的輸入是否有誤\n");
            return -1;
        }
 
    }while(1);
    return 0;
}
--------------------------------------------------------------------------------------
linux@ubuntu:~/haitao/squeue_linklist_隊列$ ./test 
 
        1 創建隊列
        2 向隊列插入元素
        3 從隊列刪除元素
        4 顯示隊列中元素
        5 退出
-------------------------------
請輸入對應操作的數字(1 2 3 4 5)
1
請輸入你要創建元素的個數:
3
1
1入隊成功
2
2入隊成功
3
3入隊成功
 
        1 創建隊列
        2 向隊列插入元素
        3 從隊列刪除元素
        4 顯示隊列中元素
        5 退出
-------------------------------
請輸入對應操作的數字(1 2 3 4 5)
3
outqueue-data:1
 
        1 創建隊列
        2 向隊列插入元素
        3 從隊列刪除元素
        4 顯示隊列中元素
        5 退出
-------------------------------
請輸入對應操作的數字(1 2 3 4 5)
4
data = 2
data = 3
顯示結束
 
        1 創建隊列
        2 向隊列插入元素
        3 從隊列刪除元素
        4 顯示隊列中元素
        5 退出
-------------------------------
請輸入對應操作的數字(1 2 3 4 5)
5
正在退出....

 


免責聲明!

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



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