(C)链栈的基本操作

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stack_data{
	int num;
	char name[20];
};
struct stack_node{
	int num;
	char name[20];
	struct stack_node *next;
}; 
typedef struct stack_node *Stack;
typedef struct stack_data Stack_data;

enum  res_val{malloc_no=100,malloc_ok,create_stack_ok,create_stack_no,push_ok,push_no,pop_ok,pop_no,
				empty_ok,empty_no
};

int create_node(Stack *new_node)
{
	*new_node = (Stack)malloc(sizeof(struct stack_node));
	if(*new_node == NULL)
	{
		return malloc_no;
	}
	return malloc_ok;
}
int create_stack(Stack *stack)
{
	
	if(create_node(stack) == malloc_ok)
	{
		(*stack)->next = NULL;
		
		return create_stack_ok;
		 
	}
	else
	{	
		return create_stack_no;
			
	}
 } 
int push(Stack stack,Stack_data node_data)
{
	Stack new_node = NULL;
	
	if(create_node(&new_node) == malloc_ok)
	{
		new_node->num=node_data.num;
		strcpy(new_node->name,node_data.name);
		
		new_node->next=stack->next;
		stack->next = new_node;
		return push_ok;
		 
	}
	else
	{
		
			return push_no;
			
	}
}
int is_empty(Stack stack)
{
	if(stack->next==NULL)
	{
		printf("the stack is empty!\n");
		return empty_ok;
	}
	else
	{
		return empty_no;
	}
}
int pop_stack(Stack stack,Stack_data  node_data)
{
	Stack p=stack->next;
	if(is_empty(stack) == empty_ok)
	{
		return pop_no;
	}
	else
	{
		node_data.num=p->num;
		strcpy(node_data.name,p->name);
		stack->next=p->next;
		free(p);
		p=NULL;
		return pop_ok; 
		
	}
}
int main()
{
	Stack stack=NULL;
	Stack_data node_data;
	if(create_stack(&stack)==create_stack_ok)
	{
		printf("create stack success\n");
		
	}
	else
	{
		printf("create stack fail");
	}
	
	node_data.num=1;
	strcpy(node_data.name,"werqwr");
	push(stack,node_data);

	
	return 0;
}#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stack_data{
	int num;
	char name[20];
};
struct stack_node{
	int num;
	char name[20];
	struct stack_node *next;
}; 
typedef struct stack_node *Stack;
typedef struct stack_data Stack_data;

enum  res_val{malloc_no=100,malloc_ok,create_stack_ok,create_stack_no,push_ok,push_no,pop_ok,pop_no,
				empty_ok,empty_no
};

int create_node(Stack *new_node)
{
	*new_node = (Stack)malloc(sizeof(struct stack_node));
	if(*new_node == NULL)
	{
		return malloc_no;
	}
	return malloc_ok;
}
int create_stack(Stack *stack)
{
	
	if(create_node(stack) == malloc_ok)
	{
		(*stack)->next = NULL;
		
		return create_stack_ok;
		 
	}
	else
	{	
		return create_stack_no;
			
	}
 } 
int push(Stack stack,Stack_data node_data)
{
	Stack new_node = NULL;
	
	if(create_node(&new_node) == malloc_ok)
	{
		new_node->num=node_data.num;
		strcpy(new_node->name,node_data.name);
		
		new_node->next=stack->next;
		stack->next = new_node;
		return push_ok;
		 
	}
	else
	{
		
			return push_no;
			
	}
}
int is_empty(Stack stack)
{
	if(stack->next==NULL)
	{
		printf("the stack is empty!\n");
		return empty_ok;
	}
	else
	{
		return empty_no;
	}
}
int pop_stack(Stack stack,Stack_data  node_data)
{
	Stack p=stack->next;
	if(is_empty(stack) == empty_ok)
	{
		return pop_no;
	}
	else
	{
		node_data.num=p->num;
		strcpy(node_data.name,p->name);
		stack->next=p->next;
		free(p);
		p=NULL;
		return pop_ok; 
		
	}
}
int main()
{
	Stack stack=NULL;
	Stack_data node_data;
	if(create_stack(&stack)==create_stack_ok)
	{
		printf("create stack success\n");
		
	}
	else
	{
		printf("create stack fail");
	}
	
	node_data.num=1;
	strcpy(node_data.name,"werqwr");
	push(stack,node_data);

	
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值