IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 数据结构与算法分析——C语言描述(第3章 表、栈和队列③) -> 正文阅读

[数据结构与算法]数据结构与算法分析——C语言描述(第3章 表、栈和队列③)

3.4 队列(Queue)ADT

像栈一样,队列也是表。然而,使用队列时插入在一端进行而删除则在另一端进行。

3.4.1 队列模型

队列的基本操作:

  • Enqueue(入队)——在表的末端(叫作队尾(rear))插入一个元素。
  • Dequeue(出队)——删除(或返回)在表的开头(叫作队头(front))的元素.
    队列模型

3.4.2 队列的实现

像栈一样,对于队列而言,任何表的实现都是合法的,无论是链表实现还是数组实现对于每一种ADT操作都给出快速的 O ( 1 ) O(1) O(1)运行时间。

3.4.2.1 队列的链表实现

队列ADT链表实现的类型声明:

#ifndef _Queue_h

struct Node;
struct QNode;
typedef struct Node *PtrToNode;
typedef struct QNode *Queue;

int IsEmpty( Queue Q );
Queue CreateQueue( void );
void DisposeQueue( Queue Q );
void MakeEmpty( Queue Q );
void Enqueue( ElementType X, Queue Q );
ElementType Front( Queue Q );
void Dequeue( Queue Q );
ElementType FrontAndDequeue( Queue Q );

#endif /* _Queue_h */

/* Place in implementation file */
/* Stack implementation is a linked list */
struct Node
{
    ElementType Element;
    PtrToNode Next;
};
struct QNode
{
    PtrToNode rear;
    PtrToNode front;
};

具体函数实现:

/* Return true if Q is empty */
int
IsEmpty( Queue Q )
{
    return Q->front == NULL;
}

/* Create an empty queue */
Queue
CreateQueue( void )
{
    Queue Q;
    
    Q = malloc( sizeof( struct QNode ) );
    if( Q == NULL )
        FatalError( "Out of space!!!" );
    Q->front = NULL;
    Q->rear = NULL;
    MakeEmpty( Q );
    return Q;
}

/* Dispose Q */
void
DisposeQueue( Queue Q )
{
    if( Q != NULL )
    {
        MakeEmpty( Q );
        free( Q );
    }
}

/* Make Q empty*/
void
MakeEmpty( Queue Q )
{
    if( Q == NULL )
    	Error( "Must use CreateQueue first" );
    else
        while( !IsEmpty( Q ) )
            Dequeue( Q );
}

/* Enqueue */
void
Enqueue( ElementType X, Queue Q )
{
    PtrToNode TmpCell;

    TmpCell = malloc( sizeof( struct Node ) );
    if ( TmpCell == NULL )
        FatalError( "Out of space!!!" );
    TmpCell->Element = X;
    TmpCell->Next = NULL;
    if( Q->rear == NULL )
    {
        Q->rear = TmpCell;
        Q->front = TmpCell;
    } 
    else
    {
        Q->rear->Next = TmpCell;
        Q->rear = TmpCell;
    }
}

/* Return the front element */
ElementType
Front( Queue Q )
{
    if( !IsEmpty( Q ) )
        return Q->front->Element;
    Error( "Empty queue" );
    return 0;
}

/* Dequeue */
void
Dequeue( Queue Q )
{
    PtrToNode FrontCell;
    if( IsEmpty( Q ) )
    	Error( "Empty queue" );
    else
    {
    	FrontCell = Q->front;
    	if( Q->front == Q->rear )
            Q->front = Q->rear = NULL;
        else
            Q->front = Q->front -> Next;
        free( FrontCell );
    }
}

/* Return the front element and Dequeue*/
ElementType
FrontAndDequeue( Queue Q )
{
    ElementType X = 0;

    if( IsEmpty( Q ) )
        Error( "Empty queue" );
    else
    {
        X = Front( Q );
        Dequeue( Q );
    }
    return X;
}

3.4.2.2 队列的数组实现

队列ADT数组实现的类型声明:

#ifndef _Queue_h

struct QueueRecord;
typedef struct QueueRecord *Queue;

int IsEmpty( Queue Q );
int IsFull( Queue Q );
Queue CreateQueue( int MaxElements );
void DisposeQueue( Queue Q );
void MakeEmpty( Queue Q );
void Enqueue( ElementType X, Queue Q );
ElementType Front( Queue Q );
void Dequeue( Queue Q );
ElementType FrontAndDequeue( Queue Q );

#endif /* _Queue_h */

/* Place in implementation file */
/* Queue implementation is a dynamically allocated array */
#define MinQueueSize ( 5 )
struct QueueRecord
{
	int Capacity;
	int Front;
	int Rear;
	int Size;
	ElementType *Array;
};

具体函数实现:

/* Return true if Q is empty */
int
IsEmpty( Queue Q )
{
    return Q->Size == 0;
}

/* Return true if Q is full */
int
IsFull( Queue Q )
{
    return Q->Size == Q->Capacity;
}

/* Create an empty queue */
Queue
CreateQueue( int MaxElements )
{
	Queue Q;
	
	if( MaxElements < MinQueueSize )
		Error( "Queue size is too small" );

	Q = malloc( sizeof( struct QueueRecord ) );
	if( Q == NULL )
		FatalError( "Out of space!!!" );

	Q->Array = malloc( sizeof( ElementType ) * MaxElements );
	if( Q->Array == NULL )
		FatalError( "Out of space!!!" );
	Q->Capacity = MaxElements;
	MakeEmpty( Q );
	return Q;
}

/* Dispose Q */
void
DisposeQueue( Queue Q )
{
    if( Q != NULL )
    {
        free( Q->Array );
        free( Q );
    }
}

/* Make Q empty*/
void
MakeEmpty( Queue Q )
{
	Q->Size = 0;
	Q->Front = 1;
	Q->Rear = 0;
}

/* Enqueue */
static int
Succ( int Value, Queue Q )
{
    if( ++Value == Q->Capacity )
        Value = 0;
    return Value;
}

void
Enqueue( ElementType X, Queue Q )
{
    if( IsFull( Q ) )
        Error( "Full queue" );
    else
    {
        Q->Size++;
        Q->Rear = Succ( Q->Rear, Q );
        Q->Array[ Q->Rear ] = X;
    }
}

/* Return the front element */
ElementType
Front( Queue Q )
{
    if( !IsEmpty( Q ) )
        return Q->Array[ Q->Front ];
    Error( "Empty queue" );
    return 0;
}

/* Dequeue */
void
Dequeue( Queue Q )
{
    if ( IsEmpty( Q ) )
    	Error( "Empty queue" );
    else
    {
    	Q->Size--;
    	Q->Front = Succ( Q->Front, Q );
    }
}

/* Return the front element and Dequeue*/
ElementType
FrontAndDequeue( Queue Q )
{
    ElementType X = 0;

    if( IsEmpty( Q ) )
        Error( "Empty queue" );
    else
    {
    	Q->Size--;
    	X = Q->Array[ Q->Front ];
    	Q->Front = Succ( Q->Front, Q );
	}
    return X;
}

注意:为避免已经有元素出队而导致的越界,我们让Front或Rear到达数组的尾端时绕回到开头,即循环数组(circular array)实现。

3.4.3 队列的应用

实际生活例子
排队论(queueing theory)

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2022-09-15 02:14:52  更:2022-09-15 02:16:52 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 20:02:58-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码