数据结构与算法(C语言)------栈

1、定义

栈(stack)是限定仅在表尾进行插入和删除操作的线性表。
把允许插入和删除的一端称为栈顶,另一端称为栈底,不含任何数据元素的栈称为空栈。栈又称为先进后出的线性表。

2、代码

1、代码(顺序栈)

#ifndef __SQSTACK_H
#define __SQSTACK_H
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
#define OK    1
#define ERROR 0

typedef int states;  //返回状态量
typedef int ElementType; //栈中的数据类型

//栈的顺序存储结构---顺序栈
/** 顺序栈结构 */
typedef struct {
	int top;                    //栈顶元素下标
	int size;                   //栈中元素个数
	ElementType data[MAX_SIZE];
}SqStack;

/** 初始化栈 */
states InitSqStack(SqStack *stack);

/** 入栈 */
states PushSqStack(SqStack *stack,ElementType e);

/** 出栈 */
states PopSqStack(SqStack *stack,ElementType *e);

/** 判断栈是否为空 */
states IsSqStackEmpty(SqStack *stack);

/** 获取栈中元素个数 */
int GetSqStackElemNum(SqStack *stak);

/** 获取栈顶元素 */
states GetSqStackTopElem(SqStack *stack,ElementType *top);

/** 清空栈 */
states ClearSqStack(SqStack *stack);

/** 打印栈中元素 */
void PrintfSqStack(SqStack *stack);

/** 测试栈的函数 */
void TestSqStack(void);

#endif /* __SQSTACK_H */

#include "sqstack.h"

/** 初始化栈 */
states InitSqStack(SqStack *stack)
{
	int i;
	stack->top = -1;     //初始化时,指向栈顶下标的变量赋值-1
	stack->size = 0;     //栈中元素个数为0
	for(i = 0;i < MAX_SIZE;i++){
		stack->data[i] = 0;
	}
	return OK;
}

/** 入栈 */
states PushSqStack(SqStack *stack,ElementType e)
{
	//首先判断顺序栈是否已满
	if(stack->size == MAX_SIZE)
		return ERROR;
	stack->data[++stack->top] = e;
	stack->size++;
	return OK;
}

/** 出栈 */
states PopSqStack(SqStack *stack,ElementType *e)
{
	//首先判断栈是否为空,为空直接返回
	if(stack->top == -1 || stack->size == 0)
		return ERROR;
	*e = stack->data[stack->top--];
	stack->size--;
	return OK;
}

/** 判断栈是否为空 1-代表栈空;0-代表栈非空*/
states IsSqStackEmpty(SqStack *stack)
{
	if(stack->top == -1 || stack->size == 0)
		return 1;                
	return 0;
}

/** 获取栈中元素个数 */
int GetSqStackElemNum(SqStack *stak)
{
	return stak->size;
}

/** 获取栈顶元素 */
states GetSqStackTopElem(SqStack *stack,ElementType *top)
{
	if(IsSqStackEmpty(stack))
		return ERROR;
	*top = stack->data[stack->top];
	return OK;
}

/** 清空栈 */
states ClearSqStack(SqStack *stack)
{
	stack->top = -1;
	stack->size = 0;
	return OK;
}

/** 打印栈中元素 */
void PrintfSqStack(SqStack *stack)
{
	int i;
	printf("stack:");
	for(i = 0;i < stack->size;i++){
		printf("%d ",stack->data[i]);
	}
}

/** 测试栈的函数 */
void TestSqStack(void)
{
	ElementType e;
	SqStack s;
	InitSqStack(&s);
	printf("栈为空y/n:%c\n",IsSqStackEmpty(&s)?'y':'n');
	printf("入栈:\n");
	PushSqStack(&s,1);
	PushSqStack(&s,2);
	PushSqStack(&s,3);
	PushSqStack(&s,4);
	PrintfSqStack(&s);
	printf("栈中元素个数:%d\n",GetSqStackElemNum(&s));
	printf("出栈:\n");
	PopSqStack(&s,&e);
	printf("e = %d\n",e);
	PopSqStack(&s,&e);
	printf("e = %d\n",e);
	PopSqStack(&s,&e);
	printf("e = %d\n",e);
	GetSqStackTopElem(&s,&e);
	printf("top = %d\n",e);
	ClearSqStack(&s);
	printf("栈中元素个数:%d\n",GetSqStackElemNum(&s));
}

2、代码(链栈)

#ifndef __LINKEDSTACK_H
#define __LINKEDSTACK_H
#include <stdio.h>
#include <stdlib.h>

#define OK    1
#define ERROR 0

typedef int states;  //返回状态量
typedef int ElementType; //栈中的数据类型

