昨天忘更了 罪恶 要presevere
#include<stdio.h>
#include<algorithm>
using namespace std;
typedef struct DataNode{
int elem;
struct DataNode *next;
}DataNode;
typedef struct{
int count;
DataNode *front;
DataNode *rear;
}HeadNode,*hnode;
int Init(hnode *Q)
{
hnode q=(HeadNode *)malloc(sizeof(HeadNode));
DataNode *p=(DataNode *)malloc(sizeof(DataNode));
if(p==NULL||q==NULL)
{
return -1;
}
p->next = NULL;
q->count=0;
q->front=p;
q->rear =p;
(*Q)=q;
return 0;
}
int EnQueue(hnode *Q,int elem)
{
DataNode *p=(DataNode *)malloc(sizeof(DataNode));
if(p==NULL)
{
return -1;
}
p->elem=elem;
p->next=NULL;
(*Q)->rear->next=p;
(*Q)->rear = p;
(*Q)->count+=1;
return 0;
}
int DeQueue(hnode *Q,int *elem)
{
DataNode *p=(*Q)->front->next;
*elem = p->elem;
(*Q)->front->next = p->next;
if(p == (*Q)->rear)
{
(*Q)->rear = (*Q)->front;
}
free(p);
(*Q)->count-=1;
return 0;
}
int GetHead(hnode Q,int *elem)
{
*elem = Q->front->next->elem;
return 0;
}
int Clear(hnode *Q)
{
while((*Q)->count!=0)
{
DataNode *p=(*Q)->front->next;
(*Q)->front->next = p->next;
if(p == (*Q)->rear)
{
(*Q)->rear = (*Q)->front;
}
free(p);
(*Q)->count-=1;
}
return 0;
}
int Destory(hnode *Q)
{
while((*Q)->front!=NULL)
{
(*Q)->rear = (*Q)->front->next;
free(*Q);
(*Q)->front = (*Q)->rear;
}
free(*Q);
(*Q)=NULL;
return 0;
}
int Traverse(hnode Q)
{
DataNode *p=Q->front->next;
for(int i=0;i<Q->count;i++)
{
printf("%d",p->elem);
p=p->next;
}
return 0;
}