You are on page 1of 83

Practical File

Of
DATA STRUCTURE
BCA-16-307

[Lab Based On BCA-16-305]

SUBMITTED TO: SUBMITTED BY:


MRS. LUVJINDER Roll No.-22049145
Class-BCA-3rd Sem.

Department Of Computer Science


GURU NANAK GIRLS COLLEGE
Model Town, Ludhiana
(Session- 2023-2024)
INDEX
Sr. No. Content Page No. Signature
1 Program using Array 1
2 Program to enter data into two dimensional 2-3
array
3 Program to add two matrices using Array 4-5

4 Program to multiply two matrices using Array 6-7

5 Program to insert an element from the user and 8-9


gets one another element and location for its
insertion
6 Program to delete an element from Array 10-11

7 Program to copy all the elements of an array 12


into another array
8 Program to sort elements by Merge Sort 13-14
Technique
9 Program to create a link list containing ‘n’ 15-17
nodes
10 Program to implement operations like insertion, 18-25
deletion, search etc. usnig singly link list
11 Program to implement operations like insertion, 26-33
deletion, search etc. usnig doubly link list
Program to create static stack along with its two 34-36
12 operations
13 Program to create dynamic stack along with its 37-39
two operations
14 Program in C to evaluate postfix notation 40-42

15 Program in C to convert infix notation to postfix 43-45


notation.
16 Program to print reverse string by using stack 46-47

17 Program to find the factorial of a positive 48


number using recursion
18 Program to generate the Fibonacci series using 49
recursion
19 Program to solve the Tower of Hanoi using 50
recursion
20 Program to implement linear queue using array 51-53

21 Program to implement circular queue using 54-56


array
22 Program to implement simple queue using 57-60
linked list
23 Program for Post order Tree Traversal in Binary 61
Tree
24 Program for In order Tree Traversal in Binary 62
Tree
25 Program for Pre order Tree Traversal in Binary 63-64
Tree
26 Program to search an element using linear 65-66
search
27 Program to search an element using Binary 67-68
search
28 Program to sort N elements using Bubble sort 69-70

29 Program to sort N elements using Insertion Sort 71

30 Program to sort N elements using Quick sort 72-73

31 Program of adjacency list representation of a 74-76


graph
32 Program to implement BFS traversal on graph 77-78
implemented using adjacency list
33 Program to implement DFS traversal on graph 79-80
implemented using adjacency list
 Program to show element of one dimensional array:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[40];
int n,i;
clrscr();
printf("\n Enter the number of value:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter value in a");
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
printf("\n Marks in %d subjects is %d",i+1,a[i]);
}
getche();
}
 Output:

1
 Entering data into two dimensional array from user:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][4];
int i,j;
clrscr();
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("\n Enter number");
scanf("%d",&a[i][j]);
}
}
printf("\n Data entered is=\n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%d \t",a[i][j]);
}
printf("\n");
}
getche();
}
 Output:

2
3
 Program to add two matrices using array:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],s[10][10],r,c,i,j;
clrscr();
printf("\n Enter rows and columns of 2 matrices:");
scanf("%d %d",&r,&c);
printf("\n Enter elements of first matrix");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
printf("\n Enter elements of second matrix:");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&b[i][j]);
/*Sum of two matrices*/
for(i=0;i<r;i++)
for(j=0;j<c;j++)
s[i][j]=a[i][j]+b[i][j];
printf("\n Sum of two matrices is \n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%d \t",s[i][j]);
printf("\n");
}
getch();
}
 Output:

4
5
 Program to multiply two matrices using array:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,r1,c1,r2,c2;
clrscr();
printf("\n Enter the number of rows and columns of 1st matrix:");
scanf("%d %d",&r1,&c1);
printf("\n Enter the number of rows and columns of 2nd matrix:");
scanf("%d %d",&r2,&c2);
if(c1==r2)
{
printf("\n Enter elements of 1st matrix:");
for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
printf("\n Enter elements of 2nd matrix:");
for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
scanf("%d",&b[i][j]);
/*calculate product of two matrices*/
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("\n Product of two matrices is.......\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
printf("%d \t",c[i][j]);
printf("\n");
}
}
else
{
printf("\n Matrix Multiplication is not possible");
}
getch();
6
}
 Output:

7
 Program to get the N-1 elements of array from the user and gets one
another element and location for its insertion:
#include<stdio.h>
#include<conio.h>
#define N 10
void main()
{
int la[N],loc,ITEM;
int i,j;
clrscr();
printf("\n Enter %d element of an array \n",N-1);
for(i=0;i<N-1;i++)
{
scanf("%d",&la[i]);
}
printf("\n Enter item to be inserted in array");
scanf("%d",&ITEM);
getagain:
printf("\n Enter location of array<=%d \n ",N);
scanf("%d",&loc);
if(loc>N)
goto getagain;
j=N-2;
while(j>=loc-1)
{
la[j+1]=la[j];
j--;
}
la[loc-1]=ITEM;
printf("\n Elements of Array are");
for(i=0;i<N;i++)
{
printf("\n %d",la[i]);
}
getche();
}
 Output:

8
9
 This program gets N elements of Array from the user and gets the