//栈的链式存储结构---链栈
/** 链栈中的结点结构 */
typedef struct StackNode{
	ElementType data;            //数据域
	struct StackNode *next;      //指针域
}StackNode;

/** 链栈的结构 */
typedef struct {
	StackNode *top;       //栈中的顶点指针
	int size;             //栈中的元素个数
}LinkedStack;

/** 初始化链栈 */
states InitLinkedStack(LinkedStack *stack);

/** 入栈 */
states PushLinkedStack(LinkedStack *stack,StackNode e);

/** 出栈 */
states PopLinkedStack(LinkedStack *stack,StackNode *e);

/** 判断链栈是否为空 */
states IsLinkedStackEmpty(LinkedStack *stack);

/** 获取链栈中的元素个数 */
int GetLinkedStackElemNum(LinkedStack *stack);

/** 获取链栈中栈顶元素 */
states GetLinkedStackTopElem(LinkedStack *stack,StackNode *top);

/** 清空链栈 */
states ClearLinkedStack(LinkedStack *stack);

/** 打印链栈中元素 */
void PrintfLinkedStack(LinkedStack *stack);

/** 测试链栈相关操作 */
void TestLinkedStack(void);

#endif /* __LINKEDSTACK_H */

#incldue "linkedstack.h"

/** 初始化链栈 */
states InitLinkedStack(LinkedStack *stack)
{
	stack->top = NULL;
	stack->size = 0;
	return OK;
}

/** 入栈 */
states PushLinkedStack(LinkedStack *stack,ElementType e)
{
	StackNode *node = (StackNode *)malloc(sizeof(StackNode));
	node->data = e;
	node->next = stack->top;
	stack->top = node;
	stack->size++;
	return OK;
}

/** 出栈 */
states PopLinkedStack(LinkedStack *stack,ElementType *e)
{
	StackNode *tempnode;
	if(stack->size == 0 || stack->top == NULL)
		return ERROR;
	tempnode = stack->top;
	*e = tempnode->data;
	stack->top = tempnode->next;
	free(tempnode);
	stack->size--;
	return OK;
}

/** 判断链栈是否为空 */
states IsLinkedStackEmpty(LinkedStack *stack)
{
	if(stack->size == 0 || !stack->top)
		return 1;
	return 0;
}

/** 获取链栈中的元素个数 */
int GetLinkedStackElemNum(LinkedStack *stack)
{
	return stack->size;
}

/** 获取链栈中栈顶元素 */
states GetLinkedStackTopElem(LinkedStack *stack,ElementType *top)
{
	if(IsLinkedStackEmpty(stack))
		return ERROR;
	*top = stack->top->data;
	return OK;
}

/** 清空链栈 */
states ClearLinkedStack(LinkedStack *stack)
{
	StackNode *p,*q;
	p = stack->top;
	while(p){
		q = p->next;
		free(p);
		p =q;
		stack->size--;
	}
	stack->top = NULL;
	return OK;
}

/** 打印链栈中元素 */
void PrintfLinkedStack(LinkedStack *stack)
{
	int i;
	StackNode *node = stack->top;
	if(!node)
		printf("栈空\n");
	else
		printf("栈top: ");
	for(i = 0;i < stack->size;i++){
		printf("%d-> ",node->data);
		node = node->next;
	}
}

/** 测试链栈相关操作 */
void TestLinkedStack(void)
{
	LinkedStack s;
	ElementType e;
	InitLinkedStack(&s);
	printf("入栈:\n");
	PushLinkedStack(&s,1);
	PushLinkedStack(&s,6);
	PushLinkedStack(&s,9);
	PushLinkedStack(&s,52);
	PushLinkedStack(&s,100);
	PrintfLinkedStack(&s);
	printf("\n栈中元素个数:%d\n",GetLinkedStackElemNum(&s));
	printf("出栈:\n");
	PopLinkedStack(&s,&e);
	printf("e = %d\n",e);
	PopLinkedStack(&s,&e);
	printf("e = %d\n",e);
	PopLinkedStack(&s,&e);
	printf("e = %d\n",e);
	PopLinkedStack(&s,&e);
	printf("e = %d\n",e);
	GetLinkedStackTopElem(&s,&e);
	printf("top = %d\n",e);
	printf("清空栈\n");
	ClearLinkedStack(&s);
	printf("栈中元素个数:%d\n",GetLinkedStackElemNum(&s));
}

3、编程笔记

最近写代码的一些心得体会写在这里。对于简单的数据结构如链表、栈、队列等,首先要清楚怎么用代码对数据结构进行描述(如何采用结构体进行封装),这是关键的一个环节。当这个环节清楚了之后,那么再去想数据结构操作的细节。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值