双链表优先队列ADT的实现与测试

源代码如下:


#include <stdlib.h>
#include <stdio.h>
typedef struct pq* PQ;
typedef struct PQnode* PQlink;
struct Item{int data;char c;};
struct PQnode{Item key;PQlink prev,next;};
struct pq{PQlink head,tail;};

PQ PQinit(){
	PQ pq = (PQ)malloc(sizeof*pq);
	PQlink h = (PQlink)malloc(sizeof*h),
			t = (PQlink)malloc(sizeof*t);
	h->prev = t; h->next = t;
	t->next = h; t->prev = h;   //头尾指针不存放数据 
	pq->head = h; pq->tail = t;
	return pq;
}

int PQempty(PQ pq){
	return pq->head->next->next == pq->head;
}

PQlink PQinsert(PQ pq, Item v){
	PQlink t = (PQlink)malloc(sizeof *t);
	//设置节点t的相关信息 
	t->key = v;t->next=pq->head->next;t->prev=pq->head; 
	//维护双链表
	t->next->prev=t;pq->head->next = t; 
	return t;
}

Item PQdelmax(PQ pq){
	Item max;struct PQnode *t,*x=pq->head->next;
	for(t=x; t->next!=pq->head;t=t->next)
		if(t->key.data>x->key.data) x = t;
	max =x->key;
	//维护双链表
	x->next->prev=x->prev;x->prev->next=x->next;
	free(x);return max;
}

void PQchange(PQ pq, PQlink x, Item v){
	x->key=v;
}

void PQdelete(PQ pq, PQlink x){
	x->next->prev=x->prev;x->prev->next=x->next;
	free(x);
}

//并不破坏b的结构??!!(<span style="color:#ff0000;">求答疑</span>) 将b队列追加在a尾处 
void PQjoin(PQ a, PQ b){ 
	a->tail->prev->next = b->head->next;
	b->head->next->prev = a->tail->prev;
	a->head->prev = b->tail;
	b->tail->next = a->head;
	free(a->tail);free(b->head); 
}

void test(){
	PQ pq = PQinit();
	PQ pq2 = PQinit();
	Item a = {2,'a'};
	Item b = {3,'b'};
	Item c = {1,'c'};
	PQinsert(pq,a);PQinsert(pq2,b);PQinsert(pq2,c);
	printf("%d %d \n",pq2->head->next->key.data,pq2->head->next->next->key.data);
	PQjoin(pq,pq2); //
	printf("%d %d \n",pq2->head->next->key.data,pq2->head->next->next->key.data);
	Item d = {21,'d'};
	PQchange(pq,pq->head->next,d); 
	PQdelete(pq,pq->head->next->next);
	while(!PQempty(pq)){
		printf("%d \n",PQdelmax(pq).data);
	}
}

main(){
	test();
}


运行结果




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环队列是一种特殊的队列,它的队尾指针指向最后一个元素的下一个位置,队头指针指向队列中第一个元素。当队列满时,队尾指针和队头指针相等,但此时队列不是空的。 循环队列的实现可以使用数组来存储元素,定义一个结构体来表示队列,其中包含队列的头指针、尾指针和数组元素。下面是循环队列ADT实现: ``` typedef struct { int *base; // 队列的存储空间 int front; // 队头指针 int rear; // 队尾指针 int size; // 队列的最大长度 } Queue; void InitQueue(Queue *q, int size) { q->base = (int *)malloc(size * sizeof(int)); q->front = q->rear = 0; q->size = size; } void EnQueue(Queue *q, int x) { if ((q->rear + 1) % q->size == q->front) { printf("Queue is full.\n"); return; } q->base[q->rear] = x; q->rear = (q->rear + 1) % q->size; } int DeQueue(Queue *q) { if (q->front == q->rear) { printf("Queue is empty.\n"); return -1; } int x = q->base[q->front]; q->front = (q->front + 1) % q->size; return x; } int GetQueueLength(Queue *q) { return (q->rear - q->front + q->size) % q->size; } ``` 使用循环队列可以方便地实现一些算法,如广度优先搜索(BFS)和滑动窗口等。下面是一个使用循环队列实现滑动窗口的例子: ``` void SlidingWindow(int *a, int n, int k) { Queue q; InitQueue(&q, k); int i; for (i = 0; i < k; i++) { while (GetQueueLength(&q) > 0 && a[i] >= a[q.base[q.rear-1]]) { DeQueue(&q); } EnQueue(&q, i); } for (; i < n; i++) { printf("%d ", a[q.base[q.front]]); while (GetQueueLength(&q) > 0 && a[i] >= a[q.base[q.rear-1]]) { DeQueue(&q); } while (GetQueueLength(&q) > 0 && q.base[q.front] <= i - k) { DeQueue(&q); } EnQueue(&q, i); } printf("%d\n", a[q.base[q.front]]); } ``` 该函数可以实现在一个长度为n的数组a中,找出每个长度为k的子数组中的最大值。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值