location for deletion of an element from array:
#include<stdio.h>
#include<conio.h>
#define N 10
void main()
{
int la[N],loc,item;
int i,j;
clrscr();
printf("\n Enter %d element of an Array \n",N);
for(i=0;i<N;i++)
{
scanf("%d",&la[i]);
}
getagain:
printf("\n Enter location of element to be deleted<=%d \n",N);
scanf("%d",&loc);
if(loc>N)
goto getagain;
item=la[loc-1];
for(j=loc-1;j<N-1;j++)
{
la[j]=la[j+1];
}
printf("\n Deleted item is=%d",item);
printf("\n Elements of Array are");
for(i=0;i<N-1;i++)
{
printf("\n %d",la[i]);
}
getche();
}
 Output:

10
11
 This program gets N elements of Array from the user and copies all the
elements to another array:
#include<stdio.h>
#include<conio.h>
#define N 10
void main()
{
int la[N],lb[N];
int i,j;
clrscr();
printf("\n Enter %d element of an Array \n",N);
for(i=0;i<N;i++)
{
scanf("%d",&la[i]);
}
for(i=0;i<N;i++)
{
lb[i]=la[i];
}
printf("\n Copied elements of Array are");
for(i=0;i<N-1;i++)
{
printf("\n %d",lb[i]);
}
getche();
}

 Output:

12
 Program to sort the elements by Merge Sort Technique:
#include<stdio.h>
#include<conio.h>
#define M 100
#define N 100
void merge_sort(int*,int,int*,int,int*);
void merge_sort(int la[],int m,int lb[],int n,int lc[])
{
int i,j,k;
i=0;j=0;k=0;
while(i<m && j<n)
{
if(la[i]<lb[j])
{
lc[k]=la[i];
i++;
}
else
{
lc[k]=lb[j];
j++;
}
k++;
}
while(i<m)
{
lc[k]=la[i];
i++;
k++;
}
while(j<n)
{
lc[k]=lb[j];
j++;
k++;
}
}
void main()
{
int la[M],lb[N],lc[M+N];
int asize,bsize,i,j,k;
clrscr();
printf("\n Enter the size of First List \n");
scanf("%d",&asize);
13
printf("\n Enter %d element for sorted Array-I\n",asize);
for(i=0;i<asize;i++)
{
scanf("%d",&la[i]);
}
printf("\n Enter the size of Second List \n ");
scanf("%d",&bsize);
printf("\n Enter %d element for Sorted Array-II \n",bsize);
for(j=0;j<bsize;j++)
{
scanf("%d",&lb[j]);
}
merge_sort(la,asize,lb,bsize,lc);
printf("\n Sorted List after applying merge sort is \n");
for(k=0;k<asize+bsize;k++)
{
printf("\n %d",lc[k]);
}
getche();
}
 Output:

14
 Program to create a link list containing 'n' nodes:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int num; //Data of the node
struct node *nextptr; //Address of the next node
}*stnode;
void createNodeList(int n); //function to create the list
void displayList(); //function to display the list
int main()
{
int n;
clrscr();
printf("\n \n Linked List: To create and display Singly Linked List:\n");
printf("------------------------\n");
printf("Input the number of nodes:");
scanf("%d",&n);
createNodeList(n);
printf("\n Data entered in the List:\n");
displayList();
return 0;
}
void createNodeList(int n)
{
struct node *fnNode,*tmp;
int num,i;
stnode=(struct node*)malloc(sizeof(struct node));
if(stnode==NULL)
{
printf("\n Memory can not be allocated:");
}
else
{
printf("\n Input data for node 1:");
scanf("%d",&num);
stnode->num=num;
stnode->nextptr=NULL;
tmp=stnode;
for(i=2;i<=n;i++)
{
fnNode=(struct node*)malloc(sizeof(struct node));
if(fnNode==NULL)
15
{
printf("\n Memory cannot be allocated:");
break;
}
else
{
printf("\n Input data for node %d:",i);
scanf("%d",&num);
fnNode->num=num;
fnNode->nextptr=NULL;
tmp->nextptr=fnNode;
tmp=tmp->nextptr;
}
}
}
}
void displayList()
{
struct node *tmp;
if(stnode==NULL)
{
printf("\n List is empty:");
}
else
{
tmp=stnode;
while(tmp!=NULL)
{
printf("Data=%d \n",tmp->num);
tmp=tmp->nextptr;
}
}
}
 Output:

