1. 采用链式存储实现栈的初始化、入栈、出栈操作。
存储结构:
typedef int Status;
typedef int SElemType;
typedef struct SNode{
SElemType data;
struct SNode *next;
}SNode,*LinkStack;
程序:
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int SElemType;
typedef struct SNode{
SElemType data;
struct SNode *next;
}SNode,*LinkStack;
//链栈的初始化
Status InitStack (LinkStack &S)
{
S=NULL;
return OK;
}
// 链栈的入栈
Status Push(LinkStack &S , SElemType e)
{
SNode *p = new SNode;
if(!p)
{
return OVERFLOW;
}
p->data = e;
p->next = S;
S = p;
return OK;
}
//链栈的出栈
Status Pop (LinkStack &S,SElemType & e)
{
SNode *p;
if(!S)
return ERROR;
e = S->data;
p = S;
S = S->next;
delete p;
return OK;
}
int main()
{
LinkStack s;
SElemType e;
cout<<"进栈元素依次为:"<<endl;
if(InitStack(s)==OK)
for(int j=1;j<=25;j++)
{
Push(s,j);
cout<<j<<" ";
}
cout<<endl<<"依次弹出的栈顶元素为:"<<endl;
while(Pop(s,e) == OK)
{
cout<<e<<" ";
}
return 0;
}
实验结果:
2. 采用链式存储实现栈的初始化、入栈、出栈操作。
存储结构:
typedef int Status;
typedef int SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
程序;
#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//顺序栈的初始化
Status InitStack(SqStack &S)
{
S.base = new SElemType[MAXSIZE];
if(!S.base)
exit (OVERFLOW);
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
//顺序栈的入栈
Status Push(SqStack &S,SElemType &e)
{
if(S.top-S.base==S.stacksize)
return ERROR;
*(S.top++) = e; //元素e压入栈顶,栈顶指针加1
return OK;
}
//顺序栈的出栈
Status Pop(SqStack &S,SElemType &e)
{
if(S.base == S.top)
return ERROR;
e = *(--S.top); //栈顶指针减1,将栈顶元素赋给e
return OK;
}
int main()
{
SqStack s;
SElemType e;
SElemType t;
cout<<"进栈元素依次为:"<<endl;
if(InitStack(s)==OK)
for(int j=1;j<=12;j++)
{
Push(s,j);
cout<<j<<" ";
}
cout<<endl<<"依次弹出的栈顶元素为:"<<endl;
while(s.base!=s.top)
{
Pop(s,t);
cout<<t<<" ";
}
return 0;
}
实验结果:
3. 采用链式存储实现队列的初始化、入队、出队操作。
存储结构:
typedef int QElemType;
typedef int Status;
typedef struct QNode{
QElemType data;
QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
程序:
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int QElemType;
typedef int Status;
typedef struct QNode{
QElemType data;
QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
//链队的初始化
Status InitQueue(LinkQueue &Q)
{
Q.front = new QNode;
if(Q.front == NULL)
{
return OVERFLOW;
}
Q.front->next = NULL;
Q.rear = Q.front;
return OK;
}
//链队的入队
Status EnQueue(LinkQueue &Q,QElemType e)
{
QNode *p = new QNode;
if(p == NULL)
{
return OVERFLOW;
}
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p; // 修改队尾指针
return OK;
}
//链队的出队
Status DeQueue(LinkQueue &Q,QElemType &e)
{
if(Q.front == Q.rear)
{
return ERROR;
}
QNode *p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if(Q.rear == p)
{
Q.rear = Q.front;
}
delete p;
return OK;
}
int main()
{
LinkQueue Q;
QElemType e;
cout<<"进链队的元素依次为:"<<endl;
if(InitQueue(Q)==OK)
for(int j=1;j<=30;j++)
{
EnQueue(Q,j);
cout<<j<<" ";
}
cout<<endl<<"依次出链队的元素为:"<<endl;
while(DeQueue(Q,e) == OK)
{
cout<<e<<" ";
}
return 0;
}
实验结果:
4.采用顺序存储实现循环队列的初始化、入队、出队操作。
存储结构:
typedef int QElemType;
typedef int Status;
typedef struct{
QElemType *base;
int front; //头指针
int rear; //尾指针
}SqQueue;
程序:
#include<iostream>
using namespace std;
#define MAXQSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int QElemType;
typedef int Status;
typedef struct{
QElemType *base;
int front; //头指针
int rear; //尾指针
}SqQueue;
//循环队列的初始化
Status InitQueue(SqQueue &Q)
{
Q.base = new QElemType[MAXQSIZE];
if(!Q.base)
{
return OVERFLOW;
}
Q.front = Q.rear=0;
return OK;
}
//循环队列的入队
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%MAXQSIZE == Q.front)
{
return ERROR;//队列满
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear+1)%MAXQSIZE;
return OK;
}
//循环队列的出队
Status DeQueue(SqQueue &Q,QElemType &e)
{
if(Q.rear == Q.front)
{
return ERROR;
}
e = Q.base[Q.front];
Q.front = (Q.front+1)%MAXQSIZE;
return OK;
}
int main()
{
SqQueue Q;
QElemType e;
cout<<"进循环队列的元素依次为:"<<endl;
if(InitQueue(Q)==OK)
for(int j=1;j<=18;j++)
{
EnQueue(Q,j);
cout<<j<<" ";
}
cout<<endl<<"依次出队的元素为:"<<endl;
while(Q.rear != Q.front)
{
DeQueue(Q,e);
cout<<e<<" ";
}
return 0;
}
实验结果:
存储结构:
typedef int Status;
typedef int SElemType;
typedef struct SNode{
SElemType data;
struct SNode *next;
}SNode,*LinkStack;
程序:
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int Status;
typedef int SElemType;
typedef struct SNode{
SElemType data;
struct SNode *next;
}SNode,*LinkStack;
//链栈的初始化
Status InitStack (LinkStack &S)
{
S=NULL;
return OK;
}
// 链栈的入栈
Status Push(LinkStack &S , SElemType e)
{
SNode *p = new SNode;
if(!p)
{
return OVERFLOW;
}
p->data = e;
p->next = S;
S = p;
return OK;
}
//链栈的出栈
Status Pop (LinkStack &S,SElemType & e)
{
SNode *p;
if(!S)
return ERROR;
e = S->data;
p = S;
S = S->next;
delete p;
return OK;
}
int main()
{
LinkStack s;
SElemType e;
cout<<"进栈元素依次为:"<<endl;
if(InitStack(s)==OK)
for(int j=1;j<=25;j++)
{
Push(s,j);
cout<<j<<" ";
}
cout<<endl<<"依次弹出的栈顶元素为:"<<endl;
while(Pop(s,e) == OK)
{
cout<<e<<" ";
}
return 0;
}
实验结果:
2. 采用链式存储实现栈的初始化、入栈、出栈操作。
存储结构:
typedef int Status;
typedef int SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
程序;
#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 100
typedef int Status;
typedef int SElemType;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
//顺序栈的初始化
Status InitStack(SqStack &S)
{
S.base = new SElemType[MAXSIZE];
if(!S.base)
exit (OVERFLOW);
S.top = S.base;
S.stacksize = MAXSIZE;
return OK;
}
//顺序栈的入栈
Status Push(SqStack &S,SElemType &e)
{
if(S.top-S.base==S.stacksize)
return ERROR;
*(S.top++) = e; //元素e压入栈顶,栈顶指针加1
return OK;
}
//顺序栈的出栈
Status Pop(SqStack &S,SElemType &e)
{
if(S.base == S.top)
return ERROR;
e = *(--S.top); //栈顶指针减1,将栈顶元素赋给e
return OK;
}
int main()
{
SqStack s;
SElemType e;
SElemType t;
cout<<"进栈元素依次为:"<<endl;
if(InitStack(s)==OK)
for(int j=1;j<=12;j++)
{
Push(s,j);
cout<<j<<" ";
}
cout<<endl<<"依次弹出的栈顶元素为:"<<endl;
while(s.base!=s.top)
{
Pop(s,t);
cout<<t<<" ";
}
return 0;
}
实验结果:
3. 采用链式存储实现队列的初始化、入队、出队操作。
存储结构:
typedef int QElemType;
typedef int Status;
typedef struct QNode{
QElemType data;
QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
程序:
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int QElemType;
typedef int Status;
typedef struct QNode{
QElemType data;
QNode *next;
}QNode,*QueuePtr;
typedef struct{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
//链队的初始化
Status InitQueue(LinkQueue &Q)
{
Q.front = new QNode;
if(Q.front == NULL)
{
return OVERFLOW;
}
Q.front->next = NULL;
Q.rear = Q.front;
return OK;
}
//链队的入队
Status EnQueue(LinkQueue &Q,QElemType e)
{
QNode *p = new QNode;
if(p == NULL)
{
return OVERFLOW;
}
p->data = e;
p->next = NULL;
Q.rear->next = p;
Q.rear = p; // 修改队尾指针
return OK;
}
//链队的出队
Status DeQueue(LinkQueue &Q,QElemType &e)
{
if(Q.front == Q.rear)
{
return ERROR;
}
QNode *p = Q.front->next;
e = p->data;
Q.front->next = p->next;
if(Q.rear == p)
{
Q.rear = Q.front;
}
delete p;
return OK;
}
int main()
{
LinkQueue Q;
QElemType e;
cout<<"进链队的元素依次为:"<<endl;
if(InitQueue(Q)==OK)
for(int j=1;j<=30;j++)
{
EnQueue(Q,j);
cout<<j<<" ";
}
cout<<endl<<"依次出链队的元素为:"<<endl;
while(DeQueue(Q,e) == OK)
{
cout<<e<<" ";
}
return 0;
}
实验结果:
4.采用顺序存储实现循环队列的初始化、入队、出队操作。
存储结构:
typedef int QElemType;
typedef int Status;
typedef struct{
QElemType *base;
int front; //头指针
int rear; //尾指针
}SqQueue;
程序:
#include<iostream>
using namespace std;
#define MAXQSIZE 100
#define OK 1
#define ERROR 0
#define OVERFLOW -2
typedef int QElemType;
typedef int Status;
typedef struct{
QElemType *base;
int front; //头指针
int rear; //尾指针
}SqQueue;
//循环队列的初始化
Status InitQueue(SqQueue &Q)
{
Q.base = new QElemType[MAXQSIZE];
if(!Q.base)
{
return OVERFLOW;
}
Q.front = Q.rear=0;
return OK;
}
//循环队列的入队
Status EnQueue(SqQueue &Q,QElemType e)
{
if((Q.rear+1)%MAXQSIZE == Q.front)
{
return ERROR;//队列满
}
Q.base[Q.rear] = e;
Q.rear = (Q.rear+1)%MAXQSIZE;
return OK;
}
//循环队列的出队
Status DeQueue(SqQueue &Q,QElemType &e)
{
if(Q.rear == Q.front)
{
return ERROR;
}
e = Q.base[Q.front];
Q.front = (Q.front+1)%MAXQSIZE;
return OK;
}
int main()
{
SqQueue Q;
QElemType e;
cout<<"进循环队列的元素依次为:"<<endl;
if(InitQueue(Q)==OK)
for(int j=1;j<=18;j++)
{
EnQueue(Q,j);
cout<<j<<" ";
}
cout<<endl<<"依次出队的元素为:"<<endl;
while(Q.rear != Q.front)
{
DeQueue(Q,e);
cout<<e<<" ";
}
return 0;
}
实验结果: