lab4

来源:互联网 发布:storm1.0java源码 编辑:程序博客网 时间:2024/05/21 11:15
#include <malloc.h>
#include <iostream>
#define max 1000
using namespace std;


typedef struct  celltype
{
    int  data;
    celltype  *next;
} cell;
struct  QUEUE
{
    celltype  *f;
    celltype  *rear;
};


void  MakeNull(QUEUE *Q)
{
    cell *q = (cell*)malloc(sizeof(cell));
    Q->f = q;
    Q->f->next = NULL;
    Q->rear = Q->f;
}


bool Empty(QUEUE *Q)
{
    if  (Q->f == Q->rear)
        return  true ;
    else
        return  false;
}


void  EnQueue(int x,QUEUE *Q)
{
    cell *q = (cell*)malloc(sizeof(cell));
    q->next=NULL;
    q->data=x ;
    q->next=Q->rear->next;
    Q->rear->next=q;
    Q->rear=q;
}


void DeQueue(QUEUE *Q)
{
    celltype *p;
    if (Q->rear==Q->f)
        cout<<"队空";
    else
    {
        cout<<Q->f->next->data<<endl;
        p=Q->f->next;
        Q->f->next=p->next;
    }
    if (p->next==NULL)
        Q->rear=Q->f;
    delete p;
}


int Front(QUEUE Q)
{
    if(Q.f->next)
        return Q.f->next->data;
}


int main()
{
    struct QUEUE *Q = (QUEUE *)malloc(sizeof(QUEUE));
    MakeNull(Q);
    EnQueue(4,Q);
    EnQueue(5,Q);
    EnQueue(8,Q);
    DeQueue(Q);
    EnQueue(11,Q);
    EnQueue(15,Q);
    EnQueue(3,Q);
    DeQueue(Q);
    EnQueue(2,Q);
    EnQueue(7,Q);
    DeQueue(Q);
    DeQueue(Q);
    DeQueue(Q);
    DeQueue(Q);
    EnQueue(23,Q);
    while(Empty(Q)!=true)
        DeQueue(Q);
}
原创粉丝点击