队列的基本操作 C语言描述

作者在 2009-11-09 18:18:51 发布以下内容

1、填写第四部分的基本程序,使程序能正常运行;

2编制主函数,使函数具有如下功能:

a 初始化队列

b 依次元素abcde进队

c  输出出队序列。

 

 

#include<malloc.h> /* malloc()等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<process.h> /* exit() */
struct QueueRecord;
typedef struct QueueRecord *Queue;
#define MinQueueSize 5
typedef  char ElementType ;
struct QueueRecord
{
     int Capacity;
     int Front;
     int Rear;
     int Size;
     ElementType *Array;
};
void MakeEmpty(Queue Q)
{
Q->Size=0;
Q->Front=1;
Q->Rear=0;
}

int IsEmpty(Queue Q )
{ return Q->Size==0;
}

int IsFull(Queue Q)
{ return Q->Size==Q->Capacity;
}


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

void Enqueue(ElementType X, Queue Q)
{
       if(IsFull (Q ) )
         printf("Full queue");
       else
{
       Q->Size++                 ;//队列大小加1
  Q->Rear=Succ(Q->Rear,Q);
     Q->Array[Q->Rear]=X                   ;//将元素X插入队列中
}
}

 

ElementType Dequeue(Queue Q)
{ ElementType c2;
       if(IsEmpty (Q) )
         {printf("Empty");
         exit(0);}
    Q->Size--;//队列大小减一
  if(Q->Front<=Q->Capacity-1)
   {
   c2=Q->Array[Q->Front];
   Q->Array[Q->Front]=Q->Array[Q->Front++];
   }
   else c2=Q->Array[Q->Rear];
return c2;
}

Queue   CreateQueue(int MaxElements )
         {
             Queue Q;

/* 1 */        if(MaxElements<MinQueueSize)
/* 2 */          { printf("Queue size is too small");
                   exit(0);
                 }
/* 3 */           Q=(Queue)malloc(sizeof(struct QueueRecord ) );
/* 4 */           if(Q==NULL)
/* 5 */             { printf("Out of space!!l");
                      exit(0);
                 }
/* 6 */           Q->Array=(ElementType *)malloc(sizeof(ElementType)*MaxElements);
/* 7 */           if(Q->Array==NULL)
/* 8 */                { printf("Out of space!!");
                            exit(0);
                 }
/* 9 */           Q->Capacity=MaxElements;
/*10*/           MakeEmpty(Q);
/*11*/           return Q;
}
void main(void)
{
 printf("输入a,b,c,d,e\n");
 char c1,c2;
Queue Q;
int i,MAX;
scanf("%d",&MAX);
getchar();
Q=CreateQueue(MAX);//建立一个大小MAX的队列
for(i=1;i<=5;i++)
{scanf("%c",&c1);getchar();
Enqueue(c1,Q);//将元素c1插入队列中
}
printf("出队序列:");
for(i=0;i<5;i++)
{c2=Dequeue(Q);
printf("%c ",c2);}
/*以下程序语句(可以有一条或多条)的功能输出出队序列*/
}

默认分类 | 阅读 890 次
文章评论,共0条
游客请输入验证码