You are on page 1of 27

Appendix E – Lab Programs

Appendix E – Lab Programs


I. C Programs

1. Odd or Even
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf(“\n Enter the value of num”);
scanf(“%d”,&num);
if(num%2==0)
printf("%d is Even number");
else
printf("%d is Odd number");
getch();
}

2. Find the sum and reverse of the given Number


#include<stdio.h>
void main()
{
unsigned long int a,num,sum=0,rnum=0,rem;
clrscr();
printf("\nEnter the number...");
scanf("%ld",&num);
a=num;
while(num!=0)
{
rem=num%10;
sum=sum+rem;
rnum=rnum*10+rem;
num=num/10;
}
printf("\nThe sum of the digits of %ld is %ld\n",a,sum);
printf("\nThe reverse number of the %ld is %ld",a,rnum);
}

1
Appendix E – Lab Programs

3. Summation of n numbers using arrays


#include <stdio.h>
int main()
{
int marks[10],i,n,sum=0;
printf("Enter number of students: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("Enter marks of student%d: ",i+1);
scanf("%d",&marks[i]);
sum+=marks[i];
}
printf("Sum= %d",sum);
return 0;
}

4. Factorial of a number using recursive function


#include<stdio.h>
int fact (int);
void main()
{
int n,fact1;
scanf("%d",&n);
fact1=fact(n);
printf("%d",fact1);
}
int fact(int n)
{
int x=1;
while(n>1)
{
x=n*fact(n-1);
}
return(x);
}

2
Appendix E – Lab Programs

5. File Handling
#include <stdio.h>
void main()
{
FILE *fopen(), *fp;
int c ;
fp = fopen( “prog.c”, “r” );
c = getc( fp ) ;
while ( c != EOF )
{
putchar( c );
c = getc ( fp );
}
fclose( fp );
}

II. Data Structures Programs

6. Implementation of Single Linked List


# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
int pos,pos1,i,c;
struct list
{
int no;
struct list *next;
}
*q,*temp,*head;
void create();
void insert();
void delete();
void count();
void display();
void main()
{
int choice;
clrscr();

3
Appendix E – Lab Programs

head=(struct list*)malloc(sizeof(struct list));


head->next=NULL;
while(1)
{
printf("\n\t\tSingle Linked List");
printf("\n\t1.Create\n\t2.insert\n\t3.delete\n\t4.Count\n\t5.Display\n\t6.Exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1: create();
break;
case 2: insert();
break;
case 3: delete();
break;
case 4: count();
break;
case 5: display();
break;
default:
exit(0);
break;
}
}
}
void create()
{
temp=(struct list*)malloc(sizeof(struct list));
printf("\n Enter the element:");
scanf("%d",&temp->no);
head->next=temp;
temp->next=NULL;
}
void insert()
{
int ch;
temp = (struct list*) malloc(sizeof(struct list));
printf("\n enter the element to insert:");

4
Appendix E – Lab Programs

scanf("%d",&temp->no);
printf("\n Enter the position:");
scanf("%d",&pos);
if(pos==1)
{
temp->next=head->next;
head->next=temp;
}
else
{
q=head->next;
for(i=1;i<pos-1;i++)
{
q=q->next;
}
temp->next=q->next;
q->next=temp;
}
}
void delete()
{
if(head->next==NULL)
printf("\nlist is empty");
else
{
printf("\n Enter the position u want to delete:");
scanf("%d",&pos1);
q=head->next;
for(i=1;i<pos1-1;i++)
q=q->next;
q->next=q->next->next;
}
}
void count()
{
if(head->next==NULL)
printf("\nlist is empty");
else
{

5
Appendix E – Lab Programs

q=head->next;
while(q!=NULL)
{
q=q->next;
c++;
}
printf("\n The no.of elements are: %d",c);
getch();
}
}

void display()
{
if(head->next==NULL)
printf("\nlist is empty");
else
{
q=head->next;
printf("\n The elements are:\n\t");
while(q!=NULL)
{
printf("%d->",q->no);
q=q->next;
}
getch();
}
}

7. Implementation of Doubly Linked List

# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
int pos,pos1,i;
struct list
{
int no;
struct list *prev,*next;
}

6
Appendix E – Lab Programs

*p,*q,*temp,*head;
void insert();
void delete();
void display();
void exit();
void main()
{
int choice;
clrscr();
head=(struct list*)malloc(sizeof(struct list));
head->next=NULL;
while(1)
{
printf("\n 1.insert\n2.delete\n3.list all\n 4.exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
default:
exit(0);
break;
}
}
}

