• 队列


    // duilie.cpp : 定义控制台应用程序的入口点。
    //
    
    #include "stdafx.h"
    #include<iostream>
    #include <stdlib.h>
    using namespace std;
    
    struct QueueRecord;
    typedef struct QueueRecord *Queue;
    
    
    int IsEmpty(Queue Q);
    int IsFull(Queue Q);
    Queue CreateQueue(int MaxElement);
    void DisposeQueue(Queue Q);
    void MakeEmpty(Queue Q);
    void Enqueue(ElementType x, Queue Q);
    ElementType Dequeue(Queue Q);
    
    
    
    #define MinQueueSize (5)
    typedef int ElementType;
    
    struct QueueRecord
    {
    	int Capacity;
    	int Front;
    	int Rear;
    	int Size;
    	ElementType *Array;
    };
    
    Queue CreateQueue(int MaxElement)
    {
    	Queue Q;
    	if (MaxElement < MinQueueSize)
    		cout << "too small";
    	Q = (Queue)malloc(sizeof(QueueRecord));
    	if (Q == NULL)
    		cout << "out of space";
    	Q->Array= (ElementType *)malloc(sizeof(ElementType)*MaxElement);
    	if (Q->Array == NULL)
    		cout << "out of space";
    	Q->Capacity = MaxElement;
    	MakeEmpty(Q);
    	return Q;
    }
    
    void DisposeQueue(Queue Q)
    {
    	if (Q != NULL)
    	{
    		free(Q->Array);
    		free(Q);
    	}
    }
    
    int IsEmpty(Queue Q)
    {
    	return Q->Size == 0;
    }
    
    void MakeEmpty(Queue Q)
    {
    	Q->Size = 0;
    	Q->Front = 1;
    	Q->Rear = 0;
    }
    
    static int Succ(int Value, Queue Q)
    {
    	if (++Value == Q->Capacity)
    		Value = 0;
    	return Value;
    }
    void Enqueue(ElementType x, Queue Q)
    {
    	if (IsFull(Q))
    		cout << "full";
    	else
    	{
    		Q->Size ++;
    		Q->Rear = Succ(Q->Rear,Q);
    		Q->Array[Q->Rear] = x;
    	}
    }
    
    int IsFull(Queue Q)
    {
    	return (Q->Rear) - (Q->Front) == -1;
    }
    
    ElementType Dequeue(Queue Q)
    {
    	ElementType x;
    	if (IsEmpty(Q))
    	{
    		cout << "Empty";
    		return 0;
    	}
    	else
    	{
    		x = Q->Array[Q->Front];
    		Q->Size--;
    		Q->Front = Succ(Q->Front, Q);
    		return x;
    	}
    }
    
    void print(Queue Q)
    {
    	for (int i = 0; i <Q->Size; i++)
    		cout << Q->Array[i] << "  ";
    	cout << endl;
    }
    
    int main()
    {
    	Queue Q = CreateQueue(10);
    	Enqueue(10, Q);
    	Enqueue(5, Q);
    	print(Q);
    	ElementType e = Dequeue(Q);
    	print(Q);
    	while (1);
        return 0;
    }
    

      

  • 相关阅读:
    Java高并发秒时啊API之Service层
    Junit测试类生成
    Java高并发秒时啊API之业务分析与Dao层2
    Java高并发秒时啊API之业务分析与Dao层1
    JSP知识_EL表达式
    Maven_3_webAPP
    Maven_2_核心知识
    添加bean的监听
    异步上传文件
    ACTIVITI工作流的集成记录
  • 原文地址:https://www.cnblogs.com/linear/p/6597561.html
Copyright © 2020-2023  润新知