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 ...
随机推荐
- Mysql查询正在运行的事务
查询 正在执行的事务:SELECT * FROM information_schema.INNODB_TRX 根据这个事务的线程ID(trx_mysql_thread_id): 可以使用mysql命令 ...
- 为什么我们要做三份 Webpack 配置文件
时至今日,Webpack 已经成为前端工程必备的基础工具之一,不仅被广泛用于前端工程发布前的打包,还在开发中担当本地前端资源服务器(assets server).模块热更新(hot module re ...
- Web—10-前端性能优化
前端性能优化 从用户访问资源到资源完整的展现在用户面前的过程中,通过技术手段和优化策略,缩短每个步骤的处理时间从而提升整个资源的访问和呈现速度.网站的性能直接会影响到用户的数量,所有前端性能优化很重要 ...
- thinkpad T61 Fn功能键的完整说明及有关问题解决
FN键的作用Fn 键是一个组合键,它自己本身没有用,需要和其他功能键组合起来以迅速改变操作特征,它可以组合的键在键盘上都用蓝色标注,具体作用为: Fn+F2:锁定计算机显示器.要再次打开计算机显示器, ...
- CentOS 7紧急救援模式修改root用户密码的方法
最近无聊在网上搜索linux系统root用户密码破解方法,看来很多朋友的博文,同时也试了一下,但是感觉他们写的还是不是很清晰.简洁,因此自己就心血来潮写了这篇博文,提供一个比较清晰的思路给新手,如果有 ...
- 日期插件rolldate.js的使用
日期插件rolldate.js的使用 下载地址:http://www.jq22.com/jquery-info19834 效果: 代码: <!DOCTYPE html> <html ...
- 学习html的第一天
HTML 第一章: webde 运行环境: 1. 什么是HTML HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言:HyperText Markup Languge 超文本 就是网 ...
- nRF52832 BLE_DFU空中升级OTA(三)准备升级工程(SDK14.2.0)
准备需要加入DFU功能的工程 在工程main文件services_init函数中加入DFU服务 uint32_t err_code; // Initialize the async SVCI inte ...
- 用函数指针将字符串 ”hello world“ 输出为 "world hello”
/************************************************************************* > File Name: 指针数组2.c & ...
- 2017-2018-1 20155318《信息安全技术》实验二——Windows口令破解
2017-2018-1 20155318<信息安全技术>实验二--Windows口令破解 一.实验原理 口令破解方法 口令破解主要有两种方法:字典破解和暴力破解. 字典破解是指通过破解者对 ...