Showing posts with label Data Structure. Show all posts
Showing posts with label Data Structure. Show all posts

Program of circular queue implementation and operation on circular queue

#include
#include
#define max 5

typedef struct queue

{
int front,rear;
int item[max];
}queue;
queue q1;

void insert()

{
int n;
if((q1.front==0 && q1.rear==max-1) || (q1.front==q1.rear+1))
printf("\nQueue is full");
else
{
if (q1.front==-1)
q1.rear=q1.front=0;
else
if(q1.rear==max-1)
q1.rear=0;
else
q1.rear++;
printf("\nenter the element for insertion in queue:- ");
scanf("%d", &n);
q1.item[q1.rear]=n;
}
}

void del()

{
if (q1.front==-1)
printf("\nQueue is empty");
else
{
printf("\n%d is deleted from queue:- ",q1.item[q1.front]);
if(q1.front==q1.rear)
q1.front=q1.rear=-1;
else
if(q1.front==max-1)
q1.front=0;
else
q1.front++;
}
}

void display()

{
int front_temp=q1.front,rear_temp=q1.rear;
if(q1.front==-1)
printf("\nQueue is empty");
else
{
printf("\nelement of the queue are:-\n");
if(front_temp<=rear_temp)
{
while(front_temp<=rear_temp)
printf("%d\t",q1.item[front_temp++]);
}
else
{
while(front_temp<=max-1)
printf("%d\t",q1.item[front_temp++]);
front_temp=0;
while(front_temp<=rear_temp)
printf("%d\t",q1.item[front_temp++]);
}
}
}

void main()

{
int ch;
clrscr();
q1.front=q1.rear=-1;
while(1)
{
printf("\n\n1:for insertion");
printf("\n2:for deletion");
printf("\n3:for exit");
printf("\nEnter your choice:- ");
scanf("%d",&ch);
switch(ch)
{

case 1 :

insert();
display();
break;
case 2 :
del();
display();
break;
case 3:
exit(0);
default:
printf("\nyou entered wrong choice");
}
}
}
/*
Output:-

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 1
1

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 2
element of the queue are:-
1 2

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 3
element of the queue are:-
1 2 3

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 4
element of the queue are:-
1 2 3 4

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 5
element of the queue are:-
1 2 3 4 5

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
Queue is full
element of the queue are:-
1 2 3 4 5

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 2
1 is deleted from queue:-
element of the queue are:-
2 3 4 5

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 2
2 is deleted from queue:-
element of the queue are:-
3 4 5

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 1
element of the queue are:-
3 4 5 1

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 2
element of the queue are:-
3 4 5 1 2

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 3

*/

Program from insert, delete using circular queue | How to write

#include

#include
#define max 5

typedef struct queue

{
int front,rear;
int item[max];
}queue;
queue q1;

void insert()

{
int n;
if((q1.front==0 && q1.rear==max-1) || (q1.front==q1.rear+1))
printf("\nQueue is full");
else
{
if (q1.front==-1)
q1.rear=q1.front=0;
else
if(q1.rear==max-1)
q1.rear=0;
else
q1.rear++;
printf("\nenter the element for insertion in queue:- ");
scanf("%d", &n);
q1.item[q1.rear]=n;
}
}

void del()

{
if (q1.front==-1)
printf("\nQueue is empty");
else
{
printf("\n%d is deleted from queue:- ",q1.item[q1.front]);
if(q1.front==q1.rear)
q1.front=q1.rear=-1;
else
if(q1.front==max-1)
q1.front=0;
else
q1.front++;
}
}

void display()

{
int front_temp=q1.front,rear_temp=q1.rear;
if(q1.front==-1)
printf("\nQueue is empty");
else
{
printf("\nelement of the queue are:-\n");
if(front_temp<=rear_temp)
{
while(front_temp<=rear_temp)
printf("%d\t",q1.item[front_temp++]);
}
else
{
while(front_temp<=max-1)
printf("%d\t",q1.item[front_temp++]);
front_temp=0;
while(front_temp<=rear_temp)
printf("%d\t",q1.item[front_temp++]);
}
}
}

void main()

{
int ch;
clrscr();
q1.front=q1.rear=-1;
while(1)
{
printf("\n\n1:for insertion");
printf("\n2:for deletion");
printf("\n3:for exit");
printf("\nEnter your choice:- ");
scanf("%d",&ch);
switch(ch)
{

case 1 :

insert();
display();
break;
case 2 :
del();
display();
break;
case 3:
exit(0);
default:
printf("\nyou entered wrong choice");
}
}
}
/*
Output:-

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 1
1

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 2
element of the queue are:-
1 2

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 3
element of the queue are:-
1 2 3

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 4
element of the queue are:-
1 2 3 4

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 5
element of the queue are:-
1 2 3 4 5

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
Queue is full
element of the queue are:-
1 2 3 4 5

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 2
1 is deleted from queue:-
element of the queue are:-
2 3 4 5

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 2
2 is deleted from queue:-
element of the queue are:-
3 4 5

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 1
element of the queue are:-
3 4 5 1

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 1
enter the element for insertion in queue:- 2
element of the queue are:-
3 4 5 1 2

1:for insertion

2:for deletion
3:for exit
Enter your choice:- 3

*/

