C语言—栈
栈的操作:进栈和出栈
#include "stdafx.h"
#include "stack.h"
#define maxsize 20
typedef int Elemtype; /*顺序存储—数组形式*/
struct stack
{
int data[maxsize];
int top;
}; void init(struct stack *ps)
{
ps->top = -;
} void init2(struct Doublestack * ps)
{
ps->top1 = -;
ps->top2 =maxsize;
} void print(int a[],int len)
{
int i;
// len = sizeof(a) / sizeof(a[0]);这样子你得不到长度的,此时a退化为了指针
for (i = ; i <= len - ; i++)
{
printf("%d\n", a[i]);
}
}
int push(struct stack * s, int e)
{
if (s->top == maxsize - )
return -;
s->data[++s->top] = e;
return ;
} Elemtype pop(struct stack *s)
{
int n;
if (s->top == )
return -;
n = s->data[s->top--]; return n;
} //两栈共享
struct Doublestack
{
int data[maxsize];
int top1;
int top2;
}; int push_two(struct Doublestack *s, int e, int stackflag)
{
if (s->top1 == s->top2 - ) //full stack
return -;
if (stackflag == )
{
s->data[++s->top1] = e;
}
else if (stackflag == )
{
s->data[--s->top2] = e;
}
return ;
}
Elemtype pop_two(struct Doublestack *s, int stackflag)
{
int n;
if (stackflag == )
{
if (s->top1 == -) //s1 empty
return -;
n = s->data[s->top1--];
}
else if (stackflag == )
{
if (s->top2 ==maxsize) //s2 empty
return -;
n = s->data[s->top2++];
}
//printf("%d\n", n);
return n;
}
主函数
#include "stdafx.h"
#include "link.h"
#include "stack.h" int main()
{ /*stack_test*/
//struct stack s;
//struct stack *ps = &s;
//init(ps); struct Doublestack s;
struct Doublestack * ps = &s;
init2(ps);
int e = ;
int n;
//push( ps, e);
//push(ps, 2);
//print(ps->data,2);
//n = pop(ps);
//printf("%d\n", n); push_two(ps, e,);
push_two(ps, , );
print(ps->data, );
n=pop_two(ps,);
printf("n =%d\n", n); system("pause"); }
C语言—栈的更多相关文章
- C语言栈与调用惯例
C语言栈与调用惯例 1.前言 最近在再看<程序员的自我修养>这本书,对程序的链接.装载与库有了更深入的认识.关于这本书的评价可以去豆瓣看看http://book.douban.com/su ...
- C语言栈调用机制初探
学习linux离不开c语言,也离不开汇编,二者之间的相互调用在源代码中几乎随处可见.所以必须清楚地理解c语言背后的汇编结果才能更好地读懂linux中相关的代码.否则会有很多疑惑,比如在head.s中会 ...
- C语言 栈 链式结构 实现
一个C语言链式结构实现的栈 mStack (GCC编译). /** * @brief C语言实现的链式结构类型的栈 * @author wid * @date 2013-10-30 * * @note ...
- C语言 栈 顺序结构 实现
一个能够自动扩容的顺序结构的栈 ArrStack 实例 (GCC编译). /** * @brief C语言实现的顺序结构类型的栈 * @author wid * @date 2013-10-29 * ...
- [数据结构]C语言栈的实现
有始有终,所以我准备把各种数据结构都讲一次,栈也分顺序存储和链式储存,这里我们选择链式存储来讲,顺序存储没有难度(链式其实也是) 作为数据结构中最简单的栈,这里不会说太多,首先考虑一下下面的model ...
- C语言栈的实现
栈是常用的数据结构之一,下面给出一个链式栈的实现~~头文件Stack.h #ifndef Stack_H #define Stack_H typedef int Item; typedef struc ...
- c语言栈的链表实现
#include <stdio.h> #include <stdlib.h> #include"PublicDS.h" typedef int ElemTy ...
- C语言 - 栈和单链表的实现
单链表:linkList.h linkList.c #ifndef LINKLIST_H_INCLUDE #define LINKLIST_H_INCLUDE #include <Windows ...
- Go语言栈定义及相关方法实现
// stack 栈 package Algorithm import ( "errors" "reflect" ) // 栈定义 type Stack str ...
随机推荐
- 每天to do list
至少写一页书 写代码做一个实验 读10+页专业书 一年时间,如果经济状况没有改善的话,回归企业.
- java 进销存 库存管理 销售报表 商户管理 springmvc SSM crm 项目
系统介绍: 1.系统采用主流的 SSM 框架 jsp JSTL bootstrap html5 (PC浏览器使用) 2.springmvc +spring4.3.7+ mybaits3.3 SSM ...
- JOB SERVER 负载均衡
JOB SERVER 负载均衡 一.体系结构 1.job server group job server group 是由一个或者多个job server 组成的,做为一个整体对外提供服务,在内部实现 ...
- md5,md2加密加盐
数组是没有重写object的toString()方法.byte[].toString()
- The Bitizens Team
bitizens.bitguild.com 首个区块链3D艺术品. https://mybitizens.bitguild.com/#/igo https://www.youtube.com/watc ...
- SpringAOP的自定义注解实践
springaop属于spring的重要属性,在java中有相当广泛的用途,大家一般都接触过aop实现事务的管理,在xml里配好声明式事务,然后直接在service上直接加上相应注解即可, 今天我们来 ...
- Golang 对接宝付、通联、富友金账户...填坑记
一.宝付私钥加密,公钥解密 由于对RSA加密解密原理不是很熟悉,宝付也没有Golang的Demo提供.Go语言库里一般都是私钥解密.公钥加密,或者私钥签名.公钥验签.宝付需要反过来,这里也到好找到了h ...
- 消除运行MATLAB生成独立可执行程序的DOS黑屏
基于Matlab生成独立可执行文件后,每次运行都存在DOS黑屏问题,可通过以下方法解决: 在Matlab命令窗口中输入: cd(prefdir) edit compopts.bat 在打开的文件最后添 ...
- style.attr,currentStyle,getComputedStyle获取元素css
老朋友object.style.attr 平常我们都是使用object.style.attr的方式来获取元素的属性, 但是这种方法具有很大的局限性——只能获取内联样式, 而下面的两种方法可以获取到元素 ...
- 20155211 2016-2017-2《Java程序设计》课程总结
20155211 2016-2017-2<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:对师生关系的理解 预备作业2:熟能生巧及学习c语言的心的 预备作业3:关于假期 ...