无序双向链表的优先队列

来源:互联网 发布:云南广电网络营业厅 编辑:程序博客网 时间:2024/04/26 05:13

#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
原创粉丝点击