栈的连式存储

昨晚写了个栈的链式存储,自己发现了一些问题,在此提出来与大家共勉:

首先上代码:

typedef struct stack{
	int iData;
	stack *next;
}sStack;
定义一个结构体, 存放数据

int initStack(sStack *(&s))
{
	s = (sStack *)malloc(sizeof(sStack));
	s->next = NULL;
	return 0;
}
初始化栈
int inputStack(sStack *(&s), int x, int &change)
{
	if(0 == change)
	{
		change = 1;
		s->iData = x;
		s->next = NULL;
	}
	else
	{
		sStack *temp;
		temp = (sStack *)malloc(sizeof(sStack));
		temp->iData = x;
		temp->next = s;
		s = temp;
		printf("插入成功!");
		system("pause");
	}
	return 0;
}
输入栈元素,这个输入栈还有别的方法,比如:当前开辟了一个空间,就给当前节点赋值,然后节点上移。
int outputStack(sStack *s)
{
	sStack *temp = s;
	while(s != NULL)
	{
		printf("%d ", s->iData);
		s = s->next;
	}
	s = temp;
	printf("\n");
	system("pause");
	return 0;
}

输出栈元素

重要提示:

在调用函数的时候记着要使用 '&' 这个字符串,将 'mian' 函数中的变量使其在外函数中使用,如果直接像

int initStack(sStack *s)
{
      ********
}
调用的输入输出函数将无效。

这个是输出效果:

实现栈的节点移动输出:

typedef struct stack{
	int iData;
	stack *next;
}sStack;
int initStack(sStack *(&s))
{
	s = (sStack *)malloc(sizeof(sStack));
	s->next = NULL;
	return 0;
}
int inputStack(sStack *(&s), int x, int &change)
{
	if(0 == change)
	{
		change = 1;
		s->iData = x;
		s->next = NULL;
	}
	else
	{
		sStack *temp;
		temp = (sStack *)malloc(sizeof(sStack));
		temp->iData = x;
		temp->next = s;
		s = temp;
		printf("插入成功!");
		system("pause");
	}
	return 0;
}
int outputStack(sStack *(&s))
{
	sStack *temp = s;
	if(s != NULL)
	{
		printf("%d ", s->iData);
		s = s->next;
	}
	free(temp);
	printf("\n");
	system("pause");
	return 0;
}


转载请注明链接

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值