栈和队列的操作

1.顺序栈的操作

#include <iostream>
using namespace std;
#define MAXSIZE 100
#define OK 1
#define ERROR 0
typedef int SElemType ;
typedef int Status;
typedef struct{
    SElemType *base;
    SElemType *top;///栈顶指针
    int stacksize;///当前已分配的存储空间
}SqStack;
///顺序栈初始化
Status InitStack(SqStack &S)
{///构造一个空栈
    S.base=new SElemType[MAXSIZE];
    S.top=S.base;///空栈
    S.stacksize=MAXSIZE;///栈空标志
    return OK;
}
///判断顺序栈是否为空
bool StackEmpty(SqStack S)
{
    if(S.top==S.base)
        return true;
    else return false;
}
///求顺序栈的长度
Status StackLength(SqStack S)
{
    return S.top-S.base;
}
///入栈
Status push(SqStack &S, SElemType e)
{
    if(S.top-S.base==MAXSIZE)
        return ERROR;
    *S.top++=e;
    return OK;
}
///出栈
Status pop(SqStack &S, SElemType &e)
{
    if(S.top==S.base)
        return ERROR;
    --S.top;
    e=*S.top;
    return OK;
}
///取栈顶元素
Status GetTop(SqStack S, SElemType &e)
{
    if(S.top==S.base)
        return ERROR;
    e=*(S.top-1);
    return OK;
}
int main()
{
    SqStack S;
    InitStack(S);
    int len;
    int e;
    int a;
    cout << "输入顺序栈长度:";
    cin >> len;
    cout << "输入入栈元素";
    for(int i=0;i<len;i++)
    {
        cin >> a;
        push(S,a);///入栈
    }
    GetTop(S,e);///获得栈顶元素
    cout << "输出栈顶元素:" << e << endl;
    pop(S,e);///出栈
    cout << "输出出栈元素:" << e << endl;
    cout << StackLength(S) << endl;
    return 0;
}

2.链栈的操作

#include <iostream>
#include <stdlib.h>
using namespace std;
typedef int SElemType;
typedef int Status;
#define OK 1
#define ERROR 0
///链栈只在头部进行操作,没有必要附加头结点
typedef struct StackNode
{
    SElemType data;///数据域
    struct StackNode *next;///指针域
}StackNode,*LinkStack;
///初始化
Status InitStack(LinkStack &S)
{
    S=NULL;
    return OK;
}
///判断链栈是否为空
bool StackEmpty(LinkStack S)
{
    if(S==NULL)
        return true;
    else
        return false;
}
///进栈
Status Push(LinkStack &S,SElemType e)
{
    LinkStack p;
    p=(StackNode*)malloc(sizeof(StackNode));
    p->data=e;
    p->next=S;
    S=p;
    return OK;
}
///出栈
Status Pop(LinkStack &S,SElemType &e)
{
    LinkStack p;
    if(S==NULL)
        return ERROR;
    e=S->data;
    p=S;
    S=p->next;
    free(p);
    return OK;
}
///取栈顶元素
SElemType GetTop(LinkStack S)
{
    if(S!=NULL)
        return S->data;
}
int main()
{
    LinkStack S;
    InitStack(S);
    int e;
    cout << "输入入栈元素:";
    cin >> e;///输入入栈元素(也可以用一个循环)
    Push(S,e);///入栈
    cin >> e;
    Push(S,e);
    if(StackEmpty(S))
    	cout << "栈为空" << endl;
    else cout << "栈不为空" << endl;
    cout << "输出栈顶元素:" << GetTop(S) << endl;
    Pop(S,e);///出栈
    cout << "输出出栈元素:" << e << endl;
    return 0;
}

3.链队列的操作

