头文件
#ifndef SEQSTACK_H
#define SEQSTACK_H
const int stacksize=10;
class seqstack
{
private:
int data[stacksize];
int top;
public:
seqstack();
~seqstack(){}
void push(int x);
int pop();
int gettop();
int empty();
};
#endif
子函数
#include"seqstack.h"
seqstack::seqstack()
{
top=-1;
}
void seqstack::push(int x)
{
if(top==stacksize-1) throw"上溢";
top++;
data[top]=x;
}
int seqstack::pop()
{
int n;
if(top==-1) throw"下溢";
n=data[top];
top--;
return n;
}
int seqstack::gettop()
{
if(top==-1) throw"null";
return data[top];
}
int seqstack::empty()
{
if(top==-1) return 1;
else return 0;
}
主函数
#include<iostream>
using namespace std;
#include"seqstack.h"
void main()
{
seqstack s;
if(s.empty())
cout<<"The seqstack is empty."<<endl;
else cout<<"The seqstack is NOT empty."<<endl;
cout<<"分两次对11和14进行入栈操作"<<endl;
s.push(11);
cout<<"The top is "<<s.gettop()<<endl;
s.push(14);
cout<<"The top is "<<s.gettop()<<endl;
cout<<"执行一次出栈操作"<<endl;
s.pop();
cout<<"The top is "<<s.gettop()<<endl;
}
头文件::
#ifndef linkstack_h
#define linkstack_h
struct node
{
int data;
node *next;
};
class linkstack
{
private:
node *top;
public:
linkstack();
~linkstack(){}
void push(int x);
int pop();
int gettop();
int empty();
};
#endif
函数
#include"linkstack.h"
#include<stdlib.h>
linkstack::linkstack()
{
top=NULL;
}
void linkstack::push(int x)
{
node *s;
s=new node;
s->data=x;
s->next=top;
top=s;
}
int linkstack::pop()
{
node *p;
p=new node;
int x;
if(pop==NULL) throw"下溢";
x=top->data;
p=top;
top=top->next;
delete p;
return x;
}
int linkstack::gettop()
{
return top->data;
}
int linkstack::empty()
{
if(top==NULL) return 1;
else return 0;
}
主函数::
#include<iostream>
using namespace std;
#include<stdlib.h>
#include"linkstack.h"
void main()
{
linkstack l;
if(l.empty()) cout<<"The linkstack is empty"<<endl;
else cout<<"The linkstack is NOT empty"<<endl;
cout<<"分别对5和13进行压栈"<<endl;
l.push(5);
cout<<"栈顶元素为 "<<l.gettop()<<endl;
l.push(13);
cout<<"栈顶元素为 "<<l.gettop()<<endl;
cout<<"进行一次弹栈"<<endl;
cout<<"栈顶元素为 "<<endl;
l.pop();
cout<<l.gettop()<<endl;
}
顺序队列:
头文件:
#ifndef SEQQUEUE_H
#define SEQQUEUE_H
const int queuesize=10;
class seqQueue
{
int rear,front;
int data[queuesize];
public:
seqQueue()
{front=rear=queuesize-1;}
~seqQueue(){}
void enqueue(int x); //入队
int dequeue(); //出队
int getqueue(); //取对头元素
int empty(); //判断队列是否为空
};
#endif
函数:
#include"seqQueue.h"
void seqQueue::enqueue(int x)
{
if(front==(rear+1)%queuesize) throw"上溢";
rear=(rear+1)%queuesize;
data[rear]=x;
}
int seqQueue::dequeue()
{
if(front==rear) throw"下溢";
front=(front+1)%queuesize;
return data[front];
}
int seqQueue::getqueue()
{
int i;
if(front==rear) throw"下溢";
i=(front+1)%queuesize;
return data[i];
}
int seqQueue::empty()
{
if(front==rear)return 1;
else return 0;
}
主函数:
#include<iostream>
using namespace std;
#include"seqQueue.h"
void main()
{
seqQueue s;
if(s.empty()) cout<<"This is a NULL queue."<<endl;
else cout<<"This is NOT a NULL queue."<<endl;
cout<<"一次对4和9入队 "<<endl;
s.enqueue(4);
s.enqueue(9);
cout<<"队头元素为 "<<s.getqueue()<<endl;
cout<<"进行一次出队操作 出队元素为"<<s.dequeue()<<endl;
cout<<"队头元素为 "<<s.getqueue()<<endl;
if(s.empty()) cout<<"This is a NULL queue."<<endl;
else cout<<"This is NOT a NULL queue."<<endl;
}
链队列:
头文件:
#ifndef LINKQUEUE_H
#define LINKQUEUE_H
struct node
{
int data;
node *next;
};
class linkqueue
{
node *rear,*front;
public:
linkqueue();
~linkqueue();
void enqueue(int x);
int dequeue();
int getqueue();
int empty();
};
#endif
函数:
#include"linkqueue.h"
#include<iostream>
using namespace std;
linkqueue::linkqueue()
{
node *n;
n=new node;
n->next=NULL;
front=rear=n;
}
linkqueue::~linkqueue()
{
}
front=rear=NULL;
}
void linkqueue::enqueue(int x) //入队
{
node *s=new node;
s->data=x;
s->next=NULL;
rear->next=s;
rear=s;
}
int linkqueue::dequeue() //出队
{
if(front==rear) throw"下溢";
int x;
node *p=new node;
p=front->next;
x=p->data;
front->next=p->next;
return 0;
}
int linkqueue::getqueue() //取队头元素
{
int x;
node *q=new node;
q=front->next;
x=q->data;
return x;
}
int linkqueue::empty()
{
if(front==rear)return 1;
else return 0;
}
主函数:
#include<iostream>
using namespace std;
#include"linkqueue.h"
void main()
{
linkqueue l;
if(l.empty()) cout<<"This queue is NULL "<<endl;
else cout<<"This queue isn't NULL "<<endl;
cout<<"依次对1、2和3进行入队操作 "<<endl;
l.enqueue(1);
l.enqueue(2);
l.enqueue(3);
cout<<"取队头元素为 "<<l.getqueue()<<endl;
cout<<"进行一次出队操作 "<<endl;
l.dequeue();
cout<<"取队头元素为 "<<l.getqueue()<<endl;
if(l.empty()) cout<<"This queue is NULL "<<endl;
else cout<<"This queue isn't NULL "<<endl;
}
}
10进制换2进制
头文件:
#ifndef seqstack_H
#define seqstack_H
const int stacksize=20;
const int queuesize=20;
class seqQueue;
class seqstack
{
private:
int data[stacksize];
public:
seqstack();
~seqstack(){}
void push(int x);
int pop();
int top; //top指向栈尾
};
class seqQueue
{
int dataq[queuesize];
public:
seqQueue()
{front=rear=queuesize-1;}
~seqQueue(){}
void enqueue(int x); //入队
int dequeue(); //出队
int rear,front; //rear指向队尾,front指向队头元素前一个
};
#endif
函数定义
#include"102.h"
#include<iostream>
using namespace std;
seqstack::seqstack()
{
top=-1;
}
void seqstack::push(int x)
{
if(top==stacksize-1) throw"上溢";
top++;
data[top]=x;
}
int seqstack::pop()
{
int n;
if(top==-1) throw"下溢";
n=data[top];
cout<<n;
top--;
return n;
}
void seqQueue::enqueue(int x)
{
if(front==(rear+1)%queuesize) throw"上溢";
rear=(rear+1)%queuesize;
dataq[rear]=x;
}
int seqQueue::dequeue()
{
if(front==rear) throw"下溢";
front=(front+1)%queuesize;
cout<<dataq[front];
return dataq[front];
}
主函数
#include<iostream>
using namespace std;
#include"102.h"
int main()
{
float t,a; //t为想要转换的10进制数,a为过渡元素
int x;
int i,m,j=1; //j为停止转换标志
seqstack s; //建立栈s对象
seqQueue q; //建立队列q对象
cout<<"输入要转换为二进制的十进制数 "<<"\t";
while(j)
{cin>>t;
x=t/1.0; // 获取整数部分
for(i=0;x!=0;i++)
{m=x%2;
s.push(m); //入栈操作
x=x/2;}
a=t-(int)t; //获取小数部分
if(a!=0)
{while(a<=1)
{a=2*a;
x=a/1.0;
q.enqueue(x); //进行入队操作
}}
cout<<t<<"的二进制数为";
for(i=0;s.top>=0;i++) //输出二进制结果
s.pop();
cout<<".";
for(i=0;q.rear!=q.front;i++)
q.dequeue();
cout<<endl;
cout<<"按0结束.....按任意数字继续转换"<<endl;
cin>>j;
}
return 0;
}