数据类型如下:
typedef struct LNode{
ElemType data;
struct LNode *next;
}*LinkList;
//入队操作 //出队操作
Status enQueue(LinkList & L,ElemType e) Status DeQueue(LinkList &L,ElemType &e)
{ {
s = (LinkList)malloc(sizeof(LNode)); if(!L) return ERROR; //如果链表空,返回
if(!s) return ERROR; front = L->next; //其中L设为尾指针
s->data = e; e = front->data; //front为头指针
if(!L) //L为尾指针 if(front = L) //只有一个结点
{ {
L = s; L = NULL; //链表致为空
L->next = L; }else{
}else L->next = front->next;
{ }
s->next = L->next; free(front);
L->next = s; return OK;
} }
return OK;
}
为了方便看入队和出对的对比,把两个操作写在了并列的位置。L为链表的指针同时也是队列的尾指针,可以方便地进行入队和出对的操作。