数据结构实验之栈与队列八:栈的基本操作

Time Limit: 1000 ms Memory Limit: 65536 KiB

Problem Description

堆栈是一种基本的数据结构。堆栈具有两种基本操作方式,push 和 pop。push一个值会将其压入栈顶,而 pop 则会将栈顶的值弹出。现在我们就来验证一下堆栈的使用。

Input

首先输入整数t(1 <= t <= 10),代表测试的组数,以后是 t 组输入。

对于每组测试数据,第一行输入两个正整数 m(1 <= m <= 100)、n(1 <= n <= 1000),其中m代表当前栈的最大长度,n代表本组测试下面要输入的操作数。 而后的 n 行,每行的第一个字符可能是'P’或者'O’或者'A’;如果是'P’,后面还会跟着一个整数,表示把这个数据压入堆栈;如果是'O’,表示栈顶元素出栈;如果是'A',表示询问当前栈顶的值'。

Output

对于每组测试数据,根据其中的命令字符来处理堆栈;

(1)对所有的'P'操作,如果栈满输出'F',否则完成压栈操作;

(2)对所有的'A'操作,如果栈空,则输出'E',否则输出当时栈顶的值;

(3)对所有的'O'操作,如果栈空,则输出'E',否则输出栈顶元素的值,并让其出栈;

每个输出占据一行,每组测试数据(最后一组除外)完成后,输出一个空行。

Sample Input

2

5 10

A

P 9

A

P 6

P 3

P 10

P 8

A

P 2

O

2 5

P 1

P 3

O

P 5

A

Sample Output

E

9

8

F

8

3

5

Hint

建议: 用串的方式(%s)读入操作字符。

栈的几个基本操作,没有什么好说的,看代码就好,注意输入由于单个字符容易输入错误,所以建议用字符串读入。

注意输出。

#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct node
{
int data;
struct node *next;
}Node; typedef struct stack
{
Node *base,*top;
int len;
}Stack; struct num
{
int data,next;
}s[100050]; Node *newnode()//建立节点
{
Node *t;
t = (Node *)malloc(sizeof(Node));
t->next = NULL;
return t;
}; Stack *Newstack()//建立新栈
{
Stack *t;
t = (Stack *)malloc(sizeof(Stack));
t->top = newnode();
t->base = t->top;
t->len = 0;
return t;
} void push(Stack *t,int x)//入站
{
Node *p = newnode();
p->data = x;
p->next = t->top->next;
t->top->next = p;
t->base = p;
t->len++;
} int top(Stack *t)//询问栈顶元素
{
return t->top->next->data;
} void pop(Stack *t)//出栈
{
Node *p;
p = t->top->next;
t->top->next = t->top->next->next;
free(p);
t->len--;
} int empty(Stack *t)//询问栈是否为空
{
if(t->top->next==NULL)
return 1;
return 0;
} void del(Stack *t)//清空栈
{
while(!empty(t))
pop(t);
} int main()
{
int t,i,m,n,x;
char s[10];
Stack *q;
q = Newstack();
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
while(!empty(q))
pop(q);
for(i=0;i<m;i++)
{
scanf("%s",s);
if(s[0]=='A')
{
if(empty(q))
printf("E\n");
else
printf("%d\n",top(q));
}
else if(s[0]=='P')
{
scanf("%d",&x);
if(q->len==n)
printf("F\n");
else
push(q,x);
}
else if(s[0]=='O')
{
if(empty(q))
printf("E\n");
else
{
printf("%d\n",top(q));
pop(q);
}
}
}
if(t!=0)
printf("\n");
}
return 0;
}

线性表

#include <stdio.h>
#include <stdlib.h>
#include <string.h> typedef struct Static
{
int *top,*base;
int len;
}Static; Static newStatic()
{
Static t;
t.top = (int *)malloc(100050*sizeof(int));
t.base = t.top;
t.len = 0;
return t;
} int top(Static t)
{
return *(t.top-1);
} void pop(Static *t)
{
t->top--;
t->len--;
} void push(Static *t,int x)
{
t->len++;
*(t->top++) = x;
} int empty(Static t)
{
if(t.base==t.top)
return 1;
return 0;
} void clear(Static *t)
{
while(!empty(*t))
pop(t);
t->len = 0;
} int main()
{
Static k;
k = newStatic();
int n,t,m,x;
char s[5];
scanf("%d",&t);
while(t--)
{
if(!empty(k))
clear(&k);
scanf("%d%d",&n,&m);
while(m--)
{
scanf("%s",s);
if(s[0]=='A')
{
if(empty(k))
printf("E\n");
else
printf("%d\n",top(k));
}
else if(s[0]=='P')
{
scanf("%d",&x);
if(k.len==n)
printf("F\n");
else
push(&k,x);
}
else if(s[0]=='O')
{
if(empty(k))
printf("E\n");
else
{
printf("%d\n",top(k));
pop(&k);
}
}
}
printf("\n");
}
return 0;
}

