更多资料请点击:我的目录
本篇仅用于记录自己所学知识及应用,代码仍可优化,仅供参考,如果发现有错误的地方,尽管留言于我,谢谢!
运行结果:
顺序栈版头文件:
#ifndef SEQSTACK_H //顺序栈头文件
#define SEQSTACK_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// 顺序栈存储的元素类型,默认是int
#ifndef SEQSTACK_NODE
#define SEQSTACK_NODE int
#endif
typedef SEQSTACK_NODE datatype;
// 顺序栈的管理结构体
typedef struct seqstack
{
int size;
int top;
datatype *data;
}stack;
static stack * init_stack(int size)
{
// 申请栈的管理结构体
stack * s = malloc(sizeof(stack));
if(s != NULL)
{
s->size = size;
s->top = -1;
s->data = calloc(size, sizeof(datatype));//给数据与申请内存
if(s->data == NULL)
{
free(s);
return NULL;
}
}
return s;
}
// 判断是否栈空
static bool empty(stack *s)
{
return s->top == -1;
}
// 判断是否栈满
static bool full(stack *s)
{
return s->top == s->size-1;
}
// 入栈
static bool push(stack *s, datatype data)
{
// 栈已满,无法置入
if(full(s))
{
return false;
}
else
{
s->data[++s->top] = data;//指向s栈顶 +1的栈(新栈顶)的值
return true;
}
}
// 出栈
static bool pop(stack *s)
{
if(empty(s))
{
return false;
}
else
{
s->top--;
return true;
}
}
// 取栈顶元素
static bool top(stack *s, datatype *pdata)
{
if(empty(s))
{
return false;
}
else
{
*pdata = s->data[s->top];//指向s栈顶元素
return true;
}
}
#endif
main函数:
#include <stdio.h>
#define SEQSTACK_NODE char
#include "seqstack.h"
int main()
{
stack *s = init_stack(10);
printf("请输入一个十进制的整数:");
int x, y; char n;
scanf("%d", &x);
y = x;
while(x > 0)
{
push(s , x%8);
x /= 8 ;
}
printf("其八进制数为: 0");
while(!empty(s))
{
top(s,&n);
printf("%d",n);
pop(s);
}
printf("\n");
while(!empty(s))
{
pop(s);
}
char arr[17] = "0123456789ABCDEF";
while(y > 0)
{
push(s, arr[y%16]);
y /= 16;
}
printf("其十六进制数为:0x");
while(!empty(s))
{
top(s,&n);
printf("%c",n);
pop(s);
}
printf("\n");
return 0;
}
链式栈版头文件:
#ifndef LINKSTACK_H
#define LINKSTACK_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// 链式栈存储的元素类型,默认是int
#ifndef LINKSTACK_NODE
#define LINKSTACK_NODE int
#endif
typedef LINKSTACK_NODE datatype;
// 链式栈数据节点的类型
struct node
{
datatype data;
struct node *next;
};
// 链式栈的管理结构体
typedef struct
{
struct node *top;// 指向链式栈的顶部元素,链栈为空时,top指向NULL
int size; //链式栈的大小
}linkstack;
// 初始化空栈
static linkstack * init_stack()
{
linkstack * s = malloc(sizeof(linkstack));
if(s != NULL)
{
s->top = NULL;
s->size = 0;
}
return s;
}
// 判断栈是否为空
static bool empty(linkstack *s)
{
return s->size == 0;
}
// 入栈
static bool push(linkstack *s, datatype data)
{
struct node *new = malloc(sizeof(struct node));
if(new != NULL)
{
new->data = data;
new->next = NULL;
}
else
{
return false;
}
new->next = s->top;//节点new指向栈顶
s->top = new;//新栈顶为new
s->size++;//栈大小+1
return true;
}
// 出栈(即删除栈顶元素),并释放节点
static void pop(linkstack *s)
{
if(empty(s))
{
return;
}
else
{
struct node *p = s->top;//节点指向栈顶
s->top = s->top->next;//将栈顶指向栈顶的下一位(新栈顶)
s->size--;//栈大小-1
p->next = NULL;//节点p指向空
free(p);//释放p
}
}
// 取栈顶元素
static struct node *top(linkstack *s)
{
if(empty(s))
{
return NULL;
}
else
{
return s->top;
}
}
#endif
main函数:
#include <stdio.h>
#define SEQSTACK_NODE char
#include "linkstack.h"
int main()
{
// 初始化一个空栈
linkstack *s = init_stack();
printf("请输入一个十进制的整数:");
// 将短除法中的余数,逐个入栈
int n;
scanf("%d", &n);
while(n > 0)
{
int tmp = n%16;
switch(tmp)
{
case 0 ... 9:
push(s, tmp+'0');
break;
case 10 ... 15:
push(s, tmp+'A'-10);
break;
}
n /= 16;
}
// 将栈中的元素,逐个出栈,得到结果
char a;
printf("其十六进制数为:0x");
while(!empty(s))
{
// 取栈顶
struct node *tmp = top(s);
printf("%c", tmp->data);
// 删除栈顶
pop(s);
}
printf("\n");
return 0;
}
更多资料请点击:我的目录