0% found this document useful (0 votes)
24 views11 pages

ADS Lab Programs

The document contains multiple C programming examples demonstrating various algorithms and data structures. These include implementations of the Tower of Hanoi, matrix operations (sparse matrix check and transpose), sorting algorithms (bubble sort, selection sort, quicksort), linked list operations (creation, traversal, insertion, deletion), and binary tree operations (insertion, searching, and preorder traversal). Each example is self-contained with user input and output for clarity.

Uploaded by

javalabnhce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views11 pages

ADS Lab Programs

The document contains multiple C programming examples demonstrating various algorithms and data structures. These include implementations of the Tower of Hanoi, matrix operations (sparse matrix check and transpose), sorting algorithms (bubble sort, selection sort, quicksort), linked list operations (creation, traversal, insertion, deletion), and binary tree operations (insertion, searching, and preorder traversal). Each example is self-contained with user input and output for clarity.

Uploaded by

javalabnhce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

exp1:

#include<stdio.h>
void TOH(int,char,char,char);
int main()
{
int n;
printf("enter number of dics: ");
scanf("%d",&n);
TOH(n,'A','B','C');
return 0;
}
void TOH(int n,char A,char B,char C)
{
if(n==1)
{
printf("moved from %c to %c \n",A,C);
}
else
{
TOH(n-1,A,C,B);
TOH(1,A,B,C);
TOH(n-1,B,A,C);
}
}
-----------------------------------------------------------------------------------
-----------
exp2:

#include<stdio.h>
#include<stdlib.h>
int main(){
int row,col,i,j,arr[10][10],count=0;
printf("enter the no of rows:");
scanf("%d",&row);
printf("entwe the no of columns:");
scanf("%d",&col);
printf("Enter the elements:");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
scanf("%d",&arr[i][j]);
}
}
printf("elements are: \n");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
printf("%d \t",arr[i][j]);
}
printf("\n");
}
for(i=0;i<row;i++){
for(j=0;j<col;j++){
if(arr[i][j]==0){
count++;
}
}
}
if(count>((row*col)/2)){
printf("sparse matrix .....");
}
else
{
printf("not a sparse matrix.......");
}
}

-----------------------------------------------------------------------------------
-----------

exp 3:

#include<stdio.h>
int main(){
int row,col,i,j,arr[10][10],transpose[10][10];
printf("enter no of rows: ");
scanf("%d",&row);
printf("enter no of columns: ");
scanf("%d",&col);
printf("enter elements:");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++){
scanf("%d",&arr[i][j]);
}
}
for(i=0;i<row;i++){
for(j=0;j<col;j++){
transpose[j][i]=arr[i][j];
}
}
printf("transpose of matrix :\n");
for(i=0;i<row;i++){
for(j=0;j<col;j++){
printf("%d \t",transpose[i][j]);
}
printf("\n");
}
}

-----------------------------------------------------------------------------------
-----------

exp 4:

