/*
* =====================================================================================
*
* Filename: dynamic_stack.c
*
* Description:
*
* Version: 1.0
* Created: 2011年10月25日 16时53分20秒
* Revision: none
* Compiler: gcc
*
* Author: Wang Ran (), wangran51@126.com
* Company:
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
struct stack_node
{
int data;
struct stack_node *next_ptr;
};
void push(struct stack_node** node, int n);
int pop(struct stack_node** node);
int is_empty(struct stack_node* node);
void print_stack(struct stack_node* node);
void instructions(void);
int main()
{
struct stack_node * stack_ptr = NULL;//在第第一次push的时候NULL会被复制到top_ptr->next_ptr中
//所以这句是必须的
int i, choice, value;
for(i=0; i<5; i++)
{
printf("Enter an interger:");
scanf("%d", &value);
push(&stack_ptr, value);
//print_stack(stack_ptr);
}
for(i=0;i<5;i++)
{
if(!is_empty(stack_ptr))
{
printf("the poped value is %d\n ", pop(&stack_ptr));
}
print_stack(stack_ptr);
}
printf("end of run \n ");
return EXIT_SUCCESS;
}
void instructions(void)
{
printf("Enter choice: \n 1:push\n 2:pop\n 3:end\n ");
}
void push(struct stack_node ** top_ptr, int info)
{
struct stack_node * new_ptr;
new_ptr = malloc(sizeof (struct stack_node));
if(new_ptr != NULL)
{
new_ptr->data = info;
new_ptr->next_ptr = *top_ptr;
*top_ptr = new_ptr;
}
else
{
printf("%d not inserted, No memory available. \n", info);
}
}
int pop(struct stack_node** top_ptr)
{
struct stack_node* temp_ptr;
int pop_value;
temp_ptr = *top_ptr;
pop_value = (*top_ptr)->data;
*top_ptr = (*top_ptr)->next_ptr;
free(temp_ptr);
return pop_value;
}
void print_stack(struct stack_node* current_ptr)
{
if(current_ptr == NULL)
{
printf("the stack is empty.\n");
}
else
{
while(current_ptr != NULL)
{
printf("%d-->", current_ptr->data);
current_ptr = current_ptr->next_ptr;
}
printf("NULL\n ");
}
}
int is_empty(struct stack_node * top_ptr)
{
return top_ptr == NULL;
}
- 浏览: 124872 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (341)
- Java (18)
- J2EE (0)
- Linux (81)
- VIM (22)
- windows (6)
- DB (11)
- Algorithm (57)
- Data structure (17)
- JS (5)
- C++ (65)
- HTML (6)
- Cloud (4)
- Eclipse (7)
- Python (42)
- Play (3)
- HTTP (1)
- awk (7)
- shell (20)
- Regular expression (5)
- NLP (33)
- ML (38)
- DM (43)
- Probabilistic (6)
- Crawler (14)
- matlab (1)
- perl (4)
- Design pattern (1)
- IO[File] (2)
- Deep Learning (1)
发表评论
-
正向插入排序
2011-09-28 21:51 310/* * ========================== ... -
归并排序
2011-10-07 23:22 246/* *========================= ... -
二叉排序树
2011-10-31 15:46 242#include <stdio.h> #inclu ... -
struct in_addr
2011-11-03 17:29 303struct in_addr addr_ip; struct ... -
UNIX创建临时文件
2011-11-04 13:26 289/* * ========================= ... -
UNIX目录扫描
2011-11-04 17:09 307/* * ========================== ... -
迷宫(多出口)
2011-12-10 12:09 299/* * ========================== ... -
8皇后(按列递归)
2011-12-10 17:55 285/* * ========================== ... -
epoll(精髓)
2011-12-23 09:47 246epoll - I/O event notification ... -
Linux Epoll介绍和程序实例
2011-12-23 09:47 2461. Epoll是何方神圣? Epoll可是当前在Linu ... -
Beli Makfile for linux
2012-04-25 13:12 323.SUFFIXES: .c .u CC= gcc # CFLA ... -
gsl eclipse
2012-04-25 14:27 4641 我的gsl安装路径:D:/GnuWin32 1 ... -
Linux下获取毫秒级时间差
2012-08-22 19:46 523Linux下获取毫秒级时间差 使用Linux的ge ... -
CPU Sin
2012-08-22 21:12 3221 #include <iostream> ... -
C++ 补全插件
2012-10-30 20:16 294config vim + clang complete wi ... -
c++ clang_complete
2012-12-17 17:01 306前一段时间发现了clang complete,发现效果 ... -
C++ File IO
2012-12-18 14:50 427在C++中如何实现文件的 ... -
ifstream eof()函数
2012-12-18 17:39 346用ifstream的eof(),竟然 ... -
C++ eof()函数陷阱
2012-12-20 16:49 359C++中用eof()检查是 ... -
C++ Map用法
2012-12-20 17:14 313/*************************** ...
相关推荐
NiosII之动态栈溢出检测功能,中文版的资料 蔡伟刚编写。 密码:NiosII.cublog.cn(区分大小写)
动态创建一个链式栈的过程 因为最近在学数据结构所以把一些小代码都搬上来了。
动态规划:动态规划是一种通过将问题分解成更小的子问题来解决复杂问题的算法。常见的动态规划问题包括背包问题、最长递增子序列、编辑距离等。 贪心算法:贪心算法是一种在每一步选择中都采取当前状态下最优决策的...
c#栈和队列(两种)算法的应用。举一个简单的例子:在往箱子里面放衣物的时候,放在最上面的衣物总是...另外一种方法是用链表实现栈,这种栈叫做动态栈。 栈中通常存放着程序的局部变量等。栈通常有出栈和入栈操作。
栈的动态实现代码
使用动态数组,用户可以自定义栈的空间大小,在VS2017上编译运行
1.栈的建立和输出,入栈出栈。2.链栈的出栈和入栈。3.数组储存栈和结构体储存栈。4.动态栈和静态栈
C和C++动态内存分配如堆,栈,数据段
顺序栈实现括号配对
.3.2 动态栈 3.3 队列 3.4 动态循环队列 3.5 迷宫问题 3.6 表达式求值 3.7 多重栈与多重队列 3.8 补充习题 第4章 链表 4.1 单向链表 4.2 用C语言表示单向链表 4.3 链式栈与链式队列 4.4 多项式 4.5 其它...
栈; 3 栈; 3 栈;2. 栈的顺序存储与运算 ...下图是一个动态栈的变化示意图。;设栈s的初始状态为空,元素a,b,c,d,e,f,g 分别入栈,下列出栈序列不可能的是( ) a,b,c,d,e,f ,g b,c,a,f,e,g,d a,e,d,c,b,f,
Protected成员包括count(所用元素个数)、maxsize(栈所能存最大元素个数)、*p(动态开辟空间的指针)、辅助函数full()(判断栈是否已满)和init()(初始化栈);public成员包括构造函数、析构函数、复制构造函数和=...
数据结构----栈和队列之顺序栈(C语言),完整代码可运行
{ cerr动态存储失败!"; exit(1); //stdlib.h } } /* * 前置条件:栈已存在 * 输 入:元素值x * 功 能:在栈顶插入一个元素x * 输 出:如果插入不成功,抛出异常 * 后置条件:如果插入成功,栈顶增加了一...
建立两个动态栈A,B。A存放计算对象,B存放计算符号。 三、设计要求与内容 ................... 一、设计的内容及要求 编写时钟程序,要求画出时钟面板,时针,分针,秒针,可设定或解除闹钟时间,并提供闹钟服务...
栈在日常开发中有时还是会用到的,比如将递归函数化简为非...c语言标准库是没有栈这种结构提供的,这里提供一个种栈的实现方法,基于数组实现,支持动态增容,其优势是出入栈速度快、内存连续、和链栈一样可以扩容。
使栈能够容纳任意多的元素。 提示:根据数组元素多少来动态调整内部数组的大小。
最新的Ti公司的Zstack协议栈,ZStack-CC2530-2.5.0
用栈实现了迷宫探索,并用mfc实现了动态探索,非常适合mfc初学者和对数据结构栈的应用感兴趣的人
1. 栈的基本概念 2. 栈的顺序存储表示...(1)顺序栈动态分配存储空间方法 和对栈操作设计 (2)顺序栈静态分配存储空间方法 和对栈操作设计 * 3. 栈的链式存储结构——链栈 顺序栈静态分配存储空间方法 和对栈操作设计