链栈——c

// 链栈.cpp: 定义控制台应用程序的入口点。
#include "stdafx.h"
#include "stdio.h"
#include "malloc.h"

typedef struct  linkzhan {
	int data;
	struct linkzhan *next;
}LinkZhan;

//初始化
LinkZhan *Init() {
	LinkZhan * top;
	top = (LinkZhan *)malloc(sizeof(linkzhan));
	top->next = NULL;
	return top;
}

//判断栈为空
int Kong(LinkZhan *top) {
	if (top->next == NULL)printf("栈为空\n");
	else printf("栈不为空\n");
	return 1;
}

//入栈
int Posh(LinkZhan *top,int x) {
	LinkZhan *p;
	p = (LinkZhan*)malloc(sizeof(linkzhan));
	p->data = x;
	p->next = top->next;
	top->next = p;
	return 1;
}

//出栈
int Pop(LinkZhan *top) {
	printf("出栈元素为:%3d\n", top->next->data);
	top->next = top->next->next;
	return 1;
}

//遍历栈
int Prints(LinkZhan *top) {
	LinkZhan *p;
	p = top->next;
	for (; p != NULL;) {
		printf("元素为  %3d\n", p->data);
		p = p->next;
	}
	return 1;
}

//取栈顶元素
int Pops(LinkZhan *top) {
	int t = top->next->data;
	top->next = top->next->next;
	return t;
}
//进制转化
void JinZhiZhuanHuan(int aaa) {
	LinkZhan *top1;
	top1 = Init();
	int t = aaa % 16;
	int i = 0;
	while (aaa) {
		t = aaa % 16;
		Posh(top1, t);
		aaa /= 16;
		i++;
	}
	int s;
	printf("进制转换后的数字为:");
	while (i) {
		s = Pops(top1);
		if (s < 10) {
			printf("%d", s);
		}else {
			printf("%c", 'A'+s-10);
		}
		i--;
	}

}

int main()
{
	//初始化栈
	LinkZhan *top;
	top = Init();
	//入栈前判断栈是否为空
	Kong(top);
	//入栈
	printf("开始入栈\n\n");
	Posh(top, 10); Posh(top, 20);
	Posh(top, 30); Posh(top, 40);
	//入栈后判断栈是否为空
	Kong(top);
	//遍历栈
	Prints(top);
	//出栈
	printf("开始出栈\n\n");
	Pop(top); Pop(top);
	Pop(top); Pop(top);
	//出栈结束判断栈是否为空
	Kong(top);

	//开始进制转换
	printf("请输入需要转化的数字\n");
	int aaa;
	scanf_s("%d", &aaa);
	JinZhiZhuanHuan(aaa);
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值