Professional Documents
Culture Documents
Bài 1
Bài 1
#include <stdio.h>
#include <iostream>
#include <string>
struct Stack
float DATA[MAX];
int TOP;
};
S->TOP = -1;
return S->TOP == -1 ? 1 : 0;
printf("\nStack is full");
}
else
++S->TOP;
S->DATA[S->TOP] = item;
return (S->DATA[S->TOP]);
if (S->TOP == -1)
printf("\nStack is empty");
else
ret = S->DATA[S->TOP];
--S->TOP;
return ret;
int Precedence(char x)
if (x == '(')
return 0;
if (x == '+' || x == '-')
return 1 ;
return 2;
return 3;
Stack S;
char x, token;
init(&S);
token = infix[i];
if (isalnum(token))
postfix[j++] = token;
else
if (token == '(')
Push(&S, '(');
else
if (token == ')')
postfix[j++] = x;
else
{
while (Precedence(token) <= Precedence(top(&S)) && !
isEmpty(&S))
x = Pop(&S);
postfix[j++] = x;
Push(&S, token);
while (!isEmpty(&S))
x = Pop(&S);
postfix[j++] = x;
postfix[j] = '\0';
struct Stack S;
char *p;
S.TOP = -1;
p = &Postfix[0];
{
while (*p == ' ' || *p == '\t')
p++;
if (isdigit(*p))
int num = 0;
while (isdigit(*p))
*p++;
Push(&S, num);
else
op1 = Pop(&S);
op2 = Pop(&S);
switch (*p)
case '+':
break;
case '-':
break;
case '/':
break;
case '*':
break;
default:
printf("\nInvalid Operator");
return 0;
Push(&S, result);
p++;
result = Pop(&S);
return result;
int main()
printf("Infix : ");
gets(A);
InfixtoPostfix(A, B);
gets(A);
return 0;
BÀI 2:
#include <iostream>
#include <string>
#include <fstream>
class node {
public:
int data;
node *link;
node();
node(int d);
};
node::node() {
data = -1;
link = NULL;
};
node::node(int d) {
data = d;
link = NULL;
};
class stack {
private:
node *pHead;
int count;
public:
stack();
void push(int d);
node *pop();
int top();
bool isEmpty();
bool isFull();
int size();
~stack();
};
stack::stack() {
pHead = NULL;
count = 0;
};
void stack::push(int d) {
if (pHead == NULL)
if (pHead != NULL)
++count;
else
{
if (pHead != NULL)
pHead->link = pTemp;
++count;
};
node* stack::pop() {
if (pHead != NULL)
pHead = pHead->link;
pTemp->link = NULL;
--count;
return pTemp;
return NULL;
};
int stack::top() {
if (pHead != NULL)
return pHead->data;
return -1;
};
bool stack::isEmpty() {
if (pHead == NULL)
return true;
return false;
};
bool stack::isFull() {
if (pTemp == NULL)
delete pTemp;
return true;
return false;
};
stack::~stack() {
node *pTemp;
pTemp = pHead;
pHead = pHead->link;
delete pTemp;
};
int stack::size() {
return count;
};
bool isExpressionBalanced(string input) {
char x;
if (input[i]=='('||input[i]=='['||input[i]=='{')
s->push(input[i]);
continue;
if (s->isEmpty())
return false;
switch (input[i])
case ')':
x = s->top();
s->pop();
if (x=='{' || x=='[')
return false;
break;
case '}':
x = s->top();
s->pop();
if (x=='(' || x=='[')
return false;
break;
case ']':
x = s->top();
s->pop();
if (x =='(' || x == '{')
return false;
break;
int main()
return 0;
created
Oct '19
last reply
Oct '19
reply
2.3k
views
users
likes
Peppermint
KuroShiro
Oct '19
if ((input[i] == ')' || input[i] == ']' || input[i] == '}') && s->isEmpty()) return false;
return s->isEmpty();
}