void insert()
{
int ch;
temp = (struct list*) malloc(sizeof(struct list));
printf("\n enter the element:");

7
Appendix E – Lab Programs

scanf("%d",&temp->no);
if(head->next==NULL)
{
head->next=temp;
temp->prev=head;
temp->next=NULL;
}
else
{
printf("\n Enter the position u want to insert:");
printf("\n1.First\n2.Middle\n3.End\n--->");
scanf("%d",&ch);
switch(ch)
{
case 1:
q=head->next;
temp->next=q;
q->prev=temp;
head->next=temp;
temp->prev=head;
break;
case 2:
printf("\n Enter the position:");
scanf("%d",&pos);
q=head->next;
for(i=1;i<pos-1;i++)
{
q=q->next;
}
p=q->next;
temp->next=p;
p->prev=temp;
q->next=temp;
temp->prev=q;
break;
case 3:
q=head->next;
while(q->next!=NULL)
q=q->next;

8
Appendix E – Lab Programs

q->next=temp;
temp->prev=q;
temp->next=NULL;
break;
}
}
}
void delete()
{
if(head->next==NULL)
printf("\nlist is empty");
else
{
printf("\n Enter the position u want to delete:");
scanf("%d",&pos1);
q=head->next;
if(pos1==1)
{
head->next=q->next;
q->prev=head;
}
else
{
for(i=1;i<pos1-1;i++)
q=q->next;
p=q->next;
q->next=p->next;
p->prev=q;
}
}
}
void display()
{
if(head->next==NULL)
printf("\nlist is empty");
else
{
q=head->next;
printf("\n the elements present in the list are:\n");

9
Appendix E – Lab Programs

while(q!=NULL)
{
printf("%d->",q->no);
q=q->next;
}
getch();
}
}

8. Stack Implementation using Linked List


# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
int i;
struct stack
{
int no;
struct stack *next;
}
*q,*temp,*head;
void push();
void pop();
void display();
void main()
{
int choice;
clrscr();
head=(struct stack*)malloc(sizeof(struct stack));
head->next=NULL;
while(1)
{
rintf("\n1.Push\n2.Pop\n3.list all\n 4.exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;

10
Appendix E – Lab Programs

case 2:
pop();
break;
case 3:
display();
break;
default:
exit(0);
break;
}
}
}
void push()
{
int ch;
temp = (struct stack*) malloc(sizeof(struct stack));
printf("\n enter the element:");
scanf("%d",&temp->no);
if(head->next==NULL)
{
head->next=temp;
temp->next=NULL;
}
else
{
temp->next=head->next;
head->next=temp;
}
}
void pop()
{
if(head->next==NULL)
printf("\nstack is empty");
else
{
q=head->next;
head->next=q->next;
}
}

11
Appendix E – Lab Programs

void display()
{
if(head->next==NULL)
printf("\nstack is empty");
else
{
q=head->next;
printf("\n the elements present in the stack are:\n");
while(q!=NULL)
{
printf("%d->",q->no);
q=q->next;
}
getch();
}
}

9. Queue Implementation using Linked List


# include<stdio.h>
# include<conio.h>
# include<stdlib.h>
int pos,pos1,i;
struct queue
{
int no;
struct queue *next;
}
*q,*temp,*head;
void enqueue();
void dequeue();
void display();
void exit();
void main()
{
int choice;
clrscr();
head=(struct queue*)malloc(sizeof(struct queue));
head->next=NULL;
while(1)

12
Appendix E – Lab Programs

{
printf("\n 1.Enqueue\n2.Dequeue\n3.list all\n 4.exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;
default:
exit(0);
break;
}
}
}
void enqueue()
{
int ch;
temp = (struct queue*) malloc(sizeof(struct queue));
printf("\n enter the element:");
scanf("%d",&temp->no);
if(head->next==NULL)
{
head->next=temp;
temp->next=NULL;
}
else
{
q=head->next;
while(q->next!=NULL)
q=q->next;
q->next=temp;
temp->next=NULL;

13
Appendix E – Lab Programs

}
}
void dequeue()
{
if(head->next==NULL)
printf("\nqueue is empty");
else
{
q=head->next;
head->next=q->next;
}
}
void display()
{
if(head->next==NULL)
printf("\nqueue is empty");
else
{
q=head->next;
printf("\n the elements present in the queue are:\n");
while(q!=NULL)
{
printf("%d->",q->no);
q=q->next;
}
getch();
}
}

10. Infix to Postfix Conversion


#include <stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
int size =50;
char infix[50],postfix[50],stack[50];
int top=-1;
int precedence(char ch);
char pop();

