嵌入式学习

1.笔记

2. 顺序栈和链栈代码

1. 顺序栈

        seq_stack.h文件。

#ifndef __SEQ_STRACK_H__
#define __SEQ_STRACK_H__

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

#define MAXSIZE 10

typedef int ElemType;
typedef struct {
	ElemType data[MAXSIZE];
	int top;
}Stack,*Stack_p;


//创建栈
Stack_p create_stack();
//判空
int isEmpty_stack(Stack_p S);
//判满
int isFull_stack(Stack_p S);
//入栈/压栈
void push_stack(Stack_p S,ElemType data);
//打印栈中元素
void print_stack(Stack_p S);
//出栈
void pop_stack(Stack_p S);
//释放顺序栈
void free_stack(Stack_p *S);



#endif

        seq_stack.c文件。

#include "seq_stack.h"

//创建栈
Stack_p create_stack()
{
	Stack_p S=(Stack_p)malloc(sizeof(Stack));
	if(NULL==S)
	{
		printf("空间申请失败\n");
		return NULL;
	}
	S->top =-1;
	return S;
}

//判空
int isEmpty_stack(Stack_p S)
{
	if(NULL==S)
	{
		printf("栈不存在\n");
		return -1;
	}
	return S->top==-1?1:0;
}

//判满
int isFull_stack(Stack_p S)
{
	if(NULL==S)
	{
		printf("栈不存在\n");
		return -1;
	}
	return S->top==MAXSIZE-1?1:0;
}

//入栈/压栈
void push_stack(Stack_p S,ElemType data)
{
	if(NULL==S)
	{
		printf("栈不存在\n");
		return;
	}
	if(isFull_stack(S))
	{
		printf("栈满\n");
		return;
	}
	S->data[++S->top]=data;
	return;
}


//打印栈中元素
void print_stack(Stack_p S)
{
	if(NULL==S)
	{
		printf("栈不存在\n");
		return;
	}
	if(isEmpty_stack(S))
	{
		printf("栈空\n");
		return;
	}
	int i;
	for(i=S->top;i>=0;i--)
	{
		printf("%3d",S->data[i]);
	}
	putchar(10);
	return;
}

//出栈/弹栈
void pop_stack(Stack_p S)
{
	if(NULL==S)
	{
		printf("栈不存在\n");
		return;
	}
	if(isEmpty_stack(S))
	{
		printf("栈空\n");
		return;
	}
	ElemType e=S->data[S->top--];//如果需要出栈元素的值
	return;
}

//释放顺序栈
void free_stack(Stack_p *S)
{
	//入参为空的判断
	if(NULL==S || NULL==*S)
	{
		printf("入参为空\n");
		return;
	}
	free(*S);
	*S=NULL;
	return;
}

        main.c文件。

#include "seq_stack.h"

int main()
{
	Stack_p S=create_stack();//创建栈
	printf("压栈5个元素:\n");
	push_stack(S,1);//压栈
	push_stack(S,2);
	push_stack(S,3);
	push_stack(S,4);
	push_stack(S,5);
	print_stack(S);
	printf("出栈2个元素:\n");
	pop_stack(S);//出栈
	pop_stack(S);
	print_stack(S);
	free_stack(&S);//释放顺序栈
	printf("%p\n",S);
	return 0;
}

2. 链栈

        Link_Stack.h文件。

#ifndef __LINK_STACK_H__
#define __LINK_STACK_H__

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

typedef int ElemType;
typedef struct LNode {
	ElemType data;
	struct LNode *next;
}SNode,*SNode_p;


//申请结点
SNode_p creat_node(ElemType data);
//判空
int isEmpty(SNode_p S);
//入栈
void push(SNode_p *S,ElemType data);
//打印栈中元素
void print_stack(SNode_p S);
//出栈
void pop(SNode_p *S);


#endif

        Link_Stack.c文件。

#include "Link_Stack.h"

//申请结点
SNode_p creat_node(ElemType data)
{
	SNode_p s=(SNode_p)malloc(sizeof(SNode));
	if(NULL==s)
	{
		printf("空间申请失败\n");
		return NULL;
	}
	s->data=data;
	return s;
}

//判空
int isEmpty(SNode_p S)
{
	return S==NULL?1:0;
}

//入栈
void push(SNode_p *S,ElemType data)
{
	SNode *s= creat_node(data);	
	s->next=*S;
	*S=s;
}

//出栈
void pop(SNode_p *S)
{
	if(isEmpty(*S))
	{
		printf("栈空\n");
		return;
	}
	//ElemType e=S->data;//若需要获取出栈元素
    SNode_p p=S;
	*S=(*S)->next;
    free(p);
	return;
}

//打印栈中元素
void print_stack(SNode_p S)
{
	if(isEmpty(S))
	{
		printf("栈为空\n");
		return;
	}
	SNode_p p=S;
	while(p!=NULL)
	{
		printf("%3d",p->data);
		p=p->next;
	}
	putchar(10);
}

        main.c文件。

#include "Link_Stack.h"

int main()
{
	//申请栈顶指针
	SNode_p S=NULL;
	printf("入栈5个元素:\n");
	push(&S,1);//入栈
	push(&S,2);
	push(&S,3);
	push(&S,4);
	push(&S,5);
	print_stack(S);
	printf("出栈2个元素:\n");
	pop(&S);//出栈
	pop(&S);
	print_stack(S);
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值