Stack Struktur data dengan C++ Struktur data dengan stack Stack adalah struktur data yagn sifatn ya LIFO (Last In First Out), yaitu yang masuk belakangan akan keluar duluan. Dalam kehidupan sehari-hari dapat kita jumpai contoh stack seperti : tumpukan buku, tumpukan koin , dan lain lain. Dalam struktur data stack ada dua operasi yang digunakan pada stack, yaitu operasi push dan operasi pop. Operasi push merupakan operasi memasukkan elemen ke dalam stack, sedangkan operasi pop merupakan operasi mengeluarkan elemen ke dalam stack. Berikut ini contoh implementasi program stack dengan array. Deklarasi Class #include<"iostream.h"> #include<"conio.h"> #include<"stdio.h"> #define max_stack 10 class Stack { private: char data[max_stack]; int top; public: Stack(){ top = -1;} int isFull(); int isEmpty(); void push(); void pop(); void print(); }; Implementasi method isFull() dan isEmpty(). Method isEmpty() digunakan untuk mengecek apakah stack dalam keadaan kosong atau sudah berisi, sedangkan method isFull() digunakan untuk mengecek apakah stack sudah dalam keadaan penuh. int Stack :: isFull() { if (top == max_stack -1 ) return 1; else return 0; } int Stack :: isEmpty() { if(top == -1) return 1; else return 0 ; } Implementasi Method push() dan pop() Seperti sudah dijelaskan seb elumnya method push() digunakan untuk menambah elemen di dalam stack, sedangkan method pop() digunakan untuk mengeluarkan elemen di dalam stack. void Stack :: push() { char insert; int i ; cout << "Much of data : "; cin >> i; if( i > max_stack -1) cout << "Out Of Range"; cout << endl; for(int j = 0;j<=i-1;j++) { cout << "insert data : "; cin >> insert; top++; data[top] = insert; } getche(); } void Stack :: pop() { cout << "Insert Index Data : "; cin >> top; cout << "Data Deleted : " << data[top] << endl; top--; } Implementasi Fungsi pri nt() Fungsi print() digunakan untuk mencetak isi di dalam stack void Stack :: print() { for(int i = top ; i>=0;i--) { cout << "Data " << i << " "<< data[i] << endl; } getche(); } Sepertinya hanya itu saja yang bisa saya share dari materi struktur data yang pernah saya dapatkan , jika teman-teman semua ada yang mau menambahkan atau memperbaikinya silahkan berikan komentar,,,,OK!!^_^ # include<iostream.h> # include<process.h> # include<conio.h> # define SIZE 20 class stack { int a[SIZE]; int tos; // Top of Stack public: stack(); void push(int); int pop(); int isempty(); int isfull(); }; stack::stack() { tos=0; //Initialize Top of Stack } int stack::isempty() { return (tos==0?1:0); } int stack::isfull() { return (tos==SIZE?1:0); } void stack::push(int i) { if(!isfull()) { a[tos]=i; tos++; } else { cerr<<"Stack overflow error ! Possible Data Loss !"; } } int stack::pop() { if(!isempty()) { return(a[--tos]); } else { cerr<<"Stack is empty! What to pop...!"; } return 0; } void main() { stack s; int ch=1,num; while(ch!=0) { cout<<"Stack Operations Mani Menu 1.Push 2.Pop 3.IsEmpty 4.IsFull 0.Exit "; cin>>ch; switch(ch) { case 0: exit(1); //Normal Termination of Program case 1: cout<<"Enter the number to push"; cin>>num; s.push(num); break; case 2: cout<<"Number popped from the stack is: "<<s.pop()<<endl; break; case 3: (s.isempty())?(cout<<"Stack is empty. "):(cout<<"Stack is not empty. "); break; case 4: (s.isfull())?(cout<<"Stack is full. "):(cout<<"Stack is not full. "); break; default: cout<<"Illegal Option. Please try again "; } }//end of while getch(); } ok I have my problem entering in an expression in infix notation and it outputs the postfix notation. I now need to evaluate the postfix notation. I have some code written and there are comments as to what I want to do but am unable to get it to work. So if someone could please help me it would greatly be appreciated. Thanks for your help. code: #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; const int SIZE=100; class stack; class stacknode { private: char data; stacknode *link; stacknode(int d=0,stacknode *l=0):data(d),link(l){}; public: friend class stack; }; class stack { private: stacknode *top; public: stack(){top=0;}; void push(const char); char pop(); char empty(); }; void stack::push(const char y) { top=new stacknode(y,top); } char stack::pop() { if(top==0) return 0; char retvalue; stacknode *x=top; retvalue=top->data; top=x->link; delete x; return retvalue; } char stack::empty() { if(top==0) return '1'; else return '\0'; } //************ char isoprand(char symb) { if(symb=='+'||symb=='-'||symb=='*'||symb=='/'||symb=='^'|| symb=='(' ||symb==')' ) return '\0'; else return '1'; } int isoperator(char symb) { int r=0; if(symb=='+'||symb=='-'||symb=='*'||symb=='/'||symb=='^') r=1; return r; } //************ int precedence(char op1,char op2) { char s[4][3]={"()","+-","*/","^^"}; int i=0,j,k1,k2,r=1; for(i=0;i<4;i++) for(j=0;j<2;j++) { if(op1==s[i][j])k1=i; if(op2==s[i][j])k2=i; } if(k1<k2)r=0; return r; } //************ void infix_postfix(char *infix,char *postfix) { int position, toppos=0; char top_operator='\0',symb; stack operator_stack; for( position=0; (symb=infix[position])!='\0'; position++) { if(isoprand(symb)) { postfix[toppos++]=symb; postfix[toppos]='\0'; } else switch(symb) { case ')': while( (top_operator=operator_stack.pop()) && top_operator!='(' ) postfix[toppos++]=top_operator; break; case '(': operator_stack.push(symb); break; default : while( (top_operator=operator_stack.pop()) && precedence(top_operator,symb) ) postfix[toppos++]=top_operator; if(top_operator) operator_stack.push(top_operator); operator_stack.push(symb); } } while(!operator_stack.empty()) postfix[toppos++]=operator_stack.pop(); postfix[toppos]='\0'; } //************ int isnotexp(char *infix) { int r=0, k1=0,k2=0,n=0; char *p = infix; char ch1; while(*p) { if(*p=='(')k1++; else if(*p==')') k2++; p++; n++; } if(k1!=k2) r=1; p=infix; ch1=*p++; while(*p) { if(isoperator(ch1)&&isoperator(*p)) r=1; if(ch1==')'&&isoprand(*p)) r=1; if(ch1=='('&&isoperator(*p)) r=1; if(isoprand(ch1)&& *p=='(') r=1; ch1=*p++; } if(n>SIZE)r=n; return r; } void evaluates(char *infix) { int position; char symbol, numbers; int number1, number2; stack operator_stack; for(position=0; infix[position] !='\0'; position++) { symbol=infix[position]; if(isoprand(symbol)) { numbers=infix[position]; operator_stack.push(numbers); } else { //I want to get the top item from the stack_operator stack and the next number and add them or subtract them and so on depending on the sign. I'm not really sure what to do so if someone could please help it would greatly be appreciated. symbol=infix[position]; switch(symbol) { case '+': break; case '-': break; case '*': break; case '/': break; case '^': break; } } } } //************ int main() { char *infix=new char[SIZE]; char *postfix=new char[SIZE]; int k=0; cout<<"Please enter an expression : "<<endl; gets(infix); k=isnotexp(infix); if(!k) { infix_postfix(infix,postfix); puts(postfix); } else { if(k==1) cout<<"Wrong expression!\n"<<endl; else cout<<"infix is over flow.\n"<<endl;; } evaluates(postfix); delete [] infix; delete [] postfix; return 0; }