实验内容:
1.建立顺序栈,并在顺序栈上实现入栈和出栈操作(验证性内容)。
2.使用栈实现十进制转其他进制问题(应用性设计内容)。
3.建立链栈,并在链栈上实现入栈和出栈操作(设计性内容)。
顺序栈:
#include"stdio.h"
#define INITSIZE 100
#define INCREMENT 20
typedef char ElemType;
typedef struct {
ElemType* base;//栈底指针,数组首地址
ElemType* top;//栈顶指针
int stacksize;//容量
}sqstack;
//初始化
void init(sqstack* s) {
s->base = (ElemType*)malloc(sizeof(ElemType) * INITSIZE);
s->top = s->base;
s->stacksize = INITSIZE;//栈的大小
}
//增量分配
void increment(sqstack* s)
{
s->base = (ElemType*)realloc(s->base, (s->stacksize + INCREMENT) * sizeof(ElemType));
s->top = s->base + s->stacksize;
s->stacksize += INCREMENT;
}
//判断空栈
int isempty(sqstack s) {
return s.top - s.base == 0 ? 1 : 0;//三目运算符
}
//判断栈满
int isfull(sqstack s) {
return s.top - s.base == s.stacksize ? 1 : 0;
}
//入栈:对元素赋值,top移动一个位置
void push(sqstack* s, ElemType e)
{
if (isfull(*s)) {
increment(s);
}
*s->top++ = e;
}
//出栈
void pop(sqstack* s, ElemType* e) {
if (isempty(*s))
return;
s->top--;
*e = *s->top;
}
//输出,不是出栈,而是将元素依次打印
void output(sqstack s) {
int i;
for (i = 0; i < s.top - s.base; i++) {
printf("%c\t", *(s.base + i));
}
printf("\n");
}
void main() {
sqstack s;
ElemType e;
init(&s);
push(&s, 'A');
push(&s, 'B');
push(&s, 'C');
push(&s, 'D');
output(s);
while (isempty(s) != 1) {
pop(&s, &e);
printf("%c\n", e);
}
}
运行结果:
进制转换:
#include"stdlib.h"
#include"stdio.h"
#define STACKINITSIZE 10
#define STACKINCREMENT 2
typedef int elemtype;
typedef struct sqstack
{
elemtype* base;
elemtype* top;
int stacksize;
}sqstack;
void initstack(sqstack* s)
{
s->base = (elemtype*)malloc(STACKINITSIZE * sizeof(elemtype));
if (!s->base)exit(0);
s->top = s->base;
s->stacksize = STACKINITSIZE;
}
int stackempty(sqstack s)
{
if (s.top == s.base)return 1;
else return 0;
}
void push(sqstack* s, elemtype e)
{
if (s->top - s->base == s->stacksize)
{
s->base = (elemtype*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(elemtype));
if (!s->base)exit(0);
s->top = s->base + s->stacksize;
s->stacksize += STACKINCREMENT;
}
*(s->top)++ = e;
}
int pop(sqstack* s, elemtype* e)
{
if (s->top == s->base)return 0;
*e = *--s->top;
return 1;
}
void conversion(int n, int r)
{
sqstack s;
elemtype x;
initstack(&s);
while (n)
{
push(&s, n % r); n = n / r;
}
while (!stackempty(s)) {
pop(&s, &x);
printf_s("%d\t", x);
}
}
void main() {
int n, r;
printf("请输入要转换的十进制整数:");
scanf_s("%d", &n);
printf("请输入要转换到的数制:");
scanf_s("%d", &r);
conversion(n, r);
}
运行结果:
链栈:
//链栈:top栈顶指针就是链表头指针(不带头结点的单向链表)
#include"stdio.h"
#include"stdlib.h"
typedef int elemtype;
typedef struct stacknode {
elemtype data;
struct stacknode* next;
}stacknode, * linkstack;
//初始化
void init(linkstack* s) {
*s = NULL;
}
//创建节点
stacknode* create(elemtype data) {
stacknode* p = (stacknode*)malloc(sizeof(stacknode));
p->data = data;
p->next = NULL;
return p;
}
//是否为空
int isempty(linkstack s) {
return s == NULL ? 1 : 0;
}
//是否满了(永远都不会满)
int isfull(linkstack s) {
return 0;
}
//压栈
void push(linkstack* s, elemtype e) {
stacknode* p = create(e);
p->next = (*s);
*s = p;
}
//出栈
void pop(linkstack* s, elemtype* e) {
if (isempty(*s))
return;
stacknode* t = *s;//保存栈顶节点
*e = t->data;
*s = (*s)->next;
free(t);
}
//取栈顶元素
elemtype getpop(linkstack s) {
if (!isempty(s)) {
return s->data;
}
}
//取栈顶元素2
elemtype getpop2(linkstack s, elemtype* e) {
if (!isempty(s)) {
*e = (s->data);
return 1;
}
return 0;
}
//输出所有元素
void output(linkstack s) {
stacknode* top = s;
printf("\n/**********栈内元素**********\n");
while (top != NULL) {
printf("%d\t", top->data);
top = top->next;
}
printf("\n/**********栈内元素**********/\n");
}
//计算链栈元素个数
int getcount(linkstack s) {
int count = 0;
stacknode* top = s;
while (top != NULL) {
top = top->next;
count++;
}
return count;
}
void main() {
linkstack s;
init(&s);
//printf("%\n", isempty(s));
push(&s, 1);
push(&s, 2);
push(&s, 3);
push(&s, 9);
output(s);
elemtype e = getpop(s);
printf("top=%d,len=%d\n", getpop(s), getcount(s));
while (!isempty(s)) {
elemtype e;
pop(&s, &e);
printf("%d\t", e);
}
printf("\n");
output(s);
}
运行结果: