数据结构讲题

本文介绍了如何使用C语言手动实现栈数据结构,并通过具体示例展示了栈的基本操作,包括压栈、弹栈、判断是否为空及获取栈顶元素等。此外还提供了两个基于该栈的应用案例。


自己封装的栈

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

struct stacks
{
    struct node
    {
        char data;
        struct node *next;
    }*head;
    stacks()
    {
        head = (struct node*)malloc(sizeof(struct node));
        head->next = NULL;
    }
    bool empty()
    {
        if ( head->next )
            return 0;
        return 1;
    }
    void pop()
    {
        struct node *p;
        p = (struct node*)malloc(sizeof(struct node));
        p = head->next;
        if ( p )
        {
            head->next = p->next;
            free(p);
        }
    }
    void push(char n)
    {
        struct node *p;
        p = (struct node*)malloc(sizeof(struct node));
        p->data = n;
        p->next = head->next;
        head->next = p;
    }
    char top()
    {
        if ( head->next )
        {
            return head->next->data;
        }
        return 0;
    }
};

char ch[305];
stacks s;
stacks s1;

int main()
{
    int i;
    while ( ~scanf ( "%s", ch ) )
    {
        int len = strlen(ch);
        for ( i = 0;i < len; i++ )
        {
            if ( ch[i] == '#' )
            {
                if ( !s.empty() )
                {
                    s.pop();
                }
            }
            else if ( ch[i] == '@' )
            {
                while ( !s.empty() )
                    s.pop();
            }
            else
                s.push(ch[i]);
        }
        while ( !s.empty() )
        {
            s1.push(s.top());
            s.pop();
        }
        while ( !s1.empty() )
        {
            printf ( "%c", s1.top() );
            s1.pop();
        }
        printf ( "\n" );
    }
}

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

int len = 0;
struct stacks
{
    struct node
    {
        int data;
        struct node *next;
    }*head;
    stacks()
    {
        head = (struct node*)malloc(sizeof(struct node));
        head->next = NULL;
    }
    bool empty()
    {
        if ( head->next )
            return 0;
        return 1;
    }
    void pop()
    {
        struct node *p;
        p = (struct node*)malloc(sizeof(struct node));
        p = head->next;
        if ( p )
        {
            head->next = p->next;
            len--;
            free(p);
        }
    }
    void push(int n)
    {
        struct node *p;
        p = (struct node*)malloc(sizeof(struct node));
        p->data = n;
        p->next = head->next;
        len++;
        head->next = p;
    }
    int top()
    {
        if ( head->next )
        {
            return head->next->data;
        }
        return 0;
    }
    int size()
    {
        return len;
    }
};

stacks s;

void inti()
{
    while ( !s.empty() )
    {
        s.pop();
    }
}

int main()
{
    int n, m;
    int T, i;
    char ch[12];
    scanf ( "%d", &T );
    while ( T-- )
    {
        inti();
        scanf ( "%d %d", &n, &m );
        for ( i = 0; i < m; i++ )
        {
            scanf ( "%s", ch );
            if ( strcmp(ch, "A") == 0  )
            {
                if ( s.empty() )
                    printf ( "E\n" );
                else
                    printf ( "%d\n", s.top() );
            }
            else if ( strcmp(ch, "P") == 0 )
            {
                int t;
                scanf ( "%d", &t );
                if ( s.size() == n )
                    printf ( "F\n" );
                else
                    s.push(t);
            }
            else if ( strcmp(ch, "O") == 0 )
            {
                if ( s.empty() )
                    printf ( "E\n" );
                else
                {
                    printf ( "%d\n", s.top() );
                    s.pop();
                }
            }
        }
        if ( T != 0 )
            printf ( "\n" );
    }
    return 0;
}

代码菜鸟,如有错误,请多包涵!!!

如有帮助记得支持我一下,谢谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值