2018-05-28顺序循环队列的实现

#include
#include 
#define OK 1
#define MAXSIZE 100
#define ERROR 0
typedef struct
{
    char name[20];
}student;

typedef struct
{
    student *data;
    int front;
    int rear;
}SqQueue;

int Initlist(SqQueue *L)//构建一个空的顺序表L 
{ L->data=(student *)malloc(sizeof(student)*MAXSIZE);
if(!L->data)return ERROR;
L->front=L->rear=0;
return OK; 
} 


int EnQueue(SqQueue *L)//入队 
{
    if(((L->rear)+1)%MAXSIZE==L->front)  
    {
    printf("队已满,抱歉。\n"); 
    return ERROR;
    }
    scanf("%s",L->data[L->rear].name);
    L->rear=(L->rear+1)%MAXSIZE;
    return OK;  
}

/*int DeQueue(SqQueue *L)//出栈 
{  if(L->front==L->rear)return ERROR;
   printf("%s",L->data[L->front].name);
   L->front=(L->front+1)%MAXSIZE;
   return OK;
 }*/ 
 
char* DeQueue(SqQueue *L)//出栈 
{  if(L->front==L->rear)
   printf("抱歉你的队已经满了。\n");
   return L->data[L->front].name;
   L->front=(L->front+1)%MAXSIZE;
   
 } 

int QueueLength(SqQueue *L)//求长度 
{
    return (L->rear-L->front+MAXSIZE)%MAXSIZE; 
} 


char* Gethead(SqQueue *L)//出表头 
{
    if(L->front!=L->rear)
    return L->data[L->front].name;
}


int main()
{ 
    SqQueue SQ;
    SqQueue *L;
    L=&SQ;
    student *e;
    int a;
    printf("请输入你要进行的操作\n");
    printf("1.初始化\n");
    printf("2.入队\n");
    printf("3.出队\n");
    printf("4.求长度。\n");
    printf("5.输出表头。\n");
    while(1)
    {
        scanf("%d",&a);
        switch (a)
        {
            case 1:
                Initlist(L);
                printf("初始化成功。\n");  
                break;
            case 2:
                printf("请输入你要添加的字符串.\n");
                EnQueue(L) ;
                break;
            case 3:
                {int i;
                i=QueueLength(L);
                while(i>0)
                {
                //DeQueue(L);
                printf("%s\n", DeQueue(L)); 
                i--;
                }
                    break;
                }
            case 4:
                printf("当前的长度为%d\n",QueueLength(L));
                break;
            case 5:
                printf("%s\n",Gethead(L))   ;
                break;  
        }   
     } 
}

你可能感兴趣的:(2018-05-28顺序循环队列的实现)