Program to conversion from infix to postfix and prefix expression

How to convert from infix to postfix and prefix expression
by priority method and evaluation of postfix and prefix expression.

conversion from infix to postfix and prefix expression | priority method and evaluation of postfix and prefix expression


#include
#include
#define max 50

typedef struct stack

{
int top;
char item[max];
}stack;
stack s1;

void push(char n)

{
if(s1.top==max-1)
puts("\nstack is full");
else
s1.item[++s1.top]=n;
}

int pop()

{
if(s1.top==-1)
return s1.top;
else
return(s1.item[s1.top--]);
}

int prio(char ch)

{
int sh;
if(ch=='^' || ch=='$')
sh=3;
else if(ch=='*' || ch=='/')
sh=2;
else if(ch=='+' || ch=='-')
sh=1;
else if(ch=='{' || ch=='(' || ch=='[')
sh=0;
return sh;
}

void in_post()

{
int i,j=0;
char inf[20],post[20],ch1;
printf("\nenter infix expression:- ");
scanf("%s",&inf);
for(i=0;inf[i]!='\0';i++)
{
if(inf[i]=='{' || inf[i]=='[' || inf[i]=='(')
push(inf[i]);
else if((inf[i]>=65 && inf[i]<91) || (inf[i]>=97 && inf[i]<123))
post[j++]=inf[i];
else if(inf[i]=='^' || inf[i]=='$' || inf[i]=='+' || inf[i]=='-' || inf[i]=='*' || inf[i]=='/')
{
if(s1.top==-1)
push(inf[i]);
else
{
if(prio(inf[i])>prio(s1.item[s1.top]))
push(inf[i]);
else
{
while(prio(inf[i])<=prio(s1.item[s1.top]))
{
post[j++]=pop();
if(s1.top==-1)
break;
}
push(inf[i]);
}
}
}
else if(inf[i]==')'||inf[i]=='}'||inf[i]==']')
{
while(s1.item[s1.top]!='(' && s1.item[s1.top]!='[' && s1.item[s1.top]!='{')
post[j++]=pop();
pop();
}
}
if(s1.top!=-1)
{
while(s1.top!=-1)
post[j++]=pop();
}
post[j]='\0';
printf("\npostfix expression is:- %s",post);
}

int prio1(char ch)

{
int sh;
if(ch=='^' || ch=='$')
sh=3;
if(ch=='*' || ch=='/')
sh=2;
if(ch=='+' || ch=='-')
sh=1;
if(ch=='}' || ch==')' || ch==']')
sh=0;
return sh;
}

void in_pre()

