链式栈 (实现进制转换)

<span style="font-family:Microsoft YaHei;font-size:12px;">定义</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">#define  datatype  int
typedef struct stacknode
{
	int num;
	datatype data;
	struct stacknode *pNext;
}StackNode;
StackNode * init(StackNode * phead);//初始化
StackNode * push(StackNode * phead, int num, datatype data);//进栈
StackNode * pop(StackNode * phead, StackNode * poutdata);//出栈
StackNode * freeall(StackNode * phead);//清空
StackNode * printall(StackNode * phead);//打印</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">--------------------------------------------------------------</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">声明</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">#include"stacklinknode.h"
#include<stdio.h>
#include<stdlib.h>

StackNode * init(StackNode * phead)//初始化
{
	return NULL;
}
StackNode * push(StackNode * phead, int num, datatype data)//进栈
{
	StackNode *pnewnode = (StackNode *)malloc(sizeof(StackNode));//创建节点   类型转换(StackNode *) 明确解析方式
	pnewnode->num = num;
	pnewnode->data = data;
	pnewnode->pNext = NULL;//开辟节点并赋值
	if (NULL == phead)//空链表,直接连接上
	{
		phead = pnewnode;//连接一个节点
	}
	else
	{
		StackNode *p = phead;
		while (p->pNext!=NULL)
		{
			p = p->pNext;//一直向前
		}
		p->pNext = pnewnode;//插入
	}
	return  phead;//返回头结点
}
//显示节点数据
StackNode * printall(StackNode * phead)
{
	if (NULL==phead)
	{
		return NULL;
	}
	else
	{
		printf("%d   %d   %p   %p\n", phead->num, phead->data,phead,phead->pNext);
		printall(phead->pNext);//打印
	}
}
//出栈
StackNode * pop(StackNode * phead, StackNode * poutdata)
{
	if (NULL==phead)
	{
		return  NULL;//已经没有元素
	}
	else  if (NULL==phead->pNext)
	{
		poutdata->num = phead->num;
		poutdata->data = phead->data;//取出数据
		free(phead);//释放内存
		phead = NULL;//只有一个节点
		return phead;
	}
	else
	{
		StackNode *p = phead;
		while (p->pNext->pNext!=NULL)
		{
			p = p->pNext;//循环到倒数第二个节点
		}
		poutdata->num = p->pNext->num;
		poutdata->data = p->pNext->data;//取出数据
		free(p->pNext);//释放
		p->pNext = NULL;
		return phead;
	}
}
//删除所有节点
StackNode * freeall(StackNode * phead)
{
	if (NULL == phead)
	{
		return NULL;
	}
	else
	{
		StackNode *p1=NULL, *p2=NULL;
		p1 = phead;//头结点
		while (p1->pNext != NULL)
		{
			p2 = p1->pNext;//保存下一个节点
			p1->pNext = p2->pNext;
			free(p2);
		}
		free(phead);
		return NULL;
	}
}</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">-------------------------------------------------------</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">测试</span>
<span style="font-family:Microsoft YaHei;font-size:12px;">#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include"stacklinknode.h"

//链式链表  实现十进制数转为二进制数
void  main()
{
	printf("请输入数据:");
	int num;
	scanf("%d", &num);
	printf("num=%d\n", num);//打印数据
	StackNode *phead = NULL;//创建一个链式栈的头结点
	while (num)
	{
		printf("%d", num % 2);
		phead = push(phead, num%2, 0);//压入数据
		num /= 2;
	}
	printf("\n");

	while (phead != NULL)
	{
		StackNode *pout = (StackNode *)malloc(sizeof(StackNode));
		phead = pop(phead, pout);
		printf("%d", pout->num);//出栈
	}
	system("pause");
}


//测试   链式栈  先进后出
void main1()
{
	StackNode *phead=NULL;//创建一个链式栈的头结点
	phead = init(phead);//设置栈为空
	//插入节点
	phead = push(phead, 1, 1);
	phead = push(phead, 2, 11);
	phead = push(phead, 3, 111);
	phead = push(phead, 4, 1111);
	phead = push(phead, 5, 11111);

	printall(phead);

 	while (phead!=NULL)
 	{
 		//保存出栈的数据
 		printf("出栈\n");
 		StackNode *pout =(StackNode *) malloc(sizeof(StackNode));
 		phead = pop(phead, pout);
 		printf("出栈之后\n");
 		printall(phead);
 		printf("\n出栈之后的数据%d  %d", pout->num, pout->data);
 	}

	printall(phead);
	phead = freeall(phead);
	printf("\n释放以后\n");
	system("pause");
}</span>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SteveRocket

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值