返回
C语言中的堆栈和堆应用详解:掌握数据结构的关键技术
开发工具
2023-03-11 20:30:10
堆栈与堆:理解两种基本数据结构
目录
- 堆栈简介
- 堆简介
- 堆栈与堆的区别
- C 语言中的堆栈与堆实现
- 常见问题解答
堆栈简介
想象一下一个存放盘子的托盘,每次新添加一个盘子,我们都会将它放在托盘的顶部。当我们需要拿走一个盘子时,我们也从顶部拿。这就是堆栈的工作原理。它遵循“后进先出”(LIFO)原则,这意味着最后放入堆栈的元素将是第一个被移出的元素。堆栈通常用于存储临时数据,例如函数调用期间的局部变量或递归函数的调用栈。
堆简介
现在想象一下一棵树,每个节点都有一个值,并且每个节点的值都大于或等于其子节点的值。这就是堆的工作原理。堆通常用于实现优先队列,其中具有最高优先级的元素位于堆的顶部。此外,堆还可用于排序和选择算法以及内存管理。
堆栈与堆的区别
特性 | 堆栈 | 堆 |
---|---|---|
数据结构类型 | 线性 | 树形 |
操作 | 只允许在一端进行 | 可以发生在任意位置 |
原则 | 后进先出(LIFO) | 具有最高优先级的元素位于顶部 |
应用 | 函数调用、递归函数、表达式求值 | 优先队列、排序、选择算法、内存管理 |
C 语言中的堆栈与堆实现
堆栈实现
#include <stdio.h>
#include <stdlib.h>
typedef struct Stack {
int *items;
int top;
int size;
} Stack;
Stack *createStack(int size) {
Stack *stack = (Stack *)malloc(sizeof(Stack));
stack->items = (int *)malloc(sizeof(int) * size);
stack->top = -1;
stack->size = size;
return stack;
}
void push(Stack *stack, int item) {
if (stack->top == stack->size - 1) {
printf("Stack Overflow\n");
return;
}
stack->items[++stack->top] = item;
}
int pop(Stack *stack) {
if (stack->top == -1) {
printf("Stack Underflow\n");
return -1;
}
return stack->items[stack->top--];
}
int peek(Stack *stack) {
if (stack->top == -1) {
printf("Stack Underflow\n");
return -1;
}
return stack->items[stack->top];
}
int isEmpty(Stack *stack) {
return stack->top == -1;
}
void printStack(Stack *stack) {
printf("Stack: ");
for (int i = 0; i <= stack->top; i++) {
printf("%d ", stack->items[i]);
}
printf("\n");
}
int main() {
Stack *stack = createStack(5);
push(stack, 1);
push(stack, 2);
push(stack, 3);
push(stack, 4);
push(stack, 5);
printStack(stack);
printf("Popped element: %d\n", pop(stack));
printf("Popped element: %d\n", pop(stack));
printf("Popped element: %d\n", pop(stack));
printStack(stack);
return 0;
}
堆实现
#include <stdio.h>
#include <stdlib.h>
typedef struct Heap {
int *items;
int size;
int count;
} Heap;
Heap *createHeap(int size) {
Heap *heap = (Heap *)malloc(sizeof(Heap));
heap->items = (int *)malloc(sizeof(int) * size);
heap->size = size;
heap->count = 0;
return heap;
}
void insert(Heap *heap, int item) {
if (heap->count == heap->size) {
printf("Heap Overflow\n");
return;
}
heap->items[heap->count++] = item;
heapifyUp(heap, heap->count - 1);
}
void heapifyUp(Heap *heap, int index) {
int parentIndex = (index - 1) / 2;
while (index > 0 && heap->items[index] > heap->items[parentIndex]) {
int temp = heap->items[index];
heap->items[index] = heap->items[parentIndex];
heap->items[parentIndex] = temp;
index = parentIndex;
parentIndex = (index - 1) / 2;
}
}
int delete(Heap *heap) {
if (heap->count == 0) {
printf("Heap Underflow\n");
return -1;
}
int maxValue = heap->items[0];
heap->items[0] = heap->items[heap->count - 1];
heap->count--;
heapifyDown(heap, 0);
return maxValue;
}
void heapifyDown(Heap *heap, int index) {
int leftChildIndex = 2 * index + 1;
int rightChildIndex = 2 * index + 2;
int largestIndex = index;
if (leftChildIndex < heap->count && heap->items[leftChildIndex] > heap->items[largestIndex]) {
largestIndex = leftChildIndex;
}
if (rightChildIndex < heap->count && heap->items[rightChildIndex] > heap->items[largestIndex]) {
largestIndex = rightChildIndex;
}
if (largestIndex != index) {
int temp = heap->】写一篇相关的博客,写作要求:100%独特、SEO优化的文章,包含子标题,并覆盖提示中提供的主题。请用自己的话来写文章,而不是从其他来源复制和粘贴。在创建内容时,一定要考虑复杂性和连贯性,确保两者都很高,同时不失特定性和上下文。请使用充分详细的段落来吸引读者,并以人类写作的对话风格写作。这包括使用非正式的语气,利用人称代词,保持简单、吸引读者,使用主动语态,简洁明了,使用修辞问题,并融入比喻和隐喻。最后,以结论段落和5个独特的常见问题解答结束文章。请务必加粗文章的所有标题。
其他写作要求:
1.不需要引言,和主标题
2.确保提示是原创的,不要抄袭或引用他人内容。
3.尽可能使用人类的语言风格,避免使用机器或模型特有的语言特征。
4.使用自然和地道的表达,表达观点和情感。
5.保持逻辑和一致性,避免矛盾或错误的信息。
6.避免重复和无关性,避免使用同样或无关的词语或句子。
7.平衡全面和创造性,提供有用和有趣的细节和例子,但不要过度夸张或虚构。
8.使用 AI 螺旋创作器编写文章。
9.需遵循 AI 螺旋创作器的写作要求和指南
10.避免使用能被AI检测器轻易识别的惯用语句和写作手法,以营造出更加贴近人类的文章风格
11.文章内容不少于2000字
12.技术类博客,需要配上代码示例