Hai semuanya.. dalam kesempatan kali ini saya akan mamposting contoh penggunaan struct. diimplementasikan dalam bilangan kompleks. yuk disimak. jika ada yang ingin ditanyakan, monggo komen saja :)
#include <stdio.h>
#include <stdlib.h>
//typedef bool int
struct sNode{
char data;
struct sNode *next;
};
void push(struct sNode** top_ref, char new_data){
struct sNode *new_node=(struct sNode*)malloc(sizeof(struct sNode));
if(new_node==NULL){
printf("Stack overflow");
getchar();
exit(0);
}
new_node->data=new_data;
new_node->next=*top_ref;
*top_ref=new_node;
}
int pop(struct sNode** top_ref){
char res;
struct sNode *top;
if(*top_ref==NULL){
printf("Stack overflow");
getchar();
exit(0);
}else{
top=*top_ref;
res=top->data;
*top_ref=top->next;
free(top);
return res;
}
}
int isMatchingPair(char c1,char c2){
if(c1=='(' && c2==')')
return 1;
else if(c1=='{' && c2=='}')
return 1;
else if(c1=='[' && c2==']')
return 1;
else return 0;
}
void printstack(struct sNode *head){
struct sNode *b;
b=head;
while(b!=NULL){
printf(" %c \n",b->data);
b=b->next;
}
}
int areParenthesisBalanced(char exp[]){
int i=0;
struct sNode *stack=NULL;
while(exp[i]){
if(exp[i]=='{' || exp[i]=='(' || exp[i]=='[')
push(&stack,exp[i]);
if(exp[i]=='}' || exp[i]==')' || exp[i]==']'){
if(stack==NULL)
return 0;
else if(!isMatchingPair(pop(&stack),exp[i]))
return 0;
}
i++;
}
if(stack==NULL)
return 1;
else
return 0;
}
int main(){
char exp[100]="[()]{}";
if(areParenthesisBalanced(exp))
printf("Balanced");
else
printf("Not balanced");
return 0;
}
sekian dari saya.. terimakasih sudah berkunjung. semoga bermanfaat :)
#C
#include <stdio.h>
#include <stdlib.h>
//typedef bool int
struct sNode{
char data;
struct sNode *next;
};
void push(struct sNode** top_ref, char new_data){
struct sNode *new_node=(struct sNode*)malloc(sizeof(struct sNode));
if(new_node==NULL){
printf("Stack overflow");
getchar();
exit(0);
}
new_node->data=new_data;
new_node->next=*top_ref;
*top_ref=new_node;
}
int pop(struct sNode** top_ref){
char res;
struct sNode *top;
if(*top_ref==NULL){
printf("Stack overflow");
getchar();
exit(0);
}else{
top=*top_ref;
res=top->data;
*top_ref=top->next;
free(top);
return res;
}
}
int isMatchingPair(char c1,char c2){
if(c1=='(' && c2==')')
return 1;
else if(c1=='{' && c2=='}')
return 1;
else if(c1=='[' && c2==']')
return 1;
else return 0;
}
void printstack(struct sNode *head){
struct sNode *b;
b=head;
while(b!=NULL){
printf(" %c \n",b->data);
b=b->next;
}
}
int areParenthesisBalanced(char exp[]){
int i=0;
struct sNode *stack=NULL;
while(exp[i]){
if(exp[i]=='{' || exp[i]=='(' || exp[i]=='[')
push(&stack,exp[i]);
if(exp[i]=='}' || exp[i]==')' || exp[i]==']'){
if(stack==NULL)
return 0;
else if(!isMatchingPair(pop(&stack),exp[i]))
return 0;
}
i++;
}
if(stack==NULL)
return 1;
else
return 0;
}
int main(){
char exp[100]="[()]{}";
if(areParenthesisBalanced(exp))
printf("Balanced");
else
printf("Not balanced");
return 0;
}
sekian dari saya.. terimakasih sudah berkunjung. semoga bermanfaat :)
#C
Komentar
Posting Komentar