1.采用链式存储实现栈的初始化、入栈、出栈操作。
typedef struct node
{
int data;
struct node *next;
} Node,*LinkList;
LinkList Createzhan()//创建栈
{
LinkList top;
top=NULL;
return top;
}
bool StackEmpty(LinkList s)//判断栈是否为空,1代表空
{
if(s==NULL)
return 1;
else
return 0;
}
LinkList push(LinkList s,int x)//栈中插入元素
{
LinkList q,top=s;
q=(LinkList)malloc(sizeof(Node));
if(!q) return 0;
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 GetTop(LinkList s,int &e)//取得栈顶元素
{
if(StackEmpty(s))
printf("栈为空。");
else
e=s->data;
}
void TravealPut(LinkList s)//遍历输出栈中元素
{
if(StackEmpty(s))
printf("栈为空。");
else
{
while(s!=NULL)
{
cout<<s->data<<" ";
s=s->next;
}
cout<<endl;
}
}
2.采用顺序存储实现栈的初始化、入栈、出栈操作。
struct node
{
int data[size];
int top;
}s;
int StackEmpty(int t)//判断栈S是否为空
{
s.top=t;
if (s.top==0)
return 1;
else return 0;
}
int InitStack()
{
s.top=0;
return s.top;
}
int push(int t,int e)
{
s.top=t;
s.data[++s.top]=e;
return s.top;
}
int pop(int t,int *e)
{
s.top=t;
if(StackEmpty(s.top))
{
printf("栈为空.");
return s.top;
}
*e=s.data[s.top];
s.top--;
return s.top;
}
void GetTop(int t,int *e)
{
s.top=t;
*e=s.data[s.top];
}
3.采用链式存储实现队列的初始化、入队、出队操作。
typedef struct node
{
int data;
struct node *next;
} Node,*LinkQueue;
typedef struct NODE
{
LinkQueue front;
LinkQueue rear;
}*LinkList;
LinkList InitQueue()
{
LinkList H;
H->rear=(LinkQueue)malloc(sizeof(Node));
H->front=H->rear;
H->front->next=NULL;
return H;
}
LinkList DestoryQueue(LinkList H)
{
while(H->front)
{
H->rear=H->front->next;
free(H->front);
H->front=H->rear;
}
return H;
}
void deleteEle(LinkList H,int &e)
{
LinkQueue p;
p=H->front->next;
e=p->data;
H->front->next=p->next;
if(H->rear==p) H->rear=H->front;
free(p);
}
void EnQueue(LinkList H,int e)
{
LinkQueue p=(LinkQueue)malloc(sizeof(Node));
p->data=e;
p->next=NULL;
H->rear->next=p;
H->rear=p;
}
void printQueue(LinkList H)
{
LinkQueue L=H->front->next;
while(L!=NULL)
{
cout<<L->data<<" ";
L=L->next;
}
cout<<endl;
}
int getTop(LinkList H)
{
return H->front->next->data;
}
4.采用顺序存储实现循环队列的初始化、入队、出队操作。
typedef struct node
{
int *data;
int front,rear;
} SqQueue;
bool InitQueue(SqQueue &H)//创建队列
{
H.data=(int *)malloc(sizeof(int));
if(!H.data)
{
printf("OVERFLOW\n");
return 0;
}
H.front=H.rear=0;
return 1;
}
bool EnQueue(SqQueue &H,int e)//添加队列元素
{
if((H.rear+1)%MAXQSIZE==H.front)
{
printf("队列已满\n");
return 0l;
}
H.data[H.rear]=e;
H.rear=(H.rear+1)%MAXQSIZE;
return 1;
}
int QueueLength(SqQueue &H)//返回队列长度
{
return (H.rear-H.front+MAXQSIZE)%MAXQSIZE;
}
bool deleteEle(SqQueue &H,int &e)//删除队列元素
{
if(H.front==H.rear)
{
cout<<"队列为空!"<<endl;
return 0;
}
e=H.data[H.front];
H.front=(H.front+1)%MAXQSIZE;
return 1;
}
int getHead(SqQueue H)//得到队列头元素
{
return H.data[H.front];
}
int QueueEmpty(SqQueue H)//判断队列是否为空
{
if (H.front==H.rear)
return 1;
else
return 0;
}
void travelQueue(SqQueue H)//遍历输出
{
while(H.front!=H.rear)
{
printf("%d ",H.data[H.front]);
H.front=(H.front+1)%MAXQSIZE;
}
cout<<endl;
}
6.综合训练:1,利用栈实现表达式求值算法。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
char s[100];
int i=0;
typedef struct node
{
char data;
struct node *next;
} Node,*LinkList;
LinkList Createzhan1()//创建符号栈
{
LinkList top;
top=NULL;
return top;
}
LinkList push(LinkList s,char x)//栈中插入元素
{
LinkList q,top=s;
q=(LinkList)malloc(sizeof(Node));
if(!q) return 0;
q->data=x;
q->next=top;
top=q;
return top;
}
LinkList pop(LinkList s,char &e)//删除栈顶元素
{
e=s->data;
LinkList p=s;
s=s->next;
free(p);
return s;
}
char GetTop(LinkList s)//取得栈顶元素
{
return s->data;
}
int precede(char op1, char op2)
{
if(op1 == ')'&& op2 == '(')
return 2;
else if (op1 == '('||op2 == '(' || op2 == '#')
return 0;
else if(op1 == ')')
return 1;
else if (op1 == '+' || op1 == '-')
{
return 1;
}
else if (op1 == '*' || op1 == '/')
{
if (op2 == '+' || op2 == '-')
return 0;
else
return 1;
}
}
int isInt(char a)//判断是不是整数
{
if(a>='0'&&a<='9')
return 1;
else return 0;
}
char to(LinkList optr)//中缀表达式变后缀
{
optr=push(optr,'#');//’#’置于栈底,级别最低
char c=getchar();//读入表达式,以’#’结束
while(c!='#'||GetTop(optr)!='#')
{
if(isInt(c))//若读入字符c是数字字符,放入数组
{
s[i++]=c;
c=getchar();
}
else
{
if(c==')')//去括号
{
while(GetTop(optr)!='(')
{
optr=pop(optr,s[i++]);
}
char x;
optr=pop(optr,x);
c=getchar();
}
else if(!precede(c,GetTop(optr)))//c级别高,入栈
{
optr=push(optr,c);
c=getchar();
}
else if(precede(c,GetTop(optr)))
{
optr=pop(optr,s[i++]);
}
}
}
}
char sum(char a,char t,char b)
{
int c=a-'0';
int d=b-'0';
if(t=='+')
{
a=c+d+'0';
return a;
}
else if(t=='-')
{
a=c-d+'0';
return a;
}
else if(t=='*')
{
a=c*d+'0';
return a;
}
else if(t=='/')
{
a=c/d+'0';
return a;
}
}
char tosum(LinkList optr)//后缀表达式的计算
{
i=0;
char c=s[i++];
while(c!='#')
{
if(isInt(c))
{
optr=push(optr,c);
c=s[i++];
}
else
{
char a,b;
optr=pop(optr,b);
optr=pop(optr,a);
optr=push(optr,sum(a,c,b));//进行相应运算,结果入栈
c=s[i++];
}
}
return GetTop(optr);
}
int main()
{
LinkList top1;
top1=Createzhan1();
to(top1);
s[i]='#';
cout<<tosum(top1)-'0'<<endl;
return 0;
}
2,进制转换;
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef struct node
{
int data;
struct node *next;
} Node,*LinkList;
LinkList Createzhan()//创建栈
{
LinkList top;
top=NULL;
return top;
}
bool StackEmpty(LinkList s)//判断栈是否为空,1代表空
{
if(s==NULL)
return 1;
else
return 0;
}
LinkList push(LinkList s,int x)//栈中插入元素
{
LinkList q,top=s;
q=(LinkList)malloc(sizeof(Node));
if(!q) return 0;
q->data=x;
q->next=top;
top=q;
return top;
}
LinkList pop(LinkList s,int &e)//删除栈顶元素
{
if(StackEmpty(s))
{
printf("栈为空。");
return s;
}
else
{
e=s->data;
LinkList p=s;
s=s->next;
free(p);
}
return s;
}
void GetTop(LinkList s,int &e)//取得栈顶元素
{
if(StackEmpty(s))
printf("栈为空。");
else
e=s->data;
}
void TravealPut(LinkList s)//遍历输出栈中元素
{
if(StackEmpty(s))
printf("栈为空。");
else
{
while(s!=NULL)
{
cout<<s->data;
s=s->next;
}
cout<<endl;
}
}
int main()
{
printf("读入一个十进制数:");
int x;
cin>>x;
LinkList top;
top=Createzhan();
while(x!=0)
{
int b;
b=x%8;
x=x/8;
top=push(top,b);
}
TravealPut(top);
return 0;
}
3,循环队列输出杨辉三角;
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#define MAXQSIZE 100
using namespace std;
typedef struct node
{
int data[MAXQSIZE];
int front,rear;
} SqQueue;
bool InitQueue(SqQueue &H)//创建队列
{
H.front=0;
H.rear=0;
return 1;
}
bool EnQueue(SqQueue &H,int e)//添加队列元素
{
if((H.rear+1)%MAXQSIZE==H.front)
{
printf("队列已满\n");
return 0;
}
H.data[H.rear]=e;
H.rear=(H.rear+1)%MAXQSIZE;
return 1;
}
int QueueLength(SqQueue &H)//返回队列长度
{
return (H.rear-H.front+MAXQSIZE)%MAXQSIZE;
}
bool deleteEle(SqQueue &H,int &e)//删除队列元素
{
if(H.front==H.rear)
{
cout<<"队列为空!"<<endl;
return 0;
}
e=H.data[H.front];
H.front=(H.front+1)%MAXQSIZE;
return 1;
}
int getHead(SqQueue H)//得到队列头元素
{
return H.data[H.front];
}
int QueueEmpty(SqQueue H)//判断队列是否为空
{
if (H.front==H.rear)
return 1;
else
return 0;
}
void travelQueue(SqQueue &H,int k)//遍历输出
{
int a=0;
int p=0;
for(int i=1; i<=k; i++)
{
p=a;
deleteEle(H,a);
printf("%5d ",a);
EnQueue(H,a+p);
}
EnQueue(H,a);
cout<<endl;
}
int main()
{
int n;
while(1)
{
printf("输入要输出的杨辉三角行数:\n");
cin>>n;
SqQueue S;
InitQueue(S);
EnQueue(S,1);
for(int i=1; i<=n; i++)
{
travelQueue(S,i);
}
}
return 0;
}
数据结构实验报告2
最新推荐文章于 2024-06-28 23:40:19 发布