实验内容:
1、用顺序存储定义栈结构。写出这种存储方式下的算法实现,包括:初始化栈、判栈为空、出栈、入栈、求栈顶元素等运算,自己填写主函数。
2、利用顺序栈的基本操作,设计算法,实现将任意一个十进制整数转化为R进制整数
3、用顺序存储来定义循环队列结构。编程实现,初始化队列、判队列为空、出队列、入队列、求队列头部元素等运算,自己填写主函数。
4、利用队列打印杨辉三角形。杨辉三角形是形如:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
的三角形,其实质是二项式(a+b)的n次方展开后各项的系数排成的三角形,它的特点是
左右两边全是1,从第二行起,中间的每一个数是上一行里相邻两个数之和。输入要打印的层数n,打印出相应的杨辉三角形。
代码:
#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;
}
测试截图: