栈的操作:进栈和出栈

 #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语言—栈的更多相关文章

  1. C语言栈与调用惯例

    C语言栈与调用惯例 1.前言 最近在再看<程序员的自我修养>这本书,对程序的链接.装载与库有了更深入的认识.关于这本书的评价可以去豆瓣看看http://book.douban.com/su ...

  2. C语言栈调用机制初探

    学习linux离不开c语言,也离不开汇编,二者之间的相互调用在源代码中几乎随处可见.所以必须清楚地理解c语言背后的汇编结果才能更好地读懂linux中相关的代码.否则会有很多疑惑,比如在head.s中会 ...

  3. C语言 栈 链式结构 实现

    一个C语言链式结构实现的栈 mStack (GCC编译). /** * @brief C语言实现的链式结构类型的栈 * @author wid * @date 2013-10-30 * * @note ...

  4. C语言 栈 顺序结构 实现

    一个能够自动扩容的顺序结构的栈 ArrStack 实例 (GCC编译). /** * @brief C语言实现的顺序结构类型的栈 * @author wid * @date 2013-10-29 * ...

  5. [数据结构]C语言栈的实现

    有始有终,所以我准备把各种数据结构都讲一次,栈也分顺序存储和链式储存,这里我们选择链式存储来讲,顺序存储没有难度(链式其实也是) 作为数据结构中最简单的栈,这里不会说太多,首先考虑一下下面的model ...

  6. C语言栈的实现

    栈是常用的数据结构之一,下面给出一个链式栈的实现~~头文件Stack.h #ifndef Stack_H #define Stack_H typedef int Item; typedef struc ...

  7. c语言栈的链表实现

    #include <stdio.h> #include <stdlib.h> #include"PublicDS.h" typedef int ElemTy ...

  8. C语言 - 栈和单链表的实现

    单链表:linkList.h linkList.c #ifndef LINKLIST_H_INCLUDE #define LINKLIST_H_INCLUDE #include <Windows ...

  9. Go语言栈定义及相关方法实现

    // stack 栈 package Algorithm import ( "errors" "reflect" ) // 栈定义 type Stack str ...

随机推荐

  1. react+webpack搭建项目

    一.环境准备 ①node ②npm 二.开始搭建 ①使用npm安装create-react-app工具,在cmd命令行中输入: npm install -g create-react-app ②使用命 ...

  2. iOS:图片相关(18-02-12更)

    1.图片显示相关 1).图片聊天背景拉伸不失真 2).捏合.双击.下拉缩放 3).Banner.相册 4).动画 2.图片操作相关 1).获取.下载图片(分享.传图片用) 2).保存UIImage到本 ...

  3. [笔记][SQL] 连接join

    在学习菜鸟教程里的MySQL教程时,对左右连接的结果有点不解. 其中有如下两个表: runoob_tbl: +-----------+--------------+---------------+-- ...

  4. Redis的特性以及优势(附官网)

    NoSQL:一类新出现的数据库(not only sql) 泛指非关系型的数据库 不支持SQL语法 存储结构跟传统关系型数据库中的那种关系表完全不同,nosql中存储的数据都是KV形式 NoSQL的世 ...

  5. c++读取ini的Section节名

    // ConsoleApplication1.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include "iostream&q ...

  6. 嵌入式C语言自我修养 06:U-boot镜像自拷贝分析:section属性

    6.1 GNU C 的扩展关键字:attribute GNU C 增加一个 __atttribute__ 关键字用来声明一个函数.变量或类型的特殊属性.声明这个特殊属性有什么用呢?主要用途就是指导编译 ...

  7. 嵌入式nand flash详解

    一.s3c2440启动后会将nand flash的前4K程序复制到内部的sram中,这个过程是硬件自动完成的,但是如果我们的程序远远大于4K,这个时候就需要将程序从flash拷贝到内存中来运行了. 二 ...

  8. RUBY惯用方法(转)

    RUBY惯用方法 目录 迭代 ||=赋值 程序入口 预设变量和特殊记号 inject 并行赋值 *的匹配 rescue简单用法 命名参数的默认值 精细duck typing控制 获取metaclass ...

  9. python教程(三)·函数进阶(上)

    在介绍了函数定义的方法后,再来介绍一些进阶知识 参数收集 有时候我们需要参数的数量是任意的,比如print函数的参数的数量是任意的,print函数的内部实现我们不探究,但是单单是参数数量可变这一方面实 ...

  10. linux内存碎片的概念

    一般在内核术语中的“碎片”都是基于物理内存而言的,我没有太看懂你得出碎片是针对地址空间这个结论的逻辑.但我认为,既然你知道了malloc是用户空间调用的,那么你所谓的碎片也是从用户空间的视角而言的,但 ...