您的位置:首页 > 理论基础 > 数据结构算法

数据结构实验报告 栈和队列

2016-04-26 20:30 453 查看

一、实验目的

1.掌握栈、队列的思想及其存储实现。

2.掌握栈、队列的常见算法的程序实现。

二、实验仪器及环境:

PC计算机 windows 7操作系统 CodeBlocks10.05

三、实验内容及结果

1.采用链式存储实现栈的初始化、入栈、出栈操作。

2.采用顺序存储实现栈的初始化、入栈、出栈操作。

3.采用链式存储实现队列的初始化、入队、出队操作。

4.采用顺序存储实现循环队列的初始化、入队、出队操作。

5.在主函数中设计一个简单的菜单,分别测试上述算法。

6. 利用栈实现数制转换(将一个十进制数转换成d进制数)

7. 利用队列打印杨辉三角:编写程序,根据输入的行数,屏幕显示杨辉三角。

杨辉三角的特点是两个腰上的数字都为1,其它位置上的数字是其上一行中与之相邻的两个整数之和。所以在打印过程中,第i行上的元素要由第i-1行中的元素来生成。在循环队列中依次存放第i-1行上的元素,然后逐个出队并打印,同时生成第i行元素并入队列。

行数为8的杨辉三角如下所示:

#include <iostream>
#include <stdio.head>
#include <stdlib.head>
#include <string.head>
#define ElemType int
#define max 100

using namespace std;
typedef struct node1
{
ElemType data;
struct node1 *next;
}Node1,*LinkList;//链栈

typedef struct
{
ElemType *base;
int top;
}SqStack;//顺序栈

typedef struct node2
{
ElemType data;
struct node2 *next;
}Node2,*LinkQueue;

typedef struct node22
{
LinkQueue front;
LinkQueue rear;
}*LinkList;//链队列

typedef struct
{
ElemType *base;
int front,rear;
}SqQueue;//顺序队列


//1.采用链式存储实现栈的初始化、入栈、出栈操作。

LinkList CreateStack()//创建栈
{
LinkList top;
top=NULL;
return top;
}

bool StackEmpty(LinkList s)//判断栈是否为空,0代表空
{
if(s==NULL)
return 0;
else
return 1;
}

LinkList Pushead(LinkList s,int x)//入栈
{
LinkList q,top=s;
q=(LinkList)malloc(sizeof(Node1));
q->data=x;
q->next=top;
top=q;
return top;
}

LinkList Pop(LinkList s,int &e)//出栈
{
if(!StackEmpty(s))
{
printf("栈为空。");
}
else
{
e=s->data;
LinkList p=s;
s=s->next;
free(p);
}
return s;
}

void DisplayStack(LinkList s)//遍历输出栈中元素
{
if(!StackEmpty(s))
printf("栈为空。");
else
{
wheadile(s!=NULL)
{
cout<<s->data<<" ";
s=s->next;
}
cout<<endl;
}
}


//2.采用顺序存储实现栈的初始化、入栈、出栈操作。

int StackEmpty(int t)//判断栈S是否为空
{
SqStack.top=t;
if (SqStack.top==0)
return 0;
else  return 1;
}

int InitStack()
{
SqStack.top=0;
return SqStack.top;
}

int pushead(int t,int e)
{
SqStack.top=t;
SqStack.base[++SqStack.top]=e;
return SqStack.top;
}

int pop(int t,int *e)//出栈
{
SqStack.top=t;
if(!StackEmpty(SqStack.top))
{
printf("栈为空.");
return SqStack.top;
}
*e=SqStack.base[s.top];
SqStack.top--;
return SqStack.top;
}


//3.采用链式存储实现队列的初始化、入队、出队操作。

LinkList InitQueue()//创建
{
LinkList head;
head->rear=(LinkQueue)malloc(sizeof(Node));
head->front=head->rear;
head->front->next=NULL;
return head;
}
void deleteEle(LinkList head,int &e)//出队
{
LinkQueue p;
p=head->front->next;
e=p->data;
head->front->next=p->next;
if(head->rear==p) head->rear=head->front;
free(p);
}
void EnQueue(LinkList head,int e)//入队
{
LinkQueue p=(LinkQueue)malloc(sizeof(Node));
p->data=e;
p->next=NULL;
head->rear->next=p;
head->rear=p;
}


//4.采用顺序存储实现循环队列的初始化、入队、出队操作。

