2 catcheryp Catcheryp 于 2016.03.06 12:40 提问

求高手帮忙解答一下关于程序的题目,纠结一上午,跪求帮忙!
 #include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct node *PtrToNode;
typedef PtrToNode stack;
typedef PtrToNode Position;

struct node
{
    char *symbol;
    Position next;
};

int Isempty(stack s)
{
    return s->next == NULL;
}

void Pop(stack s)
{
    Position first;
    if(Isempty(s))
        printf("Empty stack!\n");
    else
    {
        first = s->next;
        s->next = s->next->next;
        free(first);
    }
}

void MakeEmpty(stack s)
{
    if(s == NULL)
        printf("Must use CreatStack first!\n");
    else
        while(!Isempty(s))
            Pop(s);
}


stack CreatStack()
{
    stack s = (node *)malloc(sizeof(struct node));
    if(s == NULL)
        printf("Out of space!\n");
    s->next = NULL;
    MakeEmpty(s);

    return s;
}

void Push(char *sign, stack s)
{
    Position temp = (node *)malloc(sizeof(struct node));

    if(temp == NULL)
        printf("Out of space!\n");
    else
    {
        temp->symbol = sign;
        temp->next = s->next;
        s->next = temp;
    }
}

char* Top(stack s)
{
    if(!Isempty(s))
        return s->next->symbol;
    printf("Empty stack!\n");
    return 0;
}


int main()
{
    char *sign1;
//   char *sign2 = ")";
//   char *sign3 = "begin";
//   char *sign4 = "end";
//   char *sign5 = "[";
//   char *sign6 = "}";


(1) stack s = CreatStack();

    while(scanf("%s", sign1) == 1)
    {
//(2)       stack s = CreatStack();
        if(Isempty(s))
        {
(3)         if(strcmp(sign1, "(") ==  0)
                printf("PPPPPPPPPPPP\n");
            if(strcmp(sign1, "(") == 0 || strcmp(sign1, "[") == 0 || strcmp(sign1, "{") == 0 || strcmp(sign1, "begin") == 0)
            {
                Push(sign1, s);
            }
            else 
            {
                printf("AWrong!!!\n");
                break;
            }

        }
        else
        {
            if(strcmp(sign1, "(") == 0 || strcmp(sign1, "[") == 0 || strcmp(sign1, "{") == 0 || strcmp(sign1, "begin") == 0)
            {
                Push(sign1, s);
                printf("EEEEEEEE\n");
            }
            else if(strcmp(sign1, ")") == 0 || strcmp(sign1, "]") == 0 || strcmp(sign1, "}") == 0 || strcmp(sign1, "end") == 0)
            {
                printf("DDDDDDD\n");
                if((strcmp(Top(s), "(") == 0 && strcmp(sign1, ")") == 0) || (strcmp(Top(s), "[") == 0 && strcmp(sign1, "]") == 0)||
                   (strcmp(Top(s), "{") == 0 && strcmp(sign1, "}") == 0) || (strcmp(Top(s), "begin") == 0 && strcmp(sign1, "end") == 0))
                {
                    Pop(s);
                    printf("GGGGGGGGGGGGGGGGGGG\n");
                }
                else
                {
                    printf("BWrong!!!\n");
                    break;
                }
            }
        }
    }

        if(Isempty(s))
            printf("You are right!\n");
        else
            printf("CWrong!!!\n");
        printf("%s\n", Top(s));
    return 0;
}

我搞不明白当stack s在while之前的话,只输入“(”,应该会执行(3)处的if,可事实是不执行

然后我换了下stack s的位置就可以执行了,为什么???

3个回答

bealing
bealing   Rxr 2016.03.06 12:52
已采纳

char *sign1;你没有为sign1动态分配内存,scanf得到的数据往哪存呀?你可以用char sign1[10];简单测试一下

mouyong
mouyong   2016.03.06 12:56

因为你在换位置之前没保存?

Catcheryp
Catcheryp 看我采纳的答案,一楼说的对!
接近 2 年之前 回复
WinsenJiansbomber
WinsenJiansbomber   2016.03.06 13:15

这数据类型定义和使用也太混乱了:

 typedef struct node *PtrToNode;
typedef PtrToNode stack;

     Position temp = (node *)malloc(sizeof(struct node));
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!