2023年4月3日 星期一 数据结构作业

进制转换链栈实现
在这里插入图片描述
头文件

#ifndef __HEAD_H__
#define __HEAD_H__

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

#define MAXSIZE 10

typedef int datatype;

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

stacklist create_stack();
int stacklist_push(stacklist z,datatype e);
void stacklist_output(stacklist z);
int stacklist_pop(stacklist z);
stacklist free_space(stacklist z);

//**************************链栈
typedef struct Node
{
	union
	{
		int len;
		datatype data;
	};
	struct Node *next;
}*linkstack;


linkstack create_head();
linkstack create_node();
int linkstack_head(linkstack top,datatype e);
void linkstack_output(linkstack top);
int delete_head(linkstack top);
linkstack linkstack_free(linkstack top);

#endif

主函数

#include "head.h"


int main(int argc, const char *argv[])
{
#if 0
	//申请空间
	stacklist z=create_stack();
	//入栈
	char choice[10];
	datatype e;
	while(1){
		printf("输入入栈的值:");
		scanf("%d",&e);
		stacklist_push(z,e);

		printf("要继续吗?  y/n:");
		scanf("%s",choice);
		if(strcmp(choice,"n")==0){
			break;
		}
	}
	//输出
	stacklist_output(z);
	//出栈
	stacklist_pop(z);
	stacklist_output(z);
	//释放
	z=free_space(z);
#endif
	//************************链栈
	//创建头结点
	linkstack top=create_head();
	//头插
	int n;
	datatype e;
	printf("输入要转成二进制的数:");
	scanf("%d",&n);
	while(n>0){
		e=n%2;
		linkstack_head(top,e);
		n=n/2;
	}
	//输出
	linkstack_output(top);
	//头删
//	delete_head(top);
//	printf("头删后的结果为:");
//	linkstack_output(top);
	//释放
	top=linkstack_free(top);
	return 0;
}

自定义函数

#include "head.h"

//申请空间
stacklist create_stack()
{
	stacklist z=(stacklist)malloc(sizeof(struct Stack));
	if(z==NULL)
		return NULL;
	z->top=-1;
	return z;
}

//入栈
int stacklist_push(stacklist z,datatype e)
{
	if(z==NULL||z->top==MAXSIZE-1){
		printf("插入失败\n");
		return -1;
	}
	z->top++;
	z->data[z->top]=e;
	return 0;
}

//输出
void stacklist_output(stacklist z)
{
	if(z==NULL||z->top==-1){
		printf("栈为空\n");
		return;
	}
	printf("正向:");
	for(int i=0;i<=z->top;i++){
		printf("%d  ",z->data[i]);
	}
	printf("\n反向:");
	for(int i=z->top;i>=0;i--){
		printf("%d  ",z->data[i]);
	}
	printf("\n");
}

//出栈
int stacklist_pop(stacklist z)
{
	if(z==NULL||z->top==-1){
		printf("删除失败\n");
		return -1;
	}
	printf("出栈的值为:%d\n",z->data[z->top]);
	z->top--;
	return 0;
}

stacklist free_space(stacklist z)
{
	if(z==NULL)
		return NULL;
	free(z);
	z=NULL;
	return z;
}

//******************************链栈
//创建头结点
linkstack create_head()
{
	linkstack top=(linkstack)malloc(sizeof(struct Node));
	if(top==NULL)
		return NULL;
	top->len=0;
	top->next=NULL;
	return top;
}

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

//链栈头插
int linkstack_head(linkstack top,datatype e)
{
	if(top==NULL){
		printf("插入失败\n");
		return -1;
	}
	linkstack s=create_node();
	if(s==NULL)
		return -1;
	s->data=e;
	s->next=top->next;
	top->next=s;
	top->len++;
	return 0;
}

//输出
void linkstack_output(linkstack top)
{
	if(top==NULL||top->len==0){
		printf("输出失败\n");
		return;
	}
	linkstack p=top;
	while(p->next!=NULL){
		p=p->next;
		printf("%d  ",p->data);
	}
	printf("\n");
}

//头删
int delete_head(linkstack top)
{
	if(top==NULL||top->len==0){
		printf("删除失败\n");
		return -1;
	}
	linkstack q=top->next;
	top->next=q->next;
	free(q);
	q=NULL;
	top->len--;
	return 0;
}

//释放
linkstack linkstack_free(linkstack top)
{
	if(top==NULL)
		return NULL;
	int n=top->len;
	for(int i=0;i<n;i++){
		delete_head(top);
	}
	free(top);
	top=NULL;
	return top;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值