September 2018 - c program with parth patthar

c program with parth patthar

learn c and c++ in better way and with simple example.

Believe on your own logic

Sunday, September 30, 2018

c program for split doubly link list

September 30, 2018 0
c program for split doubly link list
#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *lptr,*rptr;
};
struct node *nn,*dnode,*l,*r,*temp,*l1,*r1,*l2,*r2;;
int item;
void insfirdll()
{
nn=(struct node *)malloc(sizeof(struct node));
if(nn==NULL)
{
printf("\n memory not allocated:");
return;
}
printf("\n enter item:");
scanf("%d",&item);
nn->info=item;
if(l==NULL)
{
nn->lptr=NULL;
nn->rptr=NULL;
l=nn;
r=nn;
return;
}
nn->rptr=NULL;
nn->lptr=r;
r->rptr=nn;
r=nn;
}
void displaydll()
{
temp=l;
while(temp!=NULL)
{
printf("\n %d",temp->info);
temp=temp->rptr;
}
getch();
}
void splitdll()
{
if(l==NULL)
{
printf("\n list is empty:");
return;
}
while(l!=NULL)
{

if(l->info%2==0)
{
if(l1==NULL)
{
l1=l;
r1=l;
l1->lptr=NULL;
}
else
{
l->lptr=r1;
r1->rptr=l;
r1=l;

}
}
else
{
if(l2==NULL)
{
l2=l;
r2=l;
l2->lptr=NULL;
}
else
{
l->lptr=r2;
r2->rptr=l;
r2=l;

}
}
l=l->rptr;
}
r1->rptr=NULL;
r2->rptr=NULL;
}
void disoddlist()
{
temp=l1;
while(temp!=NULL)
{
printf("\n %d",temp->info);
temp=temp->rptr;
}
getch();
}
void disevenlist()
{
temp=l2;
while(temp!=NULL)
{
printf("\n %d",temp->info);
temp=temp->rptr;
}
getch();
}
void main()
{
int ch;
do
{
clrscr();
printf("\n 1)insert first");
printf("\n 2)display");
printf("\n 3)split");
printf("\n 4)display odd list");
printf("\n 5)display even list");
printf("\n enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 0:break;
case 1:insfirdll();
break;
case 2:displaydll();
break;
case 3:splitdll();
break;
case 4:disoddlist();
break;
case 5:disevenlist();
break;
default:printf("\n invalid input:");
getch();
}
}while(ch!=0);
}

c program for sort singly link list

September 30, 2018 0
c program for sort singly link list
#include<stdio.h>
#include<conio.h>
struct node
{
int rn,m1,m2,m3;
float per;
char name[10];
struct node *link;
};
struct node *nn,*dnode,*first,*temp;
int tot;
void addrecord()
{
nn=(struct node*)malloc(sizeof(struct node));
if(nn==NULL)
{
printf("\n mamory not allocated");
getch();
return;
}
printf("\n enter rn");
scanf("%d",&nn->rn);
printf("\n enter name:");
scanf("%s",nn->name);
printf("\n enter m1:");
scanf("%d",&nn->m1);
printf("\n enter m2:");
scanf("%d",&nn->m2);
printf("\n enter m3:");
scanf("%d",&nn->m3);
tot=nn->m1+nn->m2+nn->m3;
nn->per=tot/3;
if(first==NULL)
{
nn->link=NULL;
first=nn;
return;
}
nn->link=first;
first=nn;
}
void display()
{
temp=first;
while(temp!=NULL)
{
printf("\n %d",temp->rn);
printf("\n %f",temp->per);
temp=temp->link;
}
getch();
}
void sort()
{
dnode=first;
while(dnode!=NULL)
{
temp=dnode;
nn=dnode;
while(temp->link->per>=dnode->per&&temp->link!=NULL)
{
temp=temp->link;
}
dnode->link=temp->link;
temp->link=dnode;
dnode=dnode->link;
}
}
void main()
{
int ch;
do
{
clrscr();
printf("\n 1)for insert record:");
printf("\n 2)for display record:");
printf("\n enter your ch:");
scanf("%d",&ch);
switch(ch)
{
case 1:addrecord();
break;
case 2:display();
break;
case 3:sort();
break;
}
}while(ch!=0);
}