16
17
 Program to implement operations like insert ,delete ,search etc.:
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
int ele,item;
struct node
{
int info;
struct node *link;
};
struct node *start=NULL,*ptr,*loc=NULL,*locp=NULL,*new1,*save,*temp;
void trevers()
{
if(start==NULL)
{
printf("\n Empty List");
return;
}
ptr=start;
while(ptr!=NULL)
{
printf("\n The element %d present at location %u",ptr->info,ptr->link);
ptr=ptr->link;
}
}
struct node *searchp(int ele)
{
if(ele==start->info)
{
loc=start;
locp=NULL;
return(locp);
}
save=start;
ptr=start->link;
while(ptr!=NULL)
{
if(ele==ptr->info)
{
locp=save;
loc=ptr;
return(locp);
}
save=ptr;
18
ptr=ptr->link;
}
locp=save;
return(locp);
}
struct node *search(int ele)
{
ptr=start;
while(ptr!=NULL)
{
if(ptr->info==ele)
{
loc=ptr;
printf("\n The element %d present at %u",ptr->info,ptr->link);
getch();
return(loc);
}
ptr=ptr->link;
}
return(NULL);
}
void insrbeg(int item)
{
new1=(struct node*)malloc(sizeof(struct node));
if(new1==NULL)
{
printf("\n Overflow");
getch();
return;
}
new1->link=NULL;
new1->info=item;
if(start==NULL)
{
start=new1;
}
else
{
new1->link=start;
start=new1;
}
}
void insrend(int item)
{
new1=(struct node*)malloc(sizeof(struct node));
19
if(new1==NULL)
{
printf("\n Overflow");
getch();
return;
}
new1->link=NULL;
new1->info=item;
if(start==NULL)
{
start=new1;
}
else
{
save=start;
ptr=start->link;
while(ptr!=NULL)
{
save=ptr;
ptr=ptr->link;
}
save->link=new1;
}
}
void deletbeg()
{
if(start==NULL)
{
printf("\n UNDERFLOW");
getch();
return;
}
else
{
temp=start;
start=start->link;
free(temp);
}
}
void deletend()
{
if(start==NULL)
{
printf("\n UNDERFLOW");
getch();
20
return;
}
save=start;
ptr=start;
while(ptr->link!=NULL)
{
save=ptr;
ptr=ptr->link;
}
if(save->link==NULL)
{
temp=start;
free(temp);
start=NULL;
}
temp=ptr;
save->link=NULL;
free(temp);
}
void delet(int item)
{
locp=searchp(item);
if(loc==NULL)
{
printf("\n Item not found");
getch();
return;
}
if(locp==NULL)
{
temp=start;
start=start->link;
free(temp);
}
temp=loc;
locp->link=loc->link;
free(temp);
}
void insraft(int item)
{
printf("\n Enter the element after which u want to insert");
scanf("%d",&ele);
loc=search(ele);
if(loc==NULL)
{
21
printf("\n Location not found sorry!");
getch();
return;
}
new1=(struct node*)malloc(sizeof(struct node));
new1->link=NULL;
new1->info=item;
if(new1==NULL)
{
printf("\n Overflow");
getch();
return;
}
else
{
new1->link=loc->link;
loc->link=new1;
}
}
void insrbef(int item)
{
printf("\n Enter the element Before which u want to insert");
scanf("%d",&ele);
loc=searchp(ele);
if(loc==NULL)
{
printf("\n Item not found!");
getch();
return;
}
new1=(struct node*)malloc(sizeof(struct node));
new1->link=NULL;
new1->info=item;
if(new1==NULL)
{
printf("\n Overflow");
getch();
return;
}
if(locp==NULL)
{
new1->link=start;
start=new1;
}
else
22
{
new1->link=locp->link;
locp->link=new1;
}
}
void main()
{
int ch;
while(1)
{
clrscr();
printf("\n ********MAIN MENU********");
printf("\n1. INSERTION AT BEGINNING");
printf("\n2. INSERTION AT END");
printf("\n3. INSERTION BEFORE AN ELEMENT");
printf("\n4. INSERTION AFTER AN ELEMENT");
printf("\n5. DELETION FROM BEGINNING");
printf("\n6. DELETION FROM ENDING");
printf("\n7. DELETION AN ITEM");
printf("\n8. SEARCHING");
printf("\n9. TRAVERSING");
printf("\n10. EXIT");
printf("\n Enter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter the element");
scanf("%d",&item);
insrbeg(item);
break;
case 2:
printf("\n Enter the element");
scanf("%d",&item);
insrend(item);
break;
case 3:
printf("\n Enter the element which u want to insert");
scanf("%d",&item);
insrbef(item);
break;
case 4:
printf("\n enter the element which u want to insert");
scanf("%d",&item);
insraft(item);
23
break;
case 5:
deletbeg();
break;
case 6:
deletend();
break;
case 7:
printf("\n enter the element which u want to delete");
scanf("%d",&ele);
delet(ele);
break;
case 8:
printf("\n enter the element of search");
scanf("%d",&ele);
loc=search(ele);
if(loc==NULL)
{
printf("\n element not found");
getche();
}
break;
case 9:
trevers();
getche();
break;
case 10:
exit(1);
break;
default:
printf("\n wrong choice");
getche();
}
}
}
 Output:

24
25
 Program to implement all the operations like insertion , deletion ,
