zoukankan      html  css  js  c++  java
  • 队列

    // 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;
    }
    

      

  • 相关阅读:
    电路的耦合方式
    PCBA与PCB的区别
    vue记住密码功能
    数组变异
    element时间选择器插件转化为YYYY-MM-DD的形式
    box-shadow
    从后台传select的值
    jQuery事件(持续更新中)
    JavaScript对象(持续更新中)
    15分XX秒后订单自动关闭(倒计时)
  • 原文地址:https://www.cnblogs.com/linear/p/6597561.html
Copyright © 2011-2022 走看看