栈与堆底层实现代码

//栈
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>
typedef int DataType;
typedef struct Stack
{
	DataType *a;
	int capacity;
	int top;
}Stack;
void StackInit(Stack *s)
{
	s->a = NULL;
	s->capacity = 0;
	s->top = 0;
}
//销毁
void StackDestory(Stack*s)
{
	free(s->a);
	s->a = NULL;
	s->capacity = s->top = 0;
}
//尾插
void StackPushBack(Stack *s, DataType x)
{
	if (s->top == s->capacity)
	{
		DataType NewCapacity = s->capacity == 0 ? 10 : s->capacity * 2;
		s->a = (DataType *)realloc(s->a, sizeof(DataType)*NewCapacity);
		s->capacity = NewCapacity;
	}
	s->a[s->top] = x;
	s->top++;
}
//出栈(尾删)
void StackPopBack(Stack *s)
{
	if (s->top)
	s->top--;
}
//返回栈顶元素
DataType StackTop(Stack *s)
{
	return s->a[s->top - 1];
}
//判空
int StackEmpty(Stack *s)
{
	if (s->top == 0)
		return 1;
	else
		return 0;//1表示为空,0表示不为空
}
//打印
void StackPrint(Stack *s)
{
	while (s->top)
	{
		printf("%d->", s->a[s->top-1]);
		s->top--;
	}
}
int main()
{
	system("pause");
	return 0;
}


//堆
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
typedef int DataType;
typedef struct Heap{
	int *a;
	int size;
	int capacity;
}Heap;
//父节点:parent=(child-1)/2
//左子节点:Lchild=2*parent+1
//右子节点:Rchild=2*parent+2
//堆的初始化
void InitHeap(Heap *hp, DataType* b, int n)
{	
	assert(hp&&b);
	int i;
	hp->a = (DataType *)malloc(sizeof(DataType)*n);
	for (i = 0; i < n; ++i){
		hp->a[i] = b[i];
	}
	for (i = (n - 2) / 2; i >= 0; --i){
		DownHeap(hp->a, n, i);
	}
	hp->size = hp->capacity = n;
}

//向下调整   O(log n)
void swap(int*a, int *b)
{
	int tmp = 0;
	tmp = *a;
	*a = *b;
	*b = tmp;
}
void DownHeap(DataType *a, int n, int root)  //n为数组长度
{
	int parent = root;
	int child = 2 * parent + 1;
	while (child < n)
	{
		if (child + 1 < n&&a[child + 1] > a[child])//右孩子存在 
			child++;
		if (a[child]>a[parent])
		{
			swap(&a[child], &a[parent]);
			parent = child;
			child = 2 * parent + 1;
		}
		else
			break;
	}
}
//向上调整
void UpHeap(DataType *a, int n, int yezi)
{
	int child = yezi;
	int parent = (child - 1) / 2;
//  while(parent>=0)
	while (child>0)
	{
		if (a[child]>a[parent]){
			swap(&child, &parent);
			child = parent;
			parent = (child - 1) / 2;
		}
		else
			break;
	}
}
//插入
void HeapPush(Heap*hp, DataType x)
{	//检查容量
	if (hp->size == hp->capacity)
	{
		int newC = hp->capacity = 0 ? 10 : 2 * hp->capacity;
		hp->a = (DataType*)realloc(hp->a, newC*sizeof(DataType));
		hp->capacity = newC;
	}
	//1.尾插   2.向上调整,
	hp->a[hp->size] = x;
	hp->size++;
	UpHeap(hp->a, hp->size, hp->size - 1);
}
//判断是否为空
int HeapEmpty(Heap*hp)
{
	return hp->size == 0 ? 1 : 0;
}
//删除堆顶元素。。删除最值
void HeapPop(Heap *hp)
{//交换    尾删    向下调整
	Swap(hp->a[0], hp->a[hp->size - 1]);
	//尾删
	hp->size--;
	DownHeap(hp->a, hp->size, 0);
}
//返回堆顶

//堆排序
void HeapSort(int *a, int n)
{
	//建堆
	int i;
	for (i = (n - 2) / 2; i >= 0; --i){
		DownHeap(a, n, i);
	}
	//堆排序
	int end = n - 1;
	while (end>0)
	{
		swap(&a[0], &a[end]);
		DownHeap(a, end, 0);
		--end;
	}
}
//堆的打印函数
void PrintHeap(Heap *hp)
{
	int i;
	for (i = 0; i < hp->size; i++)
	{
		printf("%d", hp->a[i]);
	}
	printf("\n");
}






 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值