// MyQueue.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define QUEUELEN 15
// data
typedef struct {
char name[15];
int age;
}DATA;
typedef struct {
DATA data[QUEUELEN];
int head;
int tail;
}SQType;
// init
SQType *SQTypeInit()
{
SQType *q;
if(q = (SQType *)malloc(sizeof(SQType)))
{
q->head = 0;
q->tail = 0;
return q;
}
cout<<"malloc error!"<<endl;
return (SQType*)NULL;
}
// EMpty
int SQTypeEmpty(SQType *q)
{
int temp;
temp = (q->tail == q->head);
return (temp);
}
// FULL
int SQTypeFull(SQType *q)
{
int temp;
temp = (q->tail == QUEUELEN);
return (temp);
}
// Clear
void SQTypeClear(SQType *q)
{
q->tail = 0;
q->head = 0;
}
// free
void SQTypeFree(SQType *q)
{
if(q != NULL)
{
free(q);
}
}
// In
int InSQType(SQType *q, DATA data)
{
if(q->tail == QUEUELEN)
{
cout<<"Queue is full"<<endl;
return (0);
}
else
{
q->data[q->tail++] = data;
return (1);
}
}
// out
DATA *QutSQType(SQType* q)
{
if(q->head == q->tail)
{
cout<<"queue is empty!"<<endl;
exit(0);
}
else
{
return &(q->data[q->head++]);
}
}
// peek
DATA *PeekSQType(SQType *q)
{
if(SQTypeEmpty(q))
{
cout<<"empty queue!"<<endl;
return NULL;
}
else
{
return &(q->data[q->head]);
}
}
// Length
int SQTypeLen(SQType *q)
{
int temp;
temp = q->tail - q->head;
return temp;
}
int main(int argc, char* argv[])
{
SQType * stack;
DATA data;
DATA *data1;
stack = SQTypeInit();
cout<<"In Queue"<<endl;
cout<<"enter name age: "<<endl;
do
{
cin>>data.name;
cin>>data.age;
if(strcmp(data.name, "0") == 0)
{
break;
}
else
{
InSQType(stack, data);
}
} while (1);
data1 = PeekSQType(stack);
cout<<data1->name<<data1->age<<endl;
do
{
cout<<"Out queue: "<<endl;
getchar();
data1 = QutSQType(stack);
cout<<data1->name<<data1->age<<endl;
} while (1);
SQTypeFree(stack);
return 0;
}