【DS】About Stack
栈
一言以蔽之,就是后进的先出(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的更多相关文章
- 【LeetCode】栈 stack(共40题)
[20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...
- 【LeetCode】Min Stack 解题报告
[题目] Design a stack that supports push, pop, top, and retrieving the minimum element in constant tim ...
- 【leetcode】Min Stack -- python版
题目描述: Design a stack that supports push, pop, top, and retrieving the minimum element in constant ti ...
- 【leetcode】Min Stack(easy)
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【OS】Heap & Stack
操作系统概念的堆.栈不同于数据结构的堆.栈. C 语言中,一切指针占 4 字节,这意味着指针指向 RAM 中的地址可以有 232 个,最小的地址是 0,最大的地址是 231 - 1. (一)堆: 堆空 ...
- 【LeetCode225】 Implement Stack using Queues★
1.题目 2.思路 3.java代码 import java.util.LinkedList; import java.util.Queue; public class MyStack { priva ...
- 【Leetcode】【Easy】Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- 【DS】排序算法的稳定性
主要的排序算法有八种:直接插入排序,希尔排序(这两种统称为插入排序),冒泡排序,快速排序(这两种统称为交换排序),直接选择排序,堆排序(这两种统称为选择排序),归并排序,基数排序.今天我们就讨论一下它 ...
- 【DS】排序算法之插入排序(Insertion Sort)
一.算法思想 一般来说,插入排序都采用in-place在数组上实现.具体算法描述如下:1)从第一个元素开始,该元素可以认为已经被排序2)取出下一个元素,在已经排序的元素序列中从后向前扫描3)如果该元素 ...
随机推荐
- 【C++】多态性(函数重载与虚函数)
多态性就是同一符号或名字在不同情况下具有不同解释的现象.多态性有两种表现形式: 编译时多态性:同一对象收到相同的消息却产生不同的函数调用,一般通过函数重载来实现,在编译时就实现了绑定,属于静态绑定. ...
- windows10的第一天使用总结
一.快速开机设置 我的电脑配置如图,装有VS2015 2010 OFFICE等常用开发工具,在线升级后开机速度并没有明显提升. 1.保证windows font cache service服务启动,3 ...
- Windows 10 虚拟桌面切换
从Windows 10开始,终于有了和Mac一样的虚拟桌面了.但总感觉用着非常的别扭.在Mac中,切换虚拟桌面的操作可谓方便至极:除了触控板和Magic Mouse原生的支持外,通过罗技M557/55 ...
- 怎么实现ZBrush 4R7中界面视窗的快速转换
本篇教程介绍ZBrush® 4R7界面的基本操作之转换界面视窗, 教程属于入门教程可以帮助新手快速入门.因为ZBrush工 作界面不同于其他我们所熟知的3D软件,初次接触ZBrush的时候难免会有所困 ...
- Struts2中的Ognl
OGNL(Object-Graph Navigation Language)全称为对象图导航语言,是一种功能强大的表达式语言,它通过简单一致的语法,可以任意存取对象的属性或者调用对象的方法,能够遍历整 ...
- Mybatis
Mybatis MyBatis本是apache的一个开源项目iBatis,2010年这个项目有Apache software foundation 迁移到了Google code,并改名MyBatis ...
- PAT 1046. 划拳(15)
划拳是古老中国酒文化的一个有趣的组成部分.酒桌上两人划拳的方法为:每人口中喊出一个数字,同时用手比划出一个数字.如果谁比划出的数字正好等于两人喊出的数字之和,谁就赢了,输家罚一杯酒.两人同赢或两人同输 ...
- PAT 1042. 字符统计(20)
请编写程序,找出一段给定文字中出现最频繁的那个英文字母. 输入格式: 输入在一行中给出一个长度不超过1000的字符串.字符串由ASCII码表中任意可见字符及空格组成,至少包含1个英文字母,以回车结束( ...
- [LeetCode] Guess Number Higher or Lower 猜数字大小
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...
- [LeetCode] Invert Binary Tree 翻转二叉树
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...