SDUT-3335_数据结构实验之栈与队列八:栈的基本操作的更多相关文章

  1. SDUT 2133 数据结构实验之栈三:后缀式求值

    数据结构实验之栈三:后缀式求值 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 对于一个基于二元运算符的后缀表示式(基本操作数都是 ...

  2. SDUT 3345 数据结构实验之二叉树六:哈夫曼编码

    数据结构实验之二叉树六:哈夫曼编码 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 字符的编 ...

  3. SDUT 3344 数据结构实验之二叉树五:层序遍历

    数据结构实验之二叉树五:层序遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...

  4. SDUT 3401 数据结构实验之排序四:寻找大富翁.!

    数据结构实验之排序四:寻找大富翁 Time Limit: 150MS Memory Limit: 512KB Submit Statistic Problem Description 2015胡润全球 ...

  5. SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历

    数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Descript ...

  6. SDUT 3311 数据结构实验之串三:KMP应用

    数据结构实验之串三:KMP应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 有n个小朋友 ...

  7. SDUT 3346 数据结构实验之二叉树七:叶子问题

    数据结构实验之二叉树七:叶子问题 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 已知一个按 ...

  8. SDUT 3347 数据结构实验之数组三:快速转置

    数据结构实验之数组三:快速转置 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 转置运算是一 ...

  9. SDUT 2772 数据结构实验之串一:KMP简单应用

    数据结构实验之串一:KMP简单应用 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 给定两个 ...

随机推荐

  1. 微信小程序中自定义swiper轮播图面板指示点的样式

    重置样式: .swiper{ width: 100%; height: 240px; margin-bottom: 0.5rem; position:relative; } div.wx-swiper ...

  2. PHP--Smarty的template模式

    function change_year() { var ss = $('#select_year').children('option:selected').val(); $.ajax({ type ...

  3. webapp-viewport 相关知识整理

    我们在开发移动设备的网站时,最常见的的一个动作就是把下面这个东西复制到我们的head标签中: <meta name="viewport" content="widt ...

  4. idea目录结构子目录在父目录后面跟着改成树形结构

    1.点击项目窗口的设置按钮 2.取消Compact Middle Packages选项的对勾即可

  5. Django REST Framework之认证组件

    什么是认证 认证即需要知道是谁在访问服务器,需要有一个合法身份.认证的方式可以有很多种,例如session+cookie.token等,这里以token为例.如果请求中没有token,我们认为这是未登 ...

  6. 2018-2-13-wpf-PreviewTextInput-在鼠标输入获得-_u0003

    title author date CreateTime categories wpf PreviewTextInput 在鼠标输入获得 � lindexi 2018-2-13 17:23:3 +08 ...

  7. CentOS6.5在VMware中安装

    链接:https://pan.baidu.com/s/1ggqmHxh 密码:v04l 1.启动VMware的画面 2.点击File--->New Virtual Machine 创建一台新虚拟 ...

  8. 微信小程序--每个独立的page的page.json只能修改window属性

    app.json 配置项列表 app.json文件用来对微信小程序进行全局配置,决定页面文件的路径.窗口表现.设置网络超时时间.设置多 tab 等. window配置 用于设置小程序的状态栏.导航条. ...

  9. web前端学习(三)css学习笔记部分(7)-- 文字和字体相关样式、盒子相关样式、背景与边框相关样式

    12.  文字和字体相关样式 12.1  CSS3 给文字添加阴影 使用 text-shadow 属性给页面上的文字添加阴影效果,text-shadow 属性是在CSS2中定义的,在 CSS2.1 中 ...

  10. DirectX11笔记(三)--Direct3D初始化代码

    原文:DirectX11笔记(三)--Direct3D初始化代码 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010333737/article ...