c program of convert infix to prefix expression

September 30, 2018 0
c program of convert infix to prefix expression
#include<stdio.h>
#include<conio.h>
void main()
{
char q[30],s[30],p[30];
int i,top=0,a=0;
clrscr();
printf("\n enter your expretion :");
scanf("%s",q);
strcat(strrev(q),"(");
s[top]=')';
for(i=0;q[i];i++)
{
switch(q[i])
{
case ')':
top++;
s[top]=q[i];
break;
case '+':
case '-':
while(s[top]=='*'||s[top]=='/')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case '*':
case '/':
       while(s[top]=='^')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case '^':
top++;
s[top]=q[i];
break;
case '(':

while(s[top]!=')')
{
p[a]=s[top];
top--;
a++;
}
break;
default:
p[a]=q[i];
a++;
}
}
for(i=a-1;i>=0;i--)
   printf("%c",p[i]);

getch();

}

c program for convert infix to post fix expression

September 30, 2018 0
c program for convert infix to post fix expression
#include<stdio.h>
#include<conio.h>
void main()
{
char q[30],s[30],p[30];
int i,top=0,a=0;
clrscr();
printf("\n enter your expretion :");
scanf("%s",q);
strcat(q,")");
s[top]='(';
for(i=0;q[i];i++)
{
switch(q[i])
{
case '(':
top++;
s[top]=q[i];
break;
case '+':
case '-':
while(s[top]=='+'||s[top]=='-'||s[top]=='*'||s[top]=='/')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case '*':
case '/':
       while(s[top]=='*'||s[top]=='/'||s[top]=='^')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case '^':
while(s[top]=='^')
{
p[a]=s[top];
top--;
a++;
}
top++;
s[top]=q[i];
break;
case ')':

while(s[top]!='(')
{
p[a]=s[top];
top--;
a++;
}
break;
default:
p[a]=q[i];
a++;
}
}
for(i=0;i<a;i++)
   printf("%c",p[i]);

getch();

}

c program for evaluation of postfix expression

September 30, 2018 0
c program for evaluation of postfix expression
#include<stdio.h>
#include<conio.h>
void main()
{
int s[30],i,a,b,top=-1;
char p[30];
printf("\n enter your expretion:");
gets(p);
strcat(p,")");
printf("%s",p);
for(i=0;p[i]!=')';i++)
{
if(p[i]=='+'||p[i]=='-'||p[i]=='*'||p[i]=='/'||p[i]=='^')
{
a=s[top];
top--;
b=s[top];
printf("\n%d\t%d",a,b);
switch(p[i])
{
case '+':
s[top]=b+a;
printf("\t%d",s[top]);
break;

case '-':
s[top]=b-a;
printf("\t%d",s[top]);
break;

case '*':
s[top]=b*a;
printf("\t%d",s[top]);
break;

case '/':
s[top]=b/a;
printf("\t%d",s[top]);
break;

case '^':
s[top]=b^a;
printf("\t%d",s[top]);
break;
}
}
else
{
top++;
s[top]=p[i]-48;
}
}
printf("%d",s[top]);
getch();
}

c program for evaluation of prefix

September 30, 2018 0
c program for evaluation of prefix
#include<stdio.h>
#include<conio.h>
void main()
{
int s[30],i,a,b,top=-1;
char p[30];
printf("\n enter your expretion:");
gets(p);
strcat(strrev(p),"(");
printf("%s",p);
for(i=0;p[i]!='(';i++)
{
if(p[i]=='+'||p[i]=='-'||p[i]=='*'||p[i]=='/'||p[i]=='^')
{
a=s[top];
top--;
b=s[top];
printf("\n%d\t%d",a,b);
switch(p[i])
{
case '+':
s[top]=a+b;
printf("\t%d",s[top]);
break;

case '-':
s[top]=a-b;
printf("\t%d",s[top]);
break;

case '*':
s[top]=a*b;
printf("\t%d",s[top]);
break;

case '/':
s[top]=a/b;
printf("\t%d",s[top]);
break;

case '^':
s[top]=a^b;
printf("\t%d",s[top]);
break;
}
}
else
{
top++;
s[top]=p[i]-48;
}
}
printf("%d",s[top]);
getch();
}