searching etc.:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
int item,ele;
struct node
{
int info;
struct node*link,*prev;
};
struct node*temp,*new1,*start=NULL,*end=NULL,*loc=NULL,*ptr;
void trevbeg()
{
ptr=start;
printf("\n item in the lists are");
printf("item\tloc\n");
while(ptr!=NULL)
{
printf("%d\t%u\n",ptr->info,ptr->link);
ptr=ptr->link;
}
}
void trevend()
{
ptr=end;
printf("\n item in the lists are");
printf("item\tloc\n");
while(ptr!=NULL)
{
printf("%d\t%u\n",ptr->info,ptr->link);
ptr=ptr->prev;
}
}
void insertbeg(int item)
{
new1=(struct node*)malloc(sizeof(struct node));
if(new1==NULL)
{
printf("\n overflow");
getche();
return;
}
new1->link=new1->prev=NULL;
26
new1->info=item;
if(start==NULL)
{
start=end=new1;
}
else
{
new1->link=start;
start->prev=new1;
start=new1;
}
}
void insertend(int item)
{
new1=(struct node*)malloc(sizeof(struct node));
if(new1==NULL)
{
printf("\n overflow");
getche();
return;
}
new1->link=new1->prev=NULL;
new1->info=item;
if(end==NULL)
{
start=end=new1;
}
else
{
new1->prev=end;
end->link=new1;
end=new1;
}
}
struct node*search(int item)
{
ptr=start;
while(ptr!=NULL)
{
if(item==ptr->info)
{
loc=ptr;
printf("\n item found at loc %u\n",loc);
return(loc);
}
27
ptr=ptr->link;
}
return(NULL);
}
void inserbef(int item)
{
printf("\n enter the element before which u want to insert");
scanf("%d",&ele);
loc=search(ele);
if(loc==NULL)
{
printf("location not found");
getche();
return;
}
new1=(struct node*)malloc(sizeof(struct node));
if(new1==NULL)
{
printf("\n overflow");
getche();
return;
}
new1->link=new1->prev=NULL;
new1->info=item;
if(loc==start)
{
new1->link=start;
start->prev=new1;
start=new1;
}
else
{
new1->link=loc;
new1->prev=loc->prev;
loc->prev->link=new1;
loc->prev=new1;
}
}
void inseraft(int item)
{
printf("\n enter the element which u want to insert");
scanf("%d",&ele);
loc=search(ele);
if(loc==NULL)
{
28
printf("item %d not found",ele);
getche();
return;
}
new1=(struct node*)malloc(sizeof(struct node));
if(new1==NULL)
{
printf("\n overflow");
getche();
return;
}
new1->link=new1->prev=NULL;
new1->info=item;
if(loc==end)
{
new1->prev=end;
end->link=new1;
end=new1;
}
else
{
new1->prev=loc;
new1->link=loc->link;
loc->link->prev=new1;
loc->link=new1;
}
}
void deletbeg()
{
if(start==NULL)
{
printf("\n underflow");
getche();
return;
}
if(start==end)
{
temp=start;
start=end=NULL;
free(temp);
return;
}
else
{
temp=start;
29
start->link->prev=NULL;
start=start->link;
free(temp);
}
}
void deletend()
{
if(start==NULL)
{
printf("\n underflow");
getche();
return;
}
if(start==end)
{
temp=end;
start=end=NULL;
free(temp);
return;
}
else
{
temp=end;
end->prev->link=NULL;
end=end->prev;
free(temp);
}
}
void delet()
{
printf("\n enter the element which u want to delete");
scanf("%d",&ele);
loc=search(ele);
if(loc==NULL)
{
printf("\n item %d not found",ele);
getche();
return;
}
else
{
temp=loc;
loc->prev->link=loc->link;
loc->link->prev=loc->prev;
free(temp);
30
printf("%d has been deleted",ele);
getche();
}
}
void main()
{
int c;
while(1)
{
clrscr();
printf("\n**************MAIN MENU********\n");
printf("\n 1 insert at beg");
printf("\n 2 insert at end");
printf("\n 3 insert before");
printf("\n 4 insert after");
printf("\n 5 delete beg");
printf("\n 6 delete end");
printf("\n 7 delete item");
printf("\n 8 traversing begning");
printf("\n 9 traversing ending");
printf("\n 10 exit");
printf("\n enter your choice");
scanf("%d",&c);
switch(c)
{
case 1:
printf("\n enter item to be insert");
scanf("%d",&item);
insertbeg(item);
break;
case 2:
printf("\n enter item to insert");
scanf("%d",&item);
insertend(item);
break;
case 3:
printf("\n enter item to insert");
scanf("%d",&item);
inserbef(item);
break;
case 4:
printf("\n enter item to insert");
scanf("%d",&item);
inseraft(item);
break;
31
case 5:
deletbeg();
trevbeg();
getche();
break;
case 6:
deletend();
trevend();
getche();
break;
case 7:
delet();
break;
case 8:
trevbeg();
getche();
break;
case 9:
trevbeg();
getche();
break;
case 10:
exit(0);
default:
printf("\n wrong choice");
getche();
}
}
}
 Output:

32
33
 Demonstrate the creation of static stack along with its two operations
push and pop:
#include<stdio.h>
#include<conio.h>
#define max 10
int stack[max],top=-1;
//stack creation and initialization the top
void push(int item)
{
//Check for stack full
if(top==max-1)
{
printf("\n Stack Full");
getch();
return;
}
top=top+1;
//Assigning the item at the top of stack
stack[top]=item;
}
int pop(void)
{
int item;
//Check for empty stack
if(top==-1)
{
printf("\n Stack is empty");
return(NULL);
}
//Removing item from the top of stack
item=stack[top];
//Uploading the top
top=top-1;
return(item);
}
void main()
{
int ch,val;
clrscr();
while(1)
{
printf("\n 1. PUSH");
printf("\n 2. POP");
printf("\n 3. EXIT");
34
printf("\n Enter your choice");
//clearing the input buffer
fflush(stdin);
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter item to push");
scanf("%d",&val);
push(val);
break;
case 2:
val=pop();
if(val!=NULL)
{
printf("\Item from the top of stack %d",val);
}
getch();
break;
case 3:
exit(1);
default:
printf("\n Invalid input");
getch();
break;
}
}
}
 Output:

