- typedef int ElemType;
- typedef struct QNode{
- ElemType data;
- Struct QNode *next;
- }QNode,*pQNode;
- typedef struct LinkQueue{
- pQNode front,rear;
- }LinkQueue
- void initQueue(LinkQueue *pQ){
- pQNode head = (pQNode)malloc(sizeof(QNode));
- head->next = NULL;
- pQ->front = head;
- pQ->rear = head;
- }
- bool EnQueue(LinkQueue *pQ,ElemType e){ //尾插法类似于
- pQNode s = (pQNode)malloc(sizeof(QNode));
- if(!s){
- return false;
- }
- s->data = e;
- s->next = pQ->rear->next;//等价于s->next = NULL;
- pQ->rear->next = s;
- pQ->rear = s;
- return true;
- }
- bool DeQueue(LinkQueue *pQ,ElemType *e){
- pQNode p;
- if(pQ->front == pQ->rear){
- return false;
- }
- p = pQ->front->next;
- pQ->front->next = p->next;
- *e = p->data;
- if(pQ->rear == p){ //如果只删除的是最后一个元素!!!!则要重新给尾指针赋值
- pQ->rear == pQ->front;
- }
- free(p);
- return true;
- }