用c语言实现stack的初始化,push,pop和Clear操作

#include<stdio.h>
#include<stdlib.h>
#include<assert.h> struct Link
{
int data;
struct Link* Next;
}; struct Stack
{
struct Link* head;
int Size;
}; void StackInit(struct Stack* sta)
{
sta->head = NULL;
sta->Size = ;
} void StackPush(struct Stack* sta, const int data)
{
struct Link* node;
node = (struct Link*)malloc(sizeof(struct Link));
assert(node != NULL);
node->data = data;
node->Next = sta->head;
sta->head = node;
++ sta->Size;
} int StackEmpty(struct Stack* sta)
{
if(sta->Size == )
return ;
return ;
} int StackPop(struct Stack* sta, int* data)
{
if(StackEmpty(sta))
return ;
struct Link* tmp = sta->head;
*data = sta->head->data;
sta->head = tmp->Next;
free(tmp);
-- sta->Size;
return ;
}
void StackClear(struct Stack* sta)
{
struct Link* tmp;
while(sta->head)
{
tmp = sta->head;
sta->head = tmp->Next;
free(tmp);
}
sta->Size = ;
} int main()
{
struct Stack sta;
StackInit(&sta);
int i;
for(i=; i<=; i++)
StackPush(&sta, i);
while(!StackEmpty(&sta))
{
StackPop(&sta, &i);
printf("%d\n", i);
}
return ;
}

  C++实现

#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)
{
tmp = head;
head = head->Next;
delete tmp;
}
}
void Push(const int data)///插入data;
{
Link* node = new Link(data, head);
head = node;
++ Size;
}
bool Empty()
{
return (Size==);
}
bool Pop(int& data)
{
if(Empty())
return false;
Link* tmp = head;
data = head->data;
head = head->Next;
delete tmp;
Size--;
return true;
}
private:
Link* head;
int Size;
}; int main()
{
Stack sta;
int i;
for(i=; i<=; i++)
{
sta.Push(i);
}
while(!sta.Empty())
{
sta.Pop(i);
cout << i << endl;
}
return ;
}

Stack的c实现的更多相关文章

  1. 线性数据结构之栈——Stack

    Linear data structures linear structures can be thought of as having two ends, whose items are order ...

  2. Java 堆内存与栈内存异同(Java Heap Memory vs Stack Memory Difference)

    --reference Java Heap Memory vs Stack Memory Difference 在数据结构中,堆和栈可以说是两种最基础的数据结构,而Java中的栈内存空间和堆内存空间有 ...

  3. [数据结构]——链表(list)、队列(queue)和栈(stack)

    在前面几篇博文中曾经提到链表(list).队列(queue)和(stack),为了更加系统化,这里统一介绍着三种数据结构及相应实现. 1)链表 首先回想一下基本的数据类型,当需要存储多个相同类型的数据 ...

  4. Stack Overflow 排错翻译 - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder

    Stack Overflow 排错翻译  - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder 转自:ht ...

  5. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

  6. Stack操作,栈的操作。

    栈是先进后出,后进先出的操作. 有点类似浏览器返回上一页的操作, public class Stack<E>extends Vector<E> 是vector的子类. 常用方法 ...

  7. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. [LeetCode] Min Stack 最小栈

    Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...

  9. Stack的三种含义

    作者: 阮一峰 日期: 2013年11月29日 学习编程的时候,经常会看到stack这个词,它的中文名字叫做"栈". 理解这个概念,对于理解程序的运行至关重要.容易混淆的是,这个词 ...

  10. Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)

    写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...

随机推荐

  1. 同步异步,阻塞非阻塞 和nginx的IO模型

    同步与异步 同步和异步关注的是消息通信机制 (synchronous communication/ asynchronous communication).所谓同步,就是在发出一个*调用*时,在没有得 ...

  2. C++ explicit关键字详解

    本文系转载,原文链接:http://www.cnblogs.com/ymy124/p/3632634.html 首先, C++中的explicit关键字只能用于修饰只有一个参数的类构造函数, 它的作用 ...

  3. AngularJS Select(选择框)

    AngularJS 可以使用数组或对象创建一个下拉列表选项. 使用 ng-option 创建选择框 在 AngularJS 中我们可以使用 ng-option 指令来创建一个下拉列表,列表项通过对象和 ...

  4. iOS开发的小技巧

    转自简书:http://www.jianshu.com/p/50b63a221f09  http://www.jianshu.com/p/08f194e9904c 原作者:叶孤城___ self.ta ...

  5. 【Java EE 学习 16 下】【dbutils的使用方法】

    一.为什么要使用dbutils 使用dbutils可以极大程度的简化代码书写,使得开发进度更快,效率更高 二.dbutils下载地址 http://commons.apache.org/proper/ ...

  6. Oracle 11g 新特性之Highly Available IP(HAIP)

    Redundant Interconnect with Highly Available IP (HAIP) 简介   从11.2.0.2开始,Oracle 的集群软件Grid Infrastruct ...

  7. mac 下 用 glfw3 搭建opengl开发环境

    mac 下 用 glfw3 搭建opengl开发环境 下载编译 glfw3 Build Setting 里面, Library Search Paths -> 设置好编译 glfw 库的路径 H ...

  8. RobotFramework自动化测试之脚本编写(一)

    接触了上一篇的RF环境搭建及安装,相比大家都会觉得,哇塞,为什么要做这么多,那么复杂?装那么多干什么有什么用?写脚本会不会也很复杂? 其实首次安装的话 会觉得有点蒙,也不知道安装那么多是拿来干什么的, ...

  9. iOS Safari 中click点击事件失效的解决办法

    问题起因: 在微信公众号开发(微站)过程中用jquery的live方法绑定的click事件点击无效(不能执行) 问题描述 当使用委托给一个元素添加click事件时,如果事件是委托到 document  ...

  10. Spatial Transformer Networks(空间变换神经网络)

    Reference:Spatial Transformer Networks [Google.DeepMind]Reference:[Theano源码,基于Lasagne] 闲扯:大数据不如小数据 这 ...