35
36
 Program to demonstrate the creation of dynamic stack along with its
two operations Push and Pop:
#include<stdio.h>
#include<conio.h>
/*defining node structure for link list*/
struct node
{
int info;
struct node *link;
};
struct node *top=NULL;
void push(int item)
{
struct node *New;
/*allocate new memory for stack*/
New=(struct node*)(malloc(sizeof(struct node)));
/*check for memory availability*/
if(New==NULL)
{
printf("\n Overflow");
getch();
return;
}
/*assigning item to new allocated node for stack*/
New->info=item;
New->link=NULL;
/*updating the stack top pointer*/
New->link=top;
top=New;
}
int pop(void)
{
int item;
struct node *temp;
/*checking for empty stack*/
if(top==NULL)
{
printf("\n Underflow");
return(NULL);
}
/*removing item from top of stack*/
item=top->info;
/*remembering the address of stack top*/
temp=top;
37
/*updating the top pointer*/
top=top->link;
/*releasing the memory*/
free(temp);
return(item);
}
void main()
{
int ch,val;
while(1)
{
clrscr();
printf("\n 1 Push");
printf("\n 2 Pop");
printf("\n 3 Exit");
printf("\n Enter Your Choice");
/*clearing the input buffer*/
fflush(stdin);
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter item to push");
scanf("%d",&val);
push(val);
break;
case 2:
val=pop();
if(val!=NULL)
{
printf("Item from the top of stack=%d",val);
}
case 3:
exit(1);
default:
printf("Invalid Input");
getch();
break;
}
}
}
 Output:

38
39
 Program to evaluate postfix notation:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define N 40
int stack[N];
int top=-1;
void push(int n)
{
if(top==n-1)
{
printf("\n stack is full");
return;
}
else
{
top=top+1;
stack[top]=n;
printf("pushed element is %d \n",n);
}
}
int pop()
{
int n;
if(top==-1)
{
printf("\n stack is empty");
}
else
{
n=stack[top];
top=top-1;
printf("the poped element is %d \n",n);
return(n);
}
}
int evaluate(int op1,int op2,char ch)
{
int n;
clrscr();
printf("op1=%d op2=%d ch=%c\n",op1,op2,ch);
if(op1<op2)
{
n=op1;
40
op1=op2;
op2=n;
}
if(ch=='+')
n=op1+op2;
else if(ch=='-')
n=op1-op2;
else if(ch=='*')
n=op1*op2;
else if(ch=='/')
n=op1/op2;
else if(ch=='%')
n=op1%op2;
else
{
printf("the operator is not indentified\n");
exit(0);
}
printf("n=%d\n",n);
return(n);
}
int main()
{
char str[50],ch,ch1;
int i=0,n,op1,op2;
printf("enter the postfix string\n");
scanf("%s",str);
ch=str[i];
while(ch!='\0')
{
printf("the char is =%c\n",ch);
if(isdigit(ch))
{
n=ch-'0';
push(n);
}
else
{
op1=pop();
op2=pop();
n=evaluate(op1,op2,ch);
push(n);
}
ch=str[++i];
}
41
printf("the value pf the arithmetic expression is=%d\n",pop());
return;
}
 Output:

42
 Program to convert infix notation to postfix notation:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 20
char stack[MAX];
int top=-1;
char pop();
void push(char item);
int precedence(char symbol)
{
int p;
switch(symbol)
{
case'+':
case'-':
p=2;
break;
case'*':
case'/':
p=4;
break;
case'^':
case'$':
p=6;
break;
case'(':
case')':
case'#':
p=1;
break;
}
return p;
}
int isoperator(char symbol)
{
switch(symbol)
{
case'+':
case'-':
case'*':
case'/':
case'^':
case'$':
43
case'(':
case')':
return 1;
default:
return 0;
}
}
void convertip(char infix[],char postfix[])
{
int i,symbol,j=0;
stack[++top]='#';
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
if(isoperator(symbol)==0)
{
postfix[j]=symbol;
j++;
}
else
{
if(symbol=='(')
push(symbol);
else if(symbol==')')
{
while(stack[top]!='(')
{
postfix[j]=pop();
j++;
}
pop();
}
else
{
if(precedence(symbol)>precedence(stack[top]))
push(symbol);
else
{
while(precedence(symbol)<=precedence(stack[top]))
{
postfix[j]=pop();
j++;
}
push(symbol);
}
44
}
}
}
while(stack[top]!='#')
{
postfix[j]=pop();
j++;
}
postfix[j]='\0';
}
void main()
{
char infix[20],postfix[20];
clrscr();
printf("enter the valid infix notation:\n");
gets(infix);
convertip(infix,postfix);
printf("the corresponding postfix notation is:\n");
puts(postfix);
getche();
}
void push(char item)
{
top++;
stack[top]=item;
}
char pop()
{
char a;
a=stack[top];
top--;
return a;
}
 Output:

45
 Program to print reverse string by using stack:
