SeqStack.h

#ifndef __SEQSTACK_H__
#define __SEQSTACK_H__

#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0

#define MAXSIZE 20 /* 存储空间初始分配量 */

typedef int Status; /* Status 是函数的类型,其值是函数结果状态代码,如 OK 等 */
typedef int SElemType; /* SElemType 类型根据实际情况而定,这里假设为 int */

Status visit(SElemType c);

typedef struct {
	SElemType data[MAXSIZE]; /* 数组,存储数据元素 */
	int top; /* 栈顶指针 */
}SeqStack;

/* 初始化顺序栈 */
Status InitStack(SeqStack *S);

/* 判断顺序栈是否为空,是则返回TRUE;否则返回FALSE */
Status StackEmpty(SeqStack S);

/* 重置顺序栈为空栈 */
Status ClearStack(SeqStack *S);

/* 销毁顺序栈 */
// int DestroyStack(SeqStack *S);

/* 获取顺序栈中元素个数 */
Status StackLength(SeqStack S);

/* 若栈不空,则用e返回S的栈顶元素,并返回 OK;否则返回 ERROR */ 
Status GetTop(SeqStack S, SElemType *e);

/* 插入元素e 为新的栈顶元素 */ 
Status Push(SeqStack *S, SElemType e);

/* 若栈不空,则删除 S 的栈顶元素,用 e 返回其值,并 返回OK;否则返回 ERROR*/ 
Status Pop(SeqStack *S, SElemType *e);

/* 从栈底到栈顶依次对栈中每个元素显示 */ 
Status StackTraverse(SeqStack S);

#endif

SeqStack.c

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <io.h>
#include <math.h>
#include <time.h>
#include "SeqStack.h"


Status visit(SElemType c){
	printf("%d ", c);

	return OK;
}

/* 初始化顺序栈 */
Status InitStack(SeqStack *S) {
	// *S->data = (SElemType*)malloc(MAXSIZE * sizeof(SElemType));
	S->top = -1;

	return OK;
}

/* 判断顺序栈是否为空,是则返回TRUE;否则返回FALSE */
Status StackEmpty(SeqStack S) {
	if (S.top == -1) {
		return TRUE;
	}
	else {
		return FALSE;
	}
}

/* 重置顺序栈为空栈 */
Status ClearStack(SeqStack *S) {
	S->top = -1;

	return OK;
}

/* 销毁顺序栈 未动态申请空间,不须手动释放空间 */
//int DestroyStack(SeqStack *S) {
//	free(S);
//	return 0;
//}

/* 获取顺序栈中元素个数 */
int StackLength(SeqStack S) {
	return S.top + 1;
}

/* 若栈不空,则用e返回S的栈顶元素,并返回 OK;否则返回 ERROR */
Status GetTop(SeqStack S, SElemType *e) {
	if (S.top != -1) {
		*e = S.data[S.top];
	}
	else {
		return ERROR;
	}
	return OK;
}

/* 插入元素e 为新的栈顶元素 */
Status Push(SeqStack *S, SElemType e) {
	if (S->top >= MAXSIZE - 1) {
		return ERROR;
	}
	S->top++;
	S->data[S->top] = e;

	return OK;
}

/* 若栈不空,则删除 S 的栈顶元素,用 e 返回其值,并 返回OK;否则返回 ERROR*/
Status Pop(SeqStack *S, SElemType *e) {
	if (S->top != -1) {
		*e = S->data[S->top];
		S->top--;

		return OK;
	}
	else {
		return ERROR;
	}
}

/* 从栈底到栈顶依次对栈中每个元素显示 */
Status StackTraverse(SeqStack S) {
	int i = 0;
	while (i <= S.top) {
		visit(S.data[i++]);
	}
	printf("\n");
	return OK;
}

/* 数制转换,十进制数转换为二进制数 */
void conversionTo2(SeqStack S) {
	// 对于输入的任意一个非负十进制整数,打印输出与其等值的二进制数
	SElemType N;
	InitStack(&S);
	printf("请输入十进制数:");
	scanf_s("%d", &N); // 输入一个十进制整数
	while (N) {
		Push(&S, N % 2);
		N = N / 2;
	}
	printf("对应的二进制数:");
	while (!StackEmpty(S)) {
		Pop(&S, &N);
		printf("%d", N);
	}
	printf("\n");
}

/* 数制转换,十进制数转换为八进制数 */
void conversionTo8(SeqStack S) {
	// 对于输入的任意一个非负十进制整数,打印输出与其等值的八进制数
	SElemType N; 
	InitStack(&S);
	printf("请输入十进制数:");
	scanf_s("%d", &N); // 输入一个十进制整数
	while (N) {
		Push(&S, N % 8);
		N = N / 8;
	}
	printf("对应的八进制数:");
	while (!StackEmpty(S)) {
		Pop(&S, &N);
		printf("%d", N);
	}
	printf("\n");
}

/* 数制转换,十进制数转换为十六进制数 */
void conversionTo16(SeqStack S) {
	// 对于输入的任意一个非负十进制整数,打印输出与其等值的十六进制数
	SElemType N;
	InitStack(&S);
	printf("请输入十进制数:");
	scanf_s("%d", &N); // 输入一个十进制整数
	while (N) {
		Push(&S, N % 16);
		N = N / 16;
	}
	printf("对应的十六进制数:");
	while (!StackEmpty(S)) {
		Pop(&S, &N);
		if (N > 9) {
			printf("%c", N + 55);
		}
		else {
			printf("%d", N);
		}
	}
	printf("\n");
}

main.c

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

int main() {
	SeqStack s, SS, SSS, SSSS;
	int e, j;

	if (InitStack(&s) == OK) {
		for (j = 1; j <= 10; j++) {
			Push(&s, j);
		}
	}
	printf("栈中元素依次为:"); 
	StackTraverse(s); 
	printf("\n");

	Pop(&s, &e); 
	printf("弹出的栈顶元素 e=%d\n", e); 
	printf("\n");

	printf("栈空否:%d(1:空 0:否)\n", StackEmpty(s)); 
	GetTop(s, &e); 
	printf("\n");

	printf("栈顶元素 e=%d 。 栈的长度为 %d\n", e, StackLength(s)); 
	printf("\n");

	ClearStack(&s); 
	printf("清空栈后,栈空否:%d(1:空 0:否)\n", StackEmpty(s));
	printf("\n");

	conversionTo2(&SS);
	printf("\n");

	conversionTo8(&SSS);
	printf("\n");

	conversionTo16(&SSSS);
	printf("\n");

	system("pause");
	return 0;
}