日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C語言數(shù)據(jù)結(jié)構(gòu)之鏈隊(duì)列的基本操作_C 語言

作者:開始King ? 更新時(shí)間: 2022-03-20 編程語言

1.隊(duì)列的定義

隊(duì)列 (Queue)是另一種限定性的線性表,它只允許在表的一端插入元素,而在另一端刪除元素,所以隊(duì)列具有先進(jìn)先出(Fist In Fist Out,縮寫為FIFO)的特性。在隊(duì)列中,允許插入的一端叫做隊(duì)尾(rear),允許刪除的一端則稱為隊(duì)頭(front)。 假設(shè)隊(duì)列為q=(a1,a2,…,an),那么a1就是隊(duì)頭元素,an則是隊(duì)尾元素。隊(duì)列中的元素是按照a1、a2、…、an的順序進(jìn)入的, 退出隊(duì)列也必須按照同樣的次序依次出隊(duì),也就是說,只有在a1、a2、…、an-1都離開隊(duì)列之后,an才能退出隊(duì)列。

2.隊(duì)列的表示和實(shí)現(xiàn)

在這里插入圖片描述

鏈隊(duì)列可以定義如下:

#define  TRUE    1
#define  FALSE  0
typedef struct QNode{
        QElemType  data;
        struct QNode *next;
}QNode, *QueuePtr;
typedef struct{
        QueuePtr  front;
        QueuePtr  rear;
}LinkQueue;

(1) 初始化操作

Status InitQueue(LinkQueue &Q)
{ 
       Q.front = Q.rear = (Queueptr) malloc(sizeof(QNode));
       if(!Q.front) exit ( OVERFLOW);
       Q.front ->next = NULL;
       return OK;
} 

(2)銷毀隊(duì)列

Status DestroyQueue(LinkQueue &Q)
{ 
        while(Q.front) {
	Q.rear = Q.front->next;
	free (Q.front);
	Q.front = Q.rear;
        }
        return OK;
}

(3) 入隊(duì)操作

Status EnQueue (LinkQueue &Q, QelemType e)
{ 
        p= (QueuePtr) malloc(sizeof(QNode));
        if (!p) exit ( OVERFLOW);
        p->data = e;  p->next = NULL;
        Q.rear -> next =p;
        Q.rear = p;
        return OK;
}

(4) 出隊(duì)操作

Status DeQueue (LinkQueue &Q, QelemType &e)
{
        if ( Q.front == Q.rear) return ERROR;
        p=Q.front->next;
        e=p->data;
        Q.front->next =p->next;
        if (Q.rear == p) Q.rear = Q.front;
        free(p);
        return OK;
}

附錄完整代碼:

#include<iostream>
using namespace std;
#define OK 1
#define FALSE 0

typedef int QElemType;
typedef int Status;

typedef struct QNode{
    QElemType data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct{
    QueuePtr font;
    QueuePtr near;
}LinkQueue;

Status InitQueue(LinkQueue &Q)
{
    Q.font=(QueuePtr)malloc(sizeof(QNode));
    if(!Q.font) exit(FALSE);
    Q.font->next=NULL;
    Q.near=Q.font;
    return OK;
}
Status QueueEmpty(LinkQueue Q)
{
    if(Q.font->next) return OK;
    return FALSE;
}
Status EnQueue(LinkQueue &Q,QElemType e)
{
    QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
    p->data=e;
    Q.near->next = p;
    Q.near = Q.near->next;
    p->next = NULL;
    return OK;
}
Status DeQueue(LinkQueue &Q,QElemType &e)
{
    if(!Q.font->next) return FALSE;
    QueuePtr p;
    p=Q.font->next;
    e=p->data;
    Q.font->next=p->next;
    if(Q.near==p) Q.near=Q.font;   //當(dāng)是最后一個(gè)元素時(shí),Q.font=NULL,Q.near=Q.font
    free(p);
    return OK;
}
Status ClearQueue(LinkQueue &Q)
{
    QueuePtr p;
    p=Q.font->next;
    QueuePtr q;
    while(p)
    {
        q=p;
        p=p->next;
        Q.font->next=p;
        free(q);
    }
    Q.near=Q.font;
    return OK;
}
void menu()
{
    cout<<"初始化隊(duì)列:1"<<endl;
    cout<<"入隊(duì):2"<<endl;
    cout<<"出隊(duì):3"<<endl;
    cout<<"清空隊(duì)列:4"<<endl;
    cout<<"退出:5"<<endl;
}
int main()
{
    LinkQueue Q;
    while(true)
    {
        int n;
        menu();
        scanf("%d",&n);
        int e=-1;
        switch(n)
        {
            case 1:
                InitQueue(Q);
                continue;
            case 2:
                printf("請輸入一個(gè)元素");
                scanf("%d",&e);
                EnQueue(Q,e);
                continue;
            case 3:
                DeQueue(Q,e);
                printf("\n出隊(duì)元素%d\n",e);
                continue;
            case 4:
                ClearQueue(Q);
                printf("清空成功\n");
                continue;
            default:
                break;
        }
        if(n==5)break;
    }

}

總結(jié)

原文鏈接:https://blog.csdn.net/m0_52118763/article/details/122203877

欄目分類
最近更新