본문 바로가기
알고리즘

Link stack 최종

by 안선생 2022. 10. 30.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include<iostream>
#include <stdio.h>
using namespace std;
 
struct Node
{
    char data;
    Node* next;
}*top=NULL;
 
void push(char x)
{
    Node* t;
    t = new Node;
 
    if (t == NULL)
        cout << "stack is full";
    else
    {
        t->data = x;
        t->next = top;
        top = t;
    }
}
char pop()
{
    Node* t;
    int x = -1;
    if (top == NULL)
        cout << "empty";
    else
    {
        t = top;
        top = top->next;
        x = t->data;
        delete t;
    }
    return x;
}
void Display()
{
    Node* p;
    p = top;
    while (p)
    {
        cout << p->data<< " ";
        p = p->next;
    }
}
 
int isBalanced(char exp[])
{
    int i;
    for (i = 0; exp[i] != NULL; i++)
    {
        if (exp[i] == '(')
            push(exp[i]);
        else if (exp[i] == ')')
        {
            if (top == NULL)
                return 0;
            pop();
        }
    }
    if (top == NULL)
        return 1;
    else
        return 0;
}
 
int pre(char x)
{
    if (x == '+' || x == '-')
        return 1;
    else if (x == '*' || x == '/')
        return 2;
    return 0;
}
int Operand(char x)
{
    if (x == '+' || x == '-' || x == '*' || x == '/')
        return 0;
    else
        return 1;
}
 
char* intopost(char infix[]) //후위연산자로 변환
{
    char* postfix =NULL;
    int len = strlen(infix);
    postfix = new char[len + 2];
 
    int i = 0, j = 0;
    while (infix[i])
    {
        if (Operand(infix[i]))
            postfix[j++= infix[i++];
        else
        {
            if (pre(infix[i]) > pre(top->data))
                push(infix[i++]);
            else
            {
                postfix[j++= pop();
            }
 
        }
    }
    while (top != NULL)
        postfix[j++= pop();
    postfix[j] = '\0';
    return postfix;
}
 
int Eval(char postfix[])
{
    int i = 0;
    int x1, x2, r;
 
    for (i = 0; postfix[i] != NULL; i++)
    {
        if (Operand(postfix[i]))
        {
            push(postfix[i]);
        }
        else
        {
            x2 = pop(); x1 - pop();
            switch (postfix[i])
            {
            case '+':r = x1 + x2; break;
            case '-':r = x1 - x2; break;
            case '*':r = x1 * x2; break;
            case '/':r = x1 / x2; break;
            }
            push(r);
        }
    }
    return top->data;
}
int main()
{/*
    char exp[] = "((a+b)*(c+d))";
    cout << isBalanced(exp);*/
    
    
    char infix[] = "a+b*c";
    push('#');
    //char *postfix= intopost(infix);
    //cout << postfix;
 
    char postfix[] = "234*+82/-";
    Eval(postfix);
    return 0;
cs

'알고리즘' 카테고리의 다른 글

Queue C++  (0) 2022.11.07
Queue  (0) 2022.11.07
Link stack C++  (0) 2022.10.30
ADT stack  (0) 2022.10.30
c언어 stack  (0) 2022.10.29