实验二
实验名称:栈和队列的有关操作 | |
实验室名称: | 实验台号:14 |
学生姓名: | 专业班级: 2015 |
指导教师: | 实验日期:2017-6-9 |
一、实验目的
1、掌握栈、队列的思想及其存储实现。
2、掌握栈、队列的常见算法的程序实现。
二、实验仪器及环境:
PC计算机;windows XP操作系统、Visual C++6.0、CodeBlocks
三、实验内容及结果(按照具体实验题目,按照如下格式书写)
1、
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#define MAXSIZE 1000
using namespace std;
typedef struct{
int elem[MAXSIZE];
int top;
}Seqstack;
void Init(Seqstack *s){
s->top=-1;
return;
}
int Empty(Seqstack *s){
if(s->top==-1)
return 1;
return 0;
}
int Push(Seqstack *s,int x){
if(s->top==MAXSIZE-1)
return 0;
else{
s->top++;
s->elem[s->top]=x;
return 1;
}
}
int Pop(Seqstack *s,int *x){
if(s->top==-1)
return 0;
else{
*x=s->elem[s->top];
s->top--;
return 1;
}
}
int top(Seqstack *s){
int x;
if(s->top==-1)
return 0;
else{
x=s->elem[s->top];
return x;
}
}
int main()
{
Seqstack *s;
int x,num,st;
s=(Seqstack *)malloc(sizeof(Seqstack));
Init(s);
srand((unsigned)time(NULL));
cout<<"请输入随机数入栈的总数:";
cin>>num;
cout<<"入栈:";
while(num>0){
st=rand()%100+1;
Push(s,st);
num--;
cout<<st<<" ";
}
cout<<endl;
//Push(s,rand()%100+1);Push(s,rand()%100+1);Push(s,rand()%100+1);
cout<<"出栈:";
while(!Empty(s)){
cout<<top(s)<<" ";
Pop(s,&x);
}
cout<<endl;
return 0;
}
2、
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#define MAXSIZE 1000
using namespace std;
typedef struct{
int elem[MAXSIZE];
int top;
}Seqstack;
void Init(Seqstack *s){
s->top=-1;
return;
}
int Empty(Seqstack *s){
if(s->top==-1)
return 1;
return 0;
}
int Push(Seqstack *s,int x){
if(s->top==MAXSIZE-1)
return 0;
else{
s->top++;
s->elem[s->top]=x;
return 1;
}
}
int Pop(Seqstack *s,int &x){
if(s->top==-1)
return 0;
else{
x=s->elem[s->top];
s->top--;
return 1;
}
}
int top(Seqstack *s){
int x;
if(s->top==-1)
return 0;
else{
x=s->elem[s->top];
return x;
}
}
int main()
{
Seqstack *s;
int x,num=10,st,data,rr;
s=(Seqstack *)malloc(sizeof(Seqstack));
Init(s);
srand((unsigned)time(NULL));
cout<<"输入一个十进制整数:"<<endl;
cin>>data;
cout<<"输入转化的R进制整数:"<<endl;
cin>>rr;
int res;
while(data){
Push(s,data%rr);
data/=rr;
}
//Push(s,rand()%100+1);Push(s,rand()%100+1);Push(s,rand()%100+1);
while(!Empty(s)){
Pop(s,x);
cout<<x;
}
return 0;
}
3、
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#define MAXSIZE 1000
using namespace std;
typedef struct{
int data[MAXSIZE];
int front_,rear_;
}Sequeue;
int Init(Sequeue *q){
if((q=((Sequeue *)malloc(sizeof(Sequeue))))==NULL)
return 0;
q->front_=MAXSIZE;
q->rear_=MAXSIZE;
return 1;
}
int Empty(Sequeue *q){
if(q->front_==q->rear_)
return 1;
else
return 0;
}
int InSequeue(Sequeue *q,int x){
if((q->rear_+1)%MAXSIZE==q->front_){
return -1;
}
else{
q->rear_=(q->rear_+1)%MAXSIZE;
q->data[q->rear_]=x;
return 1;
}
}
int OutSequeue(Sequeue *q,int *x){
if(q->front_==q->rear_){
return -1;
}
else{
q->front_=(q->front_+1)%MAXSIZE;
*x=q->data[q->front_];
return 1;
}
}
int main()
{
Sequeue *s;
int x,num,st;
s=(Sequeue *)malloc(sizeof(Sequeue));
Init(s);
srand((unsigned)time(NULL));
cin>>num;
while(num>0){
st=rand()%100+1;
InSequeue(s,st);
num--;
cout<<st<<" ";
}
cout<<endl;
//Push(s,rand()%100+1);Push(s,rand()%100+1);Push(s,rand()%100+1);
while(!Empty(s)){
OutSequeue(s,&x);
cout<<x<<" ";
}
cout<<endl;
return 0;
}
4、
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <time.h>
#define MAXSIZE 1000
using namespace std;
typedef struct{
int data[MAXSIZE];
int front_,rear_;
}Sequeue;
int Init(Sequeue *q){
if((q=((Sequeue *)malloc(sizeof(Sequeue))))==NULL)
return 0;
q->front_=0;
q->rear_=0;
return 1;
}
int Empty(Sequeue *q){
if(q->front_==q->rear_)
return 1;
else
return 0;
}
int InSequeue(Sequeue *q,int x){
if((q->rear_+1)%MAXSIZE==q->front_){
return -1;
}
else{
q->data[q->rear_]=x;
q->rear_=(q->rear_+1)%MAXSIZE;
return 1;
}
}
int OutSequeue(Sequeue *q,int *x){
if(q->front_==q->rear_){
return -1;
}
else{
*x=q->data[q->front_];
q->front_=(q->front_+1)%MAXSIZE;
return 1;
}
}
int GetHead(Sequeue *Q,int *x){
if(Q->front_==Q->rear_)
return 0;
*x=Q->data[Q->front_];
return 1;
}
void YangHuiTriangle( Sequeue *q,int N)
{
int n,i,x,temp;
Init(q);
InSequeue(q,1);
for(n=2;n<=N;n++)
{
InSequeue(q,1);
for(i=1;i<=n-2;i++)
{
OutSequeue(q,&temp);
cout<<temp<<" ";
GetHead(q,&x);
temp=temp+x;
InSequeue(q,temp);
}
OutSequeue(q,&x);
cout<<x<<" ";
InSequeue(q,1);
cout<<endl;
}
while(!Empty(q))
{
OutSequeue(q,&x);
cout<<x<<" ";
}
}
int main()
{
Sequeue *s;
int x,num=10,st;
s=(Sequeue *)malloc(sizeof(Sequeue));
Init(s);
int n=0;
cin>>n;
YangHuiTriangle(s,n);
return 0;
}
1、
2、
3、
4、
四、实验心得体会:(包括遇到的问题及解决办法)
结构体传参时采用的是结构体的引用,循环队列注意判断队满和队空的条件,注意堆栈和队列数据进入和出去的方向。
五、指导教师意见及成绩
签名:
年 月 日