#include<stdio.h>
#include<conio.h>
typedef struct stack
{
char b[100];
int top;
}
stack;
void push(stack*s,char k)
{
if(s->top==99)
printf("\n Stack is Full");
else
s->b[++s->top]=k;
}
char pop(stack*s)
{
char c;
if(s->top==(-1))
printf("\n Stack is Empty");
else
c=s->b[s->top--];
return c;
}
void main()
{
char name[100];
int i=0;
stack s;
clrscr();
s.top=-1;
printf("\n Enter name:");
gets(name);
while(name[i]!='\0')
{
push(&s,name[i]);
i++;
}
printf("\n Reverse string is:");
while(s.top!=-1)
{
printf("%c",pop(&s));
}
46
getch();
}
 Output:

47
 Program to find the factorial of a positive number using recursion:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n Enter the number:");
scanf("%d",&n);
printf("\n The factorial of %d=%d",n,fact(n));
getche();
}
fact(int i)
{
if(i==0)
return(1);
else
return(i*fact(i-1));
}
 Output:

48
 Program to generate the Fibonacci series using recursion:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,t;
clrscr();
printf("\n Enter the number of terms to be generated:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
t=fib(i);
printf("\t %d",t);
}
getche();
}
fib(int n)
{
if(n==1)
return(0);
else
if(n==2)
return(1);
else
return(fib(n-1)+fib(n-2));
}
 Output:

49
 Program to solve the towers of Hanoi problem for 3 discs in C using
recursion:
#include<stdio.h>
#include<conio.h>
void main()
{
int no_disks;
void move(int,char,char,char); //function declaration
clrscr();
printf("\n Enter number of disks \t:");
scanf("%d",&no_disks); //Inputting number of disks
move(no_disks,'A','B','C');
getche();
}
void move(int n,char src,char inter,char dest)
{
if(n==1) //Shifting of disk
printf("\n Move disk 1 from peg %c to peg %c \n",src,dest);
else
{
move(n-1,src,dest,inter); //Source to intermediate
printf("Move disk %d from peg %c to peg %c \n",n,src,dest);
move(n-1,inter,src,dest); //Intermediate to destination
}
}
 Output:

50
 Program to implement linear queue:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define N 5
int Q[N],front,rear,item;
void qinsert(int item)
{
if(rear==N-1)
{
printf("\n \n Overflow");
getch();
return;
}
if(rear==-1&&front==-1)
{
rear=0;
front=0;
}
else
{
rear++;
Q[rear]=item;
}
void qdelete(int)
{
if(front==-1)
{
printf("\n \n UNDERFLOW");
return;
}
item=Q[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
front++;
return(item);
}
void main()
{
int ch;
51
front=rear=-1;
while(1)
{
clrscr();
printf("\n ******MAIN MENU******");
printf("\n 1 Insert");
printf("\n 2 Delete");
printf("\n 3 Exit");
printf("\n ENTER YOUR CHOICE");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter the element");
scanf("%d",&item);
qinsert(item);
break;
case 2:
item=qdelete();
if(item!=NULL);
{
printf("\nItem=%d",item);
getch();
}
break;
case 3:
exit(1);
break;
default:
printf("\n Invalid choice");
getch();
}
}
}
 Output:

52
53
 Program to implement Circular Queue using array:
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define N 5
int q[N],front,rear,item;
void qinsert(int item)
{
if((front==0&&rear==N-1)||(front==rear+1))
{
printf("\n Queue is full");
getch();
return;
}
if(front==-1)
front=rear=0;
else if(front==N-1)
rear=0;
else
rear=rear+1;
q[rear]=item;
}
int qdelete(void)
{
if(front==-1)
{
printf("\n Queue is Empty");
getch();
return(NULL);
}
item=q[front];
if(front==rear)
front=rear=-1;
else if(front==N-1)
front=0;
else
front=front+1;
return(item);
}
void main()
{
int ch;
front=rear=-1;
while(1)
54
{
clrscr();
printf("\n *******MAIN MENU*******");
printf("\n 1 INSERT");
printf("\n 2 DELETE");
printf("\n 3 EXIT");
printf("\n ENTER YOUR CHOICE");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter the element");
scanf("%d",&item);
qinsert(item);
break;
case 2:
item=qdelete();
if(item!=NULL)
{
printf("\n Item=%d",item);
getch();
}
break;
case 3:
exit(1);
break;
default:
printf("\n Invalid choice");
getch();
}
}
}
 Output:

55
56
 Program to demonstrate simple queue implementation using Linked
list:
#include<process.h>
#include<stdio.h>
#include<conio.h>
struct node
{
int info;
struct node *link;
}*front=NULL,*rear=NULL;
void Qinsert(int item)
{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Overflow");
getch();
return;
}
temp->info=item;
temp->link=NULL;
if(front==NULL)
{
front=rear=temp;
}
else
{
rear->link=temp;
rear=temp;
}
}
int Qdelete(void)
{
int Item;
struct node *temp;
if(front==NULL)
{
printf("\n Underflow");
getch();
return(NULL);
}
Item=front->info;
if(front==rear)
57
{
temp=front;
front=rear=NULL;
}
else
{
temp=front;
front=front->link;
}
free(temp);
return(Item);
}
void display()
{
struct node *ptr;
if(front==NULL)
{
printf("\n Queue is Empty");
return;
}
printf("\n Elements of Queue are \n");
ptr=front;
while(ptr!=NULL)
{
printf("%d \n",ptr->info);
ptr=ptr->link;
}
}
void main()
{
struct node Q;
int item,ch;
clrscr();
while(1)
{
printf("\n *******MENU******* ");
printf("\n \t \t 1 Insert");
printf("\n \t \t 2 Delete");
printf("\n \t \t 3 Display");
printf("\n \t \t 4 Exit");
printf("\n ***************************");
printf("\n \t \t enter your choice \n ");
fflush(stdin);
scanf("%d",&ch);
switch(ch)
58
{
case 1:
printf("\n Enter item to be insert");
scanf("%d",&item);
Qinsert(item);
break;
case 2:
item=Qdelete();
if(item!=NULL)
{
printf("\n Given item is %d",item);
getch();
}
break;
case 3:
display();
getch();
break;
case 4:
exit(0);
default:
printf("\n Invalid input");
}
}
}
 Output:

