由上节定义的链表的基础上
//////////////////////////////////////////////////
//Queue
//////////////////////////////////////////////////
typedef LinkedList Queue;
void initializeQueue(Queue *queue){
initializeLinkList(queue);
}
void enqueue(Queue *queue,void *data){
addHead(queue, data);
}
/*
之前的链表没有删除尾节点的函数,deueue函数删除最后一个节点
需要处理以下三种情况:
1.空队列:返回空
2.单节点队列:由else if语句处理
3.多节点队列:由else if分支外理
最后一种情况,用tmp指针来一个节点一个节点前进,直到到指向尾节点的前一个节点,
然后按顺序执行下面三种操作:
1>将尾节点赋值为tmp
2>将tmp指针前进一个节点
3>将尾节点的next字段置为NULL,表示后面没有节点了
*/
void *dequeue(Queue *queue){
Node *tmp = queue->head;
void *data;
if (queue->head ==NULL) {
data = NULL;
}else if(queue->head == queue->tail){
queue->head = queue->tail =NULL;
data = tmp->data;
free(tmp);
}else{
while (tmp->next != queue->tail) {
tmp =tmp->next;
}
queue->tail = tmp;
tmp = tmp->next;
queue->tail->next =NULL;
data=tmp->data;
free(tmp);
}
return data;
}
//1.4.1
int main(int argc,const char * argv[]) {
Employee *samuel = (Employee *)malloc(sizeof(Employee));
strcpy(samuel->name,"samuel");
samuel->age = 32;
Employee *sally = (Employee *)malloc(sizeof(Employee));
strcpy(sally->name,"sally");
sally->age = 28;
Employee *susan = (Employee *)malloc(sizeof(Employee));
strcpy(susan->name,"susan");
susan->age = 56;
//Queue
Queue queue;
initializeLinkList(&queue);
enqueue(&queue, samuel);
enqueue(&queue, sally);
enqueue(&queue, susan);
void *data = dequeue(&queue);
printf("Dequeued %s\n",((Employee *)data)->name);
data = dequeue(&queue);
printf("Dequeued %s\n",((Employee *)data)->name);
data = dequeue(&queue);
printf("Dequeued %s\n",((Employee *)data)->name);
return 0;
}
输出:
Dequeued samuel
Dequeued sally
Dequeued susan
Program ended with exit code: 0