无序双向链表的优先队列

#include <stdio.h>

#include <math.h>

#include <stdlib.h>

typedef int Item;

typedef struct node

{

    Item data;

    struct node *next,*prev;

}*PQlink;


typedef struct pq

{

    PQlink header,tail;

}*PQ;


PQ createLink()

{

    PQ pq = malloc(sizeof(*pq));

    if(!pq)

   {

      return null;

   }

    PQlink h = malloc(sizeof(*h));

   if(!h)

   {

      return null;

   }


    PQlink t = malloc(sizeof(*t));

    if(!t)

   {

      return null;

   }


    h->next = t;

    h->prev = t;

    t->prev = h;

    t->next = h;

    pq->header = h;

    pq->tail = t;

    return pq;

}


void insertPQ(PQ pq,Item aData)

{

    PQlink t = malloc(sizeof(*t));

    if(!t)

   {

      return;

   }


    t->data = aData;

    t->next = pq->header->next;

    t->next->prev = t;

    t->prev = pq->header;

    pq->header->next = t;

}


int emptyPQ(PQ pq)

{

    return pq->header->next->next == pq->header;

}


Item deleMax(PQ pq)

{//最大优先队列模拟

    Item max = 0;

    PQlink x = pq->header->next,t;

    for (t = x;t->next != pq->header;t = t->next)

    {

        if (x->data < t->data)

        {

            x = t;

        }

    }

    max = x->data;

    x->next->prev = x->prev;

    x->prev->next = x->next;

    free(x);

    return max;

}


Item deleMin(PQ pq)

{//最小优先队列模拟

    Item min = 0;

    PQlink x = pq->header->next,t;

    for (t = x;t->next != pq->header;t = t->next)

    {

        if (x->data > t->data)

        {

            x = t;

        }

    }

    min = x->data;

    x->next->prev = x->prev;

    x->prev->next = x->next;

    free(x);

    return min;

}


void changePQ(PQ pq,PQlink b,Item v)

{//改变其中的一个数据

    PQlink x = pq->header->next;

    for (; x->next != pq->header;x = x->next)

    {

        if (x->data == b->data)

        {

            break;

        }

    }

    if (x->next != pq->header)

    {

        b->data = v;

    }

    else

    {

        printf("change is failed");

    }

}


void joinPQ(PQ a,PQ b)

{//合并

    if (a == b)

    {

        printf("join failed,a and b is same");

        return;

    }

    if (b->header->next->next == b->header)

    {

        printf("b is nil");

        return;

    }

    a->tail->prev->next = b->header->next;

    b->header->next->prev = a->tail->prev;

    b->tail->next = a->header;

    a->tail->next = b->tail;

    free(a->tail);

    free(b->header);

}


void deletePQ(PQ a,PQlink x)

{//删除单个节点,注意没有写查找这个节点是否存在

    if (x == a->header||x == a->tail)

    {

        printf("node is delete failed");

        return;

    }

    

    x->next->prev = x->prev;

    x->prev->next = x->next;

    free(x);

}


void copyPQ(PQ a,PQ b)

{//链表拷贝

    

    for (PQlink x = a->header->next; x->next != a->header;x = x->next)

    {

        insertPQ(b,x->data);

    }

}


void print(PQ a)

{

    int j = 1;

    for(PQlink x = a->header->next; x->next != a->header;x = x->next,++j)

    {

        printf("%d ",x->data);

        if (j %10 == 0)

        {

            printf("\n");

        }

    }

}


void deleteAll(PQ pq)

{//释放内存

    PQlink t = pq->header->next;

    PQlink x;

    int i = 1;

    for (;t->next != pq->header;t = x,++i)

    {

        x = t->next;

        printf("%d  ",t->data);

        free(t);

        if (i%10 == 0)

        {

             printf("\n");

        }

    }

    free(pq->header);

    free(pq->tail);

    free(pq);

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值