/ Published in: C
Expand |
Embed | Plain Text
#include <stdio.h> #include <stdlib.h> #include <conio.h> typedef int T; struct StackCell{ T info; StackCell *legatura; }; StackCell *top; int cSize; void init(); int push(T); void afisT(T); void afisare(); T pop(); int isEmpty(); void cinT(T *); int search(T); int replace(T,T); int replaceAll(T,T); int main(){ init(); T info,info1; int opt = 1,flag=1; while(opt>0){ scanf("%d",&opt); switch(opt){ case 0:{ opt=0; break; } add: case 1:{ cinT(&info); if(push(info)){ }else{ } break; } default:{ break; } case 2:{ if(isEmpty()>0){ afisT(pop()); }else{ } break; } case 3:{ if(cSize>0){ afisare(); }else{ } break; } case 4:{ cinT(&info); int i=search(info); if(i>=0){ }else{ } break; } case 5:{ flag=1; if(cSize>0){ while(flag>0){ cinT(&info); if(search(info)>=0) flag=0; else{ } } cinT(&info1); if(replace(info,info1)){ }else{ } }else{ goto add; } break; } } } getch(); return 0; } int replace(T initial,T final){ StackCell *aux=top; int i=0; if(search(initial)>=0){ while(i<search(initial)){ aux=aux->legatura; i++; } aux->info=final; return 1; } return 0; } int search(T info){ StackCell *aux=top; int i=0; while(aux!=NULL && aux->info!=info && aux->legatura!=NULL){ aux=aux->legatura; i++; } if(aux->info == info){ return i; }else{ return -1; } } void init(){ top=NULL; cSize=0; } int push(T info){ StackCell *aux=new StackCell; if(aux==NULL){ return 0; }else{ aux->info=info; aux->legatura=top; top=aux; cSize++; return 1; } } void afisT(T n) { } void afisare(){ StackCell *contor=top; while(contor!=NULL){ afisT(contor->info); contor=contor->legatura; } } T pop(){ T buffer; if(top!=NULL){ buffer=top->info; StackCell *aux=top; top=aux->legatura; delete aux; if(cSize>0){ cSize--; }else{ cSize=0; } return buffer; } return -1; } int isEmpty(){ if(top == NULL) return 0; else return 1; } void cinT(T *n){ scanf("%d",n); }
You need to login to post a comment.