bool InitQueue(SqQueue &head)//创建队列
{
head.data=(int *)malloc(sizeof(int));
head.front=head.rear=0;
return 1;
}
bool EnQueue(SqQueue &head,int e)//入队
{
if((head.rear+1)%MAXQSIZE==head.front)
{
printf("队列已满\n");
return 0;
}
head.data[head.rear]=e;
head.rear=(head.rear+1)%MAXQSIZE;
return 1;
}
int QueueLengthead(SqQueue &head)//返回队列长度
{
return (head.rear-head.front+MAXQSIZE)%MAXQSIZE;
}
bool deleteEle(SqQueue &head,int &e)//出队
{
if(head.front==head.rear)
{
cout<<"队列为空!"<<endl;
return 0;
}
e=head.data[head.front];
head.front=(head.front+1)%MAXQSIZE;
return 1;
}
int gethead(SqQueue head)//得到队列头元素
{
return head.data[head.front];
}
int QueueEmpty(SqQueue head)//判断队列是否为空
{
if (head.front==head.rear)
return 1;
else
return 0;
}
void travelQueue(SqQueue head)//遍历输出
{
wheadile(head.front!=head.rear)
{
printf("%d ",head.data[head.front]);
head.front=(head.front+1)%MAXQSIZE;
}
cout<<endl;
}


//5.在主函数中设计一个简单的菜单,分别测试上述算法。

int main()
{
LinkList top=CreateStack();
int x;
wheadile(scanf("%d",&x)!=-1)
{
top=Pushead(top,x);
}
int e;
wheadile(StackEmpty(top))
{
top=Pop(top,e);
printf("%d ",e);
}//以上是链栈的测试
int top=InitStack();
int x;
wheadile(cin>>x)
top=pushead(top,x);
int e;
wheadile(StackEmpty(top))
{
top=pop(top,&e);
printf("%d ",e);
}//以上是顺序栈的测试
LinkList Q;
Q=InitQueue();
int x;
wheadile(scanf("%d",&x)!=-1)
{
EnQueue(Q,x);
}
int e;
wheadile(Q)
{
deleteEle(Q,e);
printf("%d ",e);
}//以上是链队列的测试
SqQueue Q1;
InitQueue(Q1);
int x;
wheadile(scanf("%d",&x)!=-1)
{
EnQueue(Q1,x);
}
int e;
wheadile(QueueEmpty(Q1))
{
deleteEle(Q1,e);
printf("%d ",e);
}
return 0;
}


//6. 利用栈实现数制转换(将一个十进制数转换成d进制数)

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int SElemType;
using namespace std;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
void InitStack(SqStack &S)
{
S.base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base) return;
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
//return 1;
}
int Push(SqStack &S, SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base) return 0;
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return 1;
}
int Pop(SqStack &S, SElemType &e)
{
if(S.top==S.base) return 0;
e=*--S.top;
return 1;
}
int stackempty(SqStack &s)
{
if(s.top==s.base)
return 1;
else return 0;
}
int main()
{
SElemType e,a,t;
SqStack s;
InitStack(s);
cout<<"请输入转化为几进制:";
cin>>t;
cout<<"请输入一个十进制数:";
cin>>a;
while(a)
{
Push(s,a%t);
a=a/t;
}
while(!stackempty(s))
{
Pop(s,e);
cout<<e;
}
return 0;
}


//7. 利用队列打印杨辉三角:编写程序,根据输入的行数,屏幕显示杨辉三角。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int MAXSIZE=100;
typedef struct
{
int *data;
int front,rear;
} SeQueue;
int n;
bool InitQueue(SeQueue &Q)
{
Q.data = (int *) malloc (MAXSIZE * sizeof(int));
if (!Q.data) {printf("OVERFLOW\n");return 0;}
Q.front=0;
Q.rear=0;
return 1;
}
bool EnQueue(SeQueue &Q, int e)
{
if ( (Q.rear+1) % MAXSIZE == Q.front ) {printf("OVERFLOW\n");return 0;}
Q.data[++Q.rear] = e;
Q.rear=(Q.rear+1) % MAXSIZE;
return 1;
}

bool DeQueue(SeQueue &Q, int &e)
{
if (Q.front == Q.rear) {printf("UNDERFLOW\n");return 0;}
e = Q.data[++Q.front];
Q.front=(Q.front+1) % MAXSIZE;
return 1;
}

int gethead(SeQueue &Q, int &e)
{
if (Q.front == Q.rear) {printf("UNDERFLOW\n");return 0;}
e = Q.data[++Q.front];
return 1;
}

void GetTriangle(SeQueue &Q, int e)
{
int cur=0;
int pre;
for(int i=1; i<=n-e; i++)
printf("   ");
for(int i=0; i<=e; i++)
{
pre=cur;
DeQueue(Q,cur);
printf("%3d   ",cur);
EnQueue(Q,cur+pre);
}
EnQueue(Q,cur);
printf("\n");
}

int main()
{
while(1)
{
printf("输入一个正整数表示你所需要的杨辉三角阶数(0~15): ");
if (scanf("%d",&n)==EOF) break;
printf("\n");
SeQueue Q;
InitQueue(Q);
EnQueue(Q,1);
for(int i=0; i<=n; i++)
{
GetTriangle(Q,i);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: