栈实现(C语言版)

背景

栈的构建,方便以后复习。

1、stack.h

//底层为一个数组
#pragma once

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<stdbool.h>


//#define N 10
//struct Stack
//{
//	int a[N];
//	int top;
//};

typedef int STDataType;
typedef struct Stack
{
	STDataType* a;
	int top;
	int capacity;
}ST;

void STInit(ST* ps);
void STDestroy(ST* ps);
void STPush(ST* ps, STDataType x);
void STPop(ST* ps);
STDataType STTop(ST* ps);

int STSize(ST* ps);
bool STEmpty(ST* ps);

2、stack.c

#include "Stack.h"

void STInit(ST* ps)
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 0;
	ps->top = 0;
}

void STDestroy(ST* ps)
{
	assert(ps);

	free(ps->a);
	ps->a = NULL;
	ps->top = ps->capacity = 0;
}

void STPush(ST* ps, STDataType x)
{
	assert(ps);
	// 11:40
	if (ps->top == ps->capacity)
	{
		int newCapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->a, sizeof(STDataType) * newCapacity);
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(-1);
		}

		ps->a = tmp;
		ps->capacity = newCapacity;
	}

	ps->a[ps->top] = x;
	ps->top++;
}

void STPop(ST* ps)
{
	assert(ps);

	// 
	assert(ps->top > 0);

	--ps->top;
}

STDataType STTop(ST* ps)
{
	assert(ps);

	// 
	assert(ps->top > 0);

	return ps->a[ps->top - 1];
}

int STSize(ST* ps)
{
	assert(ps);

	return ps->top;
}

bool STEmpty(ST* ps)
{
	assert(ps);

	return ps->top == 0;
}

3、test.c

#include "Stack.h"

void TestStack1()
{
	ST st;
	STInit(&st);
	STPush(&st, 1);
	STPush(&st, 2);
	STPush(&st, 3);
	STPush(&st, 4);
	STPush(&st, 5);

	while (!STEmpty(&st))
	{
		printf("%d ", STTop(&st));
		STPop(&st);
	}
	printf("\n");

	STDestroy(&st);
}

int main()
{
	TestStack1();

	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
快速排序的非递归可以使用数组模拟的操作,也可以使用指针实现。以下是使用指针实现C语言代码: ```c #include <stdio.h> void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } void quickSort(int arr[], int left, int right) { int stack[right - left + 1]; int top = -1; stack[++top] = left; stack[++top] = right; while (top >= 0) { right = stack[top--]; left = stack[top--]; int pivot = arr[right]; int i = left - 1; for (int j = left; j <= right - 1; j++) { if (arr[j] < pivot) { i++; swap(&arr[i], &arr[j]); } } swap(&arr[i + 1], &arr[right]); int pivotIndex = i + 1; if (pivotIndex - 1 > left) { stack[++top] = left; stack[++top] = pivotIndex - 1; } if (pivotIndex + 1 < right) { stack[++top] = pivotIndex + 1; stack[++top] = right; } } } void printArray(int arr[], int size) { for (int i = 0; i < size; i++) { printf("%d ", arr[i]); } printf("\n"); } int main() { int arr[] = {5, 9, 1, 3, 10, 4, 8, 6, 2, 7}; int size = sizeof(arr) / sizeof(arr[0]); quickSort(arr, 0, size - 1); printf("Sorted array: "); printArray(arr, size); return 0; } ``` 这个实现中,我们使用一个指针数组 `stack` 来模拟的操作,`top` 指针表示当前顶元素的位置。我们首先将左右边界压入中,然后在 while 循环中进行以下操作: - 弹出顶的右边界和左边界 - 选择最后一个元素作为 pivot,进行快速排序的划分操作 - 将 pivot 移动到正确的位置上 - 根据 pivot 的位置将左右两个子数组的边界压入中 这样我们可以使用 O(1) 的空间复杂度来实现快速排序的非递归
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值