Wednesday, 22 August 2012

Circular queue implementation and operation | front | rear

Circular queue implementation and operation | front | rear
/* Program of circular queue implementation and operation on circular queue */

#include<stdio.h>
#include<conio.h>
#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

*/

Circular linked list | malloc(sizeof) | deletion | insertion code

Circular linked list | malloc(sizeof) | deletion | insertion code



/* Program for implementation of circular linked list
and operation on cicular linked list */

# include <stdio.h>
# include <malloc.h>

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

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

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

void insmid(int data,int pos)
{

struct node *temp,*q;
int i;
q=last->link;
for(i=1;i<pos-1;i++)
q=q->link;
temp=malloc(sizeof(struct node));
temp->link=q->link;
temp->info=data;
q->link=temp;
}

void del(int data)
{
struct node *temp,*q;
if(last->link==last && last->info==data)
{
temp=last;
last=NULL;
free(temp);
}
q=last->link;
if(q->info==data)
{
temp=q;
last->link=q->link;
free(temp);
}
while(q->link!=last)
{
if(q->link->info==data)
{
temp=q->link;
q->link=temp->link;
free(temp);
printf("\n%d is deleted form list",data);
}
q=q->link;
}
if(q->link->info==data)
{
temp=q->link;
q->link=last->link;
free(temp);
last=q;
}
printf("\nElement %d not found",data);
}

void display()
{
struct node *q;
if(last==NULL)
printf("\nList is empty");
q=last->link;
while(q!=last)
{
printf("%d\t",q->info);
q=q->link;
}
printf("%d",last->info);
}

void main()
{
int ch,n,m,pos,i;
last=NULL;
clrscr();
while(1)
{
printf("\n\n1:For Create List");
printf("\n2:For Add at begining");
printf("\n3:For Add between");
printf("\n4:For Delete");
printf("\n5:For Display");
printf("\n6:For Exit");
printf("\nEnter your choice:- ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nHow many nodes you want into linked list:- ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the element:- ");
scanf("%d",&m);
create(m);
}
break;
case 2:
printf("\nEnter the element:- ");
scanf("%d",&m);
insbig(m);
break;
case 3:
printf("\nEnter the element:- ");
scanf("%d",&m);
printf("\nEnter the position at which this element is inserted:- ");
scanf("%d",&pos);
insmid(m,pos);
break;
case 4:
if(last==NULL)
printf("\nList is empty\n");
else
{
printf("\nEnter the number for deletion:- ");
scanf("%d",&m);
del(m);
}
break;
case 5:
display();
break;
case 6:
exit(0);
default:
printf("\nYou entered wrong choice");
}
}
}

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

Conversion from infix to postfix and prefix expression | priority method and evaluation of postfix and prefix expression | Output
conversion from infix to postfix and prefix expression | priority method and evaluation of
postfix and prefix expression | Output
/* How to  convert from infix to postfix and prefix expression
by priority method and evaluation of postfix and prefix expression */

#include<stdio.h>
#include<conio.h>
#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])<prio1(s1.item[s1.top]))
{
pre[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]!='}')
pre[j++]=pop();
pop();
}
}
if(s1.top!=-1)
{
while(s1.top!=-1)
pre[j++]=pop();
}
pre[j]='\0';
printf("\nprefix expression is:- %s",strrev(pre));
}

void eval_post()
{
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();
}

Insertion | Deletion using circular queue | How to write code

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

#include<stdio.h>
#include<conio.h>
#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 for conversion from infix to postfix using push() and pop()

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<stdio.h>
#include<conio.h>
#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();
}

Logivc of Implementation of Hash Function | Data Structure

Logivc of Implementation of Hash Function | Data Structure
How to write Data structure  Program for implementation of Hash Function using for loop with output
#include<stdio.h>
#include<conio.h>

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

*/

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

Program for infix to prefix conversion | Data Structure | how to write code
/* Data structure program for infix to prefix conversion with output */

#include<stdio.h>
#include<conio.h>
#include<string.h>
#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();
}

operation on linked list | Reverse | insert | add | delete | search

