队列的链式存储结构的实现2 —— 出队查看队首尾元素计算元素个数

来源:互联网 发布:搜索qq软件 编辑:程序博客网 时间:2024/04/29 13:06
//  Filename    :  list_queue.c//  Author      :   LupingChen//  Data        :   2015.05.30//  Content     :   create\destory\full\empty\push#include <stadio.h>#include <stdlib.h>//定义节点数据类型typedef struct Node {    int data;//节点数据    struct Node* next;//记录下一节点地址} Node;//定义队列数据类型typedef struct {    Node* head;//头指针} Queue;//出队int pop(Queue* pq);//查看队首元素int get_head(Queue* pq);//查看队尾元素int get_tail(Queue* pq);//计算队列中所有的元素int size(Queue* pq);int main(void){    return 0;}//出队int pop(Queue* pq){    if (empty(pq))    {        return -1;    }    //给头指针找个替身    Node* p = pq->head;    p->head = p->next;    //存储要释放的节点数据    int temp = p->data;    free(p);    p = NULL;    //返回释放节点数据    return temp;}//查看队首元素int get_head(Queue* pq){    if (empty(pq))    {        return -1;//用-1代表错误    }    return pq->head->data;}//查看队尾元素int get_tail(Queue* pq){    //队列为空    if (empty(pq))    {        return -1;    }    //队列不为空    //替身    Node* p = pq->head;    //循环移动指针,定位到尾节点    while (p->next != NULL)    {        p = p->next;    }    //返回尾节点数据    return p->data;    }//计算队列中所有的元素个数int size(Queue* pq){    int cnt = 0;    Node* p = pq->head;    while (p != NULL)    {        cnt++;        p = p->next;    }    return cnt;}

0 0
原创粉丝点击