Tuesday, October 7, 2008

Infix to Postfix

/*convert infix expression to postfix expression using stack*/

#include
#include
#include
#define MAX 20

void push(char c);
void pop();
int precedence(char o);


struct stack
{
char posfix[MAX];
char op[MAX];
int top,n;
};

struct stack s;



main()

{
s.top=-1;
s.n=0;
int i,j,k,l,e,f,r;
char exp[20];
printf("\n\nenter the expression : ");
scanf("%s",exp);

for(i=0;exp[i]!='\0';i++)
{
if(exp[i]=='(')
{
push(exp[i]);
}

else if(exp[i]==')')
{
while(s.op[s.top]!='(')
{
pop();
}
s.top--; // becoz leave the '('
}


else if(isalpha(exp[i])||isdigit(exp[i]))
{
s.posfix[s.n]=exp[i];
s.n++;
}
else
{
if(s.top==-1)
{
push(exp[i]);
}
else
{
e=precedence(exp[i]);
r=precedence(s.op[s.top]);
if(r>e)
{
pop();
push(exp[i]);
}
else if(r==e)
{
pop();

push(exp[i]);
}
else
{
push(exp[i]);
}

}
}
}

for(j=s.top;j>=0;j--)
{

pop();

}
printf("\nthe postfix string is : ");
printf("%s\n\n",s.posfix);

}





void push(char c)
{

s.op[++s.top]=c;
return;

}


void pop()
{

s.posfix[s.n++]=s.op[s.top--];
return;
}


int precedence(char o)
{
if(o=='*'||o=='/'||o=='%')
{
return 2;
}
else if(o=='+'||o=='-')
{
return 1;
}
else if(o=='(' || o==')')
return 0;
}

Queue using Arrays

/* Implementation of queue using linkedlist */
#include
#include

void enqueue();
void dequeue();
void view();


struct node
{
int data;
struct node *next;

};

struct node *front=NULL;
struct node *rear=NULL;


int main()
{

int n;

do
{
printf("\n\n\n\n"
"1.enqueue an element\n"
"2.dequeue an element\n"
"3.view and count no.of element\n"
"4.end the program\n"
"\nenter your choice number : ");

scanf("%d",&n);


switch(n)
{
case 1:
enqueue();
break;

case 2:
dequeue();
break;

case 3:
view();
break;

case 4:
printf("\n\n\t--------------THANKS FOR USING------------\n\n");
break;

default:
printf("please enter the number 1 to 5 only :\n");

scanf("%d",&n);


}

}while(n<4);

return 0;
}




void enqueue()
{
int x;

struct node *temp;

temp=(struct node*) malloc (sizeof(struct node));

if(temp==NULL)
{

printf("\nqueue is overflow\n");

}

else
{
printf("\nenter the element : ");
scanf("%d",&x);

temp->data=x;
temp->next=NULL;

if( rear==NULL && front==NULL )
{
rear=temp;
front=temp;
}

else
{
rear->next=temp;
rear=temp;
}
}

return;

}



void dequeue()
{

int x;

if( front==NULL && rear==NULL)
{

printf("the queue is underflow\n");

}

else
{

x=front->data;
printf("\nthe dequeued element is -> %d",x);

if(front==rear)
{
front=NULL;
rear=NULL;
}

else
{
front=front->next;

}

}

return;

}



void view()
{

int count=1; // when temp = rear it come out from 'for' so

struct node *temp1;
temp1=front;

printf("\nthe elements in the queue are :\n");

for( ; temp1!=rear; temp1=temp1->next) //use do-while,condition:temp!=NULL
{
printf("\t\t\t\t%d\n",temp1->data);
count++;
}

printf("\t\t\t\t%d\n\n",temp1->data);

printf("the number of elements in the queue is %d\n\n",count);


return;

}

Queue using Linkedlist

/* Implementation of queue using linkedlist */
#include
#include

void enqueue();
void dequeue();
void view();


