《queue类的实现与stack的实现》由会员分享,可在线阅读,更多相关《queue类的实现与stack的实现(4页珍藏版)》请在金锄头文库上搜索。
1、/包括queue类的实现及杨辉三角的输出#include#include#includeusing namespace std;template class queueprotected:struct nodeT data;node *next;public:node(T value,node* link)data=value;this-next=link-next;link-next=this;node(T value)data=value;private:node *last;public:queue(T value)last=new node(value);last-next=last;
2、queue()while(last!=last-next) pop();free(last);queue(queue &ori)node *p1=new node(1),*p2=ori.last;p1-data=p2-data;last=p1; while(p2-next!=ori.last)p2=p2-next;p1=new node(p2-data,p1);p1-next=last;p1=last;void push(T value)last=new node(value,last);void pop()if(last=NULL) coutnext;if(last-next=last) l
3、ast=NULL;else last-next=last-next-next;free(p);T front()return last-next-data;bool empty()if(last=NULL) return true;return false;void yhui(int n)queueque(1); que.push(1);coutn) yhui(n);return 0;/以下是 Stack 的实现代码#ifndef STACK_H#define STACK_H#include /精简版#include#include #includeusing namespace std;te
4、mplate class Stackprotected:struct nodeT data;node *next,*last;node(T item,node*ptr)data=item;ptr-next=this;this-last=ptr;node(T item) data=item; ;private:node *head;int size;public:Stack()head=NULL;size=0; Stack()while(size-)node *del=head;head=head-last;free(del);bool empty() return size=0?true:false;void push(T value)head=(+size)=1?new node(value):new node(value,head);void pop()if(size=0)coutlast;free(del);size-;T ;#endif