顺序栈、链栈

作业:进制转换

 主函数:

#include "head.h"

/*    顺序栈
int main(int argc, const char *argv[])
{
	stack s=create_stack();
	datatype e;
	char choise[10];
	while(1)
	{
		printf("请输入入栈元素:");
		scanf("%d",&e);
		stack_push(s,e);
		printf("是否继续?(y/n):");
		scanf("%s",choise);
		if(strcmp(choise,"n")==0)
			break;
	}
	output_stack(s);
	pop_stack(s);
	pop_stack(s);
*/

int main(int argc, const char *argv[])
{

	 
	linkstack top=create_head();
	datatype e;
	char choise[10];

/*	    链栈
	while(1)
	{
		printf("请输入入栈元素:");
		scanf("%d",&e);
		push_linkstack(top,e);
		printf("是否继续?(y/n):");
		scanf("%s",choise);
		if(strcmp(choise,"n")==0)
			break;
	}
	pop_linkstack(top);
	printf("遍历\n");
	output_linkstack(top);
	top=free_linkstack(top);


*/    进制转换
	int num,j;
	printf("请输入一个数:");
	scanf("%d",&num);
	printf("将这个数转为几进制:");
	scanf("%d",&j);
	while(num)
	{
		e=num%j;
		push_linkstack(top,e);
		num/=j;
	}
	output_linkstack(top);




	return 0;
}

 头文件:

#ifndef __HEAD_H__
#define __HEAD_H__


#include <stdio.h>
#include <string.h>
#include <stdlib.h>



#define MAXSIZE 10
typedef int datatype;
/*

typedef struct Stack
{
	int top;
	datatype data[MAXSIZE];
}*stack;

//建栈
stack create_stack();

//入栈
int stack_push(stack s,datatype e);

//遍历输出
int output_stack(stack s);

//出栈
int pop_stack(stack s);


*/

typedef struct Linkstack
{
	union 
	{
		int len;
		datatype data;
	};
	struct Linkstack *next;

}*linkstack;


//链栈的申请
linkstack create_head();

//创建普通结点
linkstack create_node();

//入栈
int push_linkstack(linkstack top,datatype e);

//出栈
int pop_linkstack(linkstack top);

//栈的遍历
int output_linkstack(linkstack top);

//链栈的释放
linkstack free_linkstack(linkstack top);

#endif

 自定义函数:

#include "head.h"
/*
//建栈
stack create_stack()
{
	stack s=(stack)malloc(sizeof(struct Stack)*MAXSIZE);
	if(s==NULL)
		return NULL;
	s->top=-1;
	return s;
}

//入栈
int stack_push(stack s,datatype e)
{
	if(s==NULL)
	{
		printf("入栈失败\n");
		return -1;
	}
	s->data[++s->top]=e;
	return 0;
}

//遍历输出
int output_stack(stack s)
{
	if(s==NULL || s->top==-1)
	{
		printf("输出失败\n");
		return -1;
	}
	for(int i=s->top;i<=0;i--)
	{
		printf("%d\t",s->data[i]);
	}
	puts("");
}

//出栈
int pop_stack(stack s)
{
	if(s==NULL || s->top==-1)
	{
		printf("出栈失败\n");
		return -1;
	}
	printf("出栈元素:%d\n",s->data[s->top--]);
}
*/

//-------------------------------

//链栈的申请
linkstack create_head()
{
	linkstack top=(linkstack)malloc(sizeof(struct Linkstack));
	if(top==NULL)
		return NULL;
	top->len=0;
	top->next=NULL;
	return top;
}

//创建普通结点
linkstack create_node()
{
	linkstack p=(linkstack)malloc(sizeof(struct Linkstack));
	if(p==NULL)
		return NULL;
	p->data=0;
	p->next=NULL;
	return p;
}

//入栈
int push_linkstack(linkstack top,datatype e)
{
	if(top==NULL)
	{
		printf("入栈失败\n");
		return -1;
	}
	linkstack p=create_node();
	if(p==NULL)
		return-1;
	p->data=e;
	p->next=top->next;
	top->next=p;
	top->len++;
	return 0;
}
//出栈
int pop_linkstack(linkstack top)
{
	if(top==NULL || top->len==0)
		return-1;
	linkstack p=top->next;
	top->next=p->next;
	top->len--;
	printf("出栈元素:%d\n",p->data);
	free(p);
	p=NULL;
}
//栈的遍历
int output_linkstack(linkstack top)
{
	if(top==NULL || top->len==0)
		return -1;
	linkstack p=top->next;
	while(p!=NULL)
	{
		printf("%d\t",p->data);
		p=p->next;
	}
	puts("");
}
//链栈的释放
linkstack free_linkstack(linkstack top)
{
	if(top==NULL)
		return NULL;
	for(int i=top->len;i>0;i--)
		pop_linkstack(top);
	free(top);
	top=NULL;
	return top;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值