链式栈

来源:互联网 发布:化妆品成分查询软件 编辑:程序博客网 时间:2024/05/01 05:51
#include <stdio.h>#include <stdlib.h>#include <stdbool.h>typedef int datatype;typedef struct node{datatype data;struct node *next;}stacknode, *stack_link;stack_link init_stack(void){return NULL;}bool is_empty(stack_link s){return s == NULL;}void push(stack_link *ps, datatype x){stack_link new = (stack_link)malloc(sizeof(stacknode));new->data = x;new->next = *ps;*ps = new;}void pop(stack_link *ps, datatype *loc){if(is_empty(*ps))return;*loc = (*ps)->data;stack_link q = *ps;*ps = (*ps)->next;free(q);}int main(void){stack_link s;s = init_stack();printf("please input the date to convert to octal\n");int n;scanf("%d", &n);int  flag=0;if(n<0){flag=1;n=-n;}int tmp = n, mod;while(tmp != 0){mod = tmp % 8;push(&s, mod);tmp /= 8;}int x;if(flag == 1){printf("(%d)10 = (-0", n);}elseprintf("(%d)10 = (0", n);while(!is_empty(s)){pop(&s, &x);printf("%d", x);}printf(")8\n");return 0;}