C语言-栈的实现

stack.h

#ifndef STACK_H
#define STACK_H
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>


#define data_t uint32_t
typedef struct Node{
	data_t data;
	struct Node* next;
}Node;
typedef struct stack{
	Node* top;
	uint32_t count;
}link_stack;
link_stack* init_link_stack();
Node* init_node();
link_stack* push_stack(link_stack* p,data_t data);
_Bool isEmpty(link_stack* p);
void disp_stack(link_stack* p);
link_stack* pop_stack(link_stack *p);
void clear_stack(link_stack* p);
#endif

stack.c 

#include "stack.h"
// 初始化栈
link_stack*  init_link_stack(){
	link_stack *p=calloc(1,sizeof(link_stack));
	if(p==NULL){
	printf("初始化栈失败!\n");
	exit(0);
	}
	return p;
}
// 初始化结点
Node* init_node(){
	Node* p=calloc(1,sizeof(Node));
	if(p==NULL){
		printf("初始化结点失败!\n");
		exit(0);
	}
	return p;
}
//入栈push
link_stack* push_stack(link_stack* p,data_t data){
	if(isEmpty(p)){
		return NULL;
	}
	Node* temp=init_node();
	temp->data=data;
	temp->next=p->top;
	p->top=temp;
	p->count++;
	return p;
}
// 出栈pop
link_stack* pop_stack(link_stack* p){
	if(isEmpty(p)){
		return NULL;
	}
	if(p->count==0){
		return NULL;
	}
	Node* temp=p->top;
	p->top=p->top->next;
	p->count--;
	free(temp);
	temp==NULL;
	return p;
}
// 遍历
void disp_stack(link_stack *p){
	if(isEmpty(p)){
		return;
	}
	if(p->count==0){
		printf("栈为空!\n");
		return ;
	}
	Node* temp=p->top;
		printf("```````````\n");
	while(temp!=NULL){
		printf("%d\n",temp->data);
		temp=temp->next;
	}
		printf("```````````\n");
}
void clear_stack(link_stack* p){
	if(isEmpty(p)){
		return;
	}
	Node* temp=p->top;
	while(temp!=NULL){
		Node* node=temp;
		temp=temp->next;
		free(node);
		node=NULL;
	}
	free(p);
	p=NULL;
}
// 判断栈是否为空
_Bool isEmpty(link_stack* p){
	if(p==NULL){
		return 1;
	}
	return 0;
}

main.c 

#include "stack.h"

void select_menu();
void menu();

link_stack* p;
link_stack* temp;
int judge=1;
void main(){
	while(judge){
		menu();
		select_menu();
	}
}
void select_menu(){
	printf("请选择:");
	int select=0;
	scanf("%d",&select);
	switch(select){
		case 0:
			 judge=0;
			 clear_stack(p);
			 break;	
		case 1:
			 p=init_link_stack();
			 if(p!=NULL){
				printf("初始化成功!\n");
			 }
			 break;
		case 2:
			 if(isEmpty(p)){
				printf("请先初始化!\n");
			 }else{
				data_t data=0;
				printf("data=");
				scanf("%d",&data);
				temp=push_stack(p,data);
				if(temp!=NULL){
					printf("添加成功! \n");
					disp_stack(p);
				}
			 }
			 break;
		case 3:
			 if(isEmpty(p)){
			 	printf("请先初始化!\n");
			 }else{
				if(pop_stack(p)==NULL){
					printf("栈内无元素!\n");
				}else{
					printf("pop成功!\n");
					disp_stack(p);
				}
			 }
			 break;
		case 4:
			 disp_stack(p);
			 break;
		default:
			printf("输入错误!");
	}
}
void menu(){
	printf("--------------\n");
	printf("0:退出\n");
	printf("1:初始化stack\n");
	printf("2:push\n");
	printf("3:pop \n");
	printf("4:遍历\n");
	printf("--------------\n");
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是一种后进先出(Last In First Out,LIFO)的数据结构,可以用来实现很多算法,其中一个就是的应用:十进制转八进制。 十进制转八进制的原理是将一个十进制数不断除以八,直到商为0,然后将每次的余数倒序排列起来即可得到八进制数。我们可以使用实现这个过程。 具体的算法步骤如下: 1. 初始化一个,用来存放余数。 2. 对于给定的十进制数n,不断进行以下操作,直到商为0: 1) 将n除以8,将余数压入中。 2) 将n更新为商。 3. 从中依次弹出余数,得到的结果即为八进制数。 下面是使用C语言实现这个算法的代码示例: ```c #include <stdio.h> #define MAX_SIZE 100 int stack[MAX_SIZE]; int top = -1; void push(int x) { if (top == MAX_SIZE - 1) { printf("Stack overflow\n"); return; } stack[++top] = x; } int pop() { if (top == -1) { printf("Stack underflow\n"); return -1; } return stack[top--]; } void decimal_to_octal(int n) { while (n > 0) { push(n % 8); n /= 8; } while (top != -1) { printf("%d", pop()); } } int main() { int n; printf("Enter a decimal number: "); scanf("%d", &n); printf("Octal equivalent: "); decimal_to_octal(n); printf("\n"); return 0; } ``` 在这个程序中,我们使用了一个数组来实现的功能,其中`push`函数用来将元素压入中,`pop`函数用来从中弹出元素。在`decimal_to_octal`函数中,我们使用了来存储余数,然后在弹出的过程中得到八进制数。最后,在`main`函数中我们获取用户输入的十进制数并输出转换后的八进制数。 希望这个算法的详解对你有所帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值