栈与队列相关

栈的基础操作:

#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstring>
using namespace std;

const int maxnsize=100;

typedef struct Stack
{
    int* data;
    int top;
    int maxnsize;
}SqStack;

void InitStack(SqStack &s, int n)
{
    s.data = new int [n];
    s.top = -1;
    s.maxnsize = n;
}

bool StackEmpty(SqStack &s)
{
    if(s.top == -1)
        return true;
    return false;
}

bool Push(SqStack &s, int x)
{
    if(s.top == s.maxnsize-1)
        return false;
    s.data[++s.top] = x;
    return true;
}

bool Pop(SqStack &s, int &x)
{
    if(s.top == -1)
        return false;
    x = s.data[s.top--];
    return true;
}

bool GetTop(SqStack &s, int &x)
{
    if(s.top == -1)
        return false;
    x = s.data[s.top];
    return true;
}

///3.2)编写算法,判定所给的操作序列是否合法,合法返回true
int The_three_problem(char c[])
{
    ///两个点,出栈操作是否合法。终态是否为空.
    int i=0;
    int p=0, q=0;
    while(c[i] != '\0')
    {
        if(c[i] == 'I')
        {
            p++;
        }
        else if(c[i] == 'O')
        {
            q++;
        }
        if(q>p)
            return 0;
        i++;
    }
    cout<<p<<" "<<q<<endl;
    if(p != q)
        return -1;
    return 1;
}

int main()
{
    ios::sync_with_stdio(false);
    //char a[] = {'I', 'O', 'I', 'I', 'O', 'I', 'O', 'O'};
    //char b[] = {'I', 'O', 'O', 'I', 'O', 'I', 'I', 'O'};
    //char c[] = {'I', 'I', 'I', 'O', 'I', 'O', 'I', 'O'};
    char d[] = {'I', 'I', 'I', 'O', 'O', 'I', 'O', 'O'};
   // cout<<The_three_problem(a)<<endl;
    //cout<<The_three_problem(b)<<endl;
   // cout<<The_three_problem(c)<<endl;
    cout<<The_three_problem(d)<<endl;
    return 0;
}

两个栈共享存储空间,进栈出栈代码如下(王道第五题):

#include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstring>
using namespace std;

const int maxnsize=100;

typedef struct Stack
{
    int data[maxnsize];
    int top[2];
}Stk;

void InitStack(Stk &s)
{
    s.top[0] = -1;
    s.top[1] = maxnsize;
}

int push(Stk &s, int i, int x)
{
    if(i<0 || i>1)
        return 0;
    if(s.top[1] - s.top[0] == 1)
        return 0;
    switch(i){
        case 0:s.data[++s.top[0]] = x;break;
        case 1:s.data[--s.top[1]] = x;break;
    }
    return 1;
}

int Pop(Stk &s, int i, int &x)
{
    if(i<0 || i>1)
        return 0;
    switch(i)
    {
    case 0:
        if(s.top[0] == -1)
            return 0;
        x = s.data[s.top[0]--];
        break;
    case 1:
        if(s.top[2] == maxnsize)
            return 0;
        x = s.data[s.top[1]++];
        break;
    }
    return 1;
}

int main()
{
    ios::sync_with_stdio(false);
    return 0;
}

循环队列基础操作

#include <iostream>
#include <algorithm>
#include <string>
#define maxnsize 100
using namespace std;

typedef struct Sequence
{
    int data[maxnsize];
    int front, rear;
}SqQueue;

///循环队列的各种操作
void InitQueue(SqQueue &q)
{
    q.rear = q.front = 0;
}

bool isEmpty(SqQueue &q)
{
    if(q.rear == q.front)
        return true;
    return false;
}
///进队操作
bool Enqueue(SqQueue &q, int x)
{
    if((q.rear+1) % maxnsize == q.front)
            return false;
    q.data[q.rear] = x;
    q.rear = (q.rear+1)%maxnsize;
    return true;
}
///出队操作
bool Dequeue(SqQueue &q, int &x)
{
    if(q.rear == q.front)
        return false;
    x = q.front;
    q.front = (q.front+1)%maxnsize;
    return true;
}


int main()
{
    ios::sync_with_stdio(false);

    return 0;
}

 

队列的链式存储相关操作:

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

typedef struct LinkNode
{
    int data;
    LinkNode *next;
}LNode, *LinkSQ;

typedef struct Lisquence
{
    LinkSQ front, rear;
}LinkQueue;

///初始化
void InitQueue(LinkQueue &q)
{
    q.front = q.rear = new LNode;
    q.front ->next = NULL;
}

///判空
bool isEmpty(LinkQueue &q)
{
    if(q.front == q.rear)
        return true;
    return false;
}

///入队
void Enqueue(LinkQueue &q, int x)
{
    LinkSQ s = new LinkNode;
    s->data = x;
    s->next = NULL;
    q.rear ->next = s;
    q.rear = s;
}

///出队
bool Dequeue(LinkQueue &q, int &x)
{
    if(q.rear == q.front)
        return false;
    LinkSQ p = q.front->next;
    x = p->data;
    q.front ->next = p->next;
    if(q.rear == p)
        q.rear = q.front;
    free(p);
    return true;
}

int main()
{
    ios::sync_with_stdio(false);

    return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西杭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值