operation on linked list | Reverse | insert | add | delete | search
Data structure Program | operation on linked list | Reverse | insert | add | delete | search
#include<stdio.h>
#include<conio.h>
#include<malloc.h>

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;i<pos-1;i++)
q=q->link;
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;i<pos-1;i++)
q=q->link;
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;i<pos;i++)
temp=temp->link;
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<n;i++)
{
printf("\nenter the %d element:- ",i+1);
scanf("%d",&num);
create(num);
}
menu();
break;
case 2:
display();
menu();
break;
case 3:
exit(0);
case 4:
printf("\nenter the element you want to insert:- ");
scanf("%d",&num);
insbeg(num);
menu();
break;
case 5:
printf("\nenter the element you want to insert:- ");
scanf("%d",&num);
printf("\nenter the position you want to insert:- ");
scanf("%d",&pos);
insmid(num,pos);
menu();
break;
case 6:
printf("\nenter the element you want to insert:- ");
scanf("%d",&num);
insend(num);
menu();
break;
case 7:
delfir();
menu();
break;
case 8:
printf("\nenter the element you want to delete:- ");
scanf("%d",&num);
delmid(num);
menu();
break;
case 9:
delend();
menu();
break;
case 10:
rev();
menu();
break;
case 11:
printf("\nenter the position:- ");
scanf("%d",&pos);
delpos(pos);
menu();
break;
case 12:
printf("\nenter the element you want to search:- ");
scanf("%d",&num);
search(num);
menu();
break;
case 13:
printf("\nenter the position for searching the element:- ");
scanf("%d",&pos);
searchpos(pos,n);
menu();
break;
default:
printf("\nYou enter wrong choice");
menu();
}
}

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

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

Unsorted list | Sorted list | implementation of Insertion Sort | output
/* Program for implementation of Insertion Sort */


#include<stdio.h>
#include<conio.h>
#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;i<n;i++)
{
printf("\nEnter %d no:- ",i+1);
scanf("%d",&a[i]);
}
printf("\nUnsorted list is:-\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
for(i=1;i<n;i++)
{
for(j=0;j<i;j++)
{
if(a[i]<a[j])
{
temp=a[j];
a[j]=a[i];
for(k=i;k>j;k--)
a[k]=a[k-1];
a[k+1]=temp;
}
}
}
printf("\n\nSorted list is:-\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}

/*
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

*/

stack implementation using linked list | display() | Source code

stack implementation using linked list | display() | Source code
/* Program for stack implementation using linked list */ pop() | display() function

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

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");
}
}
}

Program for queue implementation using linked list | Diaply() | Delete() | insert()

Program for queue implementation using linked list | Diaply() | Delete() | insert()
Data structure Program for queue implementation using linked list source code
/* Program for queue implementation using linked list */

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

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");
}
}
}

Multiplication of two(2) polynomials | How to write code

Multiplication of two(2) polynomials | How to write code
Data structure program | multiplication of polynomials | source code
/* Program for multiplication of polynomials */

#include<stdio.h>
#include<conio.h>
#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<x;i++)
{
printf("\nEnter the %d coefficient of polynomial:- ",i+1);
scanf("%d",&p[i].coef);
printf("\nEnter the %d degree of polynomial:- ",i+1);
scanf("%d",&p[i].deg);
}
sort(p,x);
printf("\nEntered polynomial is:- ");
displaypoly(p,x);
}

int displaypoly(poly p[],int x)
{
int i;
for(i=0;i<x-1;i++)
printf("%dX^%d+",p[i].coef,p[i].deg);
printf("%dX^%d",p[i].coef,p[i].deg);
return 0;
}

int sort(poly p[],int x)
{
int i,j,temp1,temp2;
for(i=0;i<x;i++)
{
for(j=i+1;j<x;j++)
{
if(p[i].deg<p[j].deg)
{
temp1=p[i].deg;
p[i].deg=p[j].deg;
p[j].deg=temp1;
temp2=p[i].coef;
p[i].coef=p[j].coef;
p[j].coef=temp2;
}
}
}
return 0;
}

