1、顺序栈
#include<iostream.h>
template <class type>
class a
{ public:
a();
~a(){};
void push(type x);//入栈
void out();//出栈
void get();//得顶数
void empty();//判断栈是否为空
private:
type data[5];
int top;};
template<class type>
a<type>::a()
{top=-1;}
template<class type>
void a<type>::push(type x)
{if(top==4)
cout<<"栈满"<<endl;
else {top++; data[top]=x;}
}
template<class type>
void a<type>::out()
{if(top==-1)
cout<<"栈空"<<endl;
else {cout<<data[top]<<endl;top--;}
}
template<class type>
void a<type>::get()
{if(top==-1)
cout<<"栈空"<<endl;
else cout<<data[top]<<endl;
}
template<class type>
void a<type>::empty()
{if(top==-1)
cout<<"栈空"<<endl;
else cout<<"不为空"<<endl;}
int main()
{a<char *> s;
s.empty();
s.push("ad");
s.push("bc");
cout<<"栈顶元素为:";
s.get();
cout<<"出栈一个元素"<<endl;
s.out();
cout<<"栈顶元素为:";
s.get();
return 0;}
2、链栈
#include<iostream.h>
template<class type>
struct a
{ type data;
a<type> *next;};
template<class type>
class b
{public :
b();
~b(){};
void push(type x);
void out();
void get();
void empty();
private:
a<type> *top;
};
template<class type>
b<type>::b()
{top=NULL;}
template<class type>
void b<type>::push(type x)
{a<type> *s;
s=new a<type>; s->data=x;
s->next=top;
top=s;}
template<class type>
void b<type>::out()
{type x;if(top==NULL) cout<<"栈空"<<endl;
else {a<type> *s;
x=top->data;
s=top->next;
delete top; top=s;
cout<<"删除的数为:"<<x<<endl;}} //sdadasdasdasd
template<class type>
void b<type>::get()
{if(top==NULL) cout<<"栈空"<<endl;
else {cout<<top->data<<endl;}}
template<class type>
void b<type>::empty()
{a<type> *s;s=top;
if(top==NULL) cout<<"栈空"<<endl;
else {cout<<"不为空"<<endl;
do{cout<<s->data<<endl;s=s->next;}
while(s!=NULL);
}}
int main()
{b<char *> s;
s.push("ab");
s.push("bc");
s.push("de");
s.get();
s.out();
s.get();
s.empty();
return 0;}
3、顺序队列
#include<iostream.h>
template <class type>
class list
{public:
list();
~list(){};
void in(type x);
void out();
void print();
private:
type data[5];
int rear,front;};
template <class type>
list<type>::list()
{front=rear=4;}
template <class type>
void list<type>::in(type x)
{if((rear+1)%5==front)
cout<<"队列满"<<endl;
else{rear=(rear+1)%5;data[rear]=x;
cout<<"插入数:"<<x<<endl;}}
template <class type>
void list<type>::out()
{if(rear==front)
cout<<"队列为空"<<endl;
else{front=(front+1)%5;
cout<<"出列的数为:"<<data[front]<<endl;
}}
template <class type>
void list<type>::print()
{if(rear==front)
cout<<"队列为空"<<endl;
else{int i;i=(front+1)%5;
do{cout<<data[i]<<endl;
i=(i+1)%5;}
while(i!=(rear+1)%5);
}}
void main()
{list<char *> s;
s.in("ad");
s.in("bc");
s.in("de");
s.print();
s.out();
s.print();
}
4、链队列(饭堂打菜)
#include<iostream.h>
template <class type>
struct a
{type data;
a<type> *next;};
template <class type>
class list
{public:
list();
~list();
void in(type x);
void out();
void print();
private:
a<type> *rear,*front;
int count;};
template <class type>
list<type>::list()
{a<type> *s;
s=new a<type>;
s->next=NULL;
front=rear=s;
count=0;}
template <class type>
list<type>::~list()
{a<type> *s;
if(front!=NULL)
{s=front->next;
front=s;
delete s;}
}
template <class type>
void list<type>::in(type x)
{count++;
a<type> *s;
s=new a<type>;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
cout<<" "<<rear->data<<"已在排队中,第"<<count<<"位,请稍等。"<<endl;}
template <class type>
void list<type>::out()
{if(rear==front)
cout<<"队列为空"<<endl;
else{
cout<<"第"<<count<<"号已打好饭菜,轮下一位。"<<endl;
count--;
a<type> *s;
s=front->next;
front->next=s->next;
if(s->next==NULL) rear=front;
delete s;}
}
template <class type>
void list<type>::print()
{if(rear==front)
cout<<"无人排队。"<<endl;
else{a<type> *s;
s=rear;
cout<<"总共有"<<count<<"个人在排队。"<<endl;
}}
int main()
{list<char *> s;
s.in("李静");
s.in("晓威");
s.in("张建");
s.print();
s.out();
cout<<"请输入您的名字:";
char a[10];
cin>>a;
s.in(a);
s.print();
return 0;}
5、10进制转2进制
#include<iostream.h>
int main()
{
int x;
int a[15];
for(int i=0;i<15;i++)
a[i]=0;
cout<<"输入要转为二进制的数字:";
cin>>x;
i=0;
while(x!=0)
{
a[i]=x%2;
x=x/2;
i++;
}
for(i=14;i>=0;i--)
cout<<a[i];
cout<<endl;
return 0;
}