stack.c

来自「自己做的常用库和实现的数据结构。public domain.」· C语言 代码 · 共 57 行

C
57
字号
/* Demo of using simplest stack data sturcture, coverting numbers from Dec to Oct. * * Written by Cyril Hu (cyrilhu@gmail.com), public domain. */#include<stdio.h>#include<stdlib.h>struct a {	int x;	struct a *next;} *top;int stack_empty(struct a *p){	return p ? 0 : 1;}struct a *push(int i){	struct a *p;	static int cnt=0;	p = malloc(sizeof(struct a));	p->x = i;	if(cnt++ == 0) 		p->next = NULL;	p->next = top;	top = p;	return top;}struct a *pop(struct a *p){	struct a *t;	t = p->next;	free(p);	return t;}int main(void){	int n;	puts("Input a integer number");	scanf("%d", &n);	while(n) {		top = push(n%8);		n /= 8;	}	while(!stack_empty(top)) {		printf("%d", top->x);		top = pop(top);	}	puts("");	return EXIT_SUCCESS;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?