59
60
 Program For Tree Traversal of Post order In Graph:
#include<stdio.h>
#include<stdlib.h>
// We are creating struct for the binary tree below
struct node
{
int data;
struct node *left, *right;
};
// newNode function for initialisation of the newly created node
struct node *newNode (int item)
{
struct node *temporary = (struct node *) malloc (sizeof (struct node));
temporary->data = item;
temporary->left = temporary->right = NULL;
return temporary;
}
// Here we print the postorder recursively
void postorder (struct node *root)
{
if (root != NULL)
{
postorder (root->left);
postorder (root->right);
printf ("%d ", root->data);
}
}
// Basic Program to insert new node at the correct position in BST
struct node *insert (struct node *node, int data)
{
/* When there no node in the tree(subtree) then create
and return new node using newNode function */
if (node == NULL)
return newNode (data);
/* If not then we recur down the tree to find correct position for insertion */
if (data < node->data)
node->left = insert (node->left, data);
else if (data > node->data)
node->right = insert (node->right, data);
return node;
}
int main ()
{
/* What our binary search tree looks like really
61
9
/\
7 14
/\/\
5 8 11 16 */
struct node *root = NULL;
root = insert (root, 9);
insert (root, 7);
insert (root, 5);
insert (root, 8);
insert (root, 14);
insert (root, 11);
insert (root, 16);
printf ("The postorder is :\n");
postorder (root);
return 0;
}
 Output:

62
 Program To Implement Preorder Traversal On Graph:
#include <stdio.h>
#include <stdlib.h>
struct node {
int element;
struct node* left;
struct node* right;
};
/*To create a new node*/
struct node* createNode(int val)
{
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->element = val;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
/*function to traverse the nodes of binary tree in preorder*/
void traversePreorder(struct node* root)
{
if (root == NULL)
return;
printf(" %d ", root->element);
traversePreorder(root->left);
traversePreorder(root->right);
}
int main()
{
struct node* root = createNode(40);
root->left = createNode(30);
root->right = createNode(50);
root->left->left = createNode(25);
root->left->right = createNode(35);
root->left->left->left = createNode(15);
root->left->left->right = createNode(28);
root->right->left = createNode(45);
root->right->right = createNode(60);
root->right->right->left = createNode(55);
root->right->right->right = createNode(70);
printf("\n The Preorder traversal of given binary tree is -\n");
traversePreorder(root);
getche();
return 0;
}
63
 Output:

64
 Program to search an element using linear search:
#include<stdio.h>
#include<conio.h>
#define N 10
void main()
{
int a[N],loc,item;
int i;
clrscr();
printf("\n Enter %d element of an array \n",N);
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter item to be search");
scanf("%d",&item);
loc=NULL;
for(i=0;i<N;i++)
{
if(item==a[i])
{
loc=i+1;
break;
}
}
if(loc==NULL)
{
printf("\n Item %d not found in List",item);
}
else
{
printf("\n Item %d found at %d location",item,loc);
}
getch();
}
 Output:

65
66
 Program to search an item using Binary Search:
#include<stdio.h>
#include<conio.h>
#define N 10
void main()
{
int a[N],item,beg,end,mid;
int i;
clrscr();
printf("\n Enter %d element of an array \n",N);
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter item to be search");
scanf("%d",&item);
beg=0;
end=N-1;
mid=(beg+end)/2;
while(beg<=end && a[mid]!=item)
{
if(a[mid]>item)
end=mid-1;
else
beg=mid+1;
mid=(beg+end)/2;
}
if(a[mid]!=item)
{
printf("\n Item %d Not found in List",item);
}
else
{
printf("\n Item %d found at %d location ",item,mid+1);
}
getch();
}

