数据结构讲题

来源:互联网 发布:sftp自定义端口号 编辑:程序博客网 时间:2024/06/06 23:51


自己封装的栈

#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;}

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

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

0 0
原创粉丝点击