数据结构之栈

栈的顺序存储

#include <iostream>
using namespace std;
typedef char ElemType;
const int MaxSize = 50;
typedef struct
{
    ElemType data[MaxSize];
    int top;
}SqStack;
void InitStack(SqStack *&s);
void DestroyStack(SqStack *&s);
void ClearStack(SqStack *&s);
bool StackEmpty(SqStack *s);
bool Push(SqStack *&s, ElemType e);
bool Pop(SqStack *&s, ElemType &e);
bool Top(SqStack *s, ElemType &e);
bool symmetry(SqStack *&s, ElemType str[]);
int main(void)
{
    SqStack *s;
    InitStack(s);
    if (StackEmpty(s))
        cout << "栈空!" << endl;
    ElemType e;
    while (cin >> e)
    {
        if (e == '0')
            break;
        Push(s, e);
    }
    while (!StackEmpty(s))
    {
        Pop(s, e);
        cout << e << ' ';
    }
    cout << endl;
    DestroyStack(s);/*
    char str[100] = "chqhc";
    if (symmetry(s, str))
        cout << "对称!" << endl;
    else
        cout << "不对称!" << endl;
    if (StackEmpty(s))
        cout << "栈空!" << endl;
    DestroyStack(s);*/
    
    return 0;
}
void InitStack(SqStack *&s)
{
    s = new SqStack;
    s->top = -1;
}
void DestroyStack(SqStack *&s)
{
    delete s;
    s = NULL;
}
void ClearStack(SqStack *&s)
{
    ElemType e;
    while (!StackEmpty(s))
        Pop(s, e);
}
bool StackEmpty(SqStack *s)
{
    return (s->top == -1);
}
bool Push(SqStack *&s, ElemType e)
{
    if (s->top == MaxSize-1)
        return false;
    s->top++;
    s->data[s->top] = e;
    return true;
}
bool Pop(SqStack *&s, ElemType &e)
{
    if (s->top == -1)
        return false;
    e = s->data[s->top];
    s->top--;
    return true;
}
bool Top(SqStack *s, ElemType &e)
{
    if (s->top == -1)
        return false;
    e = s->data[s->top];
    return true;
}
bool symmetry(SqStack *&s, ElemType str[])
{
    ElemType e;
    bool status = true;
    for (int i = 0; str[i] != '\0'; i++)
        Push(s, str[i]);
    for (int i = 0; str[i] != '\0'; i++)
    {
        Pop(s, e);
        if (e != str[i])
        {
            status = false;
            break;
        }
    }
    ClearStack(s);
    return status;
}

栈的链式存储

#include <iostream>

using namespace std;

typedef char ElemType;
const int MaxSize = 100;

typedef struct linknode
{
    ElemType data;
    struct linknode *next;
}LinkStack;

void InitStack(LinkStack *&s);
void DestroyStack(LinkStack *&s);
void ClearStack(LinkStack *&s);
bool StackEmpty(LinkStack *s);
void Push(LinkStack *&s, ElemType e);
bool Pop(LinkStack *&s, ElemType &e);
bool Top(LinkStack *s, ElemType &e);

int main(void)
{
    LinkStack *s;
    InitStack(s);
    if (StackEmpty(s))
        cout << "栈空!" << endl;
    ElemType e;
    while (cin >> e)
    {
        if (e == '0')
            break;
        Push(s, e);
    }
    while (!StackEmpty(s))
    {
        Pop(s, e);
        cout << e << ' ';
    }
    cout << endl;
    ClearStack(s);
    if (StackEmpty(s))
        cout << "栈空!" << endl;
    DestroyStack(s);
    
    return 0;
}

void InitStack(LinkStack *&s)
{
    s = new LinkStack;
    s->next = NULL;
}

void DestroyStack(LinkStack *&s)
{
    LinkStack *pre = s, *p = s->next;
    while (p != NULL)
    {
        delete pre;
        pre = p;
        p = p->next;
    }
    delete pre;
}

void ClearStack(LinkStack *&s)
{
    LinkStack *p = s->next, *q;
    s->next = NULL;
    while (p != NULL)
    {
        q = p->next;
        delete p;
        p = q;
    }
}

bool StackEmpty(LinkStack *s)
{
    return (s->next == NULL);
}

void Push(LinkStack *&s, ElemType e)
{
    LinkStack *q = new LinkStack;
    q->data = e;
    q->next = s->next;
    s->next = q;
}

bool Pop(LinkStack *&s, ElemType &e)
{
    if (s->next == NULL)
        return false;
    else
    {
        LinkStack *q = s->next;
        e = q->data;
        s->next = q->next;
        delete q;
        q = NULL;
        return true;
    }
}

bool Top(LinkStack *s, ElemType &e)
{
    if (s->next == NULL)
        return false;
    else
    {
        e = s->next->data;
        return true;
    }
}

共享栈

#include <iostream>

using namespace std;

typedef char ElemType;
const int MaxSize = 100;

typedef struct
{
    ElemType data[MaxSize];
    int top1, top2;
}DStack;

void InitStack(DStack *&s);
void DestroyStack(DStack *&s);
void ClearStack(DStack *&s, int i);
bool StackEmpty(DStack *s, int i);
bool Push(DStack *&s, ElemType e, int i);
bool Pop(DStack *&s, ElemType &e, int i);
bool Top(DStack *s, ElemType &e, int i);

int main(void)
{
    DStack *s;
    InitStack(s);
    ElemType e;
    if (StackEmpty(s, 1))
        cout << "栈1为空!" << endl;
    if (StackEmpty(s, 2))
        cout << "栈2为空!" << endl;
    while (cin >> e)
    {
        if (e == '0')
            break;
        Push(s, e, 1);
    }
    while (cin >> e)
    {
        if (e == '0')
            break;
        Push(s, e, 2);
    }
    while (!StackEmpty(s, 2))
    {
        Pop(s, e, 2);
        cout << e << ' ';
    }
    cout << endl;
    while (!StackEmpty(s, 1))
    {
        Pop(s, e, 1);
        cout << e << ' ';
    }
    cout << endl;
    if (StackEmpty(s, 1))
        cout << "栈1为空!" << endl;
    if (StackEmpty(s, 2))
        cout << "栈2为空!" << endl;
    DestroyStack(s);
    
    return 0;
}

void InitStack(DStack *&s)
{
    s = new DStack;
    s->top1 = -1;
    s->top2 = MaxSize;
}

void DestroyStack(DStack *&s)
{
    delete s;
    s = NULL;
}

bool StackEmpty(DStack *s, int i)
{
    if (i == 1)
    {
        if (s->top1 == -1)
            return true;
        else
            return false;
    }
    else
    {
        if (s->top2 == MaxSize)
            return true;
        else
            return false;
    }
}

bool Push(DStack *&s, ElemType e, int i)
{
    if (i == 1)
    {
        if (s->top1 == s->top2-1)
            return false;
        s->top1++;
        s->data[s->top1] = e;
        return true;
    }
    else
    {
        if (s->top2 == s->top1+1)
            return false;
        s->top2--;
        s->data[s->top2] = e;
        return true;
    }
}

bool Pop(DStack *&s, ElemType &e, int i)
{
    if (StackEmpty(s, i))
        return false;
    if (i == 1)
    {
        e = s->data[s->top1];
        s->top1--;
    }
    else
    {
        e = s->data[s->top2];
        s->top2++;
    }
    return true;
}

bool Top(DStack *s, ElemType &e, int i)
{
    if (StackEmpty(s, i))
        return false;
    if (i == 1)
        e = s->data[s->top1];
    else
        e = s->data[s->top2];
    return true;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值