{
int i,j=0;
char inf[30],pre[30],ch1;
printf("\nenter infix expression:- ");
scanf("%s",&inf);
for(i=strlen(inf);i>=0;i--)
{
if(inf[i]=='}' || inf[i]==']' || inf[i]==')')
push(inf[i]);
else if((inf[i]>=65 && inf[i]<91) || (inf[i]>=97 && inf[i]<123))
pre[j++]=inf[i];
else if(inf[i]=='^' || inf[i]=='$' || inf[i]=='+' || inf[i]=='-' || inf[i]=='*' || inf[i]=='/')
{
if(s1.top==-1)
push(inf[i]);
else
{
if(prio1(inf[i])>=prio1(s1.item[s1.top]))
push(inf[i]);
else
{
while(prio1(inf[i])

{
int a,b,c,d,e,i,op1,op2;
char pos[30];
printf("\nenter the value of a,b,c,d,e:- ");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("\nenter postfix expression:- ");
scanf("%s",&pos);
for(i=0;pos[i]!='\0';i++)
{
switch(pos[i])
{
case 'a':
push(a);
break;
case 'b':
push(b);
break;
case 'c':
push(c);
break;
case 'd':
push(d);
break;
case 'e':
push(e);
break;
default:
op2=pop();
op1=pop();
}
switch(pos[i])
{
case '+':
push(op1+op2);
break;
case '-':
push(op1-op2);
break;
case '*':
push(op1*op2);
break;
case '/':
push(op1/op2);
break;
case '%':
push(op1%op2);
break;
}
}
printf("\nvalue of postfix expression is:- %d",pop());
}

void eval_pre()

{
int a,b,c,d,e,i,op1,op2,l;
char pre[30];
printf("\nenter the value of a,b,c,d,e:- ");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("\nenter prefix expression:- ");
scanf("%s",&pre);
l=strlen(pre);
for(i=l;i>=0;i--)
{
switch(pre[i])
{
case 'a':
push(a);
break;
case 'b':
push(b);
break;
case 'c':
push(c);
break;
case 'd':
push(d);
break;
case 'e':
push(e);
break;
default:
op1=pop();
op2=pop();
break;
}
switch(pre[i])
{
case '+':
push(op1+op2);
break;
case '-':
push(op1-op2);
break;
case '*':
push(op1*op2);
break;
case '/':
push(op1/op2);
break;
case '%':
push(op1%op2);
break;
}
}
printf("\nvalue of prefix expression is:- %d",pop());
}

int menu()

{
int ch;
s1.top=-1;
printf("\n\n1:for conversion from infix to postfix expression");
printf("\n2:for conversion from infix to prefix expression");
printf("\n3:for evaluation of postfix expression");
printf("\n4:for evaluation of prefix expression");
printf("\n5:for exit");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
in_post();
menu();
break;
case 2:
in_pre();
menu();
break;
case 3:
eval_post();
menu();
break;
case 4:
eval_pre();
menu();
break;
case 5:
exit(0);
default:
printf("\nyou entered wrong choice");
menu();
}
return 0;
}

void main()

{
clrscr();
menu();
getch();
}

Implementation of Hash Function | Write Program for Hash Function

How to write Data structure Program for implementation of Hash Function using for loop with output

#include
#include

int hash(int no)

{
return no%10;
}

void main()

{
int arr[10],i,pos,cnt,no,ch;
clrscr();
for(i=0;i<10;i++)
arr[i]=-1;
do
{
printf("\n\n Enter the no. that you want to insert:- ");
scanf("%d",&no);
pos=hash(no);
if(pos>10)
pos=hash(pos);
if(arr[pos]==-1)
arr[pos]=no;
else
{
i=1;
cnt=0;
while(arr[pos+i]!=-1)
{
i++;
cnt++;
if(cnt==10)
break;
}
if(arr[pos+i]==-1)
arr[pos+i]=no;
}
if(cnt==10)
printf("\n Memory is full.");
printf("\n\n The elements in memory with address:- ");
for(i=0;i<10;i++)
printf("\n[%2d]=>%2d",i,arr[i]);
printf("\n Do you want to continue??(Yes=1/No=0):- ");
scanf("%d",&ch);
}while(ch);
getch();
}

/*

Output:-


Enter the no. that you want to insert:- 4


The elements in memory with address:-

[ 0]=>-1
[ 1]=>-1
[ 2]=>-1
[ 3]=>-1
[ 4]=> 4
[ 5]=>-1
[ 6]=>-1
[ 7]=>-1
[ 8]=>-1
[ 9]=>-1
Do you want to continue??(Yes=1/No=0):- 1

Enter the no. that you want to insert:- 67


The elements in memory with address:-

[ 0]=>-1
[ 1]=>-1
[ 2]=>-1
[ 3]=>-1
[ 4]=> 4
[ 5]=>-1
[ 6]=>-1
[ 7]=>67
[ 8]=>-1
[ 9]=>-1
Do you want to continue??(Yes=1/No=0):- 1

Enter the no. that you want to insert:- 29


The elements in memory with address:-

[ 0]=>-1
[ 1]=>-1
[ 2]=>-1
[ 3]=>-1
[ 4]=> 4
[ 5]=>-1
[ 6]=>-1
[ 7]=>67
[ 8]=>-1
[ 9]=>29
Do you want to continue??(Yes=1/No=0):- 1

Enter the no. that you want to insert:- 60


The elements in memory with address:-

[ 0]=>60
[ 1]=>-1
[ 2]=>-1
[ 3]=>-1
[ 4]=> 4
[ 5]=>-1
[ 6]=>-1
[ 7]=>67
[ 8]=>-1
[ 9]=>29
Do you want to continue??(Yes=1/No=0):- 0

*/

Write Program for conversion from infix to postfix using push() and pop()

How to write Data structure Program for conversion from infix to postfix expression source code

#include
#include
#define max 50

typedef struct stack
{
int top;
char item[max];
}stack;
stack s1;

int push(char n)
{
if(s1.top==max-1)
puts("\nstack is full");
else
s1.item[++s1.top]=n;
return 0;
}

int pop()
{
if(s1.top==-1)
{
puts("\nstack is empty");
return 0;
}
else
return(s1.item[s1.top--]);
}

void main()
{
char inf[30],pos[30],ch,temp;
int i,j=0,valid=1;
s1.top=-1;
clrscr();
printf("\nenter infix expression:- ");
scanf("%s",&inf);
for(i=0;inf[i]!='\0';i++)
{
if(inf[i]=='(' || inf[i]=='{' || inf[i]=='[')
push(inf[i]);
if(inf[i]==')' || inf[i]=='}' || inf[i]==']')
if(s1.top==-1)
valid=0;
else
{
temp=pop();
if( inf[i]==')' && (temp=='{' || temp=='[') )
valid=0;
if( inf[i]=='}' && (temp=='(' || temp=='[') )
valid=0;
if( inf[i]==']' && (temp=='(' || temp=='{') )
valid=0;
}
}
if(s1.top>=0)
valid=0;
if( valid==1 )
{
for(i=0;inf[i]!='\0';i++)
{
if((inf[i]>=65 && inf[i]<91)||(inf[i]>=97 && inf[i]<123))
pos[j++]=inf[i];
if(inf[i]=='('||inf[i]=='{'||inf[i]=='['||inf[i]=='+'||inf[i]=='-'||inf[i]=='*'||inf[i]=='/'||inf[i]=='%')
push(inf[i]);
if(inf[i]==')'||inf[i]=='}'||inf[i]==']')
{
while(s1.top!=-1)
{
ch=pop();
if(ch!='('&&ch!='['&&ch!='{')
pos[j++]=ch;
}
}
}
pos[j]='\0';
printf("\npostfix expression is:- %s",pos);
}
else
printf("\nNot Valid expression\n");
getch();
}

Program for infix to prefix conversion | how to write code to prefix conversion

Data structure program for infix to prefix conversion with output

#include
#include
#include
#define max 50

typedef struct stack
{
int top;
char item[max];
}stack;
stack s1;

int push(char n)
{
if(s1.top==max-1)
puts("\nstack is full");
else
s1.item[++s1.top]=n;
return 0;
}
int pop()
{
if(s1.top==-1)
{
puts("\nstack is empty");
return 0;
}
else
return(s1.item[s1.top--]);
}

void main()
{
char inf[30],pre[30],temp,ch;
int i,j=0,l,valid=1;
s1.top=-1;
clrscr();
printf("\nenter infix expression:- ");
scanf("%s",&inf);
l=strlen(inf);
for(i=0;inf[i]!='\0';i++)
{
if(inf[i]=='(' || inf[i]=='{' || inf[i]=='[')
push( inf[i] );
if(inf[i]==')' || inf[i]=='}' || inf[i]==']')
if(s1.top == -1)
valid=0;
else
{
temp=pop();
if( inf[i]==')' && (temp=='{' || temp=='[') )
valid=0;
if( inf[i]=='}' && (temp=='(' || temp=='[') )
valid=0;
if( inf[i]==']' && (temp=='(' || temp=='{') )
valid=0;
}
}
if(s1.top>=0)
valid=0;
if( valid==1 )
{
for(i=l;i>=0;i--)
{
if((inf[i]>=65 && inf[i]<91)||(inf[i]>=97 && inf[i]<123))
pre[j++]=inf[i];
if(inf[i]==')'||inf[i]=='}'||inf[i]==']'||inf[i]=='+'||inf[i]=='-'||inf[i]=='*'||inf[i]=='/'||inf[i]=='%')
push(inf[i]);
if(inf[i]=='('||inf[i]=='{'||inf[i]=='[')
{
while(s1.top!=-1)
{
ch=pop();
if(ch!=']'&&ch!='}'&&ch!=')')
pre[j++]=ch;
}
}
}
pre[j]='\0';
printf("\nprefix expression is:- %s",strrev(pre));
}
else
printf("\nNot Valid Expression");
getch();
}

Implementation of Insertion Sort| Sorted list | Unsorted list |output

Program for implementation of Insertion Sort


#include

#include
#define max 20

void main()

{
int i,j,n,k,t,temp,a[max];
clrscr();
printf("\How many no. you want to insert into list:- ");
scanf("%d",&n);
for(i=0;ij;k--)
a[k]=a[k-1];
a[k+1]=temp;
}
}
}
printf("\n\nSorted list is:-\n");
for(i=0;i

Output:-

How many no. you want to insert into list:- 5


Enter 1 no:- 4

Enter 2 no:- 3
Enter 3 no:- 5
Enter 4 no:- 2
Enter 5 no:- 1

Unsorted list is:-


4

3
5
2
1

Sorted list is:-


1

2
3
4
5

*/

Program with operation on linked list | Reverse | insert | add | delete | search exampoles

Related Tags:- Data structure Program | operation on linked list | Reverse | insert | add | delete | search

#include
#include
#include

struct node

{
int info;
struct node *link;
}*start;

void create(int data)

{
struct node *q,*temp;
temp=malloc(sizeof(struct node));
temp->info=data;
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=temp;
}
}

void display()

{
struct node *temp;
temp=start;
if(start==NULL)
printf("\nList Is Empty");
else
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
}

void insbeg(int data)

{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->info=data;
temp->link=start;
start=temp;
}

void insmid(int data,int pos)

{
int i;
struct node *q,*temp;
temp=malloc(sizeof(struct node));
temp->info=data;
q=start;
for(i=1;ilink;
temp->link=q->link;
q->link=temp;
}

void insend(int data)

{
struct node *temp,*q;
q=start;
temp=malloc(sizeof(struct node));
temp->info=data;
while(q->link!=NULL)
q=q->link;
q->link=temp;
temp->link=NULL;
}

void delfir()

{
struct node *temp;
temp=start;
start=start->link;
free(temp);
}

void delmid(int data)

{
struct node *temp,*q;
q=start;
while(q->link->link!=NULL)
{
if(q->link->info==data)
{
temp=q->link;
q->link=temp->link;
free(temp);
}
q=q->link;
}
}

void delend()

{
struct node *q,*temp;
q=start;
while(q->link->link!=NULL)
q=q->link;
temp=q->link;
q->link=NULL;
free(temp);
}

void rev()

{
struct node *p1,*p2,*p3;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}

void delpos(int pos)

{
int i;
struct node *q,*temp;
q=start;
for(i=1;ilink;
temp=q->link;
q->link=temp->link;
free(temp);
}

void search(int data)

{
int i=0,flag=1;
struct node *temp;
temp=start;
while(temp!=NULL)
{
i++;
if(temp->info==data)
{
printf("\n%d is found at %d position",data,i);
flag=0;
break;
}
temp=temp->link;
}
if(flag==1)
printf("%d is not found",data);
}

void searchpos(int pos,int n)

{
int i;
struct node *temp;
temp=start;
if(pos>n)
printf("\nyou enter wrong position");
else
{
for(i=1;ilink;
printf("\n%d is found at %d position",temp->info,i);
}
}

void menu()

{
int ch,n,num,i,pos;
puts("\n\n1:for create list");
puts("2:for display");
puts("3:for exit");
puts("4:for insertion at begining");
puts("5:for insertion at between");
puts("6:for insertion at end");
puts("7:for delete first element");
puts("8:for deletion at between");
puts("9:for delete last element");
puts("10:for reverse the list");
puts("11:for delete element by position");
puts("12:for searching the element");
puts("13:for searching the element by position");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nhow many node you want to enter:- ");
scanf("%d",&n);
for(i=0;i

{
clrscr();
start=NULL;
menu();
getch();
}

Data structure Program for queue implementation using linked list source code

Program for queue implementation using linked list

#include

#include
#include

struct node

{
int info;
struct node *link;
}*front=NULL,*rear=NULL;

void insert()

{
int n;
struct node *temp;
temp=malloc(sizeof(struct node));
printf("\nenter the element u want to insert:- ");
scanf("%d",&n);
temp->info=n;
temp->link=NULL;
if(front==NULL)
front=temp;
else
rear->link=temp;
rear=temp;
}

void del()

{
struct node *temp;
temp=front;
if(front==NULL)
printf("\nqueue is empty");
else
{
printf("\n%d is deleted from queue\n",front->info);
front=front->link;
free(temp);
}
}

void display()

{
struct node *temp;
temp=front;
if(front==NULL || front==rear+1)
printf("\nqueueis empty");
else
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
}

void main()

{
int ch;
clrscr();
while(1)
{
printf("\n1:for insertion");
printf("\n2:for deletion");
printf("\n3:for display");
printf("\n4:for exit");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert();
break;
case 2:
del();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nyou entered wrong choice");
}
}
}

Program for multiplication of polynomials with examples

Related Tags:- Data structure program | multiplication of polynomials | source code
/* Program for multiplication of polynomials */

#include
#include
#define max 20

typedef struct
{
int coef;
int deg;
}poly;
poly p1[max],p2[max],p3[max],p4[max];

void createpoly(poly p[],int x)
{
int i;
for(i=0;i

postfix evaluation program with examples

Related Tags:- Data structure Program | postfix evaluation | source code
/* program for postfix evaluation */

#include

#include
#define max 50

typedef struct stack

{
int top;
int item[max];
}stack;
stack s1;

void push(int n)

{
if(s1.top==max-1)
puts("\nstack is full");
else
s1.item[++s1.top]=n;
}

int pop()

{
if(s1.top==-1)
return s1.top;
else
return(s1.item[s1.top--]);
}

void main()

{
int a,b,c,d,e,i,op1,op2;
char pos[30];
s1.top=-1;
clrscr();
puts("\nenter the value of a,b,c,d,e");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("\nenter postfix expression:- ");
scanf("%s",&pos);
for(i=0;pos[i]!='\0';i++)
{
switch(pos[i])
{
case 'a':
push(a);
break;
case 'b':
push(b);
break;
case 'c':
push(c);
break;
case 'd':
push(d);
break;
case 'e':
push(e);
break;
default:
op2=pop();
op1=pop();
}
switch(pos[i])
{
case '+':
push(op1+op2);
break;
case '-':
push(op1-op2);
break;
case '*':
push(op1*op2);
break;
case '/':
push(op1/op2);
break;
case '%':
push(op1%op2);
break;
}
}
printf("\nvalue of postfix expression is:- %d",pop());
getch();
}

Write program for prefix evaluation | How to do logic

Related Tags:- Data structure program | prefix evaluation | source code

/* Program for prefix evaluation. */

#include

#include
#define max 50

typedef struct stack

{
int top;
int item[max];
}stack;
stack s1;

void push(int n)

{
if(s1.top==max-1)
puts("\nstack is full");
else
s1.item[++s1.top]=n;
}

int pop()

{
if(s1.top==-1)
return s1.top;
else
return(s1.item[s1.top--]);
}

void main()

{
int a,b,c,d,e,i,op1,op2,l;
char pre[30];
s1.top=-1;
clrscr();
puts("enter the value of a,b,c,d,e");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
printf("\nenter prefix expression:- ");
scanf("%s",&pre);
l=strlen(pre);
for(i=l;i>=0;i--)
{
switch(pre[i])
{
case 'a':
push(a);
break;
case 'b':
push(b);
break;
case 'c':
push(c);
break;
case 'd':
push(d);
break;
case 'e':
push(e);
break;
default:
op1=pop();
op2=pop();
break;
}
switch(pre[i])
{
case '+':
push(op1+op2);
break;
case '-':
push(op1-op2);
break;
case '*':
push(op1*op2);
break;
case '/':
push(op1/op2);
break;
case '%':
push(op1%op2);
break;
}
}
printf("\nvalue of prefix expression is:- %d",pop());
getch();
}

How to write Program for stack implementation using linked list

#include
#include
#include

struct node
{
int info;
struct node *link;
}*top=NULL;

void push()
{
int n;
struct node *temp;
temp=malloc(sizeof(struct node));
printf("\nenter the element u want to insert:- ");
scanf("%d",&n);
temp->info=n;
temp->link=top;
top=temp;
}

void pop()
{
struct node *temp;
temp=top;
if(top==NULL)
printf("\nstack is empty");
else
{
printf("\n%d is deleted from stack\n",top->info);
top=top->link;
free(temp);
}
}

void display()
{
struct node *temp;
temp=top;
if(top==NULL)
printf("\nstackis empty");
else
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
}

void main()
{
int ch;
clrscr();
while(1)
{
printf("\n1:for insertion");
printf("\n2:for deletion");
printf("\n3:for display");
printf("\n4:for exit");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
default:
printf("\nyou entered wrong choice");
}
}
}

Code for Implementation of linked list with examples

Program for implementation and operation on linked list

Related Tags:-create | start | temp | delete | reverse | search | position


#include
#include
#include

struct node
{
int info;
struct node *link;
}*start;

void create(int data)
{
struct node *q,*temp;
temp=malloc(sizeof(struct node));
temp->info=data;
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=temp;
}
}

void display()
{
struct node *temp;
temp=start;
if(start==NULL)
printf("\nList Is Empty");
else
{
while(temp!=NULL)
{
printf("%d\t",temp->info);
temp=temp->link;
}
}
}

void insbeg(int data)
{
struct node *temp;
temp=malloc(sizeof(struct node));
temp->info=data;
temp->link=start;
start=temp;
}

void insmid(int data,int pos)
{
int i;
struct node *q,*temp;
temp=malloc(sizeof(struct node));
temp->info=data;
q=start;
for(i=1;ilink;
temp->link=q->link;
q->link=temp;
}

void insend(int data)
{
struct node *temp,*q;
q=start;
temp=malloc(sizeof(struct node));
temp->info=data;
while(q->link!=NULL)
q=q->link;
q->link=temp;
temp->link=NULL;
}

void delfir()
{
struct node *temp;
temp=start;
start=start->link;
free(temp);
}

void delmid(int data)
{
struct node *temp,*q;
q=start;
while(q->link->link!=NULL)
{
if(q->link->info==data)
{
temp=q->link;
q->link=temp->link;
free(temp);
}
q=q->link;
}
}

void delend()
{
struct node *q,*temp;
q=start;
while(q->link->link!=NULL)
q=q->link;
temp=q->link;
q->link=NULL;
free(temp);
}

void rev()
{
struct node *p1,*p2,*p3;
p1=start;
p2=p1->link;
p3=p2->link;
p1->link=NULL;
p2->link=p1;
while(p3!=NULL)
{
p1=p2;
p2=p3;
p3=p3->link;
p2->link=p1;
}
start=p2;
}

void delpos(int pos)
{
int i;
struct node *q,*temp;
q=start;
for(i=1;ilink;
temp=q->link;
q->link=temp->link;
free(temp);
}

void search(int data)
{
int i=0,flag=1;
struct node *temp;
temp=start;
while(temp!=NULL)
{
i++;
if(temp->info==data)
{
printf("\n%d is found at %d position",data,i);
flag=0;
break;
}
temp=temp->link;
}
if(flag==1)
printf("%d is not found",data);
}

void searchpos(int pos,int n)
{
int i;
struct node *temp;
temp=start;
if(pos>n)
printf("\nyou enter wrong position");
else
{
for(i=1;ilink;
printf("\n%d is found at %d position",temp->info,i);
}
}

void menu()
{
int ch,n,num,i,pos;
puts("\n\n1:for create list");
puts("2:for display");
puts("3:for exit");
puts("4:for insertion at begining");
puts("5:for insertion at between");
puts("6:for insertion at end");
puts("7:for delete first element");
puts("8:for deletion at between");
puts("9:for delete last element");
puts("10:for reverse the list");
puts("11:for delete element by position");
puts("12:for searching the element");
puts("13:for searching the element by position");
printf("\nenter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nhow many node you want to enter:- ");
scanf("%d",&n);
for(i=0;i

Write Program for insertion in AVL tree

Program for insertion in AVL tree

#include
#include

typedef enum { FALSE ,TRUE } bool;

struct node
{
int info;
int balance;
struct node *lchild;
struct node *rchild;
};

struct node *insert (int , struct node *, int *);

struct node* search(struct node *,int);

main()

{
bool ht_inc;
int info ;
int choice;
struct node *root = (struct node *)malloc(sizeof(struct node));
root = NULL;

while(1)

{
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter the value to be inserted : ");
scanf("%d", &info);
if( search(root,info) == NULL )
root = insert(info, root, &ht_inc);
else
printf("Duplicate value ignored\n");
break;
case 2:
if(root==NULL)
{
printf("Tree is empty\n");
continue;
}
printf("Tree is :\n");
display(root, 1);
printf("\n\n");
printf("Inorder Traversal is: ");
inorder(root);
printf("\n");
break;
case 3:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/

struct node* search(struct node *ptr,int info)

{
if(ptr!=NULL)
if(info < ptr->info)
ptr=search(ptr->lchild,info);
else if( info > ptr->info)
ptr=search(ptr->rchild,info);
return(ptr);
}/*End of search()*/

struct node *insert (int info, struct node *pptr, int *ht_inc)

{
struct node *aptr;
struct node *bptr;

if(pptr==NULL)

{
pptr = (struct node *) malloc(sizeof(struct node));
pptr->info = info;
pptr->lchild = NULL;
pptr->rchild = NULL;
pptr->balance = 0;
*ht_inc = TRUE;
return (pptr);
}


if(info < pptr->info)

{
pptr->lchild = insert(info, pptr->lchild, ht_inc);
if(*ht_inc==TRUE)
{
switch(pptr->balance)
{
case -1: /* Right heavy */
pptr->balance = 0;
*ht_inc = FALSE;
break;
case 0: /* Balanced */
pptr->balance = 1;
break;
case 1: /* Left heavy */
aptr = pptr->lchild;
if(aptr->balance == 1)
{
printf("Left to Left Rotation\n");
pptr->lchild= aptr->rchild;
aptr->rchild = pptr;
pptr->balance = 0;
aptr->balance=0;
pptr = aptr;
}
else
{
printf("Left to right rotation\n");
bptr = aptr->rchild;
aptr->rchild = bptr->lchild;
bptr->lchild = aptr;
pptr->lchild = bptr->rchild;
bptr->rchild = pptr;

if(bptr->balance == 1 )

pptr->balance = -1;
else
pptr->balance = 0;
if(bptr->balance == -1)
aptr->balance = 1;
else
aptr->balance = 0;
bptr->balance=0;
pptr=bptr;
}
*ht_inc = FALSE;
}/*End of switch */
}/*End of if */
}/*End of if*/

how to find out shortest path between 2 node | Djikstra algorithm using Graph

Program of shortest path between two node in graph using Djikstra algorithm

#include

#define MAX 10

#define TEMP 0
#define PERM 1
#define infinity 9999

struct node

{
int predecessor;
int dist; /*minimum distance of node from source*/
int status;
};

int adj[MAX][MAX];

int n;
void main()
{
int i,j;
int source,dest;
int path[MAX];
int shortdist,count;

create_graph();

printf("The adjacency matrix is :\n");
display();

while(1)

{
printf("Enter source node(0 to quit) : ");
scanf("%d",&source);
printf("Enter destination node(0 to quit) : ");
scanf("%d",&dest);

if(source==0 || dest==0)

exit(1);

count = findpath(source,dest,path,&shortdist);

if(shortdist!=0)
{
printf("Shortest distance is : %d\n", shortdist);
printf("Shortest Path is : ");
for(i=count;i>1;i--)
printf("%d->",path[i]);
printf("%d",path[i]);
printf("\n");
}
else
printf("There is no path from source to destination node\n");
}/*End of while*/
}/*End of main()*/


create_graph()

{
int i,max_edges,origin,destin,wt;

printf("Enter number of vertices : ");

scanf("%d",&n);
max_edges=n*(n-1);

for(i=1;i<=max_edges;i++)

{
printf("Enter edge %d(0 0 to quit) : ",i);
scanf("%d %d",&origin,&destin);
if((origin==0) && (destin==0))
break;
printf("Enter weight for this edge : ");
scanf("%d",&wt);
if( origin > n || destin > n || origin<=0 || destin<=0)
{
printf("Invalid edge!\n");
i--;
}
else
adj[origin][destin]=wt;
}/*End of for*/
}/*End of create_graph()*/

display()

{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%3d",adj[i][j]);
printf("\n");
}

}/*End of display()*/


int findpath(int s,int d,int path[MAX],int *sdist)

{
struct node state[MAX];
int i,min,count=0,current,newdist,u,v;
*sdist=0;
/* Make all nodes temporary */
for(i=1;i<=n;i++)
{
state[i].predecessor=0;
state[i].dist = infinity;
state[i].status = TEMP;
}

/*Source node should be permanent*/

state[s].predecessor=0;
state[s].dist = 0;
state[s].status = PERM;

/*Starting from source node until destination is found*/

current=s;
while(current!=d)
{
for(i=1;i<=n;i++)
{
/*Checks for adjacent temporary nodes */
if ( adj[current][i] > 0 && state[i].status == TEMP )
{
newdist=state[current].dist + adj[current][i];
/*Checks for Relabeling*/
if( newdist < state[i].dist )
{
state[i].predecessor = current;
state[i].dist = newdist;
}
}
}/*End of for*/

/*Search for temporary node with minimum distand make it current node*/

min=infinity;
current=0;
for(i=1;i<=n;i++)
{
if(state[i].status == TEMP && state[i].dist < min)
{
min = state[i].dist;
current=i;
}
}/*End of for*/

if(current==0) /*If Source or Sink node is isolated*/

return 0;
state[current].status=PERM;
}/*End of while*/

/* Getting full path in array from destination to source */

while( current!=0 )
{
count++;
path[count]=current;
current=state[current].predecessor;
}

/*Getting distance from source to destination*/

for(i=count;i>1;i--)
{
u=path[i];
v=path[i-1];
*sdist+= adj[u][v];
}
return (count);
}/*End of findpath()*/

Djikstra algorithm using graph

Struct queue code with examples

#include
#define max 10

struct queue

{
int item[max];
int rear,front;
};



void insert(struct queue *q,int c)

{

if(q->front==-1)

q->front=0;
if(q->rear==max-1)
printf("Given queue is overflow");
else
q->item[++(q->rear)]=c;
}

int del(struct queue *q)

{
if(q->front==-1||q->front>q->rear)
printf("Queue underflow");
else
return(q->item[(q->front)++]);
}




void show(struct queue *q)

{
int i;
for(i=q->front;i<=q->rear;i++)
{
printf(" %d\n",q->item[i]);
}
}

void main()

{
struct queue q;
int i,ch;
int e;
q.front=q.rear=-1;

while(1)

{
clrscr();
printf(" *************************** Option *******************************\n");
printf(" 0:Exit.\n");
printf(" 1:Insert element into queue.\n");
printf(" 2:Delete element from queue.\n");
printf(" 3:Show all element of queue.\n");
printf(" ******************************************************************\n");

printf("Please enter your choice=");

scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter element to insert=");
scanf("%d",&e);
insert(&q,e);


break;

case 2:
printf("Deleted element=%d",del(&q));
break;
case 3:
printf("Element of queue\n");
show(&q);
break;
default:
exit(0);
}
printf("\n Press enter to next opration");
getch();
}

}