void mulpoly(int m,int n)
{
int i=0,j=0,k=0,l=0;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++,k++)
{
p3[k].coef=p1[i].coef*p2[j].coef;
p3[k].deg=p1[i].deg+p2[j].deg;
}
}
sort(p3,k);
for(i=0,j=i+1,l=0;i<k;i++,j++,l++)
{
if(p3[i].deg==p3[j].deg)
{
p4[l].coef=p3[i].coef+p3[j].coef;
p4[l].deg=p3[i++].deg;
}
else
{
p4[l].coef=p3[i].coef;
p4[l].deg=p3[i].deg;
}
}
displaypoly(p4,l);
}

void main()
{
int m,n;
clrscr();
printf("\nHow many term you want into the 1st polynomial:- ");
scanf("%d",&m);
createpoly(p1,m);
printf("\n\nHow many term you want into the 2nd polynomial:- ");
scanf("%d",&n);
createpoly(p2,n);
printf("\n\nMultiplication of two polynomials is:- ");
mulpoly(m,n);
getch();
}

postfix evaluation | How to write postfix evaluation source code

 postfix evaluation | How to write postfix evaluation source code
Data structure  Program |  postfix evaluation | source code
/* program for postfix evaluation */

#include<stdio.h>
#include<conio.h>
#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();
}

prefix evaluation | program source code for prefix evaluation

prefix evaluation | program source code for prefix evaluation
Data structure program | prefix evaluation |  source code
/* Program for prefix evaluation. */

#include<stdio.h>
#include<conio.h>
#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();
}

implementation and operation on linked list

implementation and operation on linked list
/* Program for implementation and operation on linked list */
create | start | temp | delete | reverse | search | position
#include<stdio.h>
#include<conio.h>
#include<malloc.h>

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;i<pos-1;i++)
q=q->link;
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;i<pos-1;i++)
q=q->link;
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;i<pos;i++)
temp=temp->link;
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<n;i++)
{
printf("\nenter the %d element:- ",i+1);
scanf("%d",&num);
create(num);
}
menu();
break;
case 2:
display();
menu();
break;
case 3:
exit(0);
case 4:
printf("\nenter the element you want to insert:- ");
scanf("%d",&num);
insbeg(num);
menu();
break;
case 5:
printf("\nenter the element you want to insert:- ");
scanf("%d",&num);
printf("\nenter the position you want to insert:- ");
scanf("%d",&pos);
insmid(num,pos);
menu();
break;
case 6:
printf("\nenter the element you want to insert:- ");
scanf("%d",&num);
insend(num);
menu();
break;
case 7:
delfir();
menu();
break;
case 8:
printf("\nenter the element you want to delete:- ");
scanf("%d",&num);
delmid(num);
menu();
break;
case 9:
delend();
menu();
break;
case 10:
rev();
menu();
break;
case 11:
printf("\nenter the position:- ");
scanf("%d",&pos);
delpos(pos);
menu();
break;
case 12:
printf("\nenter the element you want to search:- ");
scanf("%d",&num);
search(num);
menu();
break;
case 13:
printf("\nenter the position for searching the element:- ");
scanf("%d",&pos);
searchpos(pos,n);
menu();
break;
default:
printf("\nYou enter wrong choice");
menu();
}
}

void main()
{
clrscr();
start=NULL;
menu();
getch();
}
linked list operation and implemantation

Stack implementation using linked list | How to do code

Stack implementation using linked list | How to do code
/*How to write  Program for stack implementation using linked list */

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

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");
}
}
}
stack implementation using linked list

Simple Program for insertion in AVL tree

Simple Program for insertion in AVL tree
/*Program for insertion in AVL tree*/
#include<stdio.h>
#include<malloc.h>

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*/

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

Djikstra algorithm using Graph | how to find out shortest path between 2 node
* Program of shortest path between two node in graph using Djikstra algorithm */
#include<stdio.h>

#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

Logic for Struct code | examples struct queue

Logic for Struct code | examples struct queue
download struct queue code tricks
#include<conio.h>
#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();
}

}