#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 10
char stack[MAX];
int top = -1;
char pop()
{
char a;
a=stack[top];
top--;
return a;
}
void push(char item)
{
top++;
stack[top]=item;
}
void clear()
{
for(int c=0; c<25; c++)
printf("\n");
}
int prcd(char symbol)
{
switch(symbol)
{
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case '^':
case '$':
return 6;
case '(':
case ')':
case '#':
return 1;
}
}
int isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '(':
case ')':
return 1;
default:
return 0;
}
}
void convertip(char infix[],char prefix[])
{
int i,symbol,j=0;
char test[MAX];
infix=strrev(infix);
stack[++top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
prefix[j]=symbol;
j++;
}
else
{
if(symbol==')')
{
push(symbol);
}
else if(symbol=='(')
{
while(stack[top]!=')')
{
prefix[j]=pop();
j++;
}
pop();
}
else
{
if(prcd(symbol)>prcd(stack[top]))
{
push(symbol);
}
else
{
while(prcd(symbol)<=prcd(stack[top]))
{
prefix[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(stack[top]!='#')
{
prefix[j]=pop();
j++;
}
prefix[j]='\0';
prefix=strrev(prefix);
}
void convertipo(char infix[],char postfix[])
{
int i,symbol,j=0;
char test[MAX];
stack[++top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else
{
if(symbol=='(')
{
push(symbol);
}
else if(symbol==')')
{
while(stack[top]!='(')
{
postfix[j]=pop();
j++;
}
pop();
}
else
{
if(prcd(symbol)>prcd(stack[top]))
{
push(symbol);
}
else
{
while(prcd(symbol)<=prcd(stack[top]))
{
postfix[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(stack[top]!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';
}
int main()
{
int choice;
char infix[20],prefix[20],postfix[20];
do
{
clear();
printf(" INFIX->PREFIX INFIX->POSTFIX CONVERTER\n");
printf(" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n");
printf(" 1. Infix to Prefix\n");
printf(" 2. Infix to Postfix\n");
printf(" 3. Exit\n");
printf("\n >> Input choice : ");
scanf("%d", &choice); fflush(stdin);
switch(choice)
{
case 1 :
{
printf("\n\n\n Input An Infix Notation [a ('+','-','*','/') b ('+','-','*','/') c]: ");
gets(infix); fflush(stdin);
convertip(infix,prefix);
printf("\n\n\n\n Prefix : ");
puts(prefix); fflush(stdin);
getchar();
break;
}
case 2 :
{
printf("\n\n\n Input An Infix Notation [a ('+','-','*','/') b ('+','-','*','/') c]: ");
gets(infix); fflush(stdin);
convertipo(infix,postfix);
printf("\n\n\n\n Postfix : ");
puts(postfix); fflush(stdin);
getchar();
break;
}
}
}while(choice!=3);
}
Selasa, 18 Mei 2010
Infix to Prefix - Infix to Postfix
Selasa, 27 April 2010
Double Linked List
Penggunaan Fungsi Push depan, push belakang, pop depan, pop belakang
#include <stdio.h>
#include <stdlib.h>
struct data
{
int angka;
struct data *next, *prev;
}*head = NULL, *tail = NULL, *curr = NULL;
void clear()
{
for(int c=0; c<25; c++)
printf("\n");
}
void push_depan(int angka)
{
curr = (struct data*)malloc(sizeof(struct data));
curr->angka = angka;
curr->next = curr->prev = NULL;
if(head==NULL)
{
head = tail = curr;
}
else
{
head->prev = curr;
curr->next = head;
head = curr;
}
}
void push_belakang(int angka)
{
curr = (struct data*)malloc(sizeof(struct data));
curr->angka = angka;
curr->next = curr->prev = NULL;
if(head==NULL)
{
head = tail = curr;
}
else
{
tail->next = curr;
curr->prev = tail;
tail = curr;
}
}
void cetak()
{
if(head==NULL)
{
printf("data kosong");
}
else
{
curr = head;
while(curr)
{
printf("%d ", curr->angka);
curr=curr->next;
}
}
}
void pop_depan()
{
if(head==NULL)
{
printf("tidak ada data");
getchar();
}
else if(head==tail)
{
curr=head;
free(curr);
head = tail = curr = NULL;
}
else
{
curr=head;
head=head->next;
free(curr);
head->prev = NULL;
}
}
void pop_belakang()
{
if(head==NULL)
{
printf("tidak ada data");
getchar();
}
else if(head==tail)
{
curr=head;
free(curr);
head = tail = curr = NULL;
}
else
{
curr=tail;
tail=tail->prev;
free(curr);
tail->next = NULL;
}
}
void main()
{
int angka, pilih, pilih2, pilih3;
do
{
clear();
printf("1. Input\n");
printf("2. View\n");
printf("3. Delete\n");
printf("4. Exit\n");
printf("Pilih : ");
scanf("%d", &pilih); fflush(stdin);
switch(pilih)
{
case 1 :
{
clear();
do
{
printf("1. Push Depan\n");
printf("2. Push Belakang\n");
printf("Pilih : ");
scanf("%d", &pilih2); fflush(stdin);
}while(pilih2!=1 && pilih2!=2);
if(pilih2==1)
{
printf("Masukan Angka : ");
scanf("%d", &angka); fflush(stdin);
push_depan(angka);
}
else if(pilih2==2)
{
printf("Masukan Angka : ");
scanf("%d", &angka); fflush(stdin);
push_belakang(angka);
}
break;
}
case 2 :
{
clear();
cetak();
getchar();
break;
}
case 3 :
{
clear();
do
{
printf("1. Pop Depan\n");
printf("2. Pop Belakang\n");
printf("Pilih : ");
scanf("%d", &pilih3); fflush(stdin);
}while(pilih3!=1 && pilih3!=2);
if(pilih3==1)
{
pop_depan();
}
else if(pilih3==2)
{
pop_belakang();
}
break;
}
}
}while(pilih!=4);
}
Single Linked List
Penggunaan Push Depan, Push Belakang, Pop Depan, Pop Belakang
Onez Sumlang - 27 April 2010
#include <stdio.h>
#include <stdlib.h>
struct data
{
int angka;
struct data *next;
}*head=NULL, *curr=NULL, *tail=NULL;
void clear()
{
for(int c=0; c<25; c++)
printf("\n");
}
void push_depan(int angka)
{
curr = (struct data*)malloc(sizeof(struct data));
curr->angka = angka;
curr->next = NULL;
if(head==NULL)
{
head=tail=curr;
}
else
{
curr->next = head;
head = curr;
}
tail->next = NULL;
}
void push_belakang(int angka)
{
curr = (struct data*)malloc(sizeof(struct data));
curr->angka = angka;
curr->next = NULL;
if(head==NULL)
{
head = tail = curr;
}
else
{
tail->next = curr;
tail = curr;
}
}
void pop_depan()
{
if(head==NULL)
{
printf("tidak ada data");
getchar();
head = tail = curr = NULL;
}
else
{
curr=head;
if(head==tail)
{
head = tail = NULL;
}
else
{
head = head->next;
free(curr);
}
}
}
void pop_belakang()
{
if(head==NULL)
{
printf("tidak ada data");
getchar();
head = tail = curr = NULL;
}
else
{
curr = head;
if (head==tail)
head = tail = NULL;
else
{
while (curr->next!=tail)
curr = curr->next;
tail = curr;
curr = curr->next;
tail->next = NULL;
}
free(curr);
}
}
void cetak()
{
if(head==NULL)
{
printf("tidak ada data");
}
else
{
curr = head;
while(curr)
{
printf("%d ", curr->angka);
curr = curr->next;
}
}
}
int main()
{
int angka, pilih, pilih2, pilih3;
do
{
clear();
printf("1. Input\n");
printf("2. View\n");
printf("3. Delete\n");
printf("4. Exit\n");
printf("Pilih : ");
scanf("%d", &pilih); fflush(stdin);
switch(pilih)
{
case 1 :
{
clear();
do
{
printf("1. Push depan\n");
printf("2. Push belakang\n");
printf("Pilih : ");
scanf("%d", &pilih3); fflush(stdin);
}while(pilih3!=1 && pilih3!=2);
if(pilih3==1)
{
printf("Masukan angka : ");
scanf("%d", &angka); fflush(stdin);
push_depan(angka);
}
else if(pilih3==2)
{
printf("Masukan angka : ");
scanf("%d", &angka); fflush(stdin);
push_belakang(angka);
}
break;
}
case 2 :
{
clear();
cetak();
getchar();
break;
}
case 3 :
{
clear();
do
{
printf("1. Pop depan\n");
printf("2. Pop belakang\n");
printf("Pilih : ");
scanf("%d", &pilih2); fflush(stdin);
}while(pilih2!=1 && pilih2!=2);
if(pilih2==1)
{
pop_depan();
}
else if(pilih2==2)
{
pop_belakang();
}
break;
}
}
}while(pilih!=4);
}
Onez Sumlang - 27 April 2010
Konversi Infix ke Prefix
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
char stack[MAX];
int top = -1;
char pop();
void push(char item);
int prcd(char symbol)
{
switch(symbol)
{
case '+':
case '-':
return 2;
case '*':
case '/':
return 4;
case '^':
case '$':
return 6;
case '(':
case ')':
case '#':
return 1;
}
}
int isoperator(char symbol)
{
switch(symbol)
{
case '+':
case '-':
case '*':
case '/':
case '^':
case '$':
case '(':
case ')':
return 1;
default:
return 0;
}
}
void convertip(char infix[],char prefix[])
{
int i,symbol,j=0;
char test[MAX];
infix=strrev(infix);
stack[++top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
prefix[j]=symbol;
j++;
}
else
{
if(symbol==')')
{
push(symbol);
}
else if(symbol=='(')
{
while(stack[top]!=')')
{
prefix[j]=pop();
j++;
}
pop();//pop out (.
}
else
{
if(prcd(symbol)>prcd(stack[top]))
{
push(symbol);
}
else
{
while(prcd(symbol)<=prcd(stack[top]))
{
prefix[j]=pop();
j++;
}
push(symbol);
}
}
}
}
while(stack[top]!='#')
{
prefix[j]=pop();
j++;
}
prefix[j]='\0';//null terminate string.
prefix=strrev(prefix);
}
int main()
{
char infix[20],prefix[20];
printf("Enter the valid infix string:\n");
gets(infix);
convertip(infix,prefix);
printf("The corresponding prefix string is:\n");
puts(prefix);
getch();
return 0;
}
void push(char item)
{
top++;
stack[top]=item;
}
char pop()
{
char a;
a=stack[top];
top--;
return a;
}
Copyright @ http://wiki.answers.com/Q/C_program_to_convert_infix_to_prefix
Langganan:
Postingan (Atom)