数据结构-栈的应用

头文件SeqStack.h

#include<stdio.h>
#include<malloc.h>
#include<assert.h>
#include<stdbool.h>
#include<string.h>
#define STACK_INIT_SIZE 8
#define INC_SIZE 3
typedef char ElemType;

typedef struct SeqStack {
	ElemType *base;
	int top;
	int capacity;
}SeqStack;

void InitStack(SeqStack *s);
void Push(SeqStack *s,ElemType x);
bool IsFull(SeqStack *s);
bool IsEmpty(SeqStack *s);
void Show(SeqStack *s);
void Pop(SeqStack *s);
bool GetTop(SeqStack *s, ElemType *v);
void Clear(SeqStack*s);
void Destroy(SeqStack*s);
bool inc(SeqStack *s);
void Print(SeqStack *s);

函数实现SeqStack.c

#include"SeqStack.h"
void InitStack(SeqStack *s) {
	s->base = (ElemType *)malloc(sizeof(ElemType)* STACK_INIT_SIZE);
	assert(s->base != NULL);
	s->capacity = STACK_INIT_SIZE;
	s->top = 0;
}

bool IsFull(SeqStack *s) {
	return  s->top >= s->capacity;
}

bool IsEmpty(SeqStack *s) {
	 return s->top == 0;
}


void Push(SeqStack *s, ElemType x) {
	if (IsFull(s)&&!inc(s))
		printf("栈空间已满,%d无法继续入栈",x);
	else
		s->base[s->top++] = x;
}

void Show(SeqStack *s)
{
	for (int i = s->top - 1; i >= 0; --i) {

		printf("%d", s->base[i]);
	}
	printf("\n");
}


void Pop(SeqStack *s) {
	if (IsEmpty(s)) {
		printf("栈空间已空,不能出栈");
	}
	s->top--;
}

bool GetTop(SeqStack *s, ElemType *v)

 {

	if (IsEmpty(s))
	{
		printf("栈空间已满,不能取出栈顶元素");
		return false;
	}

	else
	{
		*v = s->base[s->top - 1];
		return true;
	}
}


void Clear(SeqStack*s) {
	s->top = 0;
}
void Destroy(SeqStack *s) {
	free(s->base);
	s->base = NULL;//记住要置为空
	s->capacity = 0;
	s->top = 0;
}

bool inc(SeqStack *s) {//自增空间函数
	ElemType *newbase = (ElemType *)realloc(s->base, sizeof(ElemType)*(s->capacity + INC_SIZE));
	if (newbase == NULL) {
		printf("增配空间失败,内存不足!\n");
		return false;
	}
	else
	{
		s->base = newbase;
		s->capacity += INC_SIZE;
		return true;
	}
}

void Print(SeqStack *s) {
	for (int i = 0; i < s->top; ++i)
		printf("%c ", s->base[i]);
}

测试函数Main.c

#include"SeqStack.h"

void Convert_8(int value) {//十进制转换为八进制
	SeqStack st;
	InitStack(&st);
	while (value) {
		Push(&st, value % 8);
		value /= 8;
	}
	Show(&st);
}

bool Check(char *str) {//括号匹配
	char v;
	SeqStack st;
	InitStack(&st);
	while (*str != '\0') {
		if (*str == '[' || *str == '(')
			Push(&st, *str);
		else if (*str == ']') {
			GetTop(&st, &v);
			if (v != '[')
				return false;
			Pop(&st);
		}
		else if (*str == ')') {
			GetTop(&st, &v);
			if (v != '(')
				return false;
			Pop(&st);
		}
		++str;
	}
	return IsEmpty(&st);
}


void LineEdit() {//行编辑程序
	SeqStack st;
	InitStack(&st);
	char ch = getchar();
	while (ch != '$') {
		while (ch != '$'&&ch != '\n') {
			switch (ch) {
			case '#':
					Pop(&st);
					break;
			case '@':
						Clear(&st);
						break;
			default:
							Push(&st, ch);
						break;

			}
			ch = getchar();
		}
		Print(&st);
		ch = getchar();
	}
	Destroy(&st);
}



int main() {
	//char *str = "[([][])]";
	//bool flag = Check(str);
	//if (flag)
		//printf("括号匹配成功\n");
	//else
		//printf("括号匹配失败\n");
	//int value = 47183;
	//Convert_8(value);
	LineEdit();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值