14
Appendix E – Lab Programs

char topelement();
void push(char ch);
void main()
{
char ele,elem,st[2];
int i,prep,pre,popped,j=0;
clrscr();
strcpy(postfix," ");
printf("\n\n Enter the infix\n");
gets(infix);
for(i=0;infix[i]!='\0';i++)
{

if(infix[i]!='('&&infix[i]!=')'&&infix[i]!='^'&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&infi
x[i]!='-')
{
postfix[j++]=infix[i];
}
else if(infix[i]=='(')
{
elem=infix[i];
push(elem);
}
else if(infix[i]==')')
{
while((popped=pop())!='(')
{
postfix[j++]=popped;
}
}
else
{
elem=infix[i];
pre=precedence(elem);
ele=topelement();
prep=precedence(ele);
if(pre > prep)
push(elem);
else

15
Appendix E – Lab Programs

{
while(prep >= pre)
{
if(ele=='#')
break;
popped=pop();
ele=topelement();
postfix[j++]=popped;
prep=precedence(ele);
}
push(elem);
}
}
}
while((popped=pop())!='#')
{
postfix[j++]=popped;
}
postfix[j]='\0';
printf("\n post fix :%s",postfix);
getch();
}
int precedence(char ch)
{
switch(ch)
{
case '^' : return 5;
case '/' : return 4;
case '*' : return 4;
case '+' : return 3;
case '-' : return 3;
default : return 0;
}
}
char pop()
{
char ret;
if(top!=-1)
{

16
Appendix E – Lab Programs

ret =stack[top];
top--;
return ret;
}
else
return '#';
}
char topelement()
{
char ch;
if(top!=-1)
ch=stack[top];
else
ch='#';
return ch;
}
void push(char ch)
{
if(top!=size-1)
{
top++;
stack[top]= ch;
}
}

11. Evaluating postfix expression


# include<stdio.h>
# include<conio.h>
# include<ctype.h>
# include<process.h>
# include<string.h>
# include<stdlib.h>
# include<math.h>
# define size 100
struct stack
{
double s[size];
int top;
}st;

17
Appendix E – Lab Programs

enum Type{operand,oprtor};
void push(double val)
{
if(st.top+1>=size)
printf("Error:Stack is full\n");
st.top++;
st.s[st.top]=val;
}
double pop()
{
double val;
if(st.top==-1)
printf("Error :stack is empty\n");
val=st.s[st.top];
st.top--;
return(val);
}
char gettokn(char exp[])
{
static int i=0;
char ch;
ch=exp[i];
i++;
return ch;
}
enum Type Gettype(char ch)
{
ch=toupper (ch);
if(ch>='0'&&ch<='9')
return operand;
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='^')
return oprtor;
printf("Invalid operator\n");
}
double post(char exp[])
{
char ch,type;
double result,val,op1,op2;
st.top=0;

18
Appendix E – Lab Programs

ch=gettokn(exp);
while(ch!='$')
{
type=Gettype(ch);
if(type==operand)
{
val=ch-48;
push(val);
}
else
if(type==oprtor)
{
op2=pop();
op1=pop();
switch(ch)
{
case '+': result=op1+op2;
break;
case'-': result =op1-op2;
break;
case'*': result =op1*op2;
break;
case'/': result =op1/op2;
break;
case'^': result =pow (op1,op2);
break;
}
push(result);
}
ch=gettokn(exp);
}
result=pop();
return(result);
}
void main()
{
char exp[size];
int len;
double result;

19
Appendix E – Lab Programs

clrscr();
printf("enter the postfix expression and put $ at end\n");
scanf("%s",exp);
exp[len]=' ';
result=post(exp);
printf("the value of the expression is %f\n",result);
getch();
exit(0);
}

12. Quick sort implementation