struct node
{
int data;
struct node *next;

};

struct node *front=NULL;
struct node *rear=NULL;


int main()
{

int n;

do
{
printf("\n\n\n\n"
"1.enqueue an element\n"
"2.dequeue an element\n"
"3.view and count no.of element\n"
"4.end the program\n"
"\nenter your choice number : ");

scanf("%d",&n);


switch(n)
{
case 1:
enqueue();
break;

case 2:
dequeue();
break;

case 3:
view();
break;

case 4:
printf("\n\n\t--------------THANKS FOR USING------------\n\n");
break;

default:
printf("please enter the number 1 to 5 only :\n");

scanf("%d",&n);


}

}while(n<4);

return 0;
}




void enqueue()
{
int x;

struct node *temp;

temp=(struct node*) malloc (sizeof(struct node));

if(temp==NULL)
{

printf("\nqueue is overflow\n");

}

else
{
printf("\nenter the element : ");
scanf("%d",&x);

temp->data=x;
temp->next=NULL;

if( rear==NULL && front==NULL )
{
rear=temp;
front=temp;
}

else
{
rear->next=temp;
rear=temp;
}
}

return;

}



void dequeue()
{

int x;

if( front==NULL && rear==NULL)
{

printf("the queue is underflow\n");

}

else
{

x=front->data;
printf("\nthe dequeued element is -> %d",x);

if(front==rear)
{
front=NULL;
rear=NULL;
}

else
{
front=front->next;

}

}

return;

}



void view()
{

int count=1; // when temp = rear it come out from 'for' so

struct node *temp1;
temp1=front;

printf("\nthe elements in the queue are :\n");

for( ; temp1!=rear; temp1=temp1->next) //use do-while,condition:temp!=NULL
{
printf("\t\t\t\t%d\n",temp1->data);
count++;
}

printf("\t\t\t\t%d\n\n",temp1->data);

printf("the number of elements in the queue is %d\n\n",count);


return;

}

Queue using Arrays

/*Implementation of queue using array*/
#include
#include
#define MAX 5

void enqueue();
void dequeue();
void view();
int isempty();
int isfull();


struct queue
{
int front,rear,a[MAX];

}q;


int main()
{ int n,p,f;
q.rear=-1;
q.front=0;

do
{
printf("\n\n\n\nenter your choice number:\n"
"1.enqueue an element\n"
"2.dequeue an element\n"
"3.view and count no.of element\n"
"4.check, is queue empty?\n"
"5.check, is queue full?\n"
"6.end the program\n");
scanf("%d",&n);

switch(n)
{
case 1:
enqueue();
break;

case 2:
dequeue();
break;

case 3:
view();
break;

case 4:
p=isempty();
if(p==1)
printf("the queue is empty\n");
else
printf("the queue is not empty\n");
break;

case 5:
f=isfull();
if(f==1)
printf("the queue is full\n");
else
printf("the queue is not full\n");
break;

case 6:
printf("\t---------------THANKS FOR USING-----------------\n\n");
break;

default:
printf("please enter the number 1 to 5 only :\n");
scanf("%d",&n);


}

}while(n<6);

return 0;
}


void enqueue()
{
int s,x;
s=isfull();
if(s==1)
{
printf("\nqueue is overflow\n");
}
else
{
printf("enter the element\n");
scanf("%d",&x);
q.a[++q.rear]=x;
}
return;
}


int isfull()
{
if(q.rear==MAX-1)
return(1);
else
return(0);
}


void dequeue()
{
int s,x;
s=isempty();
if(s==1)
{
printf("the queue is underflow\n");
}
else
{
x=q.a[q.front];
if(q.front==q.rear) // if it is the only element
{
q.front=0;
q.rear=-1;
}
else
{
q.front=q.front+1;
}
printf("the dequeued element is %d\n",x);
}
return;
}


