zoukankan      html  css  js  c++  java
  • 队列相关算法

    用链表存放队列,书上是用的伪指针,比较关键的一点是队列头部到达块的上限时,继续压入元素,会导致伪指针指向块的起始位置,链表无需顾忌这个问题

    #include <iostream>
    using namespace std;
     
    struct strqueue;
    typedef struct strqueue *StrQueue;
    StrQueue create_StrQueue(void);//creates a new StrQueue ,caller must call destroy_StrQueue)
    void destroy_StrQueue(StrQueue sq);//frees the memory occupied by StrQueue)
    void push_back(StrQueue sq, const char *str);//把str加在sq的最后)
    char *pop_front(StrQueue sq);//frees the node at the front of the sq and returns the string that was first in sq
    char *peek_front(StrQueue sq);//returns a the first item stored in sq)
    int  get_length(StrQueue sq);//returns the number of items in the queue)
     
     
    //StrQueue.c
    typedef struct Node
    {
    char *data;
    Node *next;
    }node,*pnode;//元素节点
    typedef struct  
    { 
    int length;
    pnode front;
    pnode rear;
    }_StrQueue,*pStrQueue;//队列
    StrQueue create_StrQueue(void)//创建一个队列
    {
     
    pStrQueue p=(pStrQueue)malloc(sizeof(_StrQueue));
    memset(p,0,sizeof(_StrQueue));
    return (StrQueue)p;
     
    }
    void destroy_StrQueue(StrQueue sq)//销毁一个队列
    {
    pStrQueue Q=(_StrQueue*)sq;
    pnode temp;
    if(Q->front==NULL)return;
    while(Q->front!=NULL)
    {
     
        temp=Q->front->next;
        free((pnode)(Q->front));
        Q->front=temp;
    }
    Q->length=0;
    }
     
    void push_back(StrQueue sq, const char *str)//从尾部压入一个元素
    {
        pStrQueue Q=(_StrQueue*)sq;
        pnode q = pnode(malloc(sizeof(node)));
        q->data=(char *)malloc(strlen(str));
        strcpy_s(q->data,sizeof(str),str);
        q->next=NULL;
        if(Q->rear==NULL){Q->rear=q;Q->front=Q->rear;return;}
        Q->rear->next=q;
        Q->rear=q;
        Q->length++;
    }
     
    char *pop_front(StrQueue sq)从头部弹出一个元素
    {
        pStrQueue Q=(_StrQueue*)sq;
        pnode temp=NULL;
        char *p;
        if(Q->front==NULL)return "Empty Queue";
        temp=Q->front->next;
        p=Q->front->data;
        free((pnode)Q->front);
        Q->front=temp;
        Q->length--;
        return p;
    }
    char *peek_front(StrQueue sq)//取头部元素
    {
        pStrQueue Q=(_StrQueue*)sq;
        if(Q->front==NULL)return "Empty Queue";
        return Q->front->data;
     
    }
    int  get_length(StrQueue sq)//获取队列的长度
    {
        return ((_StrQueue*)sq)->length;
     
    }
    int main()
    {
        StrQueue p=create_StrQueue();
        push_back(p,"abc");
        push_back(p,"a1c");
        push_back(p,"a2c");
        pop_front(p);
        cout<<peek_front(p)<<endl;
        destroy_StrQueue(p);
        system("pause");
        return 0;
    }
    相信世界是平的
    谨记四个字“修身养性”
    大江东去浪淘尽英雄,再牛B的人物最后也是一掊土
    向善不是目的,而是抚慰心灵,更多的感受幸福,感谢别人给你行善的机会
    相信老子的话:万物生于有,有生于无,一切的道理都源于一个无法证明的假设
    我是好是坏就自然而然的摆在那里,并不会因为别人的评价而改变什么,我也不需要别人用一张纸来说明我什么,世间最难得的是自由



    支持大额赞助:
  • 相关阅读:
    一个简易的MySQL性能查询脚本
    pt-osc原理、限制、及与原生online-ddl比较
    Netstat Commands for Linux Network Management
    MySQL 资源大全中文版
    自增表死锁问题分析及处理
    MySQL自带的性能压力测试工具mysqlslap
    iOS in-app purchase详解
    iOS 将Excel导入到SQLite3的过程
    iOS iTuns Connect官方配置教程
    OpenGL 知识二
  • 原文地址:https://www.cnblogs.com/sky-view/p/3246474.html
Copyright © 2011-2022 走看看