#include<stdio.h>
#include<conio.h>
int x[100],items,i;
void sort(int,int);
void display();
void main()
{
printf("\n enter how many element do you want to feed\n");
scanf("%d",&items);
printf("enter %d elements",items);
for(i=0;i<items;++i)
scanf("%d",& x[i]);
sort(0,items-1);
display();
}
void display()
{
printf("\n sorted elements are:\n");
for(i=0;i<items;++i)
printf("%5d",x[i]);
}
void sort(int first,int last)
{
int temp,pivot,i,j;
if(first<last)
{
pivot= x[first];
i=first;

20
Appendix E – Lab Programs

j=last;
while(i<j)
{
while(x[i]<=pivot && i<last)
i++;
while(x[j]>=pivot && j>first)
j--;
if(i<j)
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[first];
x[first]=x[j];
x[j]=temp;
sort(first,j-1);
sort(j+1,last);
getch();
}
}

13. Heap sort implementation


# include<stdio.h>
# include<conio.h>
int x[100],size,i;
void buildheap();
void heapsort();
void main()
{
clrscr();
printf("\n enter how many element do u want to sort");
scanf("%d",&size);
printf("\n enter %d elements",size);
for(i=1;i<=size;++i)
scanf("%d",&x[i]);
buildheap();
heapsort();

21
Appendix E – Lab Programs

printf("\n sorted elements are\n");


for(i=1;i<=size;++i)
printf("%5d",x[i]);
getch();
}
void buildheap()
{
int j,k,temp;
for(k=2;k<size;++k)
{
i=k;
temp=x[k];
j=i/2;
while((i>1)&&(temp>x[j]))
{
x[i]=x[j];
i =j;
j=i/2;
if(j<1)
j=1;
}
x[i]=temp;
}
}
void heapsort()
{
int j,k,temp,value;
for(k=size;k>=2;--k)
{
temp=x[1];
x[1]=x[k];
x[k]=temp;
i=1;
value=x[1];
j=2;
if((j+1)<k)
if(x[j+1]>x[j])
j++;
while((j<=(k-1))&&(x[j]>value))

22
Appendix E – Lab Programs

{
x[i]=x[j];
i=j;
j=2*i;
if((j+1)<k)
if(x[j+1]>x[j])
j++;
else
if(j>size)
j=size;
x[i]=value;
}
}
}

14. Merge sort


#include<stdio.h>
#include<conio.h>
void merge(int[],int,int,int);
void merge_sort(int a[],int first,int last)
{
int mid;
if (first < last)
{
mid = (first + last) / 2;
merge_sort(a,first,mid);
merge_sort(a,mid+1,last);
merge(a,first,mid,last);
}
}
void merge(int a[],int first,int mid,int last)
{
int i,j,k,temp[20];
i = first;
j = mid + 1;
k = first;
while (i <= mid && j <= last)
{
if (a[i] < a[j])

23
Appendix E – Lab Programs

{
temp[k] = a[i];
i++;
}
else if (a[i] > a[j])
{
temp[k] = a[j];
j++;
}
else
{
temp[k++] = a[i];
temp[k] = a[j];
i++;
j++;
}
k++;
}
while (i <= mid)
{
temp[k] = a[i];
i++;
k++;
}
while (j <= last)
{
temp[k] = a[j];
j++;
k++;
}
for (k = first;k <= last;k++)
a[k] = temp[k];
}
void main()
{
int a[50],i,j,n;
printf("\nEnter number of elements: ");
scanf("%d",&n);

24
Appendix E – Lab Programs

printf("\nEnter %d elements: ",n);


for (i = 0;i < n;i++)
scanf("%d",&a[i]);
printf("\nBefore MERGE Sort: ");
for (i = 0;i < n;i++)
printf("%d\t",a[i]);
merge_sort(a,0,n-1);
printf("\nAfter MERGE Sort: ");
for(i = 0;i < n;i++)
printf("%d\t",a[i]);
}

15. Linear Search


#include<stdio.h>
#include<conio.h>
int main()
{
int arr[20];
int i,size,sech;
printf("\n\t-- Linear Search --\n\n");
printf("Enter total no. of elements : ");
scanf("%d",&size);
for(i=0; i<size; i++)
{
printf("Enter %d element : ",i+1);
scanf("%d",&arr[i]);
}
printf("Enter the element to be searched: ");
scanf("%d",&sech);
for(i=0; i<size; i++)
{
if(sech==arr[i])
{
printf("Element exits in the list at position : %d",i+1);
break;
}
}
getch();
return 0;

25
Appendix E – Lab Programs

16. Binary search implementation


#include<stdio.h>
#include<conio.h>
void main()
{
int n,a[20],temp,i,j,s,l,h,m;
clrscr();
printf("\n Enter no of elements:");
scanf("%d",&n);
printf("Enter elements");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("sorted elements");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
getch();
printf("\n enter the element to search:");
scanf("%d",&s);
l=0;
h=n;
while(l<=h)

26
Appendix E – Lab Programs

{
m=(l+h)/2;
if(s<a[m])
h=m-1;
else if(s>a[m])
l=m+1;
else if(s==a[m])
{
printf("SEARCH SUCCESSFUL");
printf("location of %d is %d",s,m+1);
}
printf("\n SEARCH UNSUCCESSFUL");
}
getch();
}

27

You might also like