Documente Academic
Documente Profesional
Documente Cultură
Stiva
Stiva este un caz particular de lista care funcționează pe principiul LIFO (last in – first out,
ultimul intrat este primul servit, puteți sa va gândiți la o stiva de materiale pentru care un nou
material se va adăuga întotdeauna deasupra si se va extrage tot de deasupra). Prin urmare
principalele prelucrări care se refera la aceasta structura de date vor fi:
- creare stiva
- listare stiva (parcurgere in ordine inversa creării)
- adăugare la sfârșit ( peste vârful stivei, operație numita push ( ) )
- ștergere element din vârful stivei (operație numita pop( ) )
- prelucrarea vârfului stivei (operație numita top() )
Alocare statica
struct Stiva
{
int st[Nmax];
int top;
void Init(){
top = - 1;
}
int Empty(){
if(top == -1) return 1;
return 0;
}
int Full(){
if(top == Nmax-1) return 1;
return 0;
}
int Top(){
return st[top];
}
void Push(int x){
if(Full())
cout << "Stack overflow error";
else
{
top++;
st[top] = x;
}
}
void Pop(){
if(!Empty())
top--;
}
};
Alocare dinamica
struct nod
Lectia nr. 1. – Stiva. Evaluarea expresiilor.
{
int info;
nod *urm;V
};
nod *vf;
Stiva in STL
Se foloseste <stack>
stack<int> st;
st.push(x); -adăugare nod in vârf
st.empty(); -verificare daca e vida
st.pop(); -ștergere nod din vârf
st.top(); -accesare element din vârf
st.size(); -dimensiune stiva
stack<int>st;
int n,x;
char s[5];
cin>>n;
cin.get();
for(;n;n--)
{
cin>>s;
if(strcmp(s,"push")==0)
{
cin>>x;
cin.get();
st.push(x);
}
else if(strcmp(s,"pop")==0)
{
if(!st.empty()) st.pop();
}
else if(!st.empty()) cout<<st.top()<<"\n";
}
return 0;
}
Observații:
• se tine cont de ordinea efectuării operațiilor, vor fi setate priorități
• pentru operatorii care nu pot fi folosiți la un moment dat (datorita ordinii
efectuării operațiilor), se va folosi stiva
int main() {
string expresie;
getline(cin,expresie);
string polon=InfixToPostfix(expresie);
cout<<polon;
return 0;
}
bool Termen(char C) {
if(C >= '0' && C <= '9' || C >= 'a' && C <= 'z' || C >=
'A' && C <= 'Z') return true;
return false;
}
bool Semn(char C) {
if(C == '+' || C == '-' || C == '*' || C == '/' )return
true;
return false;
}
Lectia nr. 1. – Stiva. Evaluarea expresiilor.
int main()
{
string expresie;
getline(cin,expresie);
int result = EvaluatePostfix(expresie);
cout<<result<<"\n";
}
stack<int> S;
else if(IsOperator(expresie[i])) {
i++;
}
i--;
S.push(operand);
}
}
return S.top();
}
bool IsNumericDigit(char C)
{
if(C >= '0' && C <= '9') return true;
return false;
}
bool IsOperator(char C)
{
if(C == '+' || C == '-' || C == '*' || C == '/')
return true;
return false;
}
else cout<<"Eroare\n";
return -1;
}
Lectia nr. 1. – Stiva. Evaluarea expresiilor.
int main()
{
while(fin>>c)
{
if(c>='0'&&c<='9')
{
gasitnr=1;
nr=nr*10+(c-'0');
}
else
{
if(gasitnr)
{
fp[++n]=nr;
nr=0;
gasitnr=0;
}
if(c=='(' ||c=='+'||c=='-'||c=='*'||c=='/')
{
st[++k]=c;
if(st[k]=='+'||st[k]=='-')
Lectia nr. 1. – Stiva. Evaluarea expresiilor.
{
while(st[k-1]=='+'||st[k-1]=='-'|| st[k-1]=='*'||
st[k-1]=='/')
{
semn[++n]=st[k-1];
st[k-1]=st[k];
k--;
}
}
else
if(st[k]=='*'||st[k]=='/')
{
while(st[k-1]=='*'||st[k-1]=='/')
{
semn[++n]=st[k-1];
st[k-1]=st[k];
k--;
}
}
}
else
if(c==')')
{
while(st[k]!='(')
{
semn[++n]=st[k];
k--;
}
k--;
}
}
}
if(gasitnr)
{
fp[++n]=nr;
Lectia nr. 1. – Stiva. Evaluarea expresiilor.
nr=0;
gasitnr=0;
}
while(k)
{
semn[++n]=st[k];
k--;
}
k=0;
for(i=1;i<=n;++i)
{
if(semn[i]!='+'&&semn[i]!='-'&&semn[i]!='*'&&semn[i]!='/')
steval[++k]=fp[i];
else
{
switch(semn[i])
{
case '+':steval[k-1]=steval[k-1]+steval[k]; break;
case '-':steval[k-1]=steval[k-1]-steval[k]; break;
case '*':steval[k-1]=steval[k-1]*steval[k]; break;
case '/':steval[k-1]=steval[k-1]/steval[k]; break;
}
k--;
}
}
fout<<steval[1];
return 0;
}
O alta varianta :
In aplicația care urmează, structura de stocare expresiei matematice in scriere
postfixata este coada.
#include <bits/stdc++.h>
using namespace std;
Lectia nr. 1. – Stiva. Evaluarea expresiilor.
struct NodStiva{
char opr;
NodStiva* next;
};
struct NodCoada{
int opd;
char opr;
NodCoada* next;
};
NodStiva* push(NodStiva* vf,char c){
NodStiva* nou=new NodStiva;
nou->opr=c;
nou->next=vf;
return nou;
}
while(t->next)
t=t->next;
t->next=nou;
return c;
}
}
int main(){
NodStiva* stack=NULL;
NodCoada* queue=NULL;
char ExprInfix[100], SubExpr[100], o;
int vb, vb_op=0;
SubExpr[k]=ExprInfix[i];
k++;
i++;
}
SubExpr[k]='\0';
vb=1;
}
else{
SubExpr[k]=ExprInfix[i];
SubExpr[k+1]='\0';
i++;
vb=0;
}
if(vb){
o=0;
queue=put(queue,atoi(SubExpr),o);
}
else{
if(SubExpr[0]=='('){
stack=push(stack,SubExpr[0]);
}
else{
if(SubExpr[0]==')'){
stack=pop(stack,&o);
while(o!='('){
queue=put(queue,0,o);
stack=pop(stack,&o);
}
}
else{
if(prioritate(SubExpr[0])<5){
if(stack){
while(stack && prioritate(stack-
>opr)>prioritate(SubExpr[0])){
stack=pop(stack,&o);
queue=put(queue,0,o);
Lectia nr. 1. – Stiva. Evaluarea expresiilor.
}
}
stack=push(stack,SubExpr[0]);
}
else{
cout<<"Operator incorect \
introdus!";
vb_op=1;
}
}
}
}
}
while(stack){
stack=pop(stack,&o);
queue=put(queue,0,o);
}
delete t;
}
}