代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
栈和队列简单应用
作者:
尹凌风
/ 发布于
2014/6/9
/
694
把栈中的2n个元素,从栈顶到栈底:a2n,a2n-1,.....a1,变成a2n,a2n-2,...,a2,a2n-1,....,a1
#include <iostream> using namespace std; template <class DataType> struct Node { DataType data; Node<DataType> * next; }; template <class DataType> class LinkQueue { public: LinkQueue(); void EnQueue(DataType X); DataType DeQueue(); DataType GetQueue(); int Empty() { if(front == rear && flag == 1) return 1; if(front == rear && flag == 0) return 0; } private: Node<DataType> * front, *rear; int flag; }; template <class DataType> class LinkStack { public: LinkStack() { top = NULL; } void Push(DataType x); DataType pop(); DataType GetTop() { if (top != NULL) return top -> data; } int Empty() { if(top == NULL) return 1; else return 0; } private: Node<DataType> * top; }; template <class DataType> void LinkStack<DataType>::Push(DataType x) //进栈 { Node<DataType> *s; s = new Node<DataType>; s -> data = x; s -> next = top; top = s; } template <class DataType> DataType LinkStack<DataType>::pop() //出栈 { int x; Node<DataType> *p; if(top == NULL) throw "下益"; x = top -> data; p = top; top = top -> next; delete p; return x; } template <class DataType> LinkQueue<DataType>::LinkQueue() //队列初始化 { Node<DataType> *s; s = new Node<DataType>; s -> next = front; front = rear = s; flag = 0; }; template <class DataType> void LinkQueue<DataType>::EnQueue(DataType x) //入队列 { if (front == rear && flag == 1) throw "队满"; Node<DataType> *s; s = new Node<DataType>; s -> data = x; s -> next = front; rear -> next = s; rear = s; if (front == rear) flag = 1; } template <class DataType> DataType LinkQueue<DataType>::DeQueue() //出队列 { if (front == rear && flag == 0) throw "队空"; Node<DataType> *p; int x = 0; p = front -> next; x = p -> data; front -> next = p -> next; if (front == rear) flag = 0; return x; } float * Onetwo(int n) //按1,2报口号,口号为1的输出口号为2的入队的最终出队顺序 { float a[100] = {0},b[100] = {0}; int i, m, j = 0; float *t; LinkQueue<int> Grade2; cout << "输入你的各科目成绩" << endl; for (i=0; i<n; i++) { cin >> a[i]; } cout << "入队列" << endl; for (i=0; i<n; i++) { Grade2.EnQueue(a[i]); } m = n; for (i=1;i<m; i++) { if (i%2 == 0) { Grade2.EnQueue(Grade2.DeQueue()); //序列号是2时重新入队 m++; } else //序列号是1时输出 { b[j++] = Grade2.DeQueue(); } while (m-i == 1) //队列只有一个元素时直接输出 { b[j++] = Grade2.DeQueue(); break; } } cout << "按1,2报口号,口号为1的输出口号为2的入队的最终出队顺序" << endl; for (i=0; i<n; i++) { cout << b[i] << endl; } t = b; return t; } void Deviled(int n) //把从栈顶到栈底数据为a2n~a1的数据变为a2n,a2n-2,a2n-4,....a2n-1,...a1 { int i, p, j = 0; float b[100] = {0}; LinkStack<int> Grade; LinkQueue<int> Grade1; float *t; t = Onetwo(n); //调用Onetwo函数 cout << "把从栈顶到栈底数据为a2n~a1的数据变为a2n,a2n-2,a2n-4,....a2n-1,...a1" << endl; cout << "算法描述如下:" << endl; cout << "执行入栈操作:" << endl; for (i=0; i<n; i++) { b[i] = *(t+i); Grade.Push(b[i]); } cout << "从栈顶到栈底元素为: " << endl; for (i=n-1; i>=0; i--) { cout << b[i] << endl; } cout << "进行出栈和入队操作:" << endl; for (i=0; i<n; i++) { Grade1.EnQueue(Grade.pop()); } cout << "进行出队偶数入队奇数入栈操作:" << endl; { for (i=n; i>0; i--) { p = Grade1.DeQueue(); if(i % 2 == 0) Grade1.EnQueue(p); else Grade.Push(p); } } cout << "进行奇数出栈和入队操作:" << endl; for (i=1; i<=n/2; i++) { Grade1.EnQueue(Grade.pop()); } cout << "进行偶数出队和入栈操作:" << endl; for (i=1; i<=n/2; i++) { Grade.Push(Grade1.DeQueue()); } cout << "进行偶数出栈和入队操作:" << endl; for (i=1; i<=n/2; i++) { Grade1.EnQueue(Grade.pop()); } cout << "进行出队和入栈操作:" << endl; for (i=1; i<=n; i++) { Grade.Push(Grade1.DeQueue()); } cout << "执行出栈操作:" << endl; for (i=0; i<n; i++) { p = Grade.pop(); cout << p << endl; } cout << "出栈操作完成。" << endl; cout << "释放空间完成。" << endl; } int main() { int n; cout << "请输元素个数:" << endl; cin >> n; Deviled(n); //调用Deviled()函数; return 0; }
试试其它关键字
栈和队列
栈
队列
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
尹凌风
贡献的其它代码
(
1
)
.
栈和队列简单应用
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3