#include <stdio.h>
#include <stdlib.h>
#include <assert.h> struct LinkNode
{
int data;
struct LinkNode* next;
}; struct Stack
{
struct LinkNode* head;
int size;
}; void StackInit(struct Stack* stack)
{
stack->head = NULL;
stack->size = ;
} void StackPush(struct Stack* stack, const int num)
{
struct LinkNode* node = (struct LinkNode*)malloc(sizeof(struct LinkNode));
assert(node != NULL);
node->data = num;
node->next = stack->head;
stack->head = node;
++stack->size;
return;
} int StackEmpty(struct Stack* stack)
{
return (stack->size == );
} int StackPop(struct Stack* stack, int* num)
{
if (StackEmpty(stack) == )
return ; *num = stack->head->data;
struct LinkNode* tmp;
tmp = stack->head;
stack->head = stack->head->next;
--stack->size;
free(tmp);
return ;
} void StackCleanup(struct Stack* stack)
{
struct LinkNode* tmp;
while (stack->head != NULL)
{
tmp = stack->head;
stack->head = stack->head->next;
free(tmp);
} stack->size = ;
return;
} int main(void)
{
struct Stack stack;
StackInit(&stack);
int i;
for (i = ; i < ; i++)
{
StackPush(&stack, i);
} while (!StackEmpty(&stack))
{
StackPop(&stack, &i);
printf("%d ", i);
} printf("\n");
return ;
}
#include <iostream>
using namespace std; class Stack
{
struct Link
{
int data_;
Link* next_;
Link(int data, Link* next) :data_(data), next_(next)
{ }
}; public:
Stack() :head_(), size_()
{ } ~Stack()
{
Link* tmp;
while (head_ != NULL)
{
tmp = head_;
head_ = head_->next_;
delete tmp;
} return;
} void Push(const int num)
{
Link* node = new Link(num, head_);
head_ = node;
++size_;
return;
} bool Empty()
{
return (size_ == );
} bool Pop(int& num)
{
if (Empty() == )
return false; num = head_->data_;
Link* tmp = head_;
head_ = head_->next_;
--size_;
delete tmp;
return true;
} private:
Link *head_;
int size_;
}; int main(void)
{
Stack stack;
int i;
for (i = ; i < ; i++)
{
stack.Push(i);
} while (!stack.Empty())
{
stack.Pop(i);
cout<<i<<" ";
} cout<<endl;
return ;
}

用c和c++的方式实现栈的更多相关文章

  1. Java三种方式实现栈和队列

    栈:LIFO(后进先出) 队列:FIFO(先进先出) 1.栈:LIFO(后进先出) 1.1.栈的顺序存储结构实现: /** * 基于数组实现的顺序栈 * @param <E> */ pub ...

  2. ARM的栈指令

    ARM的指令系统中关于栈指令的内容比较容易引起迷惑,这是因为准确描述一个栈的特点需要两个参数: 栈地址的增长方向:ARM将向高地址增长的栈称为递增栈(Descendent Stack),将向低地址增长 ...

  3. C语言中的栈和堆

    原文出处<http://blog.csdn.net/xiayufeng520/article/details/45956305#t0> 栈内存由编译器分配和释放,堆内存由程序分配和释放. ...

  4. 【译】.NET中六个重要的概念:栈、堆、值类型、引用类型、装箱和拆箱

    为何要翻译 一来是为了感受国外优秀技术社区知名博主的高质量文章,二来是为了复习对.NET技术的基础拾遗达到温故知新的效果,最后也是为了锻炼一下自己的英文读写能力.因为是首次翻译英文文章(哎,原谅我这个 ...

  5. javascript数据结构与算法---栈

    javascript数据结构与算法---栈 在上一遍博客介绍了下列表,列表是最简单的一种结构,但是如果要处理一些比较复杂的结构,列表显得太简陋了,所以我们需要某种和列表类似但是更复杂的数据结构---栈 ...

  6. .NET中六个重要的概念:栈、堆、值类型、引用类型、装箱和拆箱 (转)

    作者: Edison Chou  来源: 博客园  发布时间: 2014-09-03 15:59  阅读: 318 次  推荐: 2   原文链接   [收藏]   原文作者:Shivprasad k ...

  7. .NET中的六个重要概念:栈、堆、值类型、引用类型、装箱和拆箱

    为何要翻译 一来是为了感受国外优秀技术社区知名博主的高质量文章,二来是为了复习对.NET技术的基础拾遗达到温故知新的效果,最后也是为了锻炼一下自己的英文读写能力.因为是首次翻译英文文章(哎,原谅我这个 ...

  8. 栈的C++实现(数组)——创建-push-pop-top-清空栈-处理栈

    今天学习了利用数组方式的栈的C++实现,这种方式跟指针实现有很多不一样的地方: 栈的指针实现,栈的创建申请头结点,push需要申请新的结点,pop释放结点,这些结点都放在第一个位置,top时,S-&g ...

  9. 栈的图文解析 和 对应3种语言的实现(C/C++/Java)

    概要 本章会先对栈的原理进行介绍,然后分别通过C/C++/Java三种语言来演示栈的实现示例.注意:本文所说的栈是数据结构中的栈,而不是内存模型中栈.内容包括:1. 栈的介绍2. 栈的C实现3. 栈的 ...

随机推荐

  1. oracle修改字段长度

    alter table 表名 modify (字段名 字段类型长度);alter table cachemsg modify (callernum varchar(40));

  2. C#中如何定义全局变量及在各窗体中使用全局变量

    using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; us ...

  3. scrollbar_test

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  4. 【Python】Celery异步处理

    参考:http://www.cnblogs.com/znicy/p/5626040.html 参考:http://www.weiguda.com/blog/73/ 参考:http://blog.csd ...

  5. swift 多线程及GCD

    1.基本概念 1)进程: 进程是指在系统中正在运行的一个应用程序.每个进程之间是独立的,每个进程运行在其专用且受保护的内存空间里.某进程内的线程在其它进程不可见 2)线程: 1个进程要执行任务,必须有 ...

  6. "System Protection" is disabled in Win10 default settings

    We could find some important clue in Restore Point because "System Protection" of volume C ...

  7. Android IOS WebRTC 音视频开发总结(八十六)-- WebRTC中RTP/RTCP协议实现分析

    本文主要介绍WebRTC中的RTP/RTCP协议,作者:weizhenwei ,文章最早发表在编风网,微信ID:befoio 支持原创,转载必须注明出处,欢迎关注我的微信公众号blacker(微信ID ...

  8. EBS中配置OAF

    配置EBS的OAF作者 redqq 16:09 | 静态链接网址 | 最新回复 (0) | 引用 (0) | ERP学习 载jdev 9.03.5带Oracle Applications Extens ...

  9. 解决openssl: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory错误

    问题 在Centos7上编译安装openssl后,运行openssl version出现如下错误: openssl: error while loading shared libraries: lib ...

  10. android subclipse subversive

    subclipse - http://subclipse.tigris.org/update_1.10.x android 开源框架 直接拿来用!最火的Android开源项目整理 http://blo ...