#include<stdio.h>
void bubble(int arr[],int n);
int main()
{
int n;
int arr[]={64,34,25,12,22,11,90};
n=sizeof(arr)/sizeof(arr[0]);
bubble(arr,n);
printf("the sorted elements are:\n");
for(int i=0;i<n;i++){
printf("%d \t",arr[i]);
}
return 0;
}
void bubble(int arr[],int n)
{
int i,j;
for(i=0;i<n-1;i++){
for(j=0;j<n-i-1;j++){
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}

-----------------------------------------------------------------------------------
-----------

exp 5:

#include<stdio.h>
void selection_sort(int arr[],int n)
{
int i,j,imin;
for(i=0;i<n-1;i++){
imin=i;
for(j=i+1;j<n;j++){
if(arr[j]<arr[imin])
{
imin=j;
}
}
int temp=arr[i];
arr[i]=arr[imin];
arr[imin]=temp;
}
}
int main()
{
int arr[]={5,6,1,2,88,66};
int n =sizeof(arr)/sizeof(arr[0]);
selection_sort(arr,n);
printf("the sorted elements are:\n");
for(int i=0;i<n;i++){
printf("%d \t",arr[i]);
}
}

-----------------------------------------------------------------------------------
-----------

exp:6

#include<stdio.h>
int partition(int arr[],int low,int high)
{
int pivot=arr[high];
int i=low-1;
for(int j=low;j<high;j++)
{
if(arr[j]<pivot)
{
i++;
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
i++;
int temp=arr[i];
arr[i]=arr[high];
arr[high]=temp;
return i;
}
void quicksort(int arr[],int low,int high)
{
if(low<high)
{
int pi=partition(arr,low,high);
quicksort(arr,low,pi-1);
quicksort(arr,pi+1,high);
}
}
int main()
{
int arr[]={34,43,11,22,1};
int n=sizeof(arr)/sizeof(arr[0]);
quicksort(arr,0,n-1);
printf("sorted array = \n");
for(int i=0;i<n;i++){
printf("%d \t",arr[i]);
}
return 0;
}

-----------------------------------------------------------------------------------
-----------
exp:7

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
typedef struct node node;
node *start=NULL;
int menu(){
int ch;
printf("enter the choice: ");
scanf("%d",&ch);
return ch;
}
node *getnode()
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("enter the data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
return newnode;
}
void createlist(int n){
int i;
node* temp;
node* newnode;
for(i=0;i<n;i++)
{
newnode=getnode();
if(start==NULL)
{
start=newnode;
}
else{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=newnode;
}
}
}
void traverse()
{
node* temp=start;
printf("traversing from left to right:\n");
if(start==NULL){
printf("empty list");
}
else{
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->next;
}
}
}
void main(void){
int ch,n;
while(1){
ch=menu();
switch(ch){
case 1:
if(start==NULL){
printf("no of node:");
scanf("%d",&n);
createlist(n);
printf("list created \n");
}
break;
case 2:
traverse();
break;
case 3:
exit(0);
break;
}
}
}

-----------------------------------------------------------------------------------
----------------
exp 8:

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
typedef struct node node;
node *start=NULL;
int menu()
{
int ch;
printf("enter choice:");
scanf("%d",&ch);
return ch;
}
node *getnode()
{
node * newnode;
newnode=(node*)malloc(sizeof(node));
printf("enter the data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
return newnode;
}
void createlist(int n){
int i;
node* temp;
node* newnode;
for(i=0;i<n;i++)
{
newnode=getnode();
if(start==NULL){
start=newnode;
}
else{
temp=start;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newnode;
}
}
}
void traversal()
{
node *temp=start;
printf("traversion from left to right: \n");
if(start==NULL){
printf("empty node:");
}
else{
while(temp!=NULL){
printf("%d-->",temp->data);
temp=temp->next;
}
}
}
void insert_at_beg()
{
node* newnode;
newnode=getnode();
if(start==NULL){
start=newnode;
}
else{
newnode->next=start;
start=newnode;
}
}
void insert_at_end()
{
node* newnode,*temp;
newnode=getnode();
if(start==NULL){
start=newnode;
}
else{
temp=start;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newnode;
}
}
void main(void){
int ch,n;
while(1){
int ch=menu();
switch(ch){
case 1:
if(start==NULL){
printf("enter no of nodes:");
scanf("%d",&n);
createlist(n);
printf("list created. \n");
break;
case 2:
traversal();
break;
case 3:
insert_at_beg();
break;
}
case 4:
insert_at_end();
break;
case 5:
exit(0);
break;
}
}
}

-----------------------------------------------------------------------------------
----------------

exp 9:

#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
typedef struct node node;
node *start=NULL;
int menu()
{
int ch;
printf("enter choice:");
scanf("%d",&ch);
return ch;
}
node *getnode()
{
node * newnode;
newnode=(node*)malloc(sizeof(node));
printf("enter the data:");
scanf("%d",&newnode->data);
newnode->next=NULL;
return newnode;
}
void createlist(int n){
int i;
node* temp;
node* newnode;
for(i=0;i<n;i++)
{
newnode=getnode();
if(start==NULL){
start=newnode;
}
else{
temp=start;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=newnode;
}
}
}
void traversal()
{
node *temp=start;
printf("traversion from left to right: \n");
if(start==NULL){
printf("empty node:");
}
else{
while(temp!=NULL){
printf("%d-->",temp->data);
temp=temp->next;
}
}
}
void delete_at_beg()
{
node *temp;
if(start==NULL){
printf("empty node.");
}
else{
temp=start;
start=temp->next;
free(temp);
}
}
void delete_at_end()
{
node*temp,*prev;
if(start==NULL){
printf("empty node");
}
else{
temp=start;
prev=start;
while(temp->next!=NULL){
prev=temp;
temp=temp->next;
}
prev->next=NULL;
free(temp);
}
}
void main(void){
int ch,n;
while(1){
int ch=menu();
switch(ch){
case 1:
if(start==NULL){
printf("enter no of nodes:");
scanf("%d",&n);
createlist(n);
printf("list created. \n");
break;
case 2:
traversal();
break;
case 3:
delete_at_beg();
break;
}
case 4:
delete_at_end();
break;
case 5:
exit(0);
break;
}
}
}

-----------------------------------------------------------------------------------
----------------
exp:10

#include <stdio.h>
#include <stdlib.h>
struct binarytree {
int key;
struct binarytree *left, *right;
};
typedef struct binarytree tree;
tree* newnode(int value) {
tree* node = (tree*)malloc(sizeof(tree));
node->key = value;
node->left = NULL;
node->right = NULL;
return node;
}
tree* insertnode(tree* root, int value) {
if (root == NULL) {
return newnode(value);
}
if (value < root->key) {
root->left = insertnode(root->left, value);
} else if (value > root->key) {
root->right = insertnode(root->right, value);
}
return root;
}
tree* findmin(tree* root) {
if (root == NULL) {
return NULL;
}
while (root->left != NULL) {
root = root->left;
}
return root;
}
void preorder(tree* root) {
if (root != NULL) {
printf("%d ", root->key);
preorder(root->left);
preorder(root->right);
}
}
tree* searchnode(tree* root, int value) {
if (root == NULL || root->key == value) {
return root;
}
if (value < root->key) {
return searchnode(root->left, value);
}
return searchnode(root->right, value);
}

int main() {
tree* root = NULL;
root = insertnode(root, 10);
insertnode(root, 13);
insertnode(root, 7);
insertnode(root, 9);
insertnode(root, 15);

if (searchnode(root, 15) != NULL) {


printf("15 found\n");
} else {
printf("15 not found\n");
}

printf("Preorder traversal: ");


preorder(root);
printf("\n");
return 0;
}

You might also like