队列 数据结构作业(二)
来源:互联网 发布:sql replace用法 编辑:程序博客网 时间:2024/05/29 18:33
无聊的作业,既然花时间写了下,就随便贴下。。。
最简单的实现。。。
/**样例输入输出:输入:1.push 2.pop 3.traverse 4.free 5.rebuild -1结束2队列已空,无法pop输入:1.push 2.pop 3.traverse 4.free 5.rebuild -1结束1input (type) a,a==-1 to end23input (type) a,a==-1 to end345input (type) a,a==-1 to end56input (type) a,a==-1 to end34input (type) a,a==-1 to end45input (type) a,a==-1 to end-1输入:1.push 2.pop 3.traverse 4.free 5.rebuild -1结束2pop(23)输入:1.push 2.pop 3.traverse 4.free 5.rebuild -1结束2pop(345)输入:1.push 2.pop 3.traverse 4.free 5.rebuild -1结束3traverse:563445输入:1.push 2.pop 3.traverse 4.free 5.rebuild -1结束4free:输入:1.push 2.pop 3.traverse 4.free 5.rebuild -1结束5rebuild:输入:1.push 2.pop 3.traverse 4.free 5.rebuild -1结束1input (type) a,a==-1 to end23input (type) a,a==-1 to end-1输入:1.push 2.pop 3.traverse 4.free 5.rebuild -1结束-1*/#include<iostream>#include<cstdlib>using namespace std;typedef int type;struct Queue{ type data; Queue *next;};Queue *head; //头节点Queue *cur; //添加时移动void Init(){ head=(Queue *)malloc(sizeof(Queue)); cur=head; cur->next=NULL;}void push(type a){ cout<<"input (type) a,a==-1 to end"<<endl; Queue *add; //加的节点 add=(Queue *)malloc(sizeof(Queue)); add->data=a; cur->next=add; cur=add; cur->next=NULL;}void pop(){ if(head->next!=NULL) { cout<<"pop("<<head->next->data<<")"<<endl; head->next=head->next->next; } else cout<<"队列已空,无法pop"<<endl;}void traverse(){ cout<<"traverse:"<<endl; Queue *p=head; while(p->next!=NULL) { p=p->next; cout<<p->data<<" "<<endl; }}void free_queue(){ Queue *p; while(head!=NULL) { p=head; head=head->next; free(p); }}int main(){ cout<<"输入:1.push 2.pop 3.traverse 4.free 5.rebuild -1结束"<<endl; int a; type b; Init(); while(cin>>a&&a!=-1) { switch(a) { case 1:{cout<<"input (type) a,a==-1 to end"<<endl;while(cin>>b&&b!=-1){push(b);};break;}; case 2:{pop();break;}; case 3:{traverse();break;}; case 4:{cout<<"free:"<<endl;free_queue();break;}; case 5:{cout<<"rebuild:"<<endl;Init();break;} default:cout<<"please input 1 or 2 or 3 or -1"<<endl; } cout<<"输入:1.push 2.pop 3.traverse 4.free 5.rebuild -1结束"<<endl; } free_queue(); return 0;}
0 0
- 队列 数据结构作业(二)
- 数据结构 - 队列(二)
- 【数据结构作业】队列
- 数据结构:循环队列(二)
- 【数据结构】优先级队列(二)
- 数据结构之队列(二)
- 数据结构栈和队列作业
- C#数据结构(二)----栈和队列
- 学习笔记--数据结构(之二)队列
- 数据结构基础(二)队列的实现
- 数据结构(二):链表、链队列
- 数据结构基础(二)栈和队列
- 数据结构学习 栈与队列(二)
- 数据结构(二)——队列
- 数据结构:队列和栈----刷题集(二)
- 数据结构基础-队列(二)
- 数据结构(二)--------队列
- 算法与数据结构第四次作业 队列
- 性能分析之-- JAVA Thread Dump 分析综述
- android 如何显示模态的 DialogFragment。
- -Android Studio导入Project的方法
- wap网页通用模板
- Cache friendly code
- 队列 数据结构作业(二)
- 机器学习理解(四)
- Python 基础——排列组合的实现
- Android studio 导入github工程
- xml被浏览器缓存了
- am335x sd卡分区制作文档
- 人的需求2
- java lucene搜索功能初探(1)
- Android 缓存-ASimplecache