int isempty()
{
if(q.rear return(1);
else
return(0);
}


void view()
{
int i,count;
count=q.rear-q.front+1;
printf("the number of elements in the queue is %d\n\n",count);
printf("elments in the queue are :\n");
for(i=q.front;i<=q.rear;i++)
{
printf("\t\t\t\t%d\n",q.a[i]);
}

return;
}

Stack using Linked List

/* Implementation of stack using linked list */

#include
#include

void push();
void pop();
void view();

struct node
{
int data;
struct node *next;
};

struct node *top=NULL;

main()
{

int n;

do
{
printf("\n\n\n\n"
"1.push an element\n"
"2.pop an element\n"
"3.view and count\n"
"4.end the program\n"
"\nenter your choice number : ");
scanf("%d",&n);

switch(n)
{
case 1:
push();
break;

case 2:
pop();
break;

case 3:
view();
break;

case 4:
printf("\n\t---------------THANKS FOR USING-------------\n\n"); break;

default:
printf("\n\nenter the choice 1 to 3 only : ");
scanf("%d",&n);

}

} while(n<4);

}


void push()
{
struct node *temp;
temp=(struct node*) malloc (sizeof(struct node));

int v;
if(temp==NULL)
{
printf("\nthe stack is over flow\n\n");

}

else
{
printf("\n\nenter the element : ");
scanf("%d",&v);

temp->data=v;
temp->next=top;
top=temp;

}

return;
}


void pop()
{
int v;

if(top==NULL)
{
printf("the stack is underflow");
}
else
{
v=top->data;
printf("\nthe poped element is %d \n\n",top->data);
top=top->next;
}
return;
}



void view()
{

struct node *temp1;
int count=0;
temp1=top;
if(temp1==NULL)
{
printf("the stack is empty");
}

else
{

printf("\nthe elements in the stack are :\n");
for(;temp1!=NULL;temp1=temp1->next)
{
printf("\t\t\t\t%d\n",temp1->data);
count=count+1;

}
}

printf("\nthe number of elements in the stack is -> %d\n\n",count);

return;

}

Stack using Array

/*Implementation of stack using array*/
#include
#define max 5

int top=-1;
int a[max];

void push();
void pop();
void peep();
int isempty();
int isfull();

main()
{ int n,p,q;

do
{
printf("\n\n\n\nenter your choice number:\n
1.push an element\n
2.pop an element\n
3.peep an element\n
4.check, is stack empty?\n
5.check, is stack full?\n
6.end the program\n");
scanf("%d",&n);

switch(n)
{
case 1:
push();
break;

case 2:
pop();
break;

case 3:
peep();
break;

case 4:
p=isempty();
if(p==1)
printf("the stack is empty\n");
else
printf("the stack is not empty\n");
break;

case 5:
q=isfull();
if(q==1)
printf("the stack is full\n");
else
printf("the stack is not full\n");
break;

case 6:
printf("\t---------------THANKS FOR USING-----------------\n\n");
break;

default:
printf("please enter the number 1 to 5 only :\n");
scanf("%d",&n);


}

}while(n<6);


}

void push()
{
int s,x;
s=isfull();
if(s==1)
{
printf("\nstack is overflow\n");
}
else
{
printf("enter the element\n");
scanf("%d",&x);
a[++top]=x;
}
return;
}

int isfull()
{
if(top==max-1)
return(1);
else
return(0);
}


void pop()
{
int s,x;
s=isempty();
if(s==1)
{
printf("the stack is underflow\n");
}
else
{
x=a[top--];
printf("the poped element is %d\n",x);
}
return;
}


int isempty()
{
if(top==-1)
return(1);
else
return(0);
}



void peep()
{
int i;
printf("the element in the stack are :\n");
for(i=top;i>=0;i--)
{
printf("\t\t\t\t%d\n",a[i]);
}
return;
}

Data Structure Programs

Here i share my little knowledge of Data Structure and its applications..

Here,some programs that Implements Data Strucures..


Thank you

Sakthi