C With Data Structures 7-14
C With Data Structures 7-14
GCD(x, y)
Begin
if y = 0 then
return x;
else if(y>x)
Call: GCD(y,x);
else
Call: GCD(y, x%y);
endif
End
#include<stdio.h>
int gcd(int m,int n)
{
if(n==0)
return m;
else if(n>m)
gcd(n,m);
else
gcd(n,m%n);
}
void main()
{
int a,b,c,n1,n2;
printf("Input Numbers: ");
scanf("%d%d%d",&a,&b,&c);
n1=gcd(a,b);
n2=gcd(n1,c);
printf("\nGCD of %d,%d and %d is %d",a,b,c,n2);
}
Output:
Input Numbers: 4 6 8
08.Write a program to insert the elements {5,7,0,6,3,9} into circular queue and delete
5,7,0,6 from it(using linked list implementation).
void ENQUEUE()
Step1: Create a newNode with the given value and set the node's pointer to NULL.
Step2: Check whether the queue is EMPTY.
Step3: If it is EMPTY, set FRONT and REAR to newNode.
Step4: Else, set the pointer of REAR to newNode and make REAR as the newNode.
void DEQUEUE()
Step1: Check if the queue is EMPTY.
Step2: If it is EMPTY, then display "EMPTY QUEUE" and exit.
Step3:Else, create a temporary node and set it to FRONT.
Step4: Make the next node as FRONT and delete the temporary node.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
struct node *f = NULL;
struct node *r = NULL;
void enqueue(int d)
{
struct node* n;
n = (struct node*)malloc(sizeof(struct node));
n->data = d;
n->next = NULL;
if((r==NULL)&&(f==NULL))
{
f = r = n;
r->next = f;
}
else
{
r->next = n;
r = n;
n->next = f;
}
}
void dequeue()
{
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else if(f == r){
f = r = NULL;
printf("\n%d is deleted",t->data);
free(t);
}
else{
f = f->next;
r->next = f;
printf("\n%d is deleted",t->data);
free(t);
}
}
void display()
{
struct node* t;
t = f;
if((f==NULL)&&(r==NULL))
printf("\nQueue is Empty");
else{
do{
printf("\n%d",t->data);
t = t->next;
}while(t != f);
}
}
void main()
{
int ch,n,i,data;
printf("\n\n1.Enqueue\n2.Dequeue \n3.Display\n4.Exit");
do{
}
}while(ch!=4);
Output:
1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter Your Choice: 1
Enter your data: 5
5
7
0
6
3
9
Enter Your Choice: 2
5 is deleted
Enter Your Choice: 2
7 is deleted
Enter Your Choice: 2
0 is deleted
Enter Your Choice: 2
6 is deleted
Enter Your Choice: 3
3
9
Enter Your Choice: 4
return count;
}
void replace()
{
char s[20],s1[20],old[20],new[20];
printf("\nEnter the string: ");
gets(s1);
printf("\nEnter the substring to be replaced : ");
gets(old);
printf("\nEnter replacement string: ");
gets(new);
int slen=lengthstr(s1);
int len1=lengthstr(old);
int len2=lengthstr(new);
strcpy(s,s1);
int i=0,j,k,flag=0,start,end;
for(i=0;i<slen;i++)
{
flag=0;
start=i;
for(j=0;s[i]==old[j];j++,i++)
if(j==len1-1)
flag=1;
end=i;
if(flag==0)
i-=j;
else
{
for(j=start;j<end;j++)
{
for(k=start;k<slen;k++)
s[k]=s[k+1];
slen--;
i--;
}
for(j=start;j<start+len2;j++)
{
for(k=slen;k>=j;k--)
s[k+1]=s[k];
s[j]=new[j-start];
slen++;
i++;
}
}
}
if(strcmp(s1,s))
printf("\n%s is replaced in %s with %s :%s ",old,s1,new,s);
else
printf("\n%s is not substring of %s",old,s1);
}
void main()
{
char s1[20],s2[20],s[20];
printf("Enter first string: ");
gets(s1);
printf("Enter second string: ");
gets(s2);
printf("\nLength of %s is %d",s1,lengthstr(s1));
printf("\nLength of %s is %d",s2,lengthstr(s2));
concatestr(s1,s2);
replace();
printf("\nEnter string to which find substring: ");
gets(s1);
int x1,x2;
printf("\nEnter start and end index of substring in string: ");
scanf("%d%d",&x1,&x2);
substring(s1,x1,x2);
Output:
Length of Flowers is 7
Length of are beautiful is 14
Concatenated string is Flowers are beautiful
Enter the string: are beautiful
char stack[100];
int top = -1;
void push(char x)
{
stack[++top] = x;
}
char pop()
{
if(top == -1)
return -1;
else
return stack[top--];
}
int priority(char x)
{
if(x == '(')
return 0;
if(x == '+' || x == '-')
return 1;
if(x == '*' || x == '/')
return 2;
if(x=='^')
return 3;
return 0;
}
void main()
{
char exp[100];
char *e, x;
printf("Enter the expression : ");
scanf("%s",exp);
printf("\n");
e = exp;
printf("Postfix expressio: ");
while(*e != '\0')
{
if(isalnum(*e))
printf("%c ",*e);
else if(*e == '(')
push(*e);
else if(*e == ')')
{
while((x = pop()) != '(')
printf("%c ", x);
}
else
{
while(priority(stack[top]) >= priority(*e))
printf("%c ",pop());
push(*e);
}
e++;
}
while(top != -1)
{
printf("%c ",pop());
}
}
Output:
Enter the expression : x^y/(5*z)+2
Postfix expression: x y ^ 5 z * / 2 +
{
case '+': push(opA+opB);break;
case '-': push(opA-opB);break;
case '*': push(opA*opB);break;
case '/': push(opA/opB);break;
case '^': push(pow(opA,opB));break;
}
}
}
return pop();
}
Output:
12.Write a program to create a binary tree with the elements 18,15,40,50,30,17,41 after
creation insert 45 and 19 into tree and delete 15,17 and 41 from tree.Display the tree on
each insertion and deletion operations.
int main()
{
struct node *root=NULL;
root=insert(root,18);
root=insert(root,15);
root=insert(root,40);
root=insert(root,50);
root=insert(root,30);
root=insert(root,17);
root=insert(root,41);
printf("\nelements in creation are:\n");
inorder(root);
root=insert(root,45);
root=insert(root,19);
printf("\nafter inserting 45 and 19 \n");
inorder(root);
root=deletenode(root,15);
printf("\nafter deleting 15\n");
inorder(root);
root=deletenode(root,17);
printf("\nafter deleting 17\n");
inorder(root);
root=deletenode(root,41);
printf("\nafter deleting 41\n");
inorder(root);
}
Output:
13.Write a program to create a binary search tree with the elements {2,5,1,3,9,0,6} and
perform inorder,preorder and postorder traversal.
node* create();
void insert(node*,node*);
void preorder(node*);
void postorder(node*);
void inorder(node*);
int main()
{
char ch;
node* root=NULL,*temp;
do
{
temp=create();
if(root==NULL)
root=temp;
else
insert(root,temp);
printf("Do you want to enter more(Y/N)?: ");
getchar();
scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
printf("Preorder Traversal: \n");
preorder(root);
printf("\nPostorder Traversal: \n");
postorder(root);
printf("\nInorder Traversal: \n");
inorder(root);
return 0;
}
node* create()
{
node* temp;
printf("\nEnter data: ");
temp=(node*)malloc(sizeof(node));
scanf("%d",&temp->data);
temp->left=temp->right=NULL;
return temp;
}
}
}
Output:
Enter data: 2
Do you want to enter more(Y/N)?: y
Enter data: 5
Do you want to enter more(Y/N)?: y
Enter data: 1
Do you want to enter more(Y/N)?: y
Enter data: 3
Do you want to enter more(Y/N)?: y
Enter data: 9
Do you want to enter more(Y/N)?: y
Enter data: 0
Do you want to enter more(Y/N)?: y
Enter data: 6
Do you want to enter more(Y/N)?: n
Preorder Traversal:
2 1 0 5 3 9 6
Postorder Traversal:
0 1 3 6 9 5 2
Inorder Traversal:
0 1 2 3 5 6 9
14.Write a program to sort the following elements using heap sort {9,16,32,8,4,1,5,8,0}.
void HeapSort(arr)
BuildMaxHeap(arr)
for i = length(arr) to 2
swap arr[1] with arr[i]
heap_size[arr] = heap_size[arr] ? 1
MaxHeapify(arr,1)
End
void BuildMaxHeap(arr)
heap_size(arr) = length(arr)
for i = length(arr)/2 to 1
MaxHeapify(arr,i)
End
void MaxHeapify(arr,i)
L = left(i)
R = right(i)
if L ? heap_size[arr] and arr[L] > arr[i]
largest = L
else
largest = i
if R ? heap_size[arr] and arr[R] > arr[largest]
largest = R
if largest != i
swap arr[i] with arr[largest]
MaxHeapify(arr,largest)
End
#include<stdio.h>
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}
heapify(arr, i, 0);
}
}
int main() {
int arr[] = {9,16,32,8,4,1,5,8,0};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Array before sorting is \n");
printArray(arr, n);
heapSort(arr, n);
Output: