一言以蔽之,就是后进的先出(LIFO)。

C语言实现代码:

#include<stdio.h>
#include<stdlib.h> typedef struct Stack
{
/*Stack has three properties.*/
int capacity; //the maximum number of elements stack can hold
int size; //current size of stack
int *elements; //array of elements
}Stack; Stack *createStack(int maxElements)
{
/*Create a Stack.*/
Stack *S;
S=(Stack*)malloc(sizeof(Stack));
/*Initialize its properties*/
S->elements=(int*)malloc(sizeof(int)*maxElements);
S->size=;
S->capacity=maxElements;
/*Return the pointer*/
return S;
} void pop(Stack *S)
{
/*We cannot pop if its size is zero(as it's empty)*/
if(S->size==)
{
printf("Stack is Empty\n");
return;
}
/*Removing an element is equivalent to reducing its size by one!!!*/
else
{
S->size--;
}
return;
} int top(Stack *S)
{
if(S->size==)
{
printf("Stack is Empty\n");
exit();
}
/*Return the topmost element*/
return S->elements[S->size-];
} void push(Stack *S,int element)
{
int *newbase;
/*If the Stack is full,extend it*/
if(S->size>=S->capacity)
{
printf("Stack is Full\n");
newbase=(int*)realloc(S->elements,(S->size+)*sizeof(int));
S->elements=newbase;
S->capacity++;
}
else
{
/*Push the new element on the top of the Stack and increase its size*/
S->elements[S->size++]=element;
}
return;
} int main()
{
int i;
int e;
int n=random()%; //set n as the capacity
printf("next!\n");
Stack *S=createStack(n);
for(i=;i<n;i++)
{
e=random()%;
push(S,e);
printf("%d ",e);
}
printf("\nTop element is %d\n",top(S));
pop(S);
printf("After the first pop,top element is:%d\n",top(S));
pop(S);
printf("After the second pop,top element is:%d\n",top(S));
pop(S);
printf("After the third pop,top element is:%d\n",top(S));
}
  • 关于指针函数啊结构体什么的,一直以来有些迷糊。当由结构体的指针变量只想起成员时,用‘->’,而当表示结构体类型变量的成员,用“.”。以上我用的是Stack *S,故全程使用"->"指向其成员。
  • 最后的测试函数使用random()函数提供数据。例如,random()%20用于获取20以内的数字。

下面一个小例子咯,数制转化,因为最后要把求得的余数按照反序输出得结果,所以可以应用到栈后进先出的特性:

//将上面的测试主函数替换为下面一个主函数加一个转换函数

void convert(int n)
{
Stack *S=createStack();
int e;
while(n!=)
{
push(S,n%);
n/=;
}
while(S->size!=)
{
pop(S);
printf("%d",S->elements[S->size]);
}
} int main()
{
convert();
}
  • 即将十进制1348转换为8进制,结果为2504。

以上为数据结构考前预习系列。唔知能不能预习完嘞。

【DS】About Stack的更多相关文章

  1. 【LeetCode】栈 stack(共40题)

    [20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...

  2. 【LeetCode】Min Stack 解题报告

    [题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...

  3. 【leetcode】Min Stack -- python版

    题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...

  4. 【leetcode】Min Stack(easy)

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

  5. 【OS】Heap & Stack

    操作系统概念的堆.栈不同于数据结构的堆.栈. C 语言中,一切指针占 4 字节,这意味着指针指向 RAM 中的地址可以有 232 个,最小的地址是 0,最大的地址是 231 - 1. (一)堆: 堆空 ...

  6. 【LeetCode225】 Implement Stack using Queues★

    1.题目 2.思路 3.java代码 import java.util.LinkedList; import java.util.Queue; public class MyStack { priva ...

  7. 【Leetcode】【Easy】Min Stack

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

  8. 【DS】排序算法的稳定性

    主要的排序算法有八种:直接插入排序,希尔排序(这两种统称为插入排序),冒泡排序,快速排序(这两种统称为交换排序),直接选择排序,堆排序(这两种统称为选择排序),归并排序,基数排序.今天我们就讨论一下它 ...

  9. 【DS】排序算法之插入排序(Insertion Sort)

    一.算法思想 一般来说,插入排序都采用in-place在数组上实现.具体算法描述如下:1)从第一个元素开始,该元素可以认为已经被排序2)取出下一个元素,在已经排序的元素序列中从后向前扫描3)如果该元素 ...

随机推荐

  1. hibernate学习-HibernateDemo

    上篇文章我们讲述了eclipse安装hibernate插件的过程,这篇文章我们来做第一个HibernateDemo. 1).hibernate的jar开发包的下载,官网下载地址:http://hibe ...

  2. SCNU 2015ACM新生赛初赛【1006. 3D打印】解题报告

            题目链接详见SCNU 2015新生网络赛 1006. 3D打印 .出题思路来自codevs 3288. 积木大赛,属于模拟题.         首先我们把“选择从第L部分到第R部分”理 ...

  3. AC日记——砍树 codevs 1388

    1388 砍树  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 伐木工人米尔科需要砍倒M米长的木 ...

  4. 00 Cadence学习总目录

    这个系列是我学习于博士CADENCE视频教程60讲时,一边学一边记的笔记.使用的CADENCE16.6. 01-03课 了解软件 创建工程 创建元件库 分裂元件的制作方法 04课 正确使用hetero ...

  5. [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串

    Given a string S, find the length of the longest substring T that contains at most two distinct char ...

  6. 使用WCF传输DataTable:DataTable和Xml格式的字符串相互转换(C#)

    背景:项目中要用到客户端向服务端传数据,使用WCF,绑定webHttpBinding,做了一个小例子. 业务逻辑简介:客户端在a表中添加了几条数据,从SQL Server数据库直接取出新添加的数据(D ...

  7. 2016第三届C++大会参会感悟(上)

    继05年第一届C++大会,09年第二届,2016年10月28日-29日,在上海举行第三届C++大会.讲师主要有C++之父 / Bjarne Stroustrup,前Facebook研究科学家 / An ...

  8. 文件上传之——用SWF插件实现文件异步上传和头像截取

    之前写过几篇文件上传,那些都不错.今天小编带领大家体会一种新的上传方法,及使用Flash插件实现文件上传. 使用Flash的好处就是可以解决浏览器兼容性问题.之前我写的一个快捷复制功能也是利用的Fla ...

  9. AngularJS Scope(作用域)

    1. AngularJS Scope(作用域) Scope(作用域) 是应用在 HTML (视图) 和 JavaScript (控制器)之间的纽带. Scope 是一个对象,有可用的方法和属性. Sc ...

  10. 【转】Windows平台下的Subversion安装配置新手指南

    原文地址:http://developer.51cto.com/art/201005/199628.htm 本文介绍Subversion安装配置快速指南,首先讲Subversion的安装和配置,Uni ...