#include <iostream>
using namespace std;
#define OK 1
#define ERROR 0
typedef int Status;
typedef int QElemType;
typedef struct QNode
{
    QElemType data;
    struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
    QueuePtr front;///队头指针
    QueuePtr rear;///队尾指针
}LinkQueue;
///初始化
Status InitQueue(LinkQueue &Q)
{///构造一个空队列Q
    Q.front=Q.rear=new QNode;
    Q.front->next=NULL;
    return OK;
}
///销毁队列
Status DestroyQueue(LinkQueue &Q)
{
    while(Q.front)
    {
        Q.rear=Q.front->next;
        delete Q.front;
        Q.front=Q.rear;
    }
    return OK;
}
///求链队列的队头元素
Status GetHad(LinkQueue Q,QElemType &e)
{
    if(Q.front==Q.rear) return ERROR;///判断是否为空队列
    e=Q.front->next->data;
    return OK;
}
///入队
Status EnQueue(LinkQueue &Q,QElemType e)
{
    QueuePtr p;
    p=new QNode;
    p->data=e;
    p->next=NULL;
    Q.rear->next=p;///修改队尾结点的指针
    Q.rear=p;///移动队尾指针
    return OK;
}
///出队
Status DeQueue(LinkQueue &Q,QElemType &e)
{
    QueuePtr p;
    if(Q.front==Q.rear)
        return ERROR;///判断队列是否为空
    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;
    InitQueue(Q);
    int e;
    int a;
    cout << "输入入队元素:";
    cin >> e;
    EnQueue(Q,e);
    cin >> e;
    EnQueue(Q,e);
    GetHad(Q,a);
    cout << "此时队头元素为:" << a << endl;
    DeQueue(Q,e);
    cout << "输出出队元素:" << e << endl;
    GetHad(Q,a);
    cout << "此时队头元素为:" << a << endl;
    return 0;
}

4.循环队列的操作

#include <iostream>
#include <stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define MAXSIZE 100
typedef int Status;
typedef int QElemType;
typedef struct
{
    QElemType *base;///存储空间的基地址
    int front;///头指针
    int rear;///尾指针
}SqQueue;
///初始化
Status InitQueue(SqQueue &Q)
{
    Q.base=new QElemType[MAXSIZE];///为队列分配一个最大容量为MAXSIZE的数组空间
    if(!Q.base)
        exit(OVERFLOW);///存储分配失败
    Q.front=Q.rear=0;///头指针和尾指针置为0,队列为空
    return OK;
}
///循环队列的长度
Status QueueLength(SqQueue Q)
{
    return (Q.rear-Q.front+MAXSIZE)%MAXSIZE;///Q的元素个数即队列的长度
}
///循环队列入队
Status EnQueue(SqQueue &Q, QElemType e)
{
    if((Q.rear+1)%MAXSIZE==Q.front)
        return ERROR;///判断是否队满
    Q.base[Q.rear]=e;///新元素插入队尾
    Q.rear=(Q.rear+1)%MAXSIZE;///队尾指针加1
    return OK;
}
///循环队列出队
Status DeQueue(SqQueue &Q,QElemType &e)
{
    if(Q.front==Q.rear)
        return ERROR;///判断队列是否为空
    e=Q.base[Q.front];///保存队头元素
    Q.front=(Q.front+1)%MAXSIZE;///队头元素加1
    return OK;
}
///取队头元素
Status GetHad(SqQueue Q,QElemType &e)
{
    if(Q.front==Q.rear)
        return ERROR;///队空
    e=Q.base[Q.front];
    return OK;
}
int main()
{
    SqQueue Q;
    InitQueue(Q);
    int e;
    cout << "输入入队元素:";
    cin >> e;
    EnQueue(Q,e);
    cin >> e;
    EnQueue(Q,e);
    cin >> e;
    EnQueue(Q,e);
    cout << "队列的长度:" << QueueLength(Q) << endl;
    GetHad(Q,e);
    cout << "输出此时的队头元素:" << e << endl;
    DeQueue(Q,e);
    cout << "输出出队元素:" << e << endl;
    GetHad(Q,e);
    cout << "输出此时的的队头元素:" << e << e
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值