数据结构讲题
来源:互联网 发布:sftp自定义端口号 编辑:程序博客网 时间:2024/06/06 23:51
自己封装的栈
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;struct stacks{ struct node { char data; struct node *next; }*head; stacks() { head = (struct node*)malloc(sizeof(struct node)); head->next = NULL; } bool empty() { if ( head->next ) return 0; return 1; } void pop() { struct node *p; p = (struct node*)malloc(sizeof(struct node)); p = head->next; if ( p ) { head->next = p->next; free(p); } } void push(char n) { struct node *p; p = (struct node*)malloc(sizeof(struct node)); p->data = n; p->next = head->next; head->next = p; } char top() { if ( head->next ) { return head->next->data; } return 0; }};char ch[305];stacks s;stacks s1;int main(){ int i; while ( ~scanf ( "%s", ch ) ) { int len = strlen(ch); for ( i = 0;i < len; i++ ) { if ( ch[i] == '#' ) { if ( !s.empty() ) { s.pop(); } } else if ( ch[i] == '@' ) { while ( !s.empty() ) s.pop(); } else s.push(ch[i]); } while ( !s.empty() ) { s1.push(s.top()); s.pop(); } while ( !s1.empty() ) { printf ( "%c", s1.top() ); s1.pop(); } printf ( "\n" ); }}
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;int len = 0;struct stacks{ struct node { int data; struct node *next; }*head; stacks() { head = (struct node*)malloc(sizeof(struct node)); head->next = NULL; } bool empty() { if ( head->next ) return 0; return 1; } void pop() { struct node *p; p = (struct node*)malloc(sizeof(struct node)); p = head->next; if ( p ) { head->next = p->next; len--; free(p); } } void push(int n) { struct node *p; p = (struct node*)malloc(sizeof(struct node)); p->data = n; p->next = head->next; len++; head->next = p; } int top() { if ( head->next ) { return head->next->data; } return 0; } int size() { return len; }};stacks s;void inti(){ while ( !s.empty() ) { s.pop(); }}int main(){ int n, m; int T, i; char ch[12]; scanf ( "%d", &T ); while ( T-- ) { inti(); scanf ( "%d %d", &n, &m ); for ( i = 0; i < m; i++ ) { scanf ( "%s", ch ); if ( strcmp(ch, "A") == 0 ) { if ( s.empty() ) printf ( "E\n" ); else printf ( "%d\n", s.top() ); } else if ( strcmp(ch, "P") == 0 ) { int t; scanf ( "%d", &t ); if ( s.size() == n ) printf ( "F\n" ); else s.push(t); } else if ( strcmp(ch, "O") == 0 ) { if ( s.empty() ) printf ( "E\n" ); else { printf ( "%d\n", s.top() ); s.pop(); } } } if ( T != 0 ) printf ( "\n" ); } return 0;}
代码菜鸟,如有错误,请多包涵!!!
如有帮助记得支持我一下,谢谢!!!
0 0
- 数据结构讲题云集
- 数据结构讲题
- 数据结构 第一讲
- 数据结构第1讲
- 数据结构第一讲
- 学习-数据结构-第1讲
- 第一讲数据结构(2)
- 数据结构第一讲-复杂度问题
- 陈越《数据结构》第一讲 基本概念
- 数据结构 第1讲 基础知识
- 数据结构 第4讲 单链表
- 数据结构 第6讲 链栈
- (笔记)数据结构第一讲-基本概念
- 第十五讲:XML、JSON数据结构解析
- (第1讲)数据结构简介
- 浙江大学公开课-数据结构第一讲笔记
- 陈越《数据结构》第三讲 树(上)
- 数据结构 第2讲 算法复杂性
- CSS3快速上手之3:边框的圆角
- win10 的这一版改动好大
- 安全测试概述
- shell三大文本处理工具grep、sed、awk心得
- 【NOIP2016提高A组五校联考4】label
- 数据结构讲题
- 【NOIP2016提高A组五校联考4】square
- Introduction to Glide, Image Loader Library for Android, recommended by Google
- 机器学习中的相似性度量
- 10.3
- Spark实践-日志查询
- “啊D”注入测试工具
- Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)A. Checking the Calendar
- 3字节起始码和4字节起始码