c program for insert at specific position and delete from specific position in singly link list

September 30, 2018 0
c program for insert at specific position and delete from specific position in singly link list
#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *link;
};
struct node *first=NULL,*temp,*prev;
int item,i,p;
void insert()
{
struct node *nn;
nn=((struct node*)malloc(sizeof(nn)));
if(nn==NULL)
{
printf("\n memory not allocate:");
getch();
return;
}
printf("\n enter position for insert:");
scanf("%d",&p);
printf("\n enter item:");
scanf("%d",&item);
nn->info=item;
if(p==1)
{
if(first==NULL)
{
nn->link=NULL;
first=nn;
}
else
{
nn->link=first;
first=nn;
}
return;
}
temp=first;
for(i=2;i<p;i++)
{
temp=temp->link;
}
nn->link=temp->link;
temp->link=nn;
}
void display()
{
temp=first;
while(temp!=NULL)
{
printf("\n %d",temp->info);
temp=temp->link;
}
getch();
}
void delet()
{
if(first==NULL)
{
printf("\n list is empty ");
getch();
return;
}
printf("\n enter position for delete:");
scanf("%d",&p);
if(p==1)
{
temp=first;
first=first->link;
free(temp);
return;
}
prev=first;
temp=first->link;
for(i=2;i<p;i++)
{
prev=temp;
temp=temp->link;
}
prev->link=temp->link;
free(temp);
}
void main()
{
int ch;
do
{
clrscr();
printf("\n 0)for exit:");
printf("\n 1)for insert:");
printf("\n 2)for display:");
printf("\n 3)for delete:");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 0:break;
case 1:insert();
break;
case 2:display();
break;
case 3:delet();
break;
default:printf("\n invalid input:");
getch();
}
}while(ch!=0);
}

c program for insert in order pid in order and delete specific patient information

September 30, 2018 0
 c program for insert in order pid in order and delete specific patient information
#include<stdio.h>
#include<conio.h>
struct node
{
int pid;
char name[20];
struct node *link;
};
struct node *nn,*prev,*first,*temp;
void insert()
{
nn=(struct node*)malloc(sizeof(struct node));
if(nn==NULL)
{
printf("\n memory not allocated");
return;
}
printf("\n enter pid:");
scanf("%d",&nn->pid);
printf("\n enter name:");
scanf("%s",nn->name);
if(first==NULL)
{
first=nn;
first->link=NULL;
return;
}
if(first->pid>nn->pid)
{
nn->link=first;
first=nn;
return;
}
temp=first;
while(temp->link!=NULL&&temp->link->pid<nn->pid)
{
temp=temp->link;
}
nn->link=temp->link;
temp->link=nn;
}
void delet()
{
int item;
if(first==NULL)
{
printf("\n list is empty:");
getch();
return;
}
printf("\n enter pid for delete:");
scanf("%d",&item);
if(first->pid==item)
{
temp=first;
first=first->link;
free(temp);
return;
}
temp=first;
prev=NULL;
while(temp->link!=NULL&&temp->pid!=item)
{
prev=temp;
temp=temp->link;
}
if(temp->pid!=item)
{
printf("\n node not found:");
getch();
return;
}
prev->link=temp->link;
free(temp);
}
void display()
{
temp=first;
while(temp!=NULL)
{
printf("\n pid=%d",temp->pid);
printf("\n name=%s",temp->name);
temp=temp->link;
}
getch();
}
void main()
{
int ch;
do
{
clrscr();
printf("\n 0)for exit");
printf("\n 1)for insert");
printf("\n 2)for display:");
printf("\n 3)for delete:");
printf("\n enter your choice:");
scanf("%d",&ch);
switch(ch)
{

case 1:insert();
break;
case 2:display();
break;
case 3:delet();
break;
}
}while(ch!=0);
}