main.c
//初始化队列
void InitQueue(LiQueue *q)
{
q=(LiQueue*)malloc(sizeof(LiQueue));
q->front=q->rear=NULL;
}
//判断是否为空
int QueueEmpty(LiQueue *q)
{
if(q->rear==NULL)
{
return 1;
}
else
{
return 0;
}
}
//释放
void ClearQueue(LiQueue *q)
{
QNode *p=q->front,*r;
if(p!=NULL)
{
r=p->next;
while(r!=NULL)
{
free(p);
p=r;
r=p->next;
}
}
free(q);
}
//实现队列的入队
void enQueue(LiQueue *q,struct TCPMASSAGE stTcpSendBuff)
{
//封装结点
QNode *s;
s=(QNode*)malloc(sizeof(QNode));
memcpy(&s->data , &stTcpSendBuff , sizeof(stTcpSendBuff));
//s->data =e;
s->next=NULL;
if(q->rear==NULL)
{
q->front =s;
q->rear =s;
}
else
{
q->rear->next =s;
q->rear =s;
}
}
//出队函数
int deQueue(LiQueue *q,struct TCPMASSAGE stTcpSendBuff)
{
QNode *t;
if(q->rear ==NULL)
{
return 0;
}
if(q->front ==q->rear )//只有一个结点
{
t=q->front;
q->front =NULL;
q->rear =NULL;
}
else
{
t=q->front;
q->front=q->front->next;
}
memcpy(stTcpSendBuff.cTcpBuff,t->data.cTcpBuff,t->data.len);
stTcpSendBuff.len = t->data.len;
free(t);
return 1;
}
/*
//出队函数
int deQueue(LiQueue *q,char *pstbuff,int *lenth)
{
QNode *t;
if(q->rear ==NULL)
{
return 0;
}
if(q->front ==q->rear )//只有一个结点
{
t=q->front;
q->front =NULL;
q->rear =NULL;
}
else
{
t=q->front;
q->front=q->front->next;
}
memcpy(pstbuff,t->data.cTcpBuff,t->data.len);
*lenth = t->data.len;
free(t);
return 1;
}*/
//实现队列的测长
int length(LiQueue *q)
{
int n=0;
QNode *p;
p=q->front ;
while(p!=NULL)
{
printf("-第-%d-元素--%s----%d--/n",n,p->data.cTcpBuff,p->data.len);
p=p->next;
n++;
}
return n;
}
main()
{
LiQueue *q;
InitQueue(q);
printf("初始化队列q/n");
struct TCPMASSAGE stTcpBuff;
struct TCPMASSAGE stTcpquBuff;
int iTimes = 0;
for(iTimes = 0 ; iTimes <20; iTimes++)
{
strcat(stTcpBuff.cTcpBuff,"11");
stTcpBuff.len = strlen(stTcpBuff.cTcpBuff);
enQueue(q,stTcpBuff);
length(q);
sleep(1);
}
printf("队列为%s/n",(QueueEmpty(q)?"空":"非空"));
char cqbuff[300];
int len = 0;
while(1)
{
memset(&stTcpquBuff,0,sizeof(struct TCPMASSAGE));
//if(deQueue(q,cqbuff,&len)==0)
if(deQueue(q,stTcpquBuff)==0)
{
printf("队空不能出对/n");
break;
}
else
{
printf("出对一个元素%s-----%d/n",stTcpquBuff.cTcpBuff,stTcpquBuff.len);
//printf("出对一个元素%s-----%d/n",cqbuff,len);
//printf("队列的元素个数:%d/n",length(q));
length(q);
sleep(1);
}
}
printf("释放队列/n");
ClearQueue(q);
}
-----------------------------------------------------------
.h 文件
struct TCPMASSAGE
{
char cTcpBuff[256];
short int len;
};
typedef struct qnode
{
struct TCPMASSAGE data;
struct qnode *next;
}QNode;
typedef struct LiQueue
{
QNode *front;
QNode *rear;
}LiQueue;
//初始化队列
void InitQueue(LiQueue *q);
//判断是否为空
int QueueEmpty(LiQueue *q);
//释放
void ClearQueue(LiQueue *q);
//实现队列的入队
void enQueue(LiQueue *q,struct TCPMASSAGE stTcpSendBuff);
//出队函数
//int deQueue(LiQueue *q,char *pstbuff,int *lenth);
int deQueue(LiQueue *q,struct TCPMASSAGE stTcpSendBuff);
//实现队列的测长
int length(LiQueue *q);