下载此文档

实验七 3.doc


文档分类:高等教育 | 页数:约16页 举报非法文档有奖
1/16
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/16 下载此文档
文档列表 文档介绍
1, 分析该程序, 并解释每一主句的功能( 该程序完成的是: 队列和栈) #include<iostream> using namespace std; class list{ // 声明一个抽象类 public: list *head; // 表头指针 list *tail; // 表尾指针 list *next; int num; list() {head=tail=next=NULL;} virtual void store(int i)=0;// 纯虚函数 store virtual int retrieve()=0;// 纯虚函数 retrieve }; class queue:public list{// 声明公有派生类 queue public: void store(int i); int retrieve(); }; void queue::store(int i)// 定义虚函数 store {list *item; item=new queue; if(!item) {cout<<"Allocation error\n"; exit(1);} item->next=NULL; if(!head)head=tail; } int queue::retrieve()// 定义虚函数 retrieve {int i; list *p; if(!head) {cout<<"list empty \n"; return 0;} i=head->num; p=head; head=head->next; delete p; return i;} class stack:public list// 声明公有派生类 stack {public: void store(int i); int retrieve(); }; void stack::store(int i)// 定义虚函数 store {list *item; item=new stack; if(!item) {cout<<"Allocation error\n"; exit(1);} item->num=i; if(!head)item->next=head; head=item; if(!tail)tail=head; } int stack::retrieve()// 定义虚函数 retrieve {int i; list *p; if(!head) {cout<<"list empty \n"; return 0;} i=head->num; p=head; head=head->next; delete p; return i;} int main() {list *p;// 定义指向抽象类 list 的指针 p queue q_ob; p=&q_ob;// 对象指针 p 指向类 queue 的对象 q_ob p->store(1); p->store(2); p->store(3); cout<<"queue: "; cout<<p->retrieve(); cout<<p->retrieve(); cout<<p->retrieve(); cout<<"\n"; stack s_ob;// 对象指针 p 指向类 stack 的对象 s_ob p=&s_ob; p->store(1); p->store(2); p->store(3); cout<<"stack: "; cout<<p->retrieve(); cout<<p->retrieve(); cout<<p->retrieve(); cout<<"\n"; return 0;}2 ,建一个堆栈出栈程序,能实现入栈也能实现出栈,创建一个类, 类具备入栈和出栈的能力,并显示出栈的数据. 解题 1: #include <iostream> #include <stack> using namespace std; bool stack_pop(stack<int> &st, int num, int data[], int &pos) { if(num == 0) return true; if(() < num) return false; for(int i=0; i<num; ++i) { data[pos++] = (int)(()); (); }} int main() { int A=0, B=0, C=0, Total=0; int output[4], pos; for(A=0; A<2; ++A) for(B=0; B<3; ++B) for(C=0; C<4; ++C) { pos = 0; stack<int> st;

实验七 3 来自淘豆网www.taodocs.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数16
  • 收藏数0 收藏
  • 顶次数0
  • 上传人yzhlya
  • 文件大小67 KB
  • 时间2017-02-24