用链表实现栈
来源:互联网 发布:程序员 1000万 编辑:程序博客网 时间:2024/06/03 18:53
#include <stdio.h>
#include <stdlib.h>
/*定义结构体,用户自定义*/
struct node
{
int num;
struct node * next;
};
typedef struct node Node;
typedef struct node * Link;
enum return_result{EMPTY_OK,EMPTY_NO,PUSH_OK,PUSH_NO,POP_OK,POP_NO};
/*分配单元*/
void is_malloc_ok(Link new_node)
{
if(new_node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void create_newnode(Link * top)
{
*top = (Link) malloc(sizeof(Node));
is_malloc_ok(*top);
}
void create_stack(Link *top)
{
*top = (Link) malloc(sizeof(Node));
is_malloc_ok(*top);
}
/*初始化函数*/
void init_stack(Link *top )
{
(*top)->next = NULL;
}
/*判断栈是否为空,由于链表,故不用判断栈满*/
int is_stack_empty(Link top)
{
if(top->next == NULL)
{
return EMPTY_OK;
}else
{
return EMPTY_NO;
}
}
/*入栈操作*/
int push_stack(Link*top,Link new_node,int num)
{
create_newnode(&new_node);
new_node->num=num+1;
new_node->next=((*top)->next);
((*top)->next)=new_node;
return PUSH_OK;
}
/*出栈操作*/
int pop_stack(Link*top)
{
int temp;
if ( is_stack_empty(*top) == EMPTY_OK )
{
printf("the stack is empty!\n");
return POP_NO;
}
else
{
temp = (*top)->next->num;
free((*top)->next);
(*top)->next = (*top)->next->next;
return temp;
}
}
int main()
{
Link top = NULL;//定义栈指针
Link new_node = NULL;//定义新结点
int i;
int temp;
create_stack(&top);
init_stack(&top);
for (i=0;i<10;i++)
{
create_stack(&new_node);
is_malloc_ok(new_node);
if(push_stack(&top,new_node,i) == PUSH_OK);
{
printf("PUSH_OK\n");
}
}
for ( i = 0 ; i <= 10 ; i++ )
{
temp = pop_stack(&top);
if(temp != POP_NO)
{
printf("%d\n",temp);
}
}
return 0;
}
#include <stdlib.h>
/*定义结构体,用户自定义*/
struct node
{
int num;
struct node * next;
};
typedef struct node Node;
typedef struct node * Link;
enum return_result{EMPTY_OK,EMPTY_NO,PUSH_OK,PUSH_NO,POP_OK,POP_NO};
/*分配单元*/
void is_malloc_ok(Link new_node)
{
if(new_node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void create_newnode(Link * top)
{
*top = (Link) malloc(sizeof(Node));
is_malloc_ok(*top);
}
void create_stack(Link *top)
{
*top = (Link) malloc(sizeof(Node));
is_malloc_ok(*top);
}
/*初始化函数*/
void init_stack(Link *top )
{
(*top)->next = NULL;
}
/*判断栈是否为空,由于链表,故不用判断栈满*/
int is_stack_empty(Link top)
{
if(top->next == NULL)
{
return EMPTY_OK;
}else
{
return EMPTY_NO;
}
}
/*入栈操作*/
int push_stack(Link*top,Link new_node,int num)
{
create_newnode(&new_node);
new_node->num=num+1;
new_node->next=((*top)->next);
((*top)->next)=new_node;
return PUSH_OK;
}
/*出栈操作*/
int pop_stack(Link*top)
{
int temp;
if ( is_stack_empty(*top) == EMPTY_OK )
{
printf("the stack is empty!\n");
return POP_NO;
}
else
{
temp = (*top)->next->num;
free((*top)->next);
(*top)->next = (*top)->next->next;
return temp;
}
}
int main()
{
Link top = NULL;//定义栈指针
Link new_node = NULL;//定义新结点
int i;
int temp;
create_stack(&top);
init_stack(&top);
for (i=0;i<10;i++)
{
create_stack(&new_node);
is_malloc_ok(new_node);
if(push_stack(&top,new_node,i) == PUSH_OK);
{
printf("PUSH_OK\n");
}
}
for ( i = 0 ; i <= 10 ; i++ )
{
temp = pop_stack(&top);
if(temp != POP_NO)
{
printf("%d\n",temp);
}
}
return 0;
}
0 0
- 用链表实现栈C++实现源码
- 用链表实现栈
- 用链表实现的栈
- 用链表实现栈
- 用链表实现栈
- 用链表实现栈
- 用链表实现栈
- 用链表实现栈
- 用链表实现栈
- 栈 stack 用链表实现
- 用链表实现栈功能
- 用链表实现栈
- 用链表实现栈
- 用链表实现栈
- 用链表实现栈和队列
- 用链表实现堆栈--链式栈
- php 用链表实现一个栈
- 类中实现简单的栈(用链表结构实现)
- BadTunnel:跨网段劫持广播协议
- Solving “Dynamic Web Module 3.0 requires Java 1.6 or newer” in Maven Projects
- U3d 使用 异步 async await
- BootStrap table 数据填充与分页应用总结
- 436. Find Right Interval
- 用链表实现栈
- 总结与目标2016年11月2日17:27:09
- 递归原理及常见应用
- 一个整型数组中数字出现两次,但是只有2个数字出现一次,求解这两个只出现一次的数的代码
- 遍历Map集合四中方法
- Struts2学习记录1
- 第十一周项目1——二叉树算法验证(3) 中序线索化二叉树的算法验证
- 基于反向投影的肤色学习以及手势模型建立
- [BZOJ2276][Poi2011]Temperature(单调队列)