栈的操作

 学习了数据结构之的顺序表与链表之后,再去接触栈的操作,觉得简单多了。自己就想用栈来实现下实际功能。但是遇到了麻烦(对于初学者来说),不管结果怎样,这都是一次成长的过程,虽然现在问题没有解决,但是我先把他以微博的形式记录下来,以遍我在以后能解决它。

 

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include"string.h"
typedef char NoteType;
//#include"LinkStack.h"
int Match(NoteType e,NoteType ch);
typedef struct
{
    NoteType ch[50];
    int top;
}LinkStack;
void PushStack(LinkStack s,NoteType e)
{
    if(s.top<50)
    {    
        s.ch[s.top]=e;
        s.top++;
    }
    else
    return ;
} 
void  PopStack(LinkStack s,NoteType *e)
{
    if(s.top!=0)
    {    *e=s.ch[s.top];
        s.top--;
    }
    else
    return ;
}
int StackEmpty(LinkStack s)
{
    if(s.top==0)
        return 1;
    else
        return 0;
}
NoteType GetTop(LinkStack s,NoteType *e)
{
    *e=s.ch[s.top];
    return *e;
}
 main()
{
    LinkStack s;
    NoteType ch[50];
    char *p;
    NoteType e;
    printf("输入括号:((),{},[])\n");
    gets(ch);
    p=ch;
    while(*p)
    {
        switch(*p)
        {
            case'(':
            case'[':
            case'{':
                PushStack(s,*p);
                
                break;
            case')':
            case']':
            case'}':
                if(StackEmpty(s))
                {
                    printf("在第个括号前缺少左括号。\n");
                    return 0;
                }
                else
                {
                    GetTop(s,&e);
                    if(Match(e,*p))
                    {
                        PopStack(s,&e);
                    }
                    else
                    {
                        printf("左右括号不匹配。n");
                        return 0;
                    }
                }
            default:
                p++;
        }
    
    }
    if(StackEmpty(s))
    {
        printf("已匹配。\n");
    }
    else
    {
        printf("第个括号没有右括号\n");
    }
}
int Match(NoteType e,NoteType ch)
{
    if(e=='('&&ch==')')
        return 1;
    else if(e=='['&&ch==']')
        return 1;
    else if(e=='{'&&ch=='}')
        return 1;
    else
        return 0;
}

懂得大神可以帮我看看,我属于这方面的小白。

上面是对顺序栈的操作,正确的使用方法是

typedef struct
{
    NoteType *base;
    NoteType *top;
    int stacksize;
}SqStack;
NoteType PushStack(SqStack &s,NoteType e)
{
    if(s.top-s.base==s.stacksize)  return -1;//满栈
    *s.top++=e;
    return 1;
}
NoteType PushStack(SqStack &s,NoteType &e)
{
    if(s.top==s.base)  return -1;//满栈
    e==*--s.top;
    return 1;
}

修改后(链栈):

#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>
#include"string.h"
typedef char NoteType;
//#include"LinkStack.h"
int Match(NoteType e,NoteType ch);
typedef struct StackNote
{
    NoteType date;
    struct StackNote *next;
}StackNote,*LinkStack;
NoteType InitStack(LinkStack &s)
{
    s=NULL;
    return 0;
}
NoteType  PushStack(LinkStack &s,NoteType e)
{
    LinkStack p;
    s=(LinkStack)malloc(sizeof(StackNote));
    s->next=NULL;//生成新节点
    p=(LinkStack)malloc(sizeof(StackNote));
    p->date=e;//将新节点的数据域置为e 
    p->next=s;//将新的节点插入栈 
    s=p;//修改栈指针 
    return 0; 
} 
NoteType   PopStack(LinkStack &s,NoteType &e)
{
    LinkStack p;
    if(s==NULL)
        return -1;
    e=s->date;
    p=s;
    s=s-> next;
    delete p;
    return 0; 
}
int StackEmpty(LinkStack s)
{
    if(s==NULL)
        return 1;
    else
        return 0;
}
NoteType GetTop(LinkStack s,NoteType *e)
{
    s=s->next; 
    *e=s->date;
    
    return *e;
}
 main()
{
    LinkStack s;
    NoteType ch[50];
    char *p;
    NoteType e;
    NoteType InitStack(LinkStack &s);
    printf("输入括号:((),{},[])\n");
    gets(ch);
    p=ch;
    while(*p)
    {
        switch(*p)
        {
            case'(':
            case'[':
            case'{':
                PushStack(s,*p);
                
                break;
            case')':
            case']':
            case'}':
                if(StackEmpty(s))
                {
                    printf("在第个括号前缺少左括号。\n");
                    return 0;
                }
                else
                {
                    GetTop(s,&e);
                    if(Match(e,*p))
                    {
                        PopStack(s,e);
                    }
                    else
                    {
                        printf("左右括号不匹配。n");
                        return 0;
                    }
                }
            default:
                p++;
        }
    
    }
    if(StackEmpty(s))
    {
        printf("已匹配。\n");
    }
    else
    {
        printf("第个括号没有右括号\n");
    }
}
int Match(NoteType e,NoteType ch)
{
    if(e=='('&&ch==')')
        return 1;
    else if(e=='['&&ch==']')
        return 1;
    else if(e=='{'&&ch=='}')
        return 1;
    else
        return 0;
}

 

posted @ 2015-04-12 22:14  Dawn-to-Dusk  阅读(173)  评论(0)    收藏  举报