Archive for » April, 2014 «
#include
#include
#include
struct stack{
char x;
struct stack *next;
} ;
struct stack *push(struct stack *top, char val){
struct stack *ptr;
ptr=(struct stack*) malloc (sizeof(struct stack*));
ptr->x=val;
if (top==NULL){
top=ptr;
top->next=NULL;
} else {
ptr->next=top;
top=ptr;
}
return top;
}
struct stack *pop (struct stack *top)
{
struct stack *ptr;
ptr=top;
if(top==NULL){
printf(“\n STACK UNDERFLOW”);
}
else
{
top = top -> next;
}
return top;
}
int preced(char val) {
if(val==’+’ || val==’-‘) return 1;
else if (val==’*’ || val==’/’) return 2;
else if (val=='(‘) return 0;
};
void main(){
char infix[51],postf[51]=””;
struct stack *opr;
opr=(struct stack*) malloc (sizeof(struct stack*));
opr=NULL;
int n=0,counter;
do{
printf(“Enter infix string [0-50 digit, single number only] : “);
scanf(“%s”,&infix) ;fflush(stdin);
for(int i=0;i=’0′ && infix[i]<='9')
|| infix[i]=='+'
|| infix[i]=='-'
|| infix[i]=='*'
|| infix[i]=='/'
|| infix[i]=='('
|| infix[i]==')') counter=1;
else {counter=0; break;}
}
} while (counter!=1);
for(int i=0;i=’0’ && infix[i]x!='(‘){
postf[n]=opr->x; n++;
opr=pop(opr);
} opr=pop(opr);
continue;
}
if(opr!=NULL && (preced(infix[i]) x))){
if (preced(infix[i]) == preced(opr->x)){
postf[n]=opr->x; n++;
opr=pop(opr);
opr=push(opr,infix[i]);
} else {
while(opr!=NULL && preced(infix[i]) x)){
postf[n]=opr->x; n++;
opr=pop(opr);
}
opr=push(opr,infix[i]);
}
}
else {
opr=push(opr,infix[i]);
}
}
}
while(opr->next!=NULL){
postf[n]=opr->x; n++;
opr=pop(opr);
} postf[n]=opr->x; n++;
opr=pop(opr);
printf(“\nPostfix: “);
for(int i=0;i<strlen(postf);i++) printf("%c",postf[i]);
getchar();
}
#include
#include
struct stack
{
int data;
struct stack *left,*right,*parent;
} *head,*temp,*curr;
struct stack *push (struct stack *head, int val){
curr=head;
if(head==NULL){
head=(struct stack*) malloc (sizeof(struct stack*));
head->data=val;
head->parent=NULL;
head->left=NULL;
head->right=NULL;
} else {
while(curr!=NULL){
temp=curr;
if(valdata){
curr=curr->left;
} else {
curr=curr->right;
}
}
curr=(struct stack*) malloc (sizeof(struct stack*));
curr->data=val;
curr->parent=temp;
if(curr->datadata){
temp->left=curr;
} else temp->right=curr;
curr->left=NULL;
curr->right=NULL;
}
return head;
}
struct stack *preorder(struct stack *head) {
if (head == NULL) return head;
printf(“%d “,head->data);
preorder(head->left) ;
preorder(head->right);
};
void main()
{
int val;
printf(“Masukkan 10 digit angka [1-50]\n\n”);
for (int i=0;i<10;i++) {
do {
printf("Angka ke-%d : ",i+1);
scanf("%d",&val); fflush(stdin);
} while (val50);
head=push(head,val);
}
printf(“\n”);
preorder(head);
getchar();
}
Recent Comments