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 ...
随机推荐
- code#5 P2 棋子
棋子 时间限制: 1.0 秒 空间限制: 512 MB 相关文件: 题目目录 题目描述 棋盘从左到右被分割成 n(n≤1000) 个格子,从左到右编号为1,2,...,n.棋盘上有 m(m≤n) ...
- WebGl 利用缓冲区对象画多个点
效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- 请问在一个命令上加什么参数可以实现下面命令的内容在同一行输出。 echo "zhaokang";echo "zhaokang"
请问在一个命令上加什么参数可以实现下面命令的内容在同一行输出. echo "zhaokang";echo "zhaokang" [root@zhaokang t ...
- js之冒泡排序与快速排序
//冒泡排序 let arr = [1, 6, 3, 7, 5, 9, 2, 8]; function sort(arr) { //升序 console.time("冒泡排序耗时" ...
- 一张图一个表——CSS选择器总结
CSS选择器总结: (这些表是一张图片^_^) 看底部 完整思维导图图片和表格的下载地址:https://download.csdn.net/download/denlnyyr/10597820 (我 ...
- MySQL5.5安装(Windows版本)
1. 官网下载mysql5.5 下载地址:http://dev.mysql.com/downloads/mysql/5.5.html#downloads 2. 安装mysql5.5(安装之前,请关闭杀 ...
- 技巧-如何通过hive开发平台上传csv文件
通过数据交换平台上传较大的文件时,经常会出现导入失败情况,换种方式通过新数据开发平台(stark)也可以轻松实现外部数据与hive的数据关联. --第一步.导入csv文件到hive --stark数据 ...
- python财经数据接口包Tushare pro的入门及简单使用方式(大数据,股票数据接口)
最近在做一个项目,需要用到股票的数据,我在网上查了很久,最终发现在股票数据上面还是tushare比较专业,而且对于将来做金融行业的大数据这一块的,tushare绝对是你的一个好帮手,所以下面我就简单介 ...
- Navicat 报错1251连接不成功Mysql
使用Navicat 连接数据库时候出现1251错误,解决方法. 1.首先打开mysql.exe,然后输入密码(mysql.exe可以在安装的位置搜索一下) 2.输入ALTER USER 'root'@ ...
- 学习笔记二:异步FIFO
, ) //用格雷码的局限性:循环计数深度必须是2的n次幂,否则就失去了每次只变化一位的特性 (wclk,wrstn,wdata,wfull,winc,rclk,rrstn,rdata,rempt ...