1、初始化
2、new一个新节点
3、判断是否空栈
4、压栈
5、出栈
代码:
#include<stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
typedef struct node
{
int data;
struct node *next;
}NODE,*PNODE;
typedef struct link_stack
{
struct node *ptop;
struct node *btom;
}STA,*PSTA;
PSTA init_link_stack(void)
{
PSTA p = malloc(sizeof(STA));
if(p != NULL)
{
p->ptop = NULL;
p->btom = NULL;
}
return p;
}
PNODE make_node(int data)
{
PNODE p;
p = malloc(sizeof(NODE));
if(p != NULL)
{
p->next = NULL;
p->data = data;
}
return p;
}
bool is_empty(PSTA p)
{
if(p->ptop == p->btom)
{
return true;
}
else
return false;
}
bool pop(PSTA p,int *n)
{
if(is_empty§)
{
return false;
}
PNODE tmp = p->ptop;
p->ptop = p->ptop->next;
*n = tmp->data;
free(tmp);
return true;
}
bool push(PSTA p,PNODE new)
{
if(p == NULL || new == NULL)
{
return false;
}
new->next = p->ptop;
p->ptop = new;
return true;
}
bool show_list_stack(PSTA p)
{
if(is_empty§)
{
return false;
}
while(p->ptop != p->btom)
{
PNODE t = p->ptop;
printf("%d",t->data);
p->ptop = p->ptop->next;
}
printf("\n");
return true;
}
int main(void)
{
int a;
PSTA test;
PNODE new;
test = init_link_stack();
printf("请输入数据:");
scanf("%d",&a);
while(a > 0)
{
new = make_node(a%8);
push(test,new);
a = a/8;
}
printf("转化为8进制数为:");
show_list_stack(test);
return 0;
}