 Output:

67
68
 Program to sort N elements using Bubble sort:
#include<stdio.h>
#define N 10
void main()
{
int a[N],p,c,t;
int i;
clrscr();
printf("\n Enter %d element of an Array \n",N);
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
for(p=1;p<=N-1;p++)
{
for(c=0;c<N-p;c++)
{
if(a[c]>a[c+1])
{
t=a[c];
a[c]=a[c+1];
a[c+1]=t;
}
}
}
printf("\n Sorted Array");
for(i=0;i<=N-1;i++)
{
printf("\t %d",a[i]);
}
getch();
}
 Output:

69
70
 Program to sort N elements using Insertion sort:
#include<stdio.h>
#include<conio.h>
#define N 10
void main()
{
int a[N],i,k,temp,ptr;
clrscr();
printf("\n Enter the elements:");
for(i=0;i<N;i++)
scanf("%d",&a[i]);
for(k=0;k<N;k++)
{
temp=a[k];
ptr=k-1;
while(temp<a[ptr]&&ptr>=0)
{
a[ptr+1]=a[ptr];
ptr--;
}
a[ptr+1]=temp;
}
printf("\n Sorted Array");
for(i=0;i<N;i++)
{
printf("\t %d",a[i]);
}
}
 Output:

71
 Program to sort n elements using Quick sort:
#include<stdio.h>
#include<conio.h>
#define N 100
void q_sort(int*,int,int);
void q_sort(int array[],int left,int right)
{
int pivot,l_hold,r_hold;
l_hold=left;
r_hold=right;
pivot=array[left];
while(left<right)
{
while((array[right]>=pivot)&&(left<right))
right--;
if(left!=right)
{
array[left]=array[right];
left++;
}
while((array[left]<=pivot)&&(left<right))
left++;
if(left!=right)
{
array[right]=array[left];
right--;
}
}
array[left]=pivot;
pivot=left;
left=l_hold;
right=r_hold;
if(left<pivot)
q_sort(array,left,pivot-1);
if(right>pivot)
q_sort(array,pivot+1,right);
}
void main()
{
int a[N],i,size;
clrscr();
printf("\n Enter the size of list \n");
scanf("%d",&size);
printf("\n Enter %d element of the list to be sorted \n",size);
72
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
q_sort(a,0,size-1);
printf("Elements after sorting are \n");
for(i=0;i<size;i++)
{
printf("%d\n",a[i]);
}
}
 Output:

73
 Program of adjacency list representation of a graph:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct node
{
int vertex;
int weight;
struct node *link;
}node;
node *adj[20];
int n;
void create_graph(int num);
void print_graph(int num);
void main()
{
node *ptr;
int i,j,m,val,wt;
clrscr();
printf("\n Enter number of nodes in graph");
scanf("%d",&n);
create_graph(n);
for(i=0;i<=n;i++)
{
printf("\n Enter nodes in the adjacency list of node %d",i);
printf("\n How many nodes in the adjacency list");
scanf("%d",&m);
for(j=1;j<=m;j++)
{
printf("\n %d node adjacent to %d",i,j);
scanf("%d",&val);
printf("\n Enter weight of the edge(%d %d):",i,j);
scanf("%d",&wt);
ptr=(node*)malloc(sizeof(struct node));
ptr->vertex=val;
ptr->weight=wt;
ptr->link=adj[i];
adj[i]=ptr;
}
}
getch();
clrscr();
printf("\n Input graph \n \n");
74
print_graph(n);
printf("\n \n Press any key to continue....");
getch();
}
void create_graph(int num)
{
int i;
for(i=1;i<=num;i++)
adj[i]=(node*)NULL;
}
void print_graph(int num)
{
node *ptr;
int i;
for(i=1;i<=num;i++)
{
ptr=adj[i];
printf("%d",i);
while(ptr!=NULL)
{
printf("->(%d %d)",ptr->vertex,ptr->weight);
ptr=ptr->link;
}
printf("\n \n");
}
}
 Output:

75
76
 Program to implement BFS traversal on a graph implemented using
adjacency list:
#include<stdio.h>
#include<conio.h>
int n,i,j,visited[10],queue[10],front=-1,rear=-1;
int adj[10][10];
void bfs(int v)
{
for(i=1;i<=n;i++)
if(adj[v][i] && !visited[i])
queue[++rear]=i;
if(front<=rear)
{
visited[queue[front]]=1;
bfs(queue[front++]);
}
}
void main()
{
int v;
clrscr();
printf("Enter number of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
queue[i]=0;
visited[i]=0;
}
printf("Enter graph data in matrix form:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%d",&adj[i][j]);
printf("Enter the starting vertex:");
scanf("%d",&v);
bfs(v);
printf("The node which are reachable are: \n");
for(i=1;i<=n;i++)
if(visited[i])
printf("%d \t",i);
else
printf("BFS is not possible.Not all nodes are reachable");
getch();
}

77
 Output:

78
 Program to implement DFS traversal on graph represented using
adjacency list:
#include<stdio.h>
#include<conio.h>
typedef struct node
{
int data;
struct node *link;
}node;
node *g[20];
int visit[20];
int n;
void read();
void edge(int,int);
void dfs(int);
void main()
{
int i;
clrscr();
read();
for(i=0;i<n;i++)
visit[i]=0;
dfs(0);
}
void dfs(int v)
{
node *p;
printf("\n Visit %d",v);
p=g[v];
visit[v]=1;
while(p!=NULL)
{
v=p->data;
if(!visit[v])
dfs(v);
p=p->link;
}
}
void read()
{
int vi,vj,i,noe;
printf("\n Enter number of nodes:");
scanf("%d",&n);
for(i=0;i<n;i++)
79
{
g[i]=NULL;
printf("Enter number of edges:");
scanf("%d",&noe);
for(i=0;i<noe;i++)
{
printf("Enter edge(vi,vj):");
scanf("%d%d",&vi,&vj);
edge(vi,vj);
}
}
}
void edge(int vi,int vj)
{
node *p,*q;
q=(node*)malloc(sizeof(node));
q->data=vj;
q->link=NULL;
if(g[vi]==NULL)
g[vi]=q;
else
{
p=g[vi];
while(p->link!=NULL);
p=p->link;
p->link=q;
}
}
 Output:

80

You might also like