码迷,mamicode.com
首页 > 编程语言 > 详细

算法:队列的实现

时间:2017-03-07 21:36:38      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:create   return   exit   malloc   type   make   pre   empty   ast   

queue.h

#ifndef _QUEUE_H
#define _QUEUE_H

struct Node;
typedef int ElementType;
typedef struct Node *PtrToNode;
typedef PtrToNode Queue;

int IsEmpty(Queue Q);
Queue CreateQueue(void);
void MakeEmpty(Queue Q);
void EnQueue(ElementType X,Queue Q);
void DeQueue(Queue Q);
PtrToNode Last(Queue Q);

#endif

struct Node
{
    ElementType element;
    PtrToNode Next;
};

queue.c

#include <stdlib.h>
#include <stdio.h>
#include "queue.h"

int IsEmpty(Queue Q)
{
    return Q->Next==NULL;
}
Queue CreateQueue(void)
{
    PtrToNode Q;
    Q=malloc(sizeof(struct Node));
    if(Q==NULL){
        printf("out of space");
        exit(1);
    }
    if(Q->Next!=NULL){
        MakeEmpty(Q);
    }
    return Q;
}
void MakeEmpty(Queue Q)
{
    PtrToNode P,temp;
    P=Q->Next;
    Q->Next=NULL;
    while(P!=NULL){
        temp=P;
        P=P->Next;
        free(P);
    }
}
void EnQueue(ElementType X,Queue Q)
{
    PtrToNode P,L;
    P=malloc(sizeof(struct Node));
    if(P==NULL){
        printf("out of space");
    }
    P->element=X;
    L=Last(Q);
    L->Next=P;
}
PtrToNode Last(Queue Q)
{
    PtrToNode P;
    P=Q;
    while(P->Next!=NULL){
        P=P->Next;
    }
    return P;
}
void DeQueue(Queue Q)
{
    PtrToNode P;
    if(Q->Next!=NULL){
        P=Q->Next;
        Q->Next=P->Next;
        free(P);
    }
}

 

算法:队列的实现

标签:create   return   exit   malloc   type   make   pre   empty   ast   

原文地址:http://www.cnblogs.com/peixiguang/p/6516942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!