SlideShare uma empresa Scribd logo
1 de 44
Baixar para ler offline
Page | 1
PROGRAM – 1
AIM: Write a program to find the sum of first ten natural numbers using
recursion.
SOURCE CODE:
#include <stdio.h>
int addNumbers(int n);
int main()
{
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Sum = %d",addNumbers(num));
return 0;
}
int addNumbers(int n)
{
if(n != 0)
return n + addNumbers(n-1);
else
return n;
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 2
PROGRAM – 2
AIM: Write a program to generate Fibonacci series using recursion.
SOURCE CODE:
#include < stdio.h >
int Fibonacci(int);
int main()
{
int n, i = 0, c;
printf("Enter the number of terms ");
scanf("%d",&n);
printf("First %d terms of Fibonacci series are :-n", n);
for ( c = 1 ; c < = n ; c++ )
{
printf("%dn", Fibonacci(i));
i++;
}
return 0;
}
int Fibonacci(int n)
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return 1;
else
return ( Fibonacci(n-1) + Fibonacci(n-2) );
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 3
PROGRAM – 3
AIM: Write a program to find factorial of number using recursion.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
long factorial(int);
int main()
{
int n;
long f;
printf("Enter an integer to find factorialn");
scanf("%d", &n);
if (n < 0)
printf("Negative integers are not allowed.n");
else
{
f = factorial(n);
printf("%d! = %ldn", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
/*recursive call to factorial function*/
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 4
PROGRAM – 4
AIM: Write a program to perform all basic operations (such as insert, delete)
on an array.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int insert(int *);
int view(int *);
int del(int *);
void main()
{
int a[100];
int ch;
while(1)
{ clrscr();
{ printf("nEnter 1 to insert element in array:t");
printf("nEnter 2 to view element in array:t");
printf("nEnter 3 to Delete element in array:t");
printf("nEnter 4 to Exit:t");
printf("n enter the choice n");
scanf("%d",&ch);
switch(ch)
{
case 1:insert(a);getch();
break;
case 2:view(a);getch();
break;
case 3:del(a);getch();
break;
case 4:exit(0);
}
}
}
}
int insert(int *a)
{
int i,n;
Page | 5
printf("Enter the no. of elements in array:t");
scanf("%d",&n);
printf("nEnter %d elements in array:t",n);
for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
}
a[i]='0';
return *a;
}
int view(int *a)
{
int j;
for(j=0;a[j]!=NULL;j++)
{ printf("nElement of array=%d",a[j]);
}
return *a;
}
int del(int *a)
{
int c,k,posi;
for(k=0;a[k]!=NULL;k++)
printf("Enter the position to delete element:t");
scanf("%d",&posi);
if(posi<=k)
{
for(c=posi-1;c<k-1;c++)
{ a[c]=a[c+1];
}
printf("nArray after Deletion");
for(c=0;c<k-1;c++)
{ printf("n%d",a[c]);
}
}
return *a;
}
ERROR: No Error.
RESULT: Code Execute successfully
Page | 6
PROGRAM – 5
AIM: Write a program which reads two matrices and then print the matrix
which is addition of these two matrices.
SOURCE CODE:
#include < stdio.h >
int main()
{
int m, n, c, d, first[10][10], second[10][10],
sum[10][10];
printf("Enter the number of rows and columns of
matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrixn");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
printf("Enter the elements of second matrixn");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
/* Matrix addition */
printf("Sum of entered matrices:-n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
Page | 7
printf("%dt", sum[c][d]);
printf("n");
}
return 0;
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 8
PROGRAM – 6
AIM: Write a program which reads two matrices & multiply them.
SOURCE CODE:
#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("Enter the number of rows and columns of first
matrixn");
scanf("%d%d", &m, &n);
printf("Enter the elements of first matrixn");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
printf("Enter the number of rows and columns of second
matrixn");
scanf("%d%d", &p, &q);
if ( n != p )
printf("Matrices with entered orders can't be multiplied
with each other.n");
else
{
printf("Enter the elements of second matrixn");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
sum = sum + first[c][k]*second[k][d];
}
Page | 9
multiply[c][d] = sum;
sum = 0;
}
}
printf("Product of entered matrices:-n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%dt", multiply[c][d]);
printf("n");
}
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 10
PROGRAM – 7
AIM: Write a program to find highest & lowest element in array
SOURCE CODE:
#include <stdio.h>
int main()
{
int arr[100];
int i, max, min, size;
/* Reads size array and elements in the array */
printf("Enter size of the array: ");
scanf("%d", &size);
printf("Enter elements in the array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}
/* Supposes the first element as maximum and minimum */
max = arr[0];
min = arr[0];
/*Finds maximum and minimum in all array elements */
for(i=1; i<size; i++)
{
/* If current element of array is greater than max */
if(arr[i]>max)
{
max = arr[i];
}
/* If current element of array is smaller than min */
if(arr[i]<min)
{
min = arr[i];
}
}
Page | 11
/* Prints the maximum and minimum element*/
printf("Maximum element = %dn", max);
printf("Minimum element = %d", min);
return 0;
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 12
PROGRAM – 8
AIM: Write a program to merge two unsorted array.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{ int arr1[10],arr2[20],arr3[20];
int i, n1, n2, m, index;
printf(“Enter the number of elements in array 1:”);
scanf(“%d”,&n1);
printf(“Enter the elements of array 1:”);
for(i=0;i<n1;i++)
{
printf(“n arr1[%d]=”,i);
scanf(“%d”,&arr1[i]);
}
printf(“Enter the number of elements in array 2:”);
scanf(“%d”,&n2);
for(i=0;i<n2;i++)
{
printf(“n arr2[%d]=”,i);
scanf(“%d”,&arr2[i]);
}
//merging
for(i=0;i<n1;i++)
{
arr3[index]=arr1[i];
index++;
}
for(i=0;i<n2;i++)
{
arr3[index]=arr2[i];
index++;
}
//Print the result
for(i=0;i<m;i++)
Page | 13
printf(“n arr3[%d]=%d”,I,arr3[i]);
getch();
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 14
PROGRAM – 9
AIM: Write a program to merge two sorted array.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
{ int arr1[10],arr2[20],arr3[20];
int i, n1, n2, index=0;
int index_first=0, index_second=0;
printf(“Enter the number of elements in array 1:”);
scanf(“%d”,&n1);
printf(“Enter the elements of array 1:”);
for(i=0;i<n1;i++)
{
printf(“n arr1[%d]=”,i);
scanf(“%d”,&arr1[i]);
}
printf(“Enter the number of elements in array 2:”);
scanf(“%d”,&n2);
for(i=0;i<n2;i++)
{
printf(“n arr2[%d]=”,i);
scanf(“%d”,&arr2[i]);
}
//merging
while(index_first<n1 && index_second<n2)
{
if(arr1[index_first]<arr2[index_second])
{
arr3[index]=arr1[index_first];
index_first++;
}
else
{ arr3[index]=arr2[index_second];
index_second++;
}
index++;
}
Page | 15
//if elements of first elements are over & second
array has some elements.
if(index_first==n1)
{
while(index_second<n2)
{
arr3[index]=arr2[index_second];
index_second++;
index++;
}
}
//if elements of second array are over & first
array has some elements.
if(index_second==n2)
{
while(index_first<n1)
{
arr3[index]=arr1[index_first];
index_first++;
index++;
}
}
//Print the result
printf(“nn The merged array is:”);
for(i=0;i<m;i++)
{
printf(“n arr3[%d]=%d”,I,arr3[i]);
}
getch();
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 16
PROGRAM – 10
AIM: Write a program to perform PUSH & POP operations on stack.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
#define max 5
void main()
{
//... create stack
int stack[max],data;
int top,option,reply;
//... init stack
top = -1;
clrscr();
do
{
printf("n 1. push");
printf("n 2. pop");
printf("n 3. exit");
printf("nSelect proper option : ");
scanf("%d",&option);
switch(option)
{
case 1 : // push
printf("n Enter a value : ");
scanf("%d",&data);
reply = push(stack,&top,&data);
if( reply == -1 )
printf("nStack is full");
else
printf("n Pushed value");
break;
case 2 : // pop
reply = pop ( stack,&top,&data);
if( reply == - 1)
Page | 17
printf("nStack is empty");
else
printf("n Popped value is %d",data);
break;
case 3 : exit(0);
} // switch
}while(1);
} // main
int push( int stack[max],int *top, int *data)
{
if( *top == max -1 )
return(-1);
else
{
*top = *top + 1;
stack[*top] = *data;
return(1);
} // else
} // push
int pop( int stack[max], int *top, int *data)
{
if( *top == -1 )
return(-1);
else
{
*data = stack[*top];
*top = *top - 1;
return(1);
} //else
} // pop
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 18
PROGRAM – 11
AIM: Write a program to implement linear queue using array.
SOURCE CODE:
#include <stdio.h>
#define MAX 50
int queue_array[MAX];
int rear = - 1;
int front = - 1;
main()
{
int choice;
while (1)
{
printf("1.Insert element to queue n");
printf("2.Delete element from queue n");
printf("3.Display all elements of queue n");
printf("4.Quit n");
printf("Enter your choice : ");
scanf("%d", &choice);
switch (choice)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice n");
} /*End of switch*/
} /*End of while*/
Page | 19
} /*End of main()*/
insert()
{
int add_item;
if (rear == MAX - 1)
printf("Queue Overflow n");
else
{
if (front == - 1)
/*If queue is initially empty */
front = 0;
printf("Inset the element in queue : ");
scanf("%d", &add_item);
rear = rear + 1;
queue_array[rear] = add_item;
}
} /*End of insert()*/
delete()
{
if (front == - 1 || front > rear)
{
printf("Queue Underflow n");
return ;
}
else
{
printf("Element deleted from queue is : %dn",
queue_array[front]);
front = front + 1;
}
} /*End of delete() */
display()
{
int i;
if (front == - 1)
printf("Queue is empty n");
else
{
printf("Queue is : n");
Page | 20
for (i = front; i <= rear; i++)
printf("%d ", queue_array[i]);
printf("n");
}
} /*End of display() */
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 21
PROGRAM – 12
AIM: Write a program to implement Circular queue using array.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct Node
{
int Data;
struct Node* next;
}*rear, *front;
void delQueue()
{
struct Node *temp, *var=rear;
if(var==rear)
{
rear = rear->next;
free(var);
}
else
printf("nQueue Empty");
}
void push(int value)
{
struct Node *temp;
temp=(struct Node *)malloc(sizeof(struct Node));
temp->Data=value;
if (front == NULL)
{
front=temp;
front->next=NULL;
rear=front;
}
else
{
Page | 22
front->next=temp;
front=temp;
front->next=rear;
}
}
void display()
{
struct Node *var=rear;
if(var!=NULL)
{
printf("nElements are as: ");
while(var!=front)
{
printf("t%d",var->Data);
var=var->next;
}
if(var==front)
{
printf("t%d",var->Data);
}
printf("n");
}
else
printf("nQueue is Empty");
}
int main(int argc, char *argv[])
{
int i=0;
front=NULL;
printf(" n1. Push to Queue");
printf(" n2. Pop from Queue");
printf(" n3. Display Data of Queue");
printf(" n4. Exitn");
while(1)
{
printf(" nChoose Option: ");
scanf("%d",&i);
switch(i)
{
case 1:
Page | 23
{
int value;
printf("nEnter a valueber to push into Queue: ");
scanf("%d",&value);
push(value);
display();
break;
}
case 2:
{
delQueue();
display();
break;
}
case 3:
{
display();
break;
}
case 4:
{
exit(0);
}
default:
{
printf("nwrong choice for operation");
}
}
}
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 24
PROGRAM – 13
AIM: : Write a program to implement Dequeue using array.
SOURCE CODE:
#include<stdio.h>
#include<process.h>
#define MAX 30
typedef struct dequeue
{
int data[MAX];
int rear,front;
}dequeue;
void initialize(dequeue *p);
int empty(dequeue *p);
int full(dequeue *p);
void enqueueR(dequeue *p,int x);
void enqueueF(dequeue *p,int x);
int dequeueF(dequeue *p);
int dequeueR(dequeue *p);
void print(dequeue *p);
void main()
{
int i,x,op,n;
dequeue q;
initialize(&q);
do
{
printf("n1.Createn2.Insert(rear)n3.Insert(front)n4.Delet
e(rear)n5.Delete(front)");
printf("n6.Printn7.ExitnnEnter your choice:");
Page | 25
scanf("%d",&op);
switch(op)
{
case 1: printf("nEnter number of elements:");
scanf("%d",&n);
initialize(&q);
printf("nEnter the data:");
for(i=0;i<n;i++)
{
scanf("%d",&x);
if(full(&q))
{
printf("nQueue is full!!");
exit(0);
}
enqueueR(&q,x);
}
break;
case 2: printf("nEnter element to be
inserted:");
scanf("%d",&x);
if(full(&q))
{
printf("nQueue is full!!");
exit(0);
}
enqueueR(&q,x);
break;
case 3: printf("nEnter the element to be
inserted:");
scanf("%d",&x);
if(full(&q))
{
Page | 26
printf("nQueue is full!!");
exit(0);
}
enqueueF(&q,x);
break;
case 4: if(empty(&q))
{
printf("nQueue is empty!!");
exit(0);
}
x=dequeueR(&q);
printf("nElement deleted is %dn",x);
break;
case 5: if(empty(&q))
{
printf("nQueue is empty!!");
exit(0);
}
x=dequeueF(&q);
printf("nElement deleted is %dn",x);
break;
case 6: print(&q);
break;
default: break;
}
}while(op!=7);
}
void initialize(dequeue *P)
{
P->rear=-1;
P->front=-1;
}
Page | 27
int empty(dequeue *P)
{
if(P->rear==-1)
return(1);
return(0);
}
int full(dequeue *P)
{
if((P->rear+1)%MAX==P->front)
return(1);
return(0);
}
void enqueueR(dequeue *P,int x)
{
if(empty(P))
{
P->rear=0;
P->front=0;
P->data[0]=x;
}
else
{
P->rear=(P->rear+1)%MAX;
P->data[P->rear]=x;
}
}
void enqueueF(dequeue *P,int x)
{
if(empty(P))
{
P->rear=0;
P->front=0;
P->data[0]=x;
}
Page | 28
else
{
P->front=(P->front-1+MAX)%MAX;
P->data[P->front]=x;
}
}
int dequeueF(dequeue *P)
{
int x;
x=P->data[P->front];
if(P->rear==P->front) //delete the last element
initialize(P);
else
P->front=(P->front+1)%MAX;
return(x);
}
int dequeueR(dequeue *P)
{
int x;
x=P->data[P->rear];
if(P->rear==P->front)
initialize(P);
else
P->rear=(P->rear-1+MAX)%MAX;
return(x);
}
void print(dequeue *P)
{
if(empty(P))
{
printf("nQueue is empty!!");
Page | 29
exit(0);
}
int i;
i=P->front;
while(i!=P->rear)
{
printf("n%d",P->data[i]);
i=(i+1)%MAX;
}
printf("n%dn",P->data[P->rear]);
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 30
PROGRAM – 14
AIM: Write a program to create a node in linked-list and display the linked-list
node.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*start=NULL;
void create()
{
char ch;
do
{
struct node *new_node,*current;
new_node=(struct node *)malloc(sizeof(struct node));
printf("nEnter the data : ");
scanf("%d",&new_node->data);
new_node->next=NULL;
if(start==NULL)
{
start=new_node;
current=new_node;
}
Page | 31
else
{ current->next=new_node;
current=new_node;
}
printf("nDo you want to create another : ");
ch=getch();
}while(ch!='n');
}
void display()
{
struct node *new_node;
printf("The Linked List : n");
new_node=start;
while(new_node!=NULL)
{
printf("%d--->",new_node->data);
new_node=new_node->next;
}
printf("NULL");
}
void main()
{
create();
display();
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 32
PROGRAM – 15
AIM: Write a program to attach two singly linked-lists.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head1, *head2, *head3;
struct node * createNode(int data)
{
struct node *ptr = (struct node *) malloc(sizeof(structnode));
ptr->data = data;
ptr->next = NULL;
return ptr;
}
/* insert data into the list in ascending order */
void insert(struct node ** myNode, int data) {
struct node *xPtr, *yPtr, *zPtr = *myNode;
xPtr = createNode(data);
/* insert at the front of the list */
if (*myNode == NULL || (*myNode)->data > data)
{ *myNode = xPtr;
(*myNode)->next = zPtr;
return;
}
/* insertion at the end or middle of the list */
while (zPtr)
{ yPtr = zPtr;
zPtr = zPtr->next;
if (!zPtr) {
yPtr->next = xPtr;
break;
}
else if ((data > yPtr->data) && (data < zPtr->data))
Page | 33
{
xPtr->next = zPtr;
yPtr->next = xPtr;
break;
}
}
return;
}
/* delete the given list */
struct node * deleteList(struct node *ptr) {
struct node *temp;
while (ptr)
{ temp = ptr->next;
free(ptr);
ptr = temp;
}
return NULL;
}
/* traverse the given list and print data in each node */
int walkList(struct node *ptr) {
int i = 0;
while (ptr)
{ printf("%d ", ptr->data);
ptr = ptr->next;
i++;
}
return (i);
}
/* merge list1 and list2 to form list3 */
void mergeList(struct node *list1, struct node *list2, struct
node **list3)
{ struct node *ptr = NULL;
/* if both list are not present, then list3 will be NULL */
if (!list1 && !list2)
{ printf("Both First and Second List are empty!!n");
return;
}
/* both lists are available */
while (list1 && list2) {
if (*list3 == NULL)
{ *list3 = (struct node *)calloc(1, sizeof (structnode));
ptr = *list3;
}
Page | 34
else
{
ptr->next = (struct node *)calloc(1, sizeof (struct node));
ptr = ptr->next;
}
if (list1->data < list2->data)
/* insert data from list1 to list3 & advance list1*/
{ ptr->data = list1->data;
list1 = list1->next;
}
else if (list1->data > list2->data)
/* insert data from list2 to list3 & advance list2 */
{ ptr->data = list2->data;
list2 = list2->next;
} else
/* insert data from list1 to list3 & advance both lists */
{ ptr->data = list1->data;
list1 = list1->next;
list2 = list2->next;
}
}
/* node left remain in list1 is inserted into list3 */
while (list1)
{
ptr->next = (struct node *)calloc(1, sizeof(struct node));
ptr = ptr->next;
ptr->data = list1->data;
list1 = list1->next;
}
/* nodes left remain in list2 is inserted into list3 */
while (list2)
{ ptr->next = (struct node *)calloc(1, sizeof(struct node));
ptr = ptr->next;
ptr->data = list2->data;
list2 = list2->next;
}
return;
}
int main (int argc, char *argv[])
{ int data, i, n;
FILE *fp1, *fp2;
Page | 35
fp1 = fopen(argv[1], "r");
fp2 = fopen(argv[2], "r");
if (!fp1 || !fp2)
{ printf("Unable to open filen");
fcloseall();
exit(0);
}
while (fscanf(fp1, "%d", &data) != EOF)
{
insert(&head1, data);
}
while (fscanf(fp2, "%d", &data) != EOF)
{
insert(&head2, data);
}
printf("nData in First Linked List:n");
n = walkList(head1);
printf("nNo of elements in linked list: %dn", n);
printf("nnData in Second Linked List:n");
n = walkList(head2);
printf("nNo of elements in linked list: %dnn", n);
mergeList(head1, head2, &head3);
printf("nData in Merged List:n");
n = walkList(head3);
printf("nNo of elements in merged list: %dnn", n);
head1 = deleteList(head1);
head2 = deleteList(head2);
head3 = deleteList(head3);
return 0;
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 36
PROGRAM – 16
AIM: Write a program to traverse the binary tree for pre-order, in-order, and
post-order
SOURCE CODE:
#include <stdio.h>
#include <stdlib.h>
struct tnode
{
int data;
struct tnode *left, *right;
};
struct tnode *root = NULL;
struct tnode * createNode(int data)
{
struct tnode *newNode;
newNode = (struct tnode *) malloc(sizeof(struct tnode));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return (newNode);
}
void insertion(struct tnode **node, int data)
{
if (!*node)
{
*node = createNode(data);
}
else if (data < (*node)->data)
{
insertion(&(*node)->left, data);
}
else if (data > (*node)->data)
{
insertion(&(*node)->right, data);
}
}
/* post order tree traversal */
void postOrder(struct tnode *node)
Page | 37
{
if (node)
{
postOrder(node->left);
postOrder(node->right);
printf("%d ", node->data);
}
return;
}
/* pre order tree traversal */
void preOrder(struct tnode *node)
{
if (node)
{ printf("%d ", node->data);
preOrder(node->left);
preOrder(node->right);
}
return;
}
/* inorder tree traversal */
void inOrder(struct tnode *node)
{
if (node)
{ inOrder(node->left);
printf("%d ", node->data);
inOrder(node->right);
}
return;
}
int main()
{
int data, ch;
while (1)
{
printf("n1. Insertionn2. Pre-ordern");
printf("3. Post-ordern4. In-ordern");
printf("5. ExitnEnter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter ur data:");
scanf("%d", &data);
insertion(&root, data);
Page | 38
break;
case 2:
preOrder(root);
break;
case 3:
postOrder(root);
break;
case 4:
inOrder(root);
break;
case 5:
exit(0);
default:
printf("U've entered wrong opetionn");
break;
}
}
return 0;
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 39
PROGRAM – 17
AIM: Write a program to implement breadth first search in a graph.
SOURCE CODE:
#include<stdio.h>
#include<conio.h> //pre-processor directives
/* utility function to perform bfs operation */
void bfs(int adj[10][10], int n, int visited[], int node)
{
int i,nd,q[20],f=-1,r=-1; // queue with rear and front
initialization
visited[node] = 1;
q[++r] = node; //first insert
while(f!=r) //checking queue not empty
{
nd = q[++f]; // then delete
printf(" %d " , nd+1); // print node
for(i=0;i<n;i++)
if(adj[nd][i] == 1 && visited[i] == 0)
{
visited[i] = 1;
q[++r] = i; // inserting
}
}
}
/* starting point of the program */
void main(void)
{
int adj[10][10]={0},visited[10]={0};
int n,e,i,node,v1,v2;
clrscr();
printf("nt Enter the number of nodes > ");
scanf("%d",&n);
printf("nt Enter the node of edges > ");
scanf("%d", &e);
printf("nt -- Enter the edges -- nt");
//adjacency matrix
for(i=0;i<e;i++)
{
scanf("%d %d",&v1,&v2);
Page | 40
adj[v1-1][v2-1] = adj[v2-1][v1-1] = 1;
}
printf("nt Enter the starting node > ");
scanf("%d", &node);
printf("nt ==> BFS <== nt");
bfs(adj,n,visited,node-1);
getch();
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 41
PROGRAM – 18
AIM: Write a program to implement depth first search in a graph.
SOURCE CODE:
#include<stdio.h>
#include<conio.h> //pre-processor directives
/* utility function to perform dfs operation */
void dfs(int adj[10][10], int n, int visited[], int node)
{
int i;
visited[node] = 1;
printf(" %d " , node+1);
for(i=0;i<n;i++)
if(adj[node][i] == 1 && visited[i] == 0)
dfs(adj,n,visited,i); //recursion(app of stack)
}
/* starting point of the program */
void main(void)
{
int adj[10][10]={0},visited[10]={0};
int n,e,i,node,v1,v2;
clrscr();
printf("nt Enter the number of nodes > ");
scanf("%d",&n);
printf("nt Enter the node of edges > ");
scanf("%d", &e);
printf("nt -- Enter the edges -- nt");
//adjacency matrix
for(i=0;i<e;i++)
{
scanf("%d %d",&v1,&v2);
adj[v1-1][v2-1] = adj[v2-1][v1-1] = 1;
}
printf("nt Enter the starting node > ");
scanf("%d", &node);
printf("nt ==> DFS <==nt");
dfs(adj,n,visited,node-1);
getch();
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 42
PROGRAM – 19
AIM: Write a program to search an element of an array using Linear search
technique.
SOURCE CODE:
#include <stdio.h>
int main()
{
int array[100], search, c, n;
printf("Enter the number of elements in arrayn");
scanf("%d",&n);
printf("Enter %d integer(s)n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the number to searchn");
scanf("%d", &search);
/* We keep on comparing each element with the element to
search until the desired element is found or list ends */
for (c = 0; c < n; c++)
{
if (array[c] == search){
/* if required element found*/
printf("%d is present at location %d.n", search, c+1);
break;
}
}
if (c == n)
printf("%d is not present in array.n", search);
return 0;
}
ERROR: No Error.
RESULT: Code Execute successfully.
Page | 43
PROGRAM – 20
AIM: Write a program to search an element of an array using Binary search
technique.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elementsn");
scanf("%d",&n);
printf("Enter %d integersn", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to findn");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first < = last )
{
if ( array[middle] == search )
{
printf("%d found at location %d.n", search, middle+1);
break;
}
else if ( array[middle] < search )
first = middle + 1;
else
last = middle - 1;
middle = (first + last)/2;
}
Page | 44
if ( first > last )
printf("Not found! %d is not present in the list.n",
search);
return 0;
}
ERROR: No Error.
RESULT: Code Execute successfully.

Mais conteúdo relacionado

Mais procurados

Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Saket Pathak
 
List and Dictionary in python
List and Dictionary in pythonList and Dictionary in python
List and Dictionary in pythonSangita Panchal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given numberMainak Sasmal
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)Make Mannan
 
Python in 30 minutes!
Python in 30 minutes!Python in 30 minutes!
Python in 30 minutes!Fariz Darari
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementSreedhar Chowdam
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File Harjinder Singh
 
Xi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsXi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsProf. Dr. K. Adisesha
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2vikram mahendra
 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionHazrat Bilal
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solutionAzhar Javed
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in PythonPooja B S
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in cBUBT
 

Mais procurados (20)

C++ Programming
C++ ProgrammingC++ Programming
C++ Programming
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
List and Dictionary in python
List and Dictionary in pythonList and Dictionary in python
List and Dictionary in python
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
C++ file
C++ fileC++ file
C++ file
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)Lab manual data structure (cs305 rgpv) (usefulsearch.org)  (useful search)
Lab manual data structure (cs305 rgpv) (usefulsearch.org) (useful search)
 
Python Programming
Python Programming Python Programming
Python Programming
 
Python in 30 minutes!
Python in 30 minutes!Python in 30 minutes!
Python in 30 minutes!
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory managementC Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
 
Python collections
Python collectionsPython collections
Python collections
 
Data Structures Practical File
Data Structures Practical File Data Structures Practical File
Data Structures Practical File
 
Deep C
Deep CDeep C
Deep C
 
Xi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programsXi CBSE Computer Science lab programs
Xi CBSE Computer Science lab programs
 
USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2USE OF PRINT IN PYTHON PART 2
USE OF PRINT IN PYTHON PART 2
 
Let us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solutionLet us c (by yashvant kanetkar) chapter 1 solution
Let us c (by yashvant kanetkar) chapter 1 solution
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
String Manipulation in Python
String Manipulation in PythonString Manipulation in Python
String Manipulation in Python
 
Chapter 5 Balagurusamy Programming ANSI in c
Chapter 5 Balagurusamy Programming ANSI  in cChapter 5 Balagurusamy Programming ANSI  in c
Chapter 5 Balagurusamy Programming ANSI in c
 

Semelhante a Data Structure using C

Semelhante a Data Structure using C (20)

Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
C basics
C basicsC basics
C basics
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
C file
C fileC file
C file
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
Examples sandhiya class'
Examples sandhiya class'Examples sandhiya class'
Examples sandhiya class'
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
Arrays
ArraysArrays
Arrays
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
Data structure output 1
Data structure output 1Data structure output 1
Data structure output 1
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
Pnno
PnnoPnno
Pnno
 
Ds
DsDs
Ds
 

Mais de Bilal Mirza

Android Operating System
Android Operating SystemAndroid Operating System
Android Operating SystemBilal Mirza
 
Seminar Report on Android OS
Seminar Report on Android OSSeminar Report on Android OS
Seminar Report on Android OSBilal Mirza
 
Android Operating System
Android Operating SystemAndroid Operating System
Android Operating SystemBilal Mirza
 
Operating System Lab Manual
Operating System Lab ManualOperating System Lab Manual
Operating System Lab ManualBilal Mirza
 
Learn JavaScript HTML & CSS
Learn JavaScript HTML & CSSLearn JavaScript HTML & CSS
Learn JavaScript HTML & CSSBilal Mirza
 
Environment Problems
Environment ProblemsEnvironment Problems
Environment ProblemsBilal Mirza
 

Mais de Bilal Mirza (7)

Android Operating System
Android Operating SystemAndroid Operating System
Android Operating System
 
Seminar Report on Android OS
Seminar Report on Android OSSeminar Report on Android OS
Seminar Report on Android OS
 
Android Operating System
Android Operating SystemAndroid Operating System
Android Operating System
 
Operating System Lab Manual
Operating System Lab ManualOperating System Lab Manual
Operating System Lab Manual
 
Learn JavaScript HTML & CSS
Learn JavaScript HTML & CSSLearn JavaScript HTML & CSS
Learn JavaScript HTML & CSS
 
Environment Problems
Environment ProblemsEnvironment Problems
Environment Problems
 
Counters
Counters Counters
Counters
 

Último

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 

Último (20)

How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 

Data Structure using C

  • 1. Page | 1 PROGRAM – 1 AIM: Write a program to find the sum of first ten natural numbers using recursion. SOURCE CODE: #include <stdio.h> int addNumbers(int n); int main() { int num; printf("Enter a positive integer: "); scanf("%d", &num); printf("Sum = %d",addNumbers(num)); return 0; } int addNumbers(int n) { if(n != 0) return n + addNumbers(n-1); else return n; } ERROR: No Error. RESULT: Code Execute successfully.
  • 2. Page | 2 PROGRAM – 2 AIM: Write a program to generate Fibonacci series using recursion. SOURCE CODE: #include < stdio.h > int Fibonacci(int); int main() { int n, i = 0, c; printf("Enter the number of terms "); scanf("%d",&n); printf("First %d terms of Fibonacci series are :-n", n); for ( c = 1 ; c < = n ; c++ ) { printf("%dn", Fibonacci(i)); i++; } return 0; } int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) ); } ERROR: No Error. RESULT: Code Execute successfully.
  • 3. Page | 3 PROGRAM – 3 AIM: Write a program to find factorial of number using recursion. SOURCE CODE: #include<stdio.h> #include<conio.h> long factorial(int); int main() { int n; long f; printf("Enter an integer to find factorialn"); scanf("%d", &n); if (n < 0) printf("Negative integers are not allowed.n"); else { f = factorial(n); printf("%d! = %ldn", n, f); } return 0; } long factorial(int n) { if (n == 0) return 1; else return(n * factorial(n-1)); /*recursive call to factorial function*/ } ERROR: No Error. RESULT: Code Execute successfully.
  • 4. Page | 4 PROGRAM – 4 AIM: Write a program to perform all basic operations (such as insert, delete) on an array. SOURCE CODE: #include<stdio.h> #include<conio.h> int insert(int *); int view(int *); int del(int *); void main() { int a[100]; int ch; while(1) { clrscr(); { printf("nEnter 1 to insert element in array:t"); printf("nEnter 2 to view element in array:t"); printf("nEnter 3 to Delete element in array:t"); printf("nEnter 4 to Exit:t"); printf("n enter the choice n"); scanf("%d",&ch); switch(ch) { case 1:insert(a);getch(); break; case 2:view(a);getch(); break; case 3:del(a);getch(); break; case 4:exit(0); } } } } int insert(int *a) { int i,n;
  • 5. Page | 5 printf("Enter the no. of elements in array:t"); scanf("%d",&n); printf("nEnter %d elements in array:t",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } a[i]='0'; return *a; } int view(int *a) { int j; for(j=0;a[j]!=NULL;j++) { printf("nElement of array=%d",a[j]); } return *a; } int del(int *a) { int c,k,posi; for(k=0;a[k]!=NULL;k++) printf("Enter the position to delete element:t"); scanf("%d",&posi); if(posi<=k) { for(c=posi-1;c<k-1;c++) { a[c]=a[c+1]; } printf("nArray after Deletion"); for(c=0;c<k-1;c++) { printf("n%d",a[c]); } } return *a; } ERROR: No Error. RESULT: Code Execute successfully
  • 6. Page | 6 PROGRAM – 5 AIM: Write a program which reads two matrices and then print the matrix which is addition of these two matrices. SOURCE CODE: #include < stdio.h > int main() { int m, n, c, d, first[10][10], second[10][10], sum[10][10]; printf("Enter the number of rows and columns of matrixn"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrixn"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the elements of second matrixn"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &second[c][d]); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) sum[c][d] = first[c][d] + second[c][d]; /* Matrix addition */ printf("Sum of entered matrices:-n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < n ; d++ )
  • 7. Page | 7 printf("%dt", sum[c][d]); printf("n"); } return 0; } ERROR: No Error. RESULT: Code Execute successfully.
  • 8. Page | 8 PROGRAM – 6 AIM: Write a program which reads two matrices & multiply them. SOURCE CODE: #include <stdio.h> int main() { int m, n, p, q, c, d, k, sum = 0; int first[10][10], second[10][10], multiply[10][10]; printf("Enter the number of rows and columns of first matrixn"); scanf("%d%d", &m, &n); printf("Enter the elements of first matrixn"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) scanf("%d", &first[c][d]); printf("Enter the number of rows and columns of second matrixn"); scanf("%d%d", &p, &q); if ( n != p ) printf("Matrices with entered orders can't be multiplied with each other.n"); else { printf("Enter the elements of second matrixn"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) scanf("%d", &second[c][d]); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) { for ( k = 0 ; k < p ; k++ ) { sum = sum + first[c][k]*second[k][d]; }
  • 9. Page | 9 multiply[c][d] = sum; sum = 0; } } printf("Product of entered matrices:-n"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) printf("%dt", multiply[c][d]); printf("n"); } } ERROR: No Error. RESULT: Code Execute successfully.
  • 10. Page | 10 PROGRAM – 7 AIM: Write a program to find highest & lowest element in array SOURCE CODE: #include <stdio.h> int main() { int arr[100]; int i, max, min, size; /* Reads size array and elements in the array */ printf("Enter size of the array: "); scanf("%d", &size); printf("Enter elements in the array: "); for(i=0; i<size; i++) { scanf("%d", &arr[i]); } /* Supposes the first element as maximum and minimum */ max = arr[0]; min = arr[0]; /*Finds maximum and minimum in all array elements */ for(i=1; i<size; i++) { /* If current element of array is greater than max */ if(arr[i]>max) { max = arr[i]; } /* If current element of array is smaller than min */ if(arr[i]<min) { min = arr[i]; } }
  • 11. Page | 11 /* Prints the maximum and minimum element*/ printf("Maximum element = %dn", max); printf("Minimum element = %d", min); return 0; } ERROR: No Error. RESULT: Code Execute successfully.
  • 12. Page | 12 PROGRAM – 8 AIM: Write a program to merge two unsorted array. SOURCE CODE: #include<stdio.h> #include<conio.h> void main() { int arr1[10],arr2[20],arr3[20]; int i, n1, n2, m, index; printf(“Enter the number of elements in array 1:”); scanf(“%d”,&n1); printf(“Enter the elements of array 1:”); for(i=0;i<n1;i++) { printf(“n arr1[%d]=”,i); scanf(“%d”,&arr1[i]); } printf(“Enter the number of elements in array 2:”); scanf(“%d”,&n2); for(i=0;i<n2;i++) { printf(“n arr2[%d]=”,i); scanf(“%d”,&arr2[i]); } //merging for(i=0;i<n1;i++) { arr3[index]=arr1[i]; index++; } for(i=0;i<n2;i++) { arr3[index]=arr2[i]; index++; } //Print the result for(i=0;i<m;i++)
  • 13. Page | 13 printf(“n arr3[%d]=%d”,I,arr3[i]); getch(); } ERROR: No Error. RESULT: Code Execute successfully.
  • 14. Page | 14 PROGRAM – 9 AIM: Write a program to merge two sorted array. SOURCE CODE: #include<stdio.h> #include<conio.h> void main() { int arr1[10],arr2[20],arr3[20]; int i, n1, n2, index=0; int index_first=0, index_second=0; printf(“Enter the number of elements in array 1:”); scanf(“%d”,&n1); printf(“Enter the elements of array 1:”); for(i=0;i<n1;i++) { printf(“n arr1[%d]=”,i); scanf(“%d”,&arr1[i]); } printf(“Enter the number of elements in array 2:”); scanf(“%d”,&n2); for(i=0;i<n2;i++) { printf(“n arr2[%d]=”,i); scanf(“%d”,&arr2[i]); } //merging while(index_first<n1 && index_second<n2) { if(arr1[index_first]<arr2[index_second]) { arr3[index]=arr1[index_first]; index_first++; } else { arr3[index]=arr2[index_second]; index_second++; } index++; }
  • 15. Page | 15 //if elements of first elements are over & second array has some elements. if(index_first==n1) { while(index_second<n2) { arr3[index]=arr2[index_second]; index_second++; index++; } } //if elements of second array are over & first array has some elements. if(index_second==n2) { while(index_first<n1) { arr3[index]=arr1[index_first]; index_first++; index++; } } //Print the result printf(“nn The merged array is:”); for(i=0;i<m;i++) { printf(“n arr3[%d]=%d”,I,arr3[i]); } getch(); } ERROR: No Error. RESULT: Code Execute successfully.
  • 16. Page | 16 PROGRAM – 10 AIM: Write a program to perform PUSH & POP operations on stack. SOURCE CODE: #include <stdio.h> #include <conio.h> #define max 5 void main() { //... create stack int stack[max],data; int top,option,reply; //... init stack top = -1; clrscr(); do { printf("n 1. push"); printf("n 2. pop"); printf("n 3. exit"); printf("nSelect proper option : "); scanf("%d",&option); switch(option) { case 1 : // push printf("n Enter a value : "); scanf("%d",&data); reply = push(stack,&top,&data); if( reply == -1 ) printf("nStack is full"); else printf("n Pushed value"); break; case 2 : // pop reply = pop ( stack,&top,&data); if( reply == - 1)
  • 17. Page | 17 printf("nStack is empty"); else printf("n Popped value is %d",data); break; case 3 : exit(0); } // switch }while(1); } // main int push( int stack[max],int *top, int *data) { if( *top == max -1 ) return(-1); else { *top = *top + 1; stack[*top] = *data; return(1); } // else } // push int pop( int stack[max], int *top, int *data) { if( *top == -1 ) return(-1); else { *data = stack[*top]; *top = *top - 1; return(1); } //else } // pop ERROR: No Error. RESULT: Code Execute successfully.
  • 18. Page | 18 PROGRAM – 11 AIM: Write a program to implement linear queue using array. SOURCE CODE: #include <stdio.h> #define MAX 50 int queue_array[MAX]; int rear = - 1; int front = - 1; main() { int choice; while (1) { printf("1.Insert element to queue n"); printf("2.Delete element from queue n"); printf("3.Display all elements of queue n"); printf("4.Quit n"); printf("Enter your choice : "); scanf("%d", &choice); switch (choice) { case 1: insert(); break; case 2: delete(); break; case 3: display(); break; case 4: exit(1); default: printf("Wrong choice n"); } /*End of switch*/ } /*End of while*/
  • 19. Page | 19 } /*End of main()*/ insert() { int add_item; if (rear == MAX - 1) printf("Queue Overflow n"); else { if (front == - 1) /*If queue is initially empty */ front = 0; printf("Inset the element in queue : "); scanf("%d", &add_item); rear = rear + 1; queue_array[rear] = add_item; } } /*End of insert()*/ delete() { if (front == - 1 || front > rear) { printf("Queue Underflow n"); return ; } else { printf("Element deleted from queue is : %dn", queue_array[front]); front = front + 1; } } /*End of delete() */ display() { int i; if (front == - 1) printf("Queue is empty n"); else { printf("Queue is : n");
  • 20. Page | 20 for (i = front; i <= rear; i++) printf("%d ", queue_array[i]); printf("n"); } } /*End of display() */ ERROR: No Error. RESULT: Code Execute successfully.
  • 21. Page | 21 PROGRAM – 12 AIM: Write a program to implement Circular queue using array. SOURCE CODE: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct Node { int Data; struct Node* next; }*rear, *front; void delQueue() { struct Node *temp, *var=rear; if(var==rear) { rear = rear->next; free(var); } else printf("nQueue Empty"); } void push(int value) { struct Node *temp; temp=(struct Node *)malloc(sizeof(struct Node)); temp->Data=value; if (front == NULL) { front=temp; front->next=NULL; rear=front; } else {
  • 22. Page | 22 front->next=temp; front=temp; front->next=rear; } } void display() { struct Node *var=rear; if(var!=NULL) { printf("nElements are as: "); while(var!=front) { printf("t%d",var->Data); var=var->next; } if(var==front) { printf("t%d",var->Data); } printf("n"); } else printf("nQueue is Empty"); } int main(int argc, char *argv[]) { int i=0; front=NULL; printf(" n1. Push to Queue"); printf(" n2. Pop from Queue"); printf(" n3. Display Data of Queue"); printf(" n4. Exitn"); while(1) { printf(" nChoose Option: "); scanf("%d",&i); switch(i) { case 1:
  • 23. Page | 23 { int value; printf("nEnter a valueber to push into Queue: "); scanf("%d",&value); push(value); display(); break; } case 2: { delQueue(); display(); break; } case 3: { display(); break; } case 4: { exit(0); } default: { printf("nwrong choice for operation"); } } } } ERROR: No Error. RESULT: Code Execute successfully.
  • 24. Page | 24 PROGRAM – 13 AIM: : Write a program to implement Dequeue using array. SOURCE CODE: #include<stdio.h> #include<process.h> #define MAX 30 typedef struct dequeue { int data[MAX]; int rear,front; }dequeue; void initialize(dequeue *p); int empty(dequeue *p); int full(dequeue *p); void enqueueR(dequeue *p,int x); void enqueueF(dequeue *p,int x); int dequeueF(dequeue *p); int dequeueR(dequeue *p); void print(dequeue *p); void main() { int i,x,op,n; dequeue q; initialize(&q); do { printf("n1.Createn2.Insert(rear)n3.Insert(front)n4.Delet e(rear)n5.Delete(front)"); printf("n6.Printn7.ExitnnEnter your choice:");
  • 25. Page | 25 scanf("%d",&op); switch(op) { case 1: printf("nEnter number of elements:"); scanf("%d",&n); initialize(&q); printf("nEnter the data:"); for(i=0;i<n;i++) { scanf("%d",&x); if(full(&q)) { printf("nQueue is full!!"); exit(0); } enqueueR(&q,x); } break; case 2: printf("nEnter element to be inserted:"); scanf("%d",&x); if(full(&q)) { printf("nQueue is full!!"); exit(0); } enqueueR(&q,x); break; case 3: printf("nEnter the element to be inserted:"); scanf("%d",&x); if(full(&q)) {
  • 26. Page | 26 printf("nQueue is full!!"); exit(0); } enqueueF(&q,x); break; case 4: if(empty(&q)) { printf("nQueue is empty!!"); exit(0); } x=dequeueR(&q); printf("nElement deleted is %dn",x); break; case 5: if(empty(&q)) { printf("nQueue is empty!!"); exit(0); } x=dequeueF(&q); printf("nElement deleted is %dn",x); break; case 6: print(&q); break; default: break; } }while(op!=7); } void initialize(dequeue *P) { P->rear=-1; P->front=-1; }
  • 27. Page | 27 int empty(dequeue *P) { if(P->rear==-1) return(1); return(0); } int full(dequeue *P) { if((P->rear+1)%MAX==P->front) return(1); return(0); } void enqueueR(dequeue *P,int x) { if(empty(P)) { P->rear=0; P->front=0; P->data[0]=x; } else { P->rear=(P->rear+1)%MAX; P->data[P->rear]=x; } } void enqueueF(dequeue *P,int x) { if(empty(P)) { P->rear=0; P->front=0; P->data[0]=x; }
  • 28. Page | 28 else { P->front=(P->front-1+MAX)%MAX; P->data[P->front]=x; } } int dequeueF(dequeue *P) { int x; x=P->data[P->front]; if(P->rear==P->front) //delete the last element initialize(P); else P->front=(P->front+1)%MAX; return(x); } int dequeueR(dequeue *P) { int x; x=P->data[P->rear]; if(P->rear==P->front) initialize(P); else P->rear=(P->rear-1+MAX)%MAX; return(x); } void print(dequeue *P) { if(empty(P)) { printf("nQueue is empty!!");
  • 29. Page | 29 exit(0); } int i; i=P->front; while(i!=P->rear) { printf("n%d",P->data[i]); i=(i+1)%MAX; } printf("n%dn",P->data[P->rear]); } ERROR: No Error. RESULT: Code Execute successfully.
  • 30. Page | 30 PROGRAM – 14 AIM: Write a program to create a node in linked-list and display the linked-list node. SOURCE CODE: #include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node *next; }*start=NULL; void create() { char ch; do { struct node *new_node,*current; new_node=(struct node *)malloc(sizeof(struct node)); printf("nEnter the data : "); scanf("%d",&new_node->data); new_node->next=NULL; if(start==NULL) { start=new_node; current=new_node; }
  • 31. Page | 31 else { current->next=new_node; current=new_node; } printf("nDo you want to create another : "); ch=getch(); }while(ch!='n'); } void display() { struct node *new_node; printf("The Linked List : n"); new_node=start; while(new_node!=NULL) { printf("%d--->",new_node->data); new_node=new_node->next; } printf("NULL"); } void main() { create(); display(); } ERROR: No Error. RESULT: Code Execute successfully.
  • 32. Page | 32 PROGRAM – 15 AIM: Write a program to attach two singly linked-lists. SOURCE CODE: #include<stdio.h> #include<stdlib.h> struct node { int data; struct node *next; }; struct node *head1, *head2, *head3; struct node * createNode(int data) { struct node *ptr = (struct node *) malloc(sizeof(structnode)); ptr->data = data; ptr->next = NULL; return ptr; } /* insert data into the list in ascending order */ void insert(struct node ** myNode, int data) { struct node *xPtr, *yPtr, *zPtr = *myNode; xPtr = createNode(data); /* insert at the front of the list */ if (*myNode == NULL || (*myNode)->data > data) { *myNode = xPtr; (*myNode)->next = zPtr; return; } /* insertion at the end or middle of the list */ while (zPtr) { yPtr = zPtr; zPtr = zPtr->next; if (!zPtr) { yPtr->next = xPtr; break; } else if ((data > yPtr->data) && (data < zPtr->data))
  • 33. Page | 33 { xPtr->next = zPtr; yPtr->next = xPtr; break; } } return; } /* delete the given list */ struct node * deleteList(struct node *ptr) { struct node *temp; while (ptr) { temp = ptr->next; free(ptr); ptr = temp; } return NULL; } /* traverse the given list and print data in each node */ int walkList(struct node *ptr) { int i = 0; while (ptr) { printf("%d ", ptr->data); ptr = ptr->next; i++; } return (i); } /* merge list1 and list2 to form list3 */ void mergeList(struct node *list1, struct node *list2, struct node **list3) { struct node *ptr = NULL; /* if both list are not present, then list3 will be NULL */ if (!list1 && !list2) { printf("Both First and Second List are empty!!n"); return; } /* both lists are available */ while (list1 && list2) { if (*list3 == NULL) { *list3 = (struct node *)calloc(1, sizeof (structnode)); ptr = *list3; }
  • 34. Page | 34 else { ptr->next = (struct node *)calloc(1, sizeof (struct node)); ptr = ptr->next; } if (list1->data < list2->data) /* insert data from list1 to list3 & advance list1*/ { ptr->data = list1->data; list1 = list1->next; } else if (list1->data > list2->data) /* insert data from list2 to list3 & advance list2 */ { ptr->data = list2->data; list2 = list2->next; } else /* insert data from list1 to list3 & advance both lists */ { ptr->data = list1->data; list1 = list1->next; list2 = list2->next; } } /* node left remain in list1 is inserted into list3 */ while (list1) { ptr->next = (struct node *)calloc(1, sizeof(struct node)); ptr = ptr->next; ptr->data = list1->data; list1 = list1->next; } /* nodes left remain in list2 is inserted into list3 */ while (list2) { ptr->next = (struct node *)calloc(1, sizeof(struct node)); ptr = ptr->next; ptr->data = list2->data; list2 = list2->next; } return; } int main (int argc, char *argv[]) { int data, i, n; FILE *fp1, *fp2;
  • 35. Page | 35 fp1 = fopen(argv[1], "r"); fp2 = fopen(argv[2], "r"); if (!fp1 || !fp2) { printf("Unable to open filen"); fcloseall(); exit(0); } while (fscanf(fp1, "%d", &data) != EOF) { insert(&head1, data); } while (fscanf(fp2, "%d", &data) != EOF) { insert(&head2, data); } printf("nData in First Linked List:n"); n = walkList(head1); printf("nNo of elements in linked list: %dn", n); printf("nnData in Second Linked List:n"); n = walkList(head2); printf("nNo of elements in linked list: %dnn", n); mergeList(head1, head2, &head3); printf("nData in Merged List:n"); n = walkList(head3); printf("nNo of elements in merged list: %dnn", n); head1 = deleteList(head1); head2 = deleteList(head2); head3 = deleteList(head3); return 0; } ERROR: No Error. RESULT: Code Execute successfully.
  • 36. Page | 36 PROGRAM – 16 AIM: Write a program to traverse the binary tree for pre-order, in-order, and post-order SOURCE CODE: #include <stdio.h> #include <stdlib.h> struct tnode { int data; struct tnode *left, *right; }; struct tnode *root = NULL; struct tnode * createNode(int data) { struct tnode *newNode; newNode = (struct tnode *) malloc(sizeof(struct tnode)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return (newNode); } void insertion(struct tnode **node, int data) { if (!*node) { *node = createNode(data); } else if (data < (*node)->data) { insertion(&(*node)->left, data); } else if (data > (*node)->data) { insertion(&(*node)->right, data); } } /* post order tree traversal */ void postOrder(struct tnode *node)
  • 37. Page | 37 { if (node) { postOrder(node->left); postOrder(node->right); printf("%d ", node->data); } return; } /* pre order tree traversal */ void preOrder(struct tnode *node) { if (node) { printf("%d ", node->data); preOrder(node->left); preOrder(node->right); } return; } /* inorder tree traversal */ void inOrder(struct tnode *node) { if (node) { inOrder(node->left); printf("%d ", node->data); inOrder(node->right); } return; } int main() { int data, ch; while (1) { printf("n1. Insertionn2. Pre-ordern"); printf("3. Post-ordern4. In-ordern"); printf("5. ExitnEnter your choice:"); scanf("%d", &ch); switch (ch) { case 1: printf("Enter ur data:"); scanf("%d", &data); insertion(&root, data);
  • 38. Page | 38 break; case 2: preOrder(root); break; case 3: postOrder(root); break; case 4: inOrder(root); break; case 5: exit(0); default: printf("U've entered wrong opetionn"); break; } } return 0; } ERROR: No Error. RESULT: Code Execute successfully.
  • 39. Page | 39 PROGRAM – 17 AIM: Write a program to implement breadth first search in a graph. SOURCE CODE: #include<stdio.h> #include<conio.h> //pre-processor directives /* utility function to perform bfs operation */ void bfs(int adj[10][10], int n, int visited[], int node) { int i,nd,q[20],f=-1,r=-1; // queue with rear and front initialization visited[node] = 1; q[++r] = node; //first insert while(f!=r) //checking queue not empty { nd = q[++f]; // then delete printf(" %d " , nd+1); // print node for(i=0;i<n;i++) if(adj[nd][i] == 1 && visited[i] == 0) { visited[i] = 1; q[++r] = i; // inserting } } } /* starting point of the program */ void main(void) { int adj[10][10]={0},visited[10]={0}; int n,e,i,node,v1,v2; clrscr(); printf("nt Enter the number of nodes > "); scanf("%d",&n); printf("nt Enter the node of edges > "); scanf("%d", &e); printf("nt -- Enter the edges -- nt"); //adjacency matrix for(i=0;i<e;i++) { scanf("%d %d",&v1,&v2);
  • 40. Page | 40 adj[v1-1][v2-1] = adj[v2-1][v1-1] = 1; } printf("nt Enter the starting node > "); scanf("%d", &node); printf("nt ==> BFS <== nt"); bfs(adj,n,visited,node-1); getch(); } ERROR: No Error. RESULT: Code Execute successfully.
  • 41. Page | 41 PROGRAM – 18 AIM: Write a program to implement depth first search in a graph. SOURCE CODE: #include<stdio.h> #include<conio.h> //pre-processor directives /* utility function to perform dfs operation */ void dfs(int adj[10][10], int n, int visited[], int node) { int i; visited[node] = 1; printf(" %d " , node+1); for(i=0;i<n;i++) if(adj[node][i] == 1 && visited[i] == 0) dfs(adj,n,visited,i); //recursion(app of stack) } /* starting point of the program */ void main(void) { int adj[10][10]={0},visited[10]={0}; int n,e,i,node,v1,v2; clrscr(); printf("nt Enter the number of nodes > "); scanf("%d",&n); printf("nt Enter the node of edges > "); scanf("%d", &e); printf("nt -- Enter the edges -- nt"); //adjacency matrix for(i=0;i<e;i++) { scanf("%d %d",&v1,&v2); adj[v1-1][v2-1] = adj[v2-1][v1-1] = 1; } printf("nt Enter the starting node > "); scanf("%d", &node); printf("nt ==> DFS <==nt"); dfs(adj,n,visited,node-1); getch(); } ERROR: No Error. RESULT: Code Execute successfully.
  • 42. Page | 42 PROGRAM – 19 AIM: Write a program to search an element of an array using Linear search technique. SOURCE CODE: #include <stdio.h> int main() { int array[100], search, c, n; printf("Enter the number of elements in arrayn"); scanf("%d",&n); printf("Enter %d integer(s)n", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); printf("Enter the number to searchn"); scanf("%d", &search); /* We keep on comparing each element with the element to search until the desired element is found or list ends */ for (c = 0; c < n; c++) { if (array[c] == search){ /* if required element found*/ printf("%d is present at location %d.n", search, c+1); break; } } if (c == n) printf("%d is not present in array.n", search); return 0; } ERROR: No Error. RESULT: Code Execute successfully.
  • 43. Page | 43 PROGRAM – 20 AIM: Write a program to search an element of an array using Binary search technique. SOURCE CODE: #include <stdio.h> #include <conio.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elementsn"); scanf("%d",&n); printf("Enter %d integersn", n); for ( c = 0 ; c < n ; c++ ) scanf("%d",&array[c]); printf("Enter value to findn"); scanf("%d",&search); first = 0; last = n - 1; middle = (first+last)/2; while( first < = last ) { if ( array[middle] == search ) { printf("%d found at location %d.n", search, middle+1); break; } else if ( array[middle] < search ) first = middle + 1; else last = middle - 1; middle = (first + last)/2; }
  • 44. Page | 44 if ( first > last ) printf("Not found! %d is not present in the list.n", search); return 0; } ERROR: No Error. RESULT: Code Execute successfully.