DS Lab Programss
DS Lab Programss
PART A
1. PROGRAM TO FIND GCD USING RECURSIVE FUNCTION
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}
OUTPUT:
OUTPUT:
Value of C(4,2) is 6
#include<stdio.h>
int fibonacci_series(int);
int main()
{
int count, c = 0, i;
printf("Enter number of terms:");
scanf("%d",&count);
printf("\nFibonacci series:\n");
for ( i = 1 ; i <= count ; i++ )
printf("%d\n", fibonacci_series(c));
c++;
}
return 0;
}
int fibonacci_series(int num)
{
if ( num == 0 )
return 0;
else if ( num == 1 )
return 1;
else
return ( fibonacci_series(num-1) + fibonacci_series(num-2) );
}
OUTPUT:
#include<stdio.h>
void tower(int,
char,char,char);
void main()
int n;
char a,b,c;
scanf("%d", &n);
printf("tower of
Hanoi of %d disc
\n",n);
tower(n,'a','b','c');
if(n<=0)
printf("\n illegal
entry");
else if(n==1)
else
tower(n-1, aux,beg,
end);
OUTPUT:
student@ubuntu:~$
gcc pgm4a.c
student@ubuntu:~$
./a.out
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,n,small,large;
int *ptr;
{
large = *(ptr + i);
}
}
printf("\nLargest number = %d", large);
free(ptr);
return 0;
}
OUTPUT:
smallest number = 2
Largest number = 8
#include<stdio.h>
#include<string.h>
int main()
{
char name[20][20],temp[20];
int i,j,n;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
OUTPUT:
Hubballi
Belagavi
Gokak
Vijayapura
Haveri
Belagavi
Gokak
Haveri
Hubballi
Vijayapura
#include<stdio.h>
int main()
{
int a[10], n, i;
void selection(int[], int);
selection(a, n);
printf("\nSorted Array\n");
for(i = 0; i < n; i++)
printf("\n%d", a[i]);
return 0;
}/* end of main */
/*function selection*/
void selection(int a[], int n)
{
int i, j, pos, temp, small;
}
}
temp = a[pos];
a[pos] = a[i];
a[i] = temp;
}
}
/*end of function selection*/
OUTPUT:
[user@localhost dslab]$ gcc assign7A.c
[user@localhost dslab]$ ./a.out
Enter size: 7
Enter 7 elements: 4 1 3 8 6 9 5
Sorted Array
1
3
4
5
6
8
9
bubble(a,n);
return 0;
}
}
}
OUTPUT:
[user@localhost dslab]$ gcc assign8A.c
[user@localhost dslab]$ ./a.out
Enter size: 6
Enter 6 elements: 8 3 7 2 6 1
The sorted element are
1 2 3 6 7 8
nsertion(a, n);
printf("\nSorted Array\n");
for(i = 0; i < n; i++)
printf("\n%d\n", a[i]);
return 0;
}/* end of main */
/*function insertion*/
void insertion(int a[], int n)
{
int i, j, temp;
for(i = 1; i < n; i++)
{
for(j = i; j >= 1; j--)
{
if( a[j] < a[j-1] )
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
}
}
}/*end of function insertion*/
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 14
UUCMS NO: Subject: DS LAB
OUTPUT:
[user@localhost dslab]$ gcc assign9A.c
[user@localhost dslab]$ ./a.out
Enter size : 7
Enter 7 elements : 2 8 1 6 5 9 3
Sorted Array
PART B
1. Program to sort the given list using quick sort technique.
#include<stdio.h>
int main()
{
int a[10], low,high, i,j, n;
printf("\nEnter number of elements : ");
scanf("%d", &n);
printf("\nEnter the elements : ");
for(i=0;i<n;i++)
{
scanf("%d", &a[i]);
}
low=0;
high=n-1;
quicksort(a, low, high);
printf("\nSorted elements are :");
for(i=0;i<n;i++)
{
printf("\t %d", a[i]);
}
return 0;
}
while(a[j]>pivot)
j=j-1;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
flag=0;
}
temp=a[low];
a[low]=a[j];
a[j]=temp;
return(j);
}
OUTPUT:
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
mergesort(a,0,n-1);
return 0;
}
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);//left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}
OUTPUT:
Enter no of elements :5
Enter array elements :98 79 25 54 76
Sorted arrays are : 25 54 76 79 98
#include<stdio.h>
int main()
{
int i,key,flag=0,a[10],n;
printf("\n Enter how many no : ");
scanf("%d",&n);
or(i=1;i<=n;i++)
{
if(a[i]==key)
{
flag=1;
break;
}
}
if(flag==1)
printf("\n Search successful!... Element found at position :%d \n", i);
else
printf("\n Search Unsuccessful!... \n");
return 0;
}
OUTPUT:
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
printf("\n Search Successful !... Element found at position :%d ",mid);
return 0;
break;
}
if(key<a[mid])
high=mid-1;
else
low = mid+1;
}
return 0;
}
OUTPUT:
int stack[MAXSIZE];
int top = -1;
void push();
void pop();
void display();
int main()
{
int ch;
while(1)
{
printf("\n\n\t\t\t STACK OPERATIONS ");
printf("\n\n\t\t\t 1.Push");
printf("\n\t\t\t 2.Pop");
printf("\n\t\t\t 3.Display");
printf("\n\t\t\t 4.Exit");
printf("\n\n\t\t\t Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:push();
break;
case 2:pop();
break;
case 3:display();
break;
case 4: exit(0);
}
}
}
/**************push function********************/
void push()
{
int element;
if(top == MAXSIZE-1)
{
printf("\n\tStack Full \n");
}
else
{
top = top + 1;
printf("\nEnter element to be inserted \n");
scanf("%d", &element);
stack[top] = element;
}
}
/**************pop function***********************/
void pop()
{
if(top == -1)
{
printf("\n \t\tStack Empty........deletion not possible");
}
else
{
printf("\nDeleted elements is = %d\n",stack[top]);
top = top -1;
}
}
/**************display function*********************/
void display()
{
int i;
if(top==-1)
{
printf("Stack Empty --------- no more elements to display\n");
}
else
{
printf("\nStack elements are\n");
for(i = top; i >= 0; i--)
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 24
UUCMS NO: Subject: DS LAB
{
printf("\t%d\n", stack[i]);
}
}
}
OUTPUT:
1.Push 1.Push
2.Pop 2.Pop
3.Display 3.Display
4.Exit 4.Exit
1.Push 1.Push
2.Pop 2.Pop
3.Display 3.Display
4.Exit 4.Exit
1.Push 1.Push
2.Pop 2.Pop
3.Display
4.Exit 3.Display
4.Exit
Enter your choice:
1 Enter your choice: 2
1.Push 1.Push
2.Pop 2.Pop
3.Display 3.Display
4.Exit 4.Exit
Deleted elements is = 20
1.Push 1.Push
2.Pop 2.Pop
3.Display 3.Display
4.Exit 4.Exit
Deleted elements is = 10
9 STACK OPERATIONS
1.Push
2.Pop
3.Display
4.Exit
#include<stdio.h>
#include<string.h>
void push(char);
char pop();
void infix_postfix(char[ ], char[ ]);
int preced(char);
int main()
{
printf("\nEnter the infix expression:");
scanf("%s", infix);
infix_postfix(infix , postfix);
return(0);
}
switch(symbol)
{
case ' ( ' : push(symbol);
break;
case ' ) ' : temp = pop();
while( temp != ' ( ' )
{
postfix[ pos++ ] = temp;
temp = pop();
}
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 27
UUCMS NO: Subject: DS LAB
break;
case ' + ' :
case ' - ' :
case ' * ' :
case ' / ' : while(preced(stack[top]) >= preced(symbol))
{
temp = pop();
postfix[pos++] = temp;
}
push(symbol);
break;
default :postfix[pos++] = symbol;
break;
}
in++;
}
while(top>0)
{
temp = pop();
postfix[pos++] = temp;
}
}
char pop()
{
char symb;
symb = stack[top];
top = top - 1;
return(symb);
}
break;
case ' ( ':
case ' ) ' : p = 0;
break;
infix expression:(a+b)*c
postfix expression:ab+c*
void insert();
void delete();
void display();
int queue[MAX];
int rear = -1;
int front = -1;
int main()
{
int choice;
while(1)
{
printf("\t\t\t1.Insert element to queue \n");
printf("\t\t\t2.Delete element from queue \n");
printf("\t\t\t3.Display all elements of queue \n");
printf("\t\t\t4.Quit \n");
switch(choice)
{
case 1 : insert();
break;
case 2 : delete();
break;
case 3 : display();
break;
case 4 : exit(0);
/**************insert function********************/
void insert()
{
int element;
if(rear == MAX-1)
{
printf("Queue Overflow \n");
}
else
{
if(front == -1)
front = 0;
rear = rear+1;
queue[rear] = element;
}
}
/**************delete function***********************/
void delete()
{
if(front == -1 || front > rear)
{
printf("Queue Underflow \n");
return;
}
else
{
printf("\tElement deleted from queue is : %d \n", queue[front]);
front = front + 1;
}
}
/**************display function*********************/
void display()
{
int i;
else
{
printf("Queue is :");
for(i = front; i<= rear; i++)
printf("\t%d", queue[i]);
printf("\n");
}
}
OUTPUT:
Queue is : 10 20 30
int main()
{
struct student *head,*temp,*newnode;
int choice;
head=NULL;
do
{
printf("\n MENU");
printf("\n 1. INSERT BEGINNING\n 2. DELETE BEGINNING \n 3. DISPlAY
\n 4. EXIT");
printf("\n Enter your Choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
newnode=(struct student *) malloc(sizeof(struct student));
printf("Enter Roll No:");
scanf("%d",&newnode->rno);
if(head == NULL)
{
head=newnode;
newnode->next=NULL;
}
else
{
newnode->next=head;
head=newnode;
}
printf("%d is inserted at beginning\n",newnode->rno);
break;
case 2:
if(head==NULL)
printf("Linked List is Empty\n");
else
{
temp=head;
JAIN COLLEGE OF BBA BCA & COMMERCE, BELAGAVI Page 36
UUCMS NO: Subject: DS LAB
head=head->next;
printf("%d is deleted at beginning\n",temp->rno);
free(temp);
}
break;
case 3:
if(head==NULL)
printf("Linked List is empty/n");
else
{
temp=head;
printf("Contents of linked list\n");
while(temp!=NULL)
{
printf("\t%d",temp->rno);
temp=temp->next;
}
}
break;
case 4:
printf("Exiting the linked list");
break;
default: printf("Invalid Option\n");
}
}
while(choice!=4);
return 0;
}
MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice1
Enter Roll No:2001
2001 is inserted at beginning
MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice1
Enter Roll No:2003
2003 is inserted at beginning
MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice3
Contents of linked list
2003 2001
MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice2
2003 is deleted at beginning
MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice3
Contents of linked list
2001
MENU
1. INSERT BEGINNING
2. DELETE BEGINNING
3. DISPlAY
4. EXIT
Enter your Choice4
Exiting the linked list
#include <stdio.h>
#include <stdlib.h>
struct node {
int item;
struct node* left;
struct node* right;
};
// Inorder traversal
void inorderTraversal(struct node* root) {
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}