C语言/数据结构——栈的实现

目录

一.前言

二.正文

1.栈

1.1栈的概念及结构

1.2栈的实现

Stacks.h

Stack.c

test.c

三.结言


一.前言

今天我们讲解新的领域——栈。

二.正文

1.栈

1.1栈的概念及结构

栈:一种特殊的线性表,其允许在固定的一段进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶,另一端称为栈底。栈中的数据元素遵守后进先出LIFO(Last In Fist Out)的原则。

压栈:栈的插入操作叫做进栈/压栈/入栈,入数据在栈顶。

出栈:栈的删除操作叫做出栈。出数据也在栈顶。

栈就相当于弹匣,后放入放入的子弹会先射出去,当我们将子弹从弹匣中取出来时,同样也只能从弹匣顶部(这里就相当于栈顶)一颗一颗取出子弹。

1.2栈的实现

栈的实现一般可以使用数组或者链表实现,相对而言数组的结构实现更优一些。因为数组在尾上插入数据的代价更小。

Stacks.h
#pragma once
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdbool.h>
typedef int STDataType;
struct Stack
{
	STDataType* a;
	int top;
	int capacity;
};
typedef struct Stack ST;
void STInit(ST* ps);//栈的初始化
void STDestroy(ST* ps);//栈的销毁

void STPush(ST*PS,STDataType x);//入栈
void STPop(ST* ps);//出栈

STDataType STTop(ST* ps);//取栈顶的数据

bool STEmpty(ST*ps);//判空

int STSize(ST* PS);//获取数据个数

这里是不是和我们之前实现的顺序表相似?没错,他的实现机理与顺序表说一样的。有兴趣的小伙伴,可以了解一下,下面是链接: 写文章-CSDN创作中心

Stack.c
#include"Stack.h"
void STInit(ST *ps)//栈的初始化
{
	assert(ps);
	ps->a = NULL;
	ps->capacity = 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);
	if (ps->capacity == ps->top)
	{
		int newcapacity = ps->capacity == 0 ? 6 : ps->capacity*2;
		STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail!");
			return;
		}
		ps->a = tmp;
		ps->capacity = newcapacity;
	}
	ps->a[ps->top] = x;
	ps->top++;
}
void STPop(ST* ps)//删除栈顶元素
{
	assert(ps);
	assert(ps->a);
	ps->top--;
}
STDataType STTop(ST* ps)//取出栈顶元素
{
	assert(ps);
	assert(ps->top >= 0);
	return ps->a[ps->top-1];
}
bool STEmpty(ST* ps)//判断栈内元素是否为空
{
	asssert(ps);
	if (ps->top == 0)
		return true;
	return false;
}
int STSize(ST* ps)//获取数据个数
{
	assert(ps);
	return ps->top ;
}
test.c
#include"Stack.h"
void test01()
{
	ST s;
	STInit(&s);
	STPush(&s,1);
	STPush(&s, 2);
	STPush(&s, 3);
	STPush(&s, 4);
	/*STPop(&s);
	STPop(&s);
	STPop(&s);
	STPop(&s);*/
	while (s.top)
	{
		printf("%d\n", STTop(&s));
		STPop(&s);
	}
	STDestroy(&s);
}
int main()
{
	test01();
	return 0;
}

上面的test.c是我为了测试代码是能正常运行写的。大家看看就行。

三.结言

栈的实现分享就到这了,如果对大家有所帮助的话,求一个三连。谢谢啦。

  • 22
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
数据结构——用C语言描述(第3版)》课后答案的描述使用C语言实现各种数据结构和算法。以下是对几个常见数据结构的描述和相关代码示例。 1. 数组(Array):数组是一种线性数据结构,用于存储相同类型的元素。C语言中使用数组可以快速访问和修改元素。示例代码如下: ```c #include <stdio.h> int main() { int arr[5] = {1, 2, 3, 4, 5}; for(int i = 0; i < 5; i++) { printf("%d ", arr[i]); } return 0; } ``` 2. 链表(Linked List):链表是一种动态数据结构,通过节点之间的指针链接来存储数据。C语言中可以使用结构体和指针来实现链表。示例代码如下: ```c #include <stdio.h> #include <stdlib.h> struct Node { int data; struct Node* next; }; void printList(struct Node* head) { struct Node* current = head; while(current != NULL) { printf("%d ", current->data); current = current->next; } } int main() { struct Node* head = NULL; struct Node* second = NULL; struct Node* third = NULL; head = (struct Node*) malloc(sizeof(struct Node)); second = (struct Node*) malloc(sizeof(struct Node)); third = (struct Node*) malloc(sizeof(struct Node)); head->data = 1; head->next = second; second->data = 2; second->next = third; third->data = 3; third->next = NULL; printList(head); return 0; } ``` 3. (Stack):是一种后进先出(LIFO)的数据结构,在C语言中可以使用数组来实现。示例代码如下: ```c #include <stdio.h> #define MAX_SIZE 100 int stack[MAX_SIZE]; int top = -1; void push(int item) { if(top == MAX_SIZE - 1) { printf("Stack Overflow\n"); } else { stack[++top] = item; } } int pop() { if(top == -1) { printf("Stack Underflow\n"); return -1; } else { return stack[top--]; } } void printStack() { for(int i = top; i >= 0; i--) { printf("%d ", stack[i]); } } int main() { push(1); push(2); push(3); printf("Popped element: %d\n", pop()); printStack(); return 0; } ``` 这些示例代码展示了如何使用C语言描述《数据结构——用C语言描述(第3版)》中介绍的数据结构。读者可以根据书中提供的习题进行编程练习,进一步巩固数据结构和算法的相关知识。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值