Beruflich Dokumente
Kultur Dokumente
19
1.Create an array of size 1000 and intialise with random integers less than 1000.Perform
linear search for 10000 random integers less than 1000 and find out the average no.of
searches.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[100],x,i,j,count=0;
for(i=0;i<1000;i++)
{
a[i]=rand()%1000;
}
for(i=0;i<1000;i++)
{
x=rand()%1000;
for(j=0;j<1000;j++)
{
if (a[j]==x)
{
count++;
break;
}
else
{
count++;
}
}
}
printf("avg.no of searches=%d\n",count/1000);
return 0;
}
Output:
avg.no of searches=96
2.Create an array of size 1000. Find the smallest from (n-i) and swap it with the ith in the
array
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[1000],i,j,temp;
for(i=0;i<1000;i++)
{
a[i]=rand()%1000;
}
for(i=0;i<1000;i++)
{
for(j=i+1;j<1000;j++)
{
if(a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}}
printf("\t%d",a[i]);
}
return 0;
}
Output:
1 2 3 3 3 6 7 7 8 8 8 8 9 10 10 11 11
15 15 17 18 18 19 20 21 21 21 22 22 23 24 25 27
28 28 28 30 30 31 31 35 35 35 36 37 37 38 38 39
40 40 40 41 41 41 43 44 44 49 50 52 53 53 53 55
58 58 60 60 60 61 64 67 67 70 71 71 72 72 72 72
73 75 75 75 75 77 80 80 82 82 84 84 85 86 87 87
88 88 90 93 93 97 98 99 101 102 103 105 106 107 108
109 109 110 111 112 112 113 113 114 115 115 116 116 117
118 119 123 124 125 127 129 129 129 131 132 132 139 140
141 141 142 142 142 142 142 144 145 145 145 146 148 150
152 152 153 153 153 154 154 155 156 157 159 161 161 164
164 168 168 168 169 169 169 170 170 171 173 173 174 175
176 177 179 181 181 182 184 185 185 186 186 187 188 189
190 190 191 191 192 192 193 193 195 195 195 196 196 199
200 200 200 200 202 202 202 205 205 209 212 213 213 213
215 215 216 220 221 221 221 221 222 222 223 224 224 226
227 229 230 232 233 234 235 240 240 245 247 249 249 253
253 255 256 256 257 258 259 260 261 262 262 263 264 264
264 264 264 269 270 270 270 271 272 279 279 281 281 281
282 282 285 285 286 286 287 287 288 288 289 290 291 292
292 292 292 295 296 296 297 297 299 300 302 303 303 303
303 303 306 308 309 309 310 313 313 313 313 313 314 314
314 315 316 316 317 318 318 318 321 322 322 323 323 324
326 328 329 330 333 333 334 334 335 337 337 342 342 343
347 348 350 350 350 350 353 353 355 355 355 355 356 357
357 357 358 359 359 360 361 361 362 363 363 365 368 369
369 370 371 372 374 375 376 380 382 383 384 386 389 391
391 392 392 393 398 401 402 404 405 409 410 410 411 411
413 413 413 414 416 416 416 418 421 421 422 422 423 423
423 423 423 424 425 426 426 428 428 429 430 430 432 433
434 436 437 439 439 439 441 443 446 447 448 450 451 452
454 455 457 457 458 459 460 461 462 463 464 464 466 466
467 467 467 472 472 474 474 476 477 477 477 478 480 481
482 483 483 483 484 484 484 485 485 487 487 488 488 489
489 491 492 493 496 497 498 500 503 503 503 504 505 506
508 508 510 510 511 511 512 512 514 515 515 518 519 519
520 520 523 525 526 527 527 529 529 532 534 535 536 537
537 538 538 538 539 540 540 541 541 541 542 543 543 545
546 547 547 548 548 548 549 549 549 549 550 555 555 556
556 556 556 557 558 559 561 561 565 565 565 565 570 573
574 576 576 577 578 580 584 584 584 585 585 587 588 588
589 589 589 591 591 593 593 595 596 596 598 600 600 600
601 601 601 602 604 604 605 606 607 608 608 609 610 611
616 617 617 617 618 619 619 620 622 623 624 624 625 625
625 625 626 626 626 626 627 627 627 629 629 629 629 634
634 634 635 636 637 637 637 639 641 643 644 646 646 648
648 648 649 650 650 651 651 652 653 654 655 655 657 658
658 659 662 662 662 663 664 667 667 668 668 670 671 673
673 673 674 675 676 676 678 678 678 679 681 683 685 685
686 687 688 689 690 690 692 693 694 694 695 695 696 698
699 700 701 701 702 703 704 704 704 705 705 705 706 710
711 711 712 712 713 716 717 718 718 721 722 723 723 724
724 725 726 726 728 729 734 734 734 734 736 737 740 741
741 745 745 748 748 750 752 753 753 754 756 756 757 757
757 758 758 758 759 759 760 760 762 763 763 763 766 767
769 771 771 773 774 775 777 778 778 781 783 783 786 786
787 788 788 789 790 790 796 798 798 798 800 801 802 805
807 808 811 812 813 813 814 815 815 815 818 818 823 824
824 825 825 827 827 829 829 829 831 831 832 832 833 833
833 835 836 838 840 841 842 843 844 844 847 848 850 850
851 851 853 855 855 855 858 859 861 864 865 866 867 868
869 869 869 869 869 870 870 874 875 875 877 878 881 881
882 885 886 887 888 888 890 892 893 893 894 895 896 896
898 900 900 900 900 901 902 902 902 903 905 909 909 911
912 912 913 913 913 923 923 924 924 924 926 928 929 930
931 932 932 932 934 935 936 937 938 938 938 940 941 941
942 942 943 944 944 944 945 945 945 946 948 948 949 949
951 954 954 954 955 956 958 958 958 959 961 961 962 962
962 962 963 964 966 966 969 970 971 971 971 972 972 974
974 975 976 977 977 982 985 985 986 989 990 992 993 993
994 995 996 997 998 999
3. Perform binary search in a sorted array of size 1000 and find out the average no.of
searches required for binary search.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,n,p,a[100];
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]=rand()%100;
}
p=2;
for(i=1;i<n;i++)
{
p*=2;
}
for(i=0;i<=p;i++)
{
for(j=0;j<n;j++)
{
if(i&(1<<j))
printf("\n%d\t",a[j]);
}
printf("\n");
}
return 0;
}
Output:
41
0
67
0
41
67
0
34
0
41
34
0
67
34
0
41
67
34
0
#include<stdio.h>
bool checkStackPermutation(int ip[], int op[], int n)
{
queue<int> input;
for (int i=0;i<n;i++)
input.push(ip[i]);
queue<int> output;
for (int i=0;i<n;i++)
output.push(op[i]);
stack <int> tempStack;
while (!input.empty())
{
int ele = input.front();
input.pop();
if (ele == output.front())
{
output.pop();
while (!tempStack.empty())
{
if (tempStack.top() == output.front())
{
tempStack.pop();
output.pop();
}
else
break;
}
}
else
tempStack.push(ele);
}
return (input.empty()&&tempStack.empty());
}
int main()
{
int input[] = {1, 2, 3};
int output[] = {2, 1, 3};
int n = 3;
if (checkStackPermutation(input, output, n))
printf( "Possible");
else
printf("Not Possible");
return 0;
}
Output:
Possible
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[100],i,j,n,k=2;
printf("No of elements is ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]=rand()%10;
printf("%d\n",a[i]);
}
printf("\n");
printf("\n");
for(i=2;i<=n;i++)
k=k*2;
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
if(i & (1<<j))
printf("%d",a[j]);
}
printf("\n");
}
return 0;
}
Input :
No of elements is 7
1
7
4
0
9
4
8
Output:
1
7
17
4
14
74
174
0
10
70
170
40
140
740
1740
9
19
79
179
49
149
749
1749
09
109
709
1709
409
1409
7409
17409
4
14
74
174
44
144
744
1744
04
104
704
1704
404
1404
7404
17404
94
194
794
1794
494
1494
7494
17494
094
1094
7094
17094
4094
14094
74094
174094
8
18
78
178
48
148
748
1748
08
108
708
1708
408
1408
7408
17408
98
198
798
1798
498
1498
7498
17498
098
1098
7098
17098
4098
14098
74098
174098
48
148
748
1748
448
1448
7448
17448
048
1048
7048
17048
4048
14048
74048
174048
948
1948
7948
17948
4948
14948
74948
174948
0948
10948
70948
170948
40948
140948
740948
1740948
#include<stdio.h>
#include<stdlib.h>
char* rev(char* str,int i,int j)
{
int temp;
if(!str[i]) return str;
int strlen=5;
if(strlen/2 && !str[i])
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
return rev(str[10],i+1,j-1);
}
}
int main()
{
char a[10]="cricket";
char b=rev(a,0,7);
11.7.19
1. Stack implementation.
#include<stdio.h>
#define MAXSIZE 5
typedef enum{FALSE,TRUE}BOOL;
typedef struct{ int vals[MAXSIZE];
int top; }STACK;
void initstack(STACK *s)
{
s->top=0;
}
BOOL isEmpty(STACK s)
{
return s.top==0;
}
BOOL isFull(STACK s)
{
return s.top==MAXSIZE-1;
}
BOOL push(STACK *s,int data)
{
if(isFull(*s))
return FALSE;
s->top=s->top+1;
s->vals[s->top]=data;
return TRUE;
}
BOOL pop(STACK *s,int *data)
{
if(isEmpty(*s))
return FALSE;
*data=s->vals[s->top];
s->top=s->top-1;
return TRUE;
}
int main()
{
STACK s1,s2;int data,i;
initstack(&s1);
if(!push(&s1,100))
printf("push failed\n");
if(!push(&s1,200))
printf("push failed\n");
if(!push(&s1,300))
printf("push failed\n");
if(!push(&s1,400))
printf("push failed\n");
if(!push(&s1,500))
printf("push failed\n");
if(pop(&s1,&data))
printf("[%d]\n",data);
if(pop(&s1,&data))
printf("[%d]\n",data);
return 0;
}
Output:
push failed
[400]
[300]
2.Read a list of integer terminated by -1 and print list of values in reverse form.
#include<stdio.h>
typedef struct {int val[100], top;}STACK;
typedef enum {FALSE,TRUE}BOOL;
void initStack(STACK *s)
{
s->top=0;
}
void push(STACK *s,int data)
{
s->top=s->top+1;s->val[s->top]=data;
}
int main()
{
STACK s1;
int i,a;
initStack(&s1);
printf("Enter the numbers:");
scanf("%d",&a);
while(a!=-1)
{
push(&s1,a);
scanf("%d",&a);
}
printf("Reversed order :\n");
if(a==-1)
{
for(i=s1.top;i>0;i--)
printf("\n%d",s1.val[i]);
}
printf("\n\n");
return 0;
}
Input:
Enter the numbers:47
39
23
87
64
55
-1
Output:
Reversed order :
55
64
87
23
39
47
#include<stdio.h>
#include<stdlib.h>
#define bool int
struct sNode
{
char data;
struct sNode *next;
};
void push(struct sNode** top_ref, int new_data);
int pop(struct sNode** top_ref);
bool isMatchingPair(char character1, char character2)
{
if (character1 == '(' && character2 == ')')
return 1;
else if (character1 == '{' && character2 == '}')
return 1;
else if (character1 == '[' && character2 == ']')
return 1;
else
return 0;
}
bool areParenthesisBalanced(char exp[])
{
int i = 0;
struct sNode *stack = NULL;
while (exp[i])
{
if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')
push(&stack, exp[i]);
if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']')
{
if (stack == NULL)
return 0;
else if ( !isMatchingPair(pop(&stack), exp[i]) )
return 0;
}
i++;
}
if (stack == NULL)
return 1;
else
return 0;
}
int main()
{
char exp[100] = "{()}[]";
if (areParenthesisBalanced(exp))
printf("Yes,it is balanced \n");
else
printf("Not balanced \n");
return 0;
}
void push(struct sNode** top_ref, int new_data)
{
struct sNode* new_node =
(struct sNode*) malloc(sizeof(struct sNode));
if (new_node == NULL)
{
printf("Stack overflow n");
getchar();
exit(0);
}
new_node->data = new_data;
new_node->next = (*top_ref);
(*top_ref) = new_node;
}
int pop(struct sNode** top_ref)
{
char res;
struct sNode *top;
if (*top_ref == NULL)
{
printf("Stack overflow n");
getchar();
exit(0);
}
else
{
top = *top_ref;
res = top->data;
*top_ref = top->next;
free(top);
return res;
}
}
Output:
Yes,it is balanced
4.Read a sequence of upper and lower case letter where upper case denotes a function call
and lower case denotes return.
#include<stdio.h>
#include<malloc.h>
#define max 50
typedef enum{FALSE,TRUE}BOOL;
typedef struct{ int val[max]; unsigned int top;}STACK;
void initSTACK(STACK *s)
{
s->top=0;
}
BOOL empty(STACK s)
{return s.top==0;}
BOOL full(STACK s) {return s.top==max-1;}
BOOL push(STACK *s,int data)
{
if(full(*s))return FALSE;
s->top=s->top+1;
s->val[s->top]=data;
printf("%3d",s->val[s->top]);
return TRUE;
}
BOOL pop(STACK *s,int *data)
{
if(empty(*s))return FALSE;
*data=s->val[s->top];
s->top=s->top-1;
return TRUE;
}
int main()
{
STACK s1,s2,s3;
int i,x,n,k,p,d,e;
initStack(&s1);
initStack(&s2);
initStack(&s3);
printf("Enter the values:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&x);
push(&s1,x);
}
for(i=0;i<n;i++)
{
k=pop(&s1);
push(&s2,k);
}
for(i=0;i<n;i++)
{
p=pop(&s2);
push(&s1,p);
push(&s3,p);
}
d=pop(&s1);
for(i=0;i<n;i++)
{
printf("%d",d);
}
e=pop(&s3);
for(i=0;i<n;i++)
{
printf("%d",e);
}
return 0;
}
Input:
Enter the values:
639283
Output:
Stack 1: 6 3 9 2 8 3
Stack 2: : 6 3 9 2 8 3
Stack 3: 1 4 7 3 6 8
Stacks 1 & 2 are same
18/07/2019
1.Create m stacks in an n element array where n is integral multiple of m.
#include<stdio.h>
#include<malloc.h>
#define max 10
typedef enum{FALSE,TRUE}BOOL;
typedef struct{ int val[max];unsigned int top;}STACK;
void initSTACK(STACK *s)
{
s->top=0;
}
BOOL empty(STACK s)
{return s.top==0;}
BOOL full(STACK s)
{return s.top==max-1;}
BOOL push(STACK *s,int data)
{
if(full(*s))return FALSE;
s->top=s->top+1;
s->val[s->top]=data;
printf("%3d",s->val[s->top]);
return TRUE;
}
BOOL pop(STACK *s,int *data)
{
if(empty(*s))return FALSE;
*data=s->val[s->top];
s->top=s->top-1;
return TRUE;
}
int main()
{
STACK *s;
int data,i,j=0,m,n,*a,k,l;
printf("No. of stack:");
scanf("%d",&m);
printf("No. of elements in array:");
scanf("%d",&n);
a=(int *)malloc(sizeof(int)*n);
if(n%m==0)
{
for(i=0;i<n;i++)
{
a[i]=rand()%10;
printf("%3d",a[i]);
}
s=(STACK *)malloc(sizeof(STACK)*m);
for(i=0;i<m;i++)
{
initSTACK(&s[i]);
}
j=n/m;
printf("\npushing\n");
for(k=0;k<m;k++)
{ printf("\nstack%d\n",k+1);
for(i=k*j;i<(k+1)*j;i++)
{ if(!push(&s[k],a[i]))
printf("\npush failed\n");
}
printf("\n");
}
printf("\npoping\n");
for(k=0;k<m;k++)
{ printf("\nstack%d\n",k+1);
while(s[k].top>0)
{ if(!pop(&s[k],&l))
printf("\npop failed\n");
printf("%3d",l);
}
printf("\n");
}
}
else
printf("\n Error!!\n");
return 0;
}
Input:
No. of stack:4
No. of elements in array:8
1
7
4
0
9
4
8
8
Output:
pushing
stack1
1 7
stack2
4 0
stack3
9 4
stack4
8 8
poping
stack1
7 1
stack2
0 4
stack3
4 9
stack4
8 8
2.Find out the postfix equivalent of given infix expression in fully parathesized form
containing unary,binary and ternary operators.
#include<stdio.h>
#define MAX 20
int main()
{
stack s;
char x;
int op1,op2,val;
init(&s);
printf("Enter the expression:\n");
scanf("%c",&x);
while((x=getchar())!='\n')
{
if(isdigit(x))
push(&s,x-48);
else
{
op2=pop(&s);
op1=pop(&s);
val=evaluate(x,op1,op2);
push(&s,val);
}
}
val=pop(&s);
printf("\nValue=%c",val);
return 0;
}
return(0);
}
return(x);
}
Input:
Enter the expression:
a+b
Output:
Value=ab+
3.Evaluate given postfix expression with single digit numbers as arguments and unary,binary
and ternary operators.
#include<stdio.h>
#define MAXSIZE 100
typedef struct{int vals[MAXSIZE];int top;}STACK;
typedef enum {FALSE,TRUE}BOOL;
void initStack(STACK *s)
{
s->top=0;
}
BOOL isEmpty(STACK s)
{
if( s.top==0)
return 1;
else
return 0;
}
BOOL isFull(STACK s)
{
return s.top==MAXSIZE-1;
}
BOOL push(STACK *s,int data)
{
if(isFull(*s))
return FALSE;
s->top=s->top+1;s->vals[s->top]=data;
return TRUE;
}
BOOL pop(STACK *s,int *data)
{
if(isEmpty(*s))
return FALSE;
*data=s->vals[s->top];
s->top=s->top-1;
return TRUE;
}
int main()
{
STACK s1,s2;
int data,i=0,j=0;
initStack(&s1);
initStack(&s2);
push(&s1,100);
push(&s1,200);
push(&s1,300);
push(&s1,400);
push(&s1,500);
push(&s2,100);
push(&s2,200);
push(&s2,300);
push(&s2,400);
push(&s2,500);
while(!isEmpty(s1))
{
if(pop(&s1,&data))
{
i++;
}
}
while(!isEmpty(s2))
{
if(pop(&s2,&data))
{
j++;
}
}
if(i==j)
{
printf("Stacks are same");
}
else
{
printf("Stacks are not same");
}
return 0;
}
Output:
Stacks are same
#include <stdio.h>
#define SIZE 5
int items[SIZE];
int front = -1, rear =-1;
int isFull()
{
if( (front == rear + 1) || (front == 0 && rear == SIZE-1)) return 1;
return 0;
}
int isEmpty()
{
if(front == -1) return 1;
return 0;
}
int deQueue()
{
int element;
if(isEmpty()) {
printf("\n Queue is empty !! \n");
return(-1);
} else {
element = items[front];
if (front == rear){
front = -1;
rear = -1;
}
else {
front = (front + 1) % SIZE;
}
printf("\n Deleted element -> %d \n", element);
return(element);
}
}
void display()
{
int i;
if(isEmpty()) printf(" \n Empty Queue\n");
else
{
printf("\n Front -> %d ",front);
printf("\n Items -> ");
for( i = front; i!=rear; i=(i+1)%SIZE) {
printf("%d ",items[i]);
}
printf("%d ",items[i]);
printf("\n Rear -> %d \n",rear);
}
}
int main()
{
deQueue();
enQueue(1);
enQueue(2);
enQueue(3);
enQueue(4);
enQueue(5);
enQueue(6);
display();
deQueue();
display();
enQueue(7);
display();
enQueue(8);
return 0;
}
Output:
Queue is empty !!
Inserted -> 1
Inserted -> 2
Inserted -> 3
Inserted -> 4
Inserted -> 5
Queue is full!!
Front -> 0
Items -> 1 2 3 4 5
Rear -> 4
Front -> 1
Items -> 2 3 4 5
Rear -> 4
Inserted -> 7
Front -> 1
Items -> 2 3 4 5 7
Rear -> 0
Queue is full!!
6.Check whether given string is a palindrome or not using stack and queue.
#include<stdio.h>
#include<string.h>
#define max 10
typedef enum{FALSE,TRUE}BOOL;
typedef struct{ char val[max];
unsigned int top;}STACK;
typedef struct{char val[max];
unsigned int front,rear;
}QUEUE;
BOOL empty(STACK s)
{return s.top==0;}
BOOL full(STACK s)
{return s.top==max-1;}
BOOL push(STACK *s,char data)
{
if(full(*s))return FALSE;
s->top=s->top+1;
s->val[s->top]=data;
return TRUE;
}
BOOL pop(STACK *s,char *data)
{
if(empty(*s))return FALSE;
*data=s->val[s->top];
s->top=s->top-1;
return TRUE;
}
void insert(QUEUE *s,char data)
{
s->val[s->rear]=data;
s->rear=s->rear+1;
}
void delete(QUEUE *s,char *data)
{
*data=s->val[s->front];
s->front=s->front+1;
}
int main()
{
STACK s1;
QUEUE q1;
int i=0,j=0,l,l1;
initSTACK(&s1);
initQUEUE(&q1);
char b[10],c,c1;
scanf("%s",b);
l=strlen(b);
l1=l;
while(b[i])
{
if(!push(&s1,b[i]))
printf("\npush failed\n");
insert(&q1,b[i]);
i++;
}
while(l1>0)
{
if(!pop(&s1,&c))
printf("\npop failed\n");
delete(&q1,&c1);
if(c==c1)
j++;
else
{j=0;
break;}
l1--;
}
if(j==l)
printf("palindrome\n");
else
printf("not a palindrome\n");
return 0;
}
Output :
nursesrun
palindrome
25.07.2019
1.Insert into a list which is pre-sorted and return the sorted list.
#include
struct node
{
int data;
struct node * next;
};
struct node * insertInSorted(struct node * head, int number)
{
int pos = 0;
while(temp != NULL)
{
if(temp->data > number)
break; // if we get the node, means we have the position, break
head = addAtPos(head, number, pos); // we can call our original made function
return head;
}
struct node * addAtPos(struct node *head, int number, int pos)
{
int initial_pos = 0;
struct node * mover = head;
while(initial_pos != pos)
{
mover = mover -> next;
initial_pos++;
}
return head;
}
return head;
}
printList(listHead);
return 0;
}
Output:
Enter a number to insert into a sorted list: 50
23 32 50 99 101 2222
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
int areIdentical(struct node *x, struct node *y)
{
while (x != NULL && y != NULL)
{
if (x->data != y->data)
return 0;
x = x->next;
y = y->next;
}
return (x == NULL && y == NULL);
}
void push(struct node** head_ref, int new_data)
{
struct node* new_node = (struct node*) malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main()
{
Output:
Identical
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node{int data;struct node* next;}node;
node* createNode()
{
node* temp=NULL;
return temp;
}
node* makeNode(int data)
{
node* temp=(node*)malloc(sizeof(node));
temp->data=data;
temp->next=NULL;
return temp;
}
node* insertEnd(node* root,int data)
{
node* t=root;
node* temp=makeNode(data);
if(!t)
return temp;
while(t->next)
t=t->next;
t->next=temp;
return root;
}
void display(node* root)
{
printf("\n");
while(root)
{
printf("%d ",root->data);
root=root->next;
}
printf("\n");
}
int commonPrefixLength(node* root1,node* root2)
{
int c=0;
while(root1 && root2)
{
if(root1->data==root2->data)
c++;
root1=root1->next;
root2=root2->next;
}
return c;
}
int main()
{
int ch,data,i;
node* root1=createNode();
node* root2=createNode();
for(i=1;i<=6;i++)
{
root1=insertEnd(root1,i);
}
display(root1);
for(i=1;i<1;i++)
{
root2=insertEnd(root2,i);
}
display(root2);
printf("\n%d\n",commonPrefixLength(root1,root2));
return 0;
}
Output:
1 2 3 4 5 6
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node *next;
};
int main()
{
struct node *p = NULL;
int n;
return 0;
}
p = q = r = *head;
p = p->next->next;
q = q->next;
r->next = NULL;
q->next = r;
while (p != NULL)
{
r = q;
q = p;
p = p->next;
q->next = r;
}
*head = q;
}
do
{
printf("Enter number: ");
scanf("%d", &c);
temp = (struct node *)malloc(sizeof(struct node));
temp->num = c;
temp->next = NULL;
if (*head == NULL)
{
*head = temp;
}
else
{
rear->next = temp;
}
rear = temp;
printf("Do you wish to continue [1/0]: ");
scanf("%d", &ch);
} while (ch != 0);
printf("\n");
}
Output:
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next;
}*head;
void createList(int n);
void traverseList();
int main()
{
int n;
createList(n);
return 0;
}
void createList(int n)
{
struct node *newNode, *temp;
int data, i;
if(head == NULL)
{
printf("Unable to allocate memory.");
exit(0);
}
temp = head;
for(i=2; i<=n; i++)
{
newNode = (struct node *)malloc(sizeof(struct node));
if(newNode == NULL)
{
printf("Unable to allocate memory.");
break;
}
newNode->data = data;
newNode->next = NULL;
temp->next = newNode;
temp = temp->next;
}
}
void traverseList()
{
struct node *temp;
if(head == NULL)
{
printf("List is empty.");
return;
}
temp = head;
while(temp != NULL)
{
printf("Data = %d\n", temp->data);
temp = temp->next;
}
Output:
25/07/2019
1.Linklist of integers with operation-insert,delete,search and display.
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node{int data;struct node*next;}NODE;
typedef NODE* LIST;
LIST makeNode(int data)
{
LIST temp;
temp=(LIST)malloc(sizeof(NODE));
temp->data=data;
temp->next=NULL;
return temp;
}
LIST insertHead(LIST l,int data)
{
LIST t=makeNode(data);
t->next=l;
return t;
}
LIST insertEnd(LIST l,int data)
{
if(!l) return makeNode(data);
LIST t=l;
while(t->next)
{
t=t->next;
}
t->next=makeNode(data);
return l;
}
LIST insertAt(LIST l,int data,int pos,int c)
{
LIST temp=madeNode(data);
if((pos==c)||(!l))
{
temp->next=l;
return temp;
}
l->next=insertAt(l->next,data,pos,c++);
return l;
}
int count(LIST l)
{
if(!l)
return 0;
return 1+count(l->next);
}
LIST deleteVal(LIST l,int val)
{
if(!l)
return NULL;
if(l->data==val)
return l->next;
l->next=deleteval(l->next,val);
return l;
}
LIST deletepos(LIST l,int pos)
{
LIST t=l;int cp=1;
if(pos==1)
return l->next;
while(t->next&&(pos-1)>cp)
{
t=t->next;
cp=cp+1;
}
if(t->next)
{
t->next=(t->next)->next;
}
return l;
}
int search (LIST l,int data)
{
LIST temp=l;
int pos=1;
if(!l) return 0;
if(l->data==data) return pos;
while(temp->next&&temp->data!=data)
{
pos++;
temp=temp->next;
if(temp->data==data) return pos;
if(!temp->next) return 0;
}
}
void displayLIST(LIST l)
{
while(l)
{
printf("%5d",l->data);
l=l->next;
}
}
int main()
{
LIST l=NULL;
int f,k;
l=insertHead(l,10);
l=insertEnd(l,50);
l=insertEnd(l,70);
l=insertEnd(l,80);
l=insertEnd(l,100);
l=insertEnd(l,200);
printf("\n the no.of elements to insert");
displayLIST(l);
printf("\n the no.of elemets in the list");
int r=count(l);
printf("%d",r);
printf("\n\n");
l=deleteval(l,50);
displayLIST(l);
l=deletpos(l,2);
printf("\n\n");
displayLIST(l);
l=insertAt(l,500,2,0);
printf("\n\n");
displayLIST(l);
printf("enter the elements to be searched");
scanf("%d",&f);
k=search(l,f);
printf("the element is found at %d",k);
return 0;
}
Output:
The no.of elements to be inserted:
7
No.of elements in the list:
1
2
3
4
5
6
7
1234567
123567
#include<stdio.h>
#include<malloc.h>
typedef struct node
{
int data;
struct node *next;
} node;
while(choice<=3)
{
printf("\n1.insert. 2.delete. 3.display");
printf("\nEnter your chioce : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n\tEnter the data for new node : ");
scanf("%d",&data);
newnode = (node *)malloc(sizeof(node));
newnode->data = data;
newnode->next = NULL;
if(empty(front))
{
front = newnode;
rear = newnode;
}else{
rear->next = newnode;
rear = newnode;
}
printf("\tElement inserted in queue.\n");
break;
case 2:
if(empty(front))
{
printf("\n\tQueue is already empty!!!\n");
}
else
{
front = front->next;
if(empty(front))
printf("\n\tQueue is empty now!!!\n");
else
printf("\n\tElement deleted\n");
}
break;
case 3:
display(front);
break;
}
}
}
Output:
Element deleted
1.insert. 2.delete. 3.display
Enter your chioce : 3
3.Linked dictionary
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct node
{
char data [ 20 ] ;
char m [ 5 ] [ 20 ] ;
int mcount ;
struct node * link ;
};
void main( )
{
char word [ 20 ] , ch ;
int i ;
while ( 1 )
{
printf ( "\n\t\tDictionary\n" ) ;
printf ( "\n\t\t1.Add Word.\n" ) ;
printf ( "\t\t2.Search Word.\n" ) ;
printf ( "\t\t3.Show Dictionary.\n" ) ;
printf ( "\t\t0.Exit." ) ;
printf ( "\n\n\t\tYour Choice: ") ;
scanf ( "%d", &ch ) ;
switch ( ch )
{
case 1 :
break ;
case 2 :
break ;
case 3 :
show( ) ;
getch( ) ;
break ;
case 0 :
deldic( ) ;
exit ( 0 ) ;
default :
i = search ( str ) ;
if ( i )
{
printf ( "\nWord already exists." ) ;
getch( ) ;
return ;
}
q = ( struct node * ) malloc ( sizeof ( struct node ) ) ;
strcpy ( q -> data, str ) ;
q -> link = NULL ;
q -> mcount = i ;
if ( dic [ j ] == NULL || strcmp ( dic [ j ] -> data, str ) > 0 )
{
r = dic [ j ] ;
dic [ j ] = q ;
q -> link = r ;
return ;
}
else
{
while ( temp != NULL )
{
if ( ( strcmp ( temp -> data, str ) < 0 ) && ( ( strcmp ( temp -> link -> data, str ) > 0 ) ||
temp -> link == NULL ) )
{
q -> link = temp -> link ;
temp -> link = q ;
return ;
}
temp = temp -> link ;
}
}
}
while ( n != NULL )
{
strcpy ( temp1, n -> data ) ;
void show( )
{
struct node *n ;
int i, j ;
printf ( "Word\t\tMeaning\n" ) ;
for ( i = 0 ; i <= 30 ; i++ )
printf ( "-" ) ;
void deldic( )
{
struct node *n, *t ;
int i ;
Output:
Dictionary
1.Add Word.
2.Search Word.
3.Show Dictionary.
0.Exit.
Your Choice: 1
1.Add Word.
2.Search Word.
3.Show Dictionary.
0.Exit.
Your Choice: 1
Enter any word : Ebb
1.Add Word.
2.Search Word.
3.Show Dictionary.
0.Exit.
Your Choice: 1
1.Add Word.
2.Search Word.
3.Show Dictionary.
0.Exit.
Your Choice:1
1.Add Word.
2.Search Word.
3.Show Dictionary.
0.Exit.
Your Choice: 1
1.Add Word.
2.Search Word.
3.Show Dictionary.
0.Exit.
Your Choice: 2
Affront disrespect
Dictionary
1.Add Word.
2.Search Word.
3.Show Dictionary.
0.Exit.
Your Choice: 3
Word Meaning
-------------------------------
Affront disrespect
Bashful shy
Ebb decrease gradually
Fickle changing frequently
Mocking making fun of someone
Dictionary
1.Add Word.
2.Search Word.
3.Show Dictionary.
0.Exit.
Your Choice: 0
4.Linked stack.
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef struct node{int data;struct node*next;}NODE;
typedef NODE* LIST;
LIST makeNode(int data)
{
LIST temp;
temp=(LIST)malloc(sizeof(NODE));
temp->data=data;
temp->next=NULL;
return temp;
}
LIST insertHead(LIST l,int data)
{
LIST t=makeNode(data);
t->next=l;
return t;
}
void displayLIST(LIST l)
{
while(l)
{
printf("%5d",l->data);
l=l->next;
}
}
LIST pop(LIST l)
{
return l->next;
}
int main()
{
LIST l=NULL;
int x,y,n;
int i,j,p;
printf("enter the values:\n");
while(x!=0)
{
scanf("%d",&x);
l=insertHead(l,x);
}
printf("\n\n");
displayLIST(l);
l=pop(l);
printf("\n\n");
displayLIST(l);
return 0;
}
Input and Output:
Enter the values: 20 30 40 45 60
20
30
40
45
60
After popping: 60 45 40 30 20
5.Linked queue.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define CAPACITY 100
typedef struct node
{
int data;
struct node * next;
} Queue;
unsigned int size = 0;
int main()
{
int ch, data;
Queue *rear, *front;
rear = NULL;
front = NULL;
while (1)
{
printf("1. Enqueue\n");
printf("2. Dequeue\n");
printf("3. Size\n");
printf("4. Get Rear\n");
printf("5. Get Front\n");
printf("0. Exit\n");
printf("--------------------------------------------\n");
printf("Select an option: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("\nEnter data to enqueue: ");
scanf("%d", &data);
if (enqueue(&rear, &front, data))
printf("Element added to queue.");
else
printf("Queue is full.");
break;
case 2:
data = dequeue(&front);
if (data == INT_MIN)
printf("Queue is empty.");
else
printf("Data => %d", data);
break;
case 3:
if (isEmpty())
printf("Queue is empty.");
else
printf("Queue size => %d", size);
break;
case 4:
data = getRear(rear);
if (data == INT_MIN)
printf("Queue is empty.");
else
printf("Rear => %d", data);
break;
case 5:
data = getFront(front);
if (data == INT_MIN)
printf("Queue is empty.");
else
printf("Front => %d", data);
break;
case 0:
printf("Exiting from app.\n");
exit(0);
default:
printf("Invalid choice, please input number between (0-5).");
break;
}
printf("\n\n");
}
}
int enqueue(Queue ** rear, Queue ** front, int data)
{
Queue * newNode = NULL;
if (isFull())
{
return 0;
}
newNode = (Queue *) malloc (sizeof(Queue));
newNode->data = data;
newNode->next = NULL;
if ( (*rear) )
{
(*rear)->next = newNode;
}
*rear = newNode;
if ( !( *front) )
{
*front = *rear;
}
size++;
return 1;
}
int dequeue(Queue ** front)
{
Queue *toDequque = NULL;
int data = INT_MIN;
if (isEmpty())
{
return INT_MIN;
}
toDequque = *front;
data = toDequque->data;
*front = (*front)->next;
size--;
free(toDequque);
return data;
}
int getRear(Queue * rear)
{
return (isEmpty())
? INT_MIN
: rear->data;
}
int getFront(Queue * front)
{
return (isEmpty())
? INT_MIN
: front->data;
}
int isEmpty()
{
return (size <= 0);
}
int isFull()
{
return (size > CAPACITY);
}
Output:
1. Enqueue
2. Dequeue
3. Size
4. Get Rear
5. Get Front
0. Exit
--------------------------------------------
Select an option: 1
1. Enqueue
2. Dequeue
3. Size
4. Get Rear
5. Get Front
0. Exit
--------------------------------------------
Select an option: 1
1. Enqueue
2. Dequeue
3. Size
4. Get Rear
5. Get Front
0. Exit
--------------------------------------------
Select an option: 1
1. Enqueue
2. Dequeue
3. Size
4. Get Rear
5. Get Front
0. Exit
--------------------------------------------
Select an option: 1
1. Enqueue
2. Dequeue
3. Size
4. Get Rear
5. Get Front
0. Exit
--------------------------------------------
Select an option: 1
1. Enqueue
2. Dequeue
3. Size
4. Get Rear
5. Get Front
0. Exit
--------------------------------------------
Select an option: 2
Data => 12
1. Enqueue
2. Dequeue
3. Size
4. Get Rear
5. Get Front
0. Exit
--------------------------------------------
Select an option: 3
Queue size => 4
1. Enqueue
2. Dequeue
3. Size
4. Get Rear
5. Get Front
0. Exit
--------------------------------------------
Select an option: 4
Rear => 89
1. Enqueue
2. Dequeue
3. Size
4. Get Rear
5. Get Front
0. Exit
--------------------------------------------
Select an option: 5
Front => 45
1. Enqueue
2. Dequeue
3. Size
4. Get Rear
5. Get Front
0. Exit
--------------------------------------------
01.08.2019
1.Create a linklist and perform a)to copy a linklist b)to compare two linklist c)to check
whether one is contained in another d)merging of two list without duplicated
#include<stdio.h>
#include<malloc.h>
typedef struct node { int data;struct node *next;}NODE;
typedef NODE* LIST;
LIST makeNode(int data) {
LIST temp;
temp = (LIST)malloc(sizeof(NODE));
temp->data = data;
temp->next = NULL;
return temp;
}
LIST createList() {
return NULL;
}
void display(LIST l) {
if(l) {
printf("%d ", l->data);
display(l->next);
}
else
printf("\n");
}
LIST insert(LIST l, int d) {
if(!l)
return makeNode(d);
LIST t = l;
while(t->next) t = t->next;
t->next = makeNode(d);
return l;
}
LIST copy(LIST l)
{
LIST t,b;
t=createList();
while(l)
{
t=insert(t,l->data);
if(t->next==NULL) b=t;
l=l->next;
}
return b;
}
int same(LIST a, LIST b) {
while(a && b) {
if(a->data != b->data)
return 0;
a = a->next;
b = b->next;
}
if(a || b)
return 0;
return 1;
}
int subset(LIST a, LIST b) {
LIST ca = a, cb;
while(b) {
if(a->data == b->data) {
cb = b;
while(a && b) {
if(a->data == b->data) {
a = a->next;
b = b->next;
}
else {
a = ca;
b = cb;
break;
}
}
if(!a || !a && !b)
return 1;
}
else {
b = b->next;
}
}
return 0;
}
LIST merge(LIST a, LIST b) {
LIST t = copy(a), temp;
while(b) {
temp = makeNode(b->data);
if(!subset(temp, a))
t = insert(t, b->data);
b = b->next;
}
return t;
}
int main() {
LIST a, b, c;
a = createList();
int d;
printf("Enter the data of a: \n");
while(1) {
scanf("%d", &d);
if(d != -1)
a = insert(a, d);
else
break;
}
b = copy(a);
printf("Enter the data of b: \n");
while(1) {
scanf("%d", &d);
if(d != -1)
b = insert(b, d);
else
break;
}
Output:
Enter the data of a:
1
2
3
4
5
-1
Enter the data of b:
1
2
3
6
7
-1
a -> 1 2 3 4 5
b -> 1 2 3 6 7
The list a and b are Not Same
The list a is in b
The merged list is:
c -> 1 2 3 4 5 6 7
2.Create a linklist of STOCK with product name,quantity and unit cost.Create another linklist
BASKET with product name and quantity.Write a program to buy all the items in the basket
and compute the total cost.
#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct s {
char name[20];
int price, quantity;
struct s *next;
}S;
typedef S* stock;
typedef struct b {
char name[20];
int quantity;
struct b *next;
}B;
typedef B* basket;
stock makeNodes(char *n, int p, int q) {
stock a = (stock)malloc(sizeof(S));
strcpy(a->name, n);
a->price = p;
a->quantity = q;
a->next = NULL;
return a;
}
basket makeNodeb(char *n, int q) {
basket a = (basket)malloc(sizeof(B));
strcpy(a->name, n);
a->quantity = q;
a->next = NULL;
return a;
}
stock insertStock(stock a, char *n, int p, int q) {
if(!a)
return makeNodes(n, p, q);
stock t = a;
while(a->next) a = a->next;
a->next = makeNodes(n, p, q);
return t;
}
basket insertBasket(basket a, char *n, int q) {
if(!a)
return makeNodeb(n, q);
basket t = a;
while(a->next) a = a->next;
a->next = makeNodeb(n, q);
return t;
}
void displayStock(stock a) {
if(a) {
printf(" Name Quantity Price\n");
while(a) {
printf("%-20s%5d%10d\n", a->name, a->quantity, a->price);
a = a->next;
}
}
}
void display(basket b) {
while(b) {
printf("%-20s%5d\n", b->name, b->quantity);
b = b->next;
}
}
basket buy(basket b, stock a) {
stock ca = a;
int price = 0;
basket cb = b, na = NULL;
while(b) {
while(a) {
if(strcmp(a->name, b->name) == 0)
break;
a = a->next;
}
if(!a) {
na = insertBasket(na, b->name, b->quantity);
a = ca;
b->quantity = 0;
b = b->next;
continue;
}
if(a->quantity >= b->quantity) {
price += a->price * b->quantity;
a->quantity = a->quantity - b->quantity;
b = b->next;
}
else {
price += a->price * a->quantity;
b->quantity = b->quantity - a->quantity;
na = insertBasket(na, b->name, b->quantity);
b->quantity = a->quantity;
b = b->next;
}
}
b = cb;
printf("Reciept:\n");
printf("Name Quantity\n");
while(b) {
if(b->quantity != 0) {
printf("%-10s%d\n", b->name, b->quantity);
}
b = b->next;
}
printf("Price -> %d\n", price);
return na;
}
typedef B* basket;
int main() {
stock a = NULL;
basket b = NULL, na;
char s[20];
int n, i, q;
a = insertStock(a, "lipstick", 500, 5);
a = insertStock(a, "kajal", 2000, 10);
a = insertStock(a, "mascara", 15000, 35);
a = insertStock(a, "eyeliner",300,50 );
printf("Available products in the store: \n");
displayStock(a);
printf("How many products do you want to buy: ");
scanf("%d", &n);
for(i = 0; i < n; i++) {
printf("Enter the %d product's name: ", i + 1);
scanf("\n%s", s);
printf("Enter the Quantity: ");
scanf("%d", &q);
b = insertBasket(b, s, q);
}
na = buy(b, a);
if(na) {
printf("\nThese items are not in stock:\n");
display(na);
}
return 0;
}
Output:
3.Create a linklist with regno ,name,dob.Create another linklist with regno,name of courses
enrolled.Find the courses registered for a given name.
#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct s{
char name[32], branch[5];
unsigned int regno;
struct s *next;
}S;
typedef S *student;
typedef struct e{
char enrol[3][10];
unsigned int regno;
struct e *next;
}E;
typedef E *enrollment;
student makeNode(char *n, char *b, unsigned int r) {
student a = (student)malloc(sizeof(S));
strcpy(a->name, n);
strcpy(a->branch, b);
a->regno = r;
return a;
}
enrollment makeNodee(unsigned int r, char *a, char *b, char *c) {
enrollment t = (enrollment)malloc(sizeof(E));
t->regno = r;
strcpy(t->enrol[0], a);
strcpy(t->enrol[1], b);
strcpy(t->enrol[2], c);
t->next = NULL;
return t;
}
enrollment inserte(enrollment q, unsigned int r, char *a, char *b, char *c) {
if(!q) return makeNodee(r, a, b, c);
enrollment t = q;
while(q->next) q = q->next;
q->next = makeNodee(r, a, b, c);
return t;
}
student insert(student a, char *n, char *b, unsigned int r) {
student t = a;
if(!a) return makeNode(n, b, r);
while(a->next) a = a->next;
a->next = makeNode(n, b, r);
return t;
}
void display(student a) {
if(a)
printf("Name branch regno\n");
while(a) {
printf("%-10s%-8s%u\n", a->name, a->branch, a->regno);
a = a->next;
}
}
int main() {
char x[10], y[10], z[10];
student a = NULL;
enrollment q = NULL;
unsigned int r;
Output:
Name Course Regno
Gayatri CT 2018503545
Yogi Mech 2018503032
Ishu Auto 2018503523
Enter the enrolled subjects:
Eng maths c
Eg phy chem
Science phy solid mech
Enter the name of the student: Gayatri
Eng maths c
4.Read a line of text and find the frequency of occurrence of each letter.
#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct n{
char a;
int count;
struct n*next;
}N;
typedef N*list;
list makeNode(char a){
list x=(list)malloc(sizeof(N));
x->a=a;
x->count=1;
x->next=NULL;
return x;
}
list insert(list t,char c){
if(!t)return makeNode(c);
list x=t;
while(t){
if(t->a==c){
t->count++;
break;
}
t=t->next;
}
if(!t){
t=x;
while(t->next)t=t->next;
t->next=makeNode(c);
}return x;
}
int main()
{
list a=NULL;
char s[60];
int i;
printf ("enter the string:");
scanf ("%[^\n]",s);
for(i=0;s[i];i++)
{
a=insert(a,s[i]);
}
printf ("The frequency of each characters in the string is\n");
while(a){
printf("%c-%d\n",a->a,a->count);
a=a->next;
}
return 0;
}
Output:
08.08.2019
1.Create a circular linklist with minimum 10 values and write functions of the following
a)next value b)previous value c)search d)count how many values
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
while(t->next != head)
{
t=t->next;
}
t->next=makeNode(data);
t->next->next=head;
return head;
}
void displayList(LIST t)
{
LIST head=t;
if(t==NULL)
{ printf("\nlist empty"); return;}
printf("%5d",t->data);
while(t->next != head)
{
printf("%5d",t->next->data);
t=t->next;
}
}
int countNode(LIST t)
{
int c=0;
LIST head = t;
if(!t) return;
if(t){c++; t=t->next;}
while(t != head)
{
c++;
t=t->next;
}
return c;
}
LIST next(LIST l)
{
return l->next;
}
int main()
{
LIST l;
l=NULL;
int n;
while(1)
{
printf("\n\n enter choice");
printf("\n1.Insert data");
printf("\n2.Display");
printf("\n3.Count no of nodes");
printf("\n4.Search data");
printf("\n5.Previous");
printf("\n6.Next");
printf("\n7.terminate\n");
scanf("%d",&n);
if(n==7)
break;
if(n==1)
{
int x;
printf("\n\nenter data to be inserted ");
while(1)
{
scanf("%d",&x);
if(x==-1)
break;
l=insertEnd(l,x);
}
}
else if(n==2)
{
printf("\nLIST : ");
displayList(l);
}
else if(n==3)
{
printf("\n\nThe no of nodes is %5d",countNode(l));
}
else if(n==4)
{
int a;
printf("\n\nenter the data to be searched ");
scanf("%d",&a);
int b=search(l,a);
if(!b)
printf("\n\nData not found");
else if(b)
printf("\n\nData is found ");
}
else if(n==5)
{
LIST t=l;
l=previous(l);
printf("\nthe Previous of %d : %d",t->data,l->data);
}
else if(n==6)
{
LIST t=l;
l=next(l);
printf("\nthe Next of %d : %d",t->data,l->data);
}
}
printf("\n\n");
return 0;
}
Output:
enter choice
1.Insert data
2.Display
3.Count no of nodes
4.Search data
5.Previous
6.Next
7.terminate
1
enter choice
1.Insert data
2.Display
3.Count no of nodes
4.Search data
5.Previous
6.Next
7.terminate
2
LIST : 23 23 34 68 94 46
enter choice
1.Insert data
2.Display
3.Count no of nodes
4.Search data
5.Previous
6.Next
7.terminate
3
The no of nodes is 6
enter choice
1.Insert data
2.Display
3.Count no of nodes
4.Search data
5.Previous
6.Next
7.terminate
4
Data is found
enter choice
1.Insert data
2.Display
3.Count no of nodes
4.Search data
5.Previous
6.Next
7.terminate
5
the Previous of 23 : 46
enter choice
1.Insert data
2.Display
3.Count no of nodes
4.Search data
5.Previous
6.Next
7.terminate
6
the Next of 46 : 23
enter choice
1.Insert data
2.Display
3.Count no of nodes
4.Search data
5.Previous
6.Next
7.terminate
7
2.Read a large integer as a string and store it as a consecutive number in three digits form in
a linklist .
#include<stdio.h>
#include<malloc.h>
typedef struct node {
int a[4];
struct node* next;
}NODE;
typedef NODE* LIST;
LIST makeNode(char *c) {
LIST t = (LIST)malloc(sizeof(NODE));
if(c[0]) t->a[0] = c[0] - '0'; else t->a[0] = -1;
if(c[1]) t->a[1] = c[1] - '0'; else t->a[1] = -1;
if(c[2]) t->a[2] = c[2] - '0'; else t->a[2] = -1;
t->next = NULL;
return t;
}
LIST insert(LIST a, char *x) {
LIST t = makeNode(x);
if(!a) return t;
t->next = a;
return t;
}
void display(LIST a) {
while(a) {
if(a->a[0] != -1)printf("%d", a->a[0]);
if(a->a[1] != -1)printf("%d", a->a[1]);
if(a->a[2] != -1)printf("%d", a->a[2]);
a = a->next;
}
printf("\n");
}
int c = 1;
LIST incre(LIST a) {
if(!a) return NULL;
if(a->next) a->next = incre(a->next);
if(a->a[2] + c == 10) {a->a[2] = 0;} else {a->a[2] = a->a[2] + c; c =0; }
if(a->a[1] + c == 10) {a->a[1] = 0;}else if(a->a[1] == -1 && c) {a->a[1] = 1; c = 0;} else
{a->a[1] = a->a[1] + c; c =0; }
if(a->a[0] + c == 10) {a->a[0] = 0;}else if(a->a[1] == -1 && c) {a->a[1] = 1; c = 0;} else
{a->a[0] = a->a[0] + c; c =0; }
return a;
}
LIST increment(LIST l){
l = incre(l);
if(c == 1) {
char ch[4] = {'\0','\0','1'};
l = insert(l, ch);
}
return l;
}
int d = -1;
LIST decrement(LIST a) {
if(!a) return NULL;
if(a->next) a->next = decrement(a->next);
if(a->a[2] + d == -1) {a->a[2] = 9;}else {a->a[2] = a->a[2] + d; d =0; }
if(a->a[1] != -1 && a->a[1] + d == -1) {a->a[1] = 9;}else {a->a[1] = a->a[1] + d; d =0; }
if(a->a[0] != -1 && a->a[0] + d == -1 && d) {a->a[0] = 9;}else {a->a[0] = a->a[0] + d; d
=0; }
return a;
}
int e=0;
LIST adds(LIST a, LIST b) {
if(!a && !b) return NULL;
if(!a) return b;
else return a;
if(a->next) {
if(b->next) {
a->next = adds(a->next, b->next);
}
else
a->next = adds(a->next, b);
}
else if(b->next){
a->next = adds(a, b->next);
}
if(a->a[2] + b->a[2] + e>= 10) {a->a[2] = a->a[2] + b->a[2] - 10; e = 1;} else {a->a[2] = a-
>a[2] + b->a[2]; e =0; }
if(a->a[1] + b->a[1] + e>= 10) {a->a[1] = a->a[1] + b->a[1] - 10; e = 1;}else if(a->a[1] ==
-1 && e) {a->a[1] = 1; c = 0;} else {a->a[1] = a->a[1] + b->a[1]; e =0; }
if(a->a[0] + b->a[0] + e>= 10) {a->a[0] = a->a[0] + b->a[0] - 10; e = 1;}else if(a->a[1] ==
-1 && e) {a->a[1] = 1; e = 0;} else {a->a[0] = a->a[0] + b->a[0]; e =0; }
return a;
}
LIST add(LIST a, LIST b) {
if(!a && !b) return NULL;
LIST c = NULL, ca = a;
char ch[4];
if(a) {
ch[0] = a->a[0]+'0';
ch[1] = a->a[1]+'0';
ch[2] = a->a[2]+'0';
c = makeNode(ch);
a = a->next;
}
LIST t = c;
while(a) {
ch[0] = a->a[0]+'0';
ch[1] = a->a[1]+'0';
ch[2] = a->a[2]+'0';
t->next = makeNode(ch);
t = t->next;
a = a->next;
}
c = adds(c, b);
return c;
}
int main() {
LIST a = NULL;
char str[100], sm[4];
int i, j, l = 0;
printf("Enter the number: ");
scanf("%[^\n]%*c", str);
for(l = 0; str[l]; l++);
for(i = l - 1; i >= 0; i-=3) {
for(j = 2; j >= 0; j--) {
if(str[i] >= '0' && str[i] <= '9')
sm[j] = str[i-(2-j)];
else
break;
}
a = insert(a, sm);
}
printf("The number is: ");
display(a);
a = increment(a);
printf("The incremented number is: ");
display(a);
a = decrement(a);
printf("The decremented number is: ");
display(a);
LIST c= adds(a, a);
display(c);
return 0;
}
Output:
Enter the number: 378
The number is: 378
The incremented number is: 379
The decremented number is: 378
378
3.Create a polynomial using link list of terms (coefficient,exponent)and terms are stored in
decreasing order of their exponent.
a)multiply a polynomial by a scalar b)multiply a polynomial by a term c)add two
polynomials
d)multiply two polynomials
#include<stdio.h>
#include<malloc.h>
typedef struct node{int c, e;struct node* next;}NODE;
typedef NODE* P;
P makeNode(int c, int e)
{
P temp = (P)malloc(sizeof(NODE));
temp->c = c;
temp->e = e;
temp->next = NULL;
return temp;
}
P insert(P t, int c, int e)
{
if(!t) return makeNode(c, e);
P x = t;
P tmp = makeNode(c, e);
if(t->e < e)
{
tmp->next = t;
return tmp;
}
else if(t->e == e)
{
t->c += c;
return x;
}
while(t->next && t->next->e > e) t = t->next;
if(!t->next) {t->next = makeNode(c, e); return x;}
if(t->next->e == e) {t->next->c += c; return x;}
tmp->next = t->next;
t->next = tmp;
return x;
}
void display(P l)
{
if(!l)
printf("\nEMPTY\n");
while(l)
{
if(l->e == 0)
printf("%d",l->c);
else if(l->e == 1)
printf("%dx",l->c);
else
{
printf("%dx^%d",l->c,l->e);
}
if(l->next)
{
if(l->next->c > 0)
printf(" +");
else
printf(" ");
}
else
printf(" = 0\n");
l=l->next;
}
}
P multiplys(P t, int c)
{
if(!t) return NULL;
P x = t;
while(t)
{
t->c *= c;
t = t->next;
}
return x;
}
P multiplyt(P t, int c, int e)
{
if(!t) return NULL;
P x = t;
while(t)
{
t->c *= c;
t->e += e;
t = t->next;
}
return x;
}
P sump(P x, P y)
{
P t = NULL, c;
while(x && y)
{
if(x->e > y->e)
{
t = insert(t, x->c, x->e);
x = x->next;
}
else if(x->e < y->e)
{
t = insert(t, y->c, y->e);
y = y->next;
}
else
{
t = insert(t, x->c + y->c, x->e);
x = x->next;
y = y->next;
}
}
c = (!x) ? y : x;
while(c)
{
t = insert(t, c->c, c->e);
}
return t;
}
P prod(P x, P y)
{
P t = NULL, c;
c = y;
while(x)
{
while(y)
{
t = insert(t, x->c * y->c, x->e + y->e);
y = y->next;
}
y = c;
x = x->next;
}
return t;
}
int main()
{
P x = NULL, y = NULL;
int exp, co;
printf("Enter the Polynomial: ");
while(1)
{
printf("\nEnter co-efficient term: ");
scanf("%d", &co);
if(co == 0) break;
printf("Enter the value of the exponent: ");
scanf("%d", &exp);
x = insert(x, co, exp);
}
printf("The polynomial expression is: \n");
display(x);
printf("Enter the scalar term: ");
scanf("%d", &co);
x = multiplys(x, co);
printf("The expression multiplied by %d is :\n", co);
display(x);
printf("Enter the term to be multiplied by the polynomial expression: ");
printf("\nEnter co-efficient term: ");
scanf("%d", &co);
printf("Enter the value of the exponent: ");
scanf("%d", &exp);
x = multiplyt(x, co, exp);
printf("The expression is:\n");
display(x);
printf("Enter the 2nd Polynomial: ");
while(1)
{
printf("\nEnter co-efficient term: ");
scanf("%d", &co);
if(co == 0) break;
printf("Enter the value of the exponent: ");
scanf("%d", &exp);
y = insert(y, co, exp);
}
printf("The polynomial expression is: \n");
display(y);
P sum = sump(x, y);
printf("The sum of the polynomial expressions is: \n");
printf("%d", sum);
display(sum);
P product = prod(x, y);
printf("The product of the polynomial expressions is: \n");
printf("%d",product);
display(product);
return 0;
}
Output:
22.08.2019
1.Create a binary search tree by repeated inserting one value at a time.Display value in
search tree in increasing order. Find max,min,mirror image and display value in decreasing
order.
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef enum{false,true}BOOL;
typedef struct node{int data;struct node * right;struct node * left;}TREE;
TREE *createTREE()
{
return NULL;
}
TREE *makenode(TREE *left,int val,TREE *right)
{
TREE *temp=(TREE*)malloc(sizeof(TREE));
temp->data=val;
temp->right=right;
temp->left=left;
return temp;
}
}
}
void inorder(TREE *t)
{
if(t)
{
inorder(t->left);
printf("%d\n",t->data);
inorder(t->right);
}
}
TREE *insertval(TREE *t,int val)
{
if(!t) return makenode(NULL,val,NULL);
if(t->data>val)
t->left=insertval(t->left,val);
if(t->data<val)
t->right=insertval(t->right,val);
return t;
}
void mirror(TREE *t)
{
if(t)
{
TREE *temp=t->left;
t->left=t->right;
t->right=temp;
if(t->left) mirror(t->left);
if(t->right) mirror(t->right);
}
}
int finmin(TREE *t)
{
if(!t->left) return t->data;
return finmin(t->left);
}
int finmax(TREE *t)
{
if(!t->right) return t->data;
return finmax(t->right);
}
int main()
{
TREE *t=createTREE();
int i,j,k,n,l;
printf("enter no. of elements");
scanf("%d",&n);
for(i=0;i<n;i++)
{
k=rand()%100;
t=insertval(t,k);
}
printf("\n\n");
j=finmin(t);
printf("min value=%d",j);
printf("\n\n");
l=finmax(t);
printf("max val=%d",l);
printf("\n\n");
printf("tree");
preorder(t);
printf("\n\n");
printf("increasing oder");
inorder(t);
printf("\n\n");
printf("mirror");
mirror(t);
preorder(t);
printf("\n\n");
printf("descending ");
inorder(t);
return 0;
}
Output:
min value=0
max val=78
tree41
34
0
24
67
69
78
increasing oder0
24
34
41
67
69
78
mirror41
67
69
78
34
0
24
descending 78
69
67
41
34
24
0
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
return(node);
}
int main()
{
struct node *root = newNode(40);
root->left = newNode(20);
root->right = newNode(60);
root->left->left = newNode(10);
root->left->right = newNode(30);
root->right->right = newNode(80);
root->right->right->right = newNode(90);
if (is_bst(root))
printf("TREE 1 Is BST");
else
printf("TREE 1 Not a BST");
prev = NULL;
struct node *root1 = newNode(50);
root1->left = newNode(20);
root1->right = newNode(30);
root1->left->left = newNode(70);
root1->left->right = newNode(80);
root1->left->left->right = newNode(40);
root1->left->left->left = newNode(90);
if (is_bst(root1))
printf("\nTREE 2 Is BST");
else
printf("\nTREE 2 Not a BST");
return 0;
}
Output:
TREE 1 Is BST
TREE 2 Not a BST
29.08.2019
1.Create a binary search tree of integers with minimum of 25 with no duplicated.Write
functions for deleting smallest and largest value.
#include <stdio.h>
#include <stdlib.h>
struct btnode
{
int value;
struct btnode *l;
struct btnode *r;
}*root = NULL, *temp = NULL, *t2, *t1;
void delete1();
void insert();
void delete();
void inorder(struct btnode *t);
void create();
void search(struct btnode *t);
void preorder(struct btnode *t);
void postorder(struct btnode *t);
void search1(struct btnode *t,int data);
int smallest(struct btnode *t);
int largest(struct btnode *t);
int flag = 1;
void main()
{
int ch;
printf("\n1 - Insert an element into tree\n");
printf("2 - Delete an element from the tree\n");
printf("3 - Inorder Traversal\n");
printf("4 - Preorder Traversal\n");
printf("5 - Postorder Traversal\n");
printf("6 - Exit\n");
while(1)
{
printf("\nEnter your choice : ");
scanf("%d", &ch);
switch (ch)
{
case 1:
insert();
break;
case 2:
delete();
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
exit(0);
default :
printf("Wrong choice, Please enter correct choice ");
break;
}
}
}
void insert()
{
create();
if (root == NULL)
root = temp;
else
search(root);
}
void create()
{
int data;
}
else
{
t1->r = t->l;
}
t = NULL;
free(t);
return;
}
else if (t->l == NULL)
{
if (t1 == t)
{
root = t->r;
t1 = root;
}
else if (t1->r == t)
t1->r = t->r;
else
t1->l = t->r;
t == NULL;
free(t);
return;
}
else if ((t->l != NULL) && (t->r != NULL))
{
t2 = root;
if (t->r != NULL)
{
k = smallest(t->r);
flag = 1;
}
else
{
k =largest(t->l);
flag = 2;
}
search1(root, k);
t->value = k;
}
}
int smallest(struct btnode *t)
{
t2 = t;
if (t->l != NULL)
{
t2 = t;
return(smallest(t->l));
}
else
return (t->value);
}
Output:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int info;
struct node* left, *right;
};
return(newnode);
}
int count = 0;
int leafnodes(struct node* newnode)
{
if(newnode != NULL)
{
leafnodes(newnode->left);
if((newnode->left == NULL) && (newnode->right == NULL))
{
count++;
}
leafnodes(newnode->right);
}
return count;
int main()
{
return 0;
}
Output:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef enum{false,true}BOOL;
typedef struct node{int data;struct node * right;struct node * left;}TREE;
TREE *createTREE()
{
return NULL;
}
TREE *makenode(TREE *left,int val,TREE *right)
{
TREE *temp=(TREE*)malloc(sizeof(TREE));
temp->data=val;
temp->right=right;
temp->left=left;
return temp;
}
TREE *insertval(TREE *t,int val)
{
if(!t) return makenode(NULL,val,NULL);
if(t->data>val)
t->left=insertval(t->left,val);
if(t->data<val)
t->right=insertval(t->right,val);
return t;
}
void inorder(TREE *t)
{
if(t)
{
inorder(t->left);
printf("%d\n",t->data);
inorder(t->right);
}
}
int count(TREE*t)
{
if(!t)return 0;
return 1+count(t->left)+count(t->right);
}
int height(TREE*t)
{
int a=0,b=0;
if(!t)return 0;
while(t)
{
if(t->left)
a++;
if(t->right)
b++;
}
if(a>b)
return a;
else
return b;
}
int main()
{
TREE *t=createTREE();
int a,b,i,k,n;
printf("enter no. of elements");
scanf("%d",&n);
for(i=0;i<n;i++)
{
k=rand()%100;
t=insertval(t,k);
}
inorder(t);
if(a>1)
printf("Not Balanced");
else
printf("Balanced");
return 0;
}
Output:
enter no. of elements6
0
24
34
41
67
69
Balanced
4.Read a line of text and display frequency of occurrence of each letter by constructing
binary search where duplicate are counted and new data is stored.
#include<stdio.h>
#include<malloc.h>
#include<string.h>
typedef struct n{
char a;
int count;
struct n*next;
}N;
typedef N*list;
list makeNode(char a){
list x=(list)malloc(sizeof(N));
x->a=a;
x->count=1;
x->next=NULL;
return x;
}
list insert(list t,char c){
if(!t)return makeNode(c);
list x=t;
while(t){
if(t->a==c){
t->count++;
break;
}
t=t->next;
}
if(!t){
t=x;
while(t->next)t=t->next;
t->next=makeNode(c);
}return x;
}
int main()
{
list a=NULL;
char s[60];
int i;
printf ("enter the string:");
scanf ("%[^\n]",s);
for(i=0;s[i];i++)
{
a=insert(a,s[i]);
}
printf ("The frequency of each characters in the string is\n");
while(a){
printf("%c-%d\n",a->a,a->count);
a=a->next;
}
return 0;
}
Output:
5.A number x is available as x1 and x2 where x1 and x2 in between 0 and 999. Waitage of x1
is x1*10^3 and x2 is x2*10^0.
6.Display all value available in the tree along with the height.
#include<stdio.h>
#include<stdlib.h>
return(node);
}
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
getchar();
return 0;
}
Output:
Height of tree is 3
05.09.2019
1. Merge Sort.
#include<stdio.h>
#include<limits.h>
#include<time.h>
int main()
{
int a[30],n,i;
double time;
clock_t s,e;
s=clock();
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
Output:
Enter no of elements:5
Enter the elements:23
12
76
54
89
time=9.771000
2. Quick sort
#include<stdio.h>
#include<time.h>
void quicksort(int number[25],int right,int left){
int i, j, pivot, temp;
if(right<left){
pivot=right;
i=right;
j=left;
while(i<j){
while(number[i]<=number[pivot]&&i<left)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,right,j-1);
quicksort(number,j+1,left);
}
}
int main(){
int i, count, number[25];
double time;
clock_t s,e;
s=clock();
quicksort(number,0,count-1);
return 0;
}
Output:
time=11.535000
3.Radix Sort:
#include<stdio.h>
#include<time.h>
int max(int a[], int n)
{
int max = a[0], i;
for(i = 1; i < n; i++)
{
if(max < a[i])
max = a[i];
}
return max;
}
void RadixSort(int a[], int n)
{
int bucket[10][10], bucket_cnt[10];
int i, j, k, r, NOP=0, divisor=1, lar, pass;
lar=max(a, n);
printf("\n The largest element is : %d\n",lar);
while(lar > 0)
{
NOP++;
lar/=10;
}
for(pass = 0; pass < NOP; pass++)
{
for(i = 0; i < 10; i++)
{
bucket_cnt[i] = 0;
}
for(i = 0; i < n; i++)
{
r = (a[i] / divisor) % 10;
bucket[r][bucket_cnt[r]] = a[i];
bucket_cnt[r] += 1;
}
i = 0;
for(k = 0; k < 10; k++)
{
for(j = 0; j < bucket_cnt[k]; j++)
{
a[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
printf("\n Numbers after completing pass %d :\n",pass+1);
for(i = 0; i < n; i++)
printf(" %d",a[i]);
printf("\n");
}
}
int main()
{
int i, n, a[10];
double time;
clock_t s,e;
s=clock();
printf(" Enter the elements to be sorted?: ");
scanf("%d",&n);
printf("\n Enter %d numbers:\n",n);
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
RadixSort(a,n);
printf("\n Sorted numbers:\n");
for(i = 0; i < n; i++)
printf(" %d",a[i]);
printf("\n");
e=clock();
time=(double)(e-s)/CLOCKS_PER_SEC;
printf("\n\ntime=%f",time);
return 0;
}
Output:
Enter 4 numbers:
65
321
67
89
Sorted numbers:
65 67 89 321
time=10.244000
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<limits.h>
#include<time.h>
#define bound 5
int** multiply(int** a,int** b,int x,int y)
{
int i;
int **sum;
sum=(int**)malloc(sizeof(int*)*2);
for(i=0;i<2;i++)
sum[i]=(int*)malloc(sizeof(int)*2);
sum[0][0]= a[x][y]*b[x][y] + a[x][y+1]*b[x+1][y];
sum[0][1]= a[x][y]*b[x][y+1] + a[x][y+1]*b[x+1][y+1];
sum[1][0]= a[x+1][y]*b[x][y] + a[x+1][y+1]*b[x+1][y];
sum[1][1]= a[x+1][y]*b[x][y+1] + a[x+1][y+1]*b[x+1][y+1];
return sum;
}
int** add(int** a,int** b,int n)
{
int** sum,i,j;
sum=(int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++)
sum[i]=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
sum[i][j]=a[i][j]+b[i][j];
}
return sum;
}
int** subtract(int** a,int** b,int n)
{
int** diff,i,j;
diff=(int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++)
diff[i]=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
diff[i][j]=a[i][j]-b[i][j];
}
return diff;
}
void copy(int** a,int** b,int x,int y,int x1,int y1)
{
int i,j,k=0,l=0;
for(i=x;i<x1;i++)
{
l=0;
for(j=y;j<y1;j++)
b[k][l++]=a[i][j];
k++;
}
}
int** create(int** a,int n)
{
int i;
a=(int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++)
a[i]=(int*)malloc(sizeof(int)*n);
return a;
}
void print(int ** a,int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
printf("\n");
}
}
int** strassen(int** m1,int** m2,int n,int x,int y)
{
if(n<=2)
return multiply(m1,m2,x,y);
int ** a=create(a,n/2),** b=create(b,n/2),** c=create(c,n/2),** d=create(d,n/2),**
e=create(e,n/2),** f=create(f,n/2),** g=create(g,n/2),** h=create(h,n/2);
copy(m1,a,x,y,n/2,n/2);
copy(m1,b,x,y+n/2,n/2,n);
copy(m1,c,x+n/2,y,n,n/2);
copy(m1,d,x+(n/2),y+(n/2),n,n);
copy(m2,e,x,y,n/2,n/2);
copy(m2,f,x,y+n/2,n/2,n);
copy(m2,g,x+n/2,y,n,n/2);
copy(m2,h,x+(n/2),y+(n/2),n,n);
int ** p1=create(p1,n/2),** p2=create(p2,n/2),** p3=create(p3,n/2),**
p4=create(p4,n/2),** p5=create(p5,n/2),** p6=create(p6,n/2),** p7=create(p7,n/2);
p1=strassen(a,subtract(f,h,n/2),n/2,0,0);
p2=strassen(add(a,b,n/2),h,n/2,0,0);
p3=strassen(add(c,d,n/2),e,n/2,0,0);
p4=strassen(d,subtract(g,e,n/2),n/2,0,0);
p5=strassen(add(a,d,n/2),add(e,h,n/2),n/2,0,0);
p6=strassen(subtract(b,d,n/2),add(g,h,n/2),n/2,0,0);
p7=strassen(subtract(a,c,n/2),add(e,f,n/2),n/2,0,0);
int** result=(int**)malloc(sizeof(int*)*n),i;
for(i=0;i<n;i++)
result[i]=(int*)malloc(sizeof(int*)*n);
int** c1=create(c1,n/2),** c2=create(c2,n/2),** c3=create(c3,n/2),** c4=create(c4,n/2);
int j;
for(i=0;i<n/2;i++)
for(j=0;j<n/2;j++)
c1[i][j]=p5[i][j]+p4[i][j]-p2[i][j]+p6[i][j];
for(i=0;i<n/2;i++)
for(j=0;j<n/2;j++)
c2[i][j]=p1[i][j]+p2[i][j];
for(i=0;i<n/2;i++)
for(j=0;j<n/2;j++)
c3[i][j]=p3[i][j]+p4[i][j];
for(i=0;i<n/2;i++)
for(j=0;j<n/2;j++)
c4[i][j]=p1[i][j]+p5[i][j]-p3[i][j]-p7[i][j];
for(i=0;i<n/2;i++)
for(j=0;j<n/2;j++)
result[i][j]=c1[i][j];
for(i=0;i<n/2;i++)
{
int k=0;
for(j=n/2;j<n;j++)
result[i][j]=c2[i][k++];
}
int k=0;
for(i=n/2;i<n;i++)
{
for(j=0;j<n/2;j++)
result[i][j]=c3[k][j];
k++;
}
k=0;
for(i=n/2;i<n;i++)
{
int l=0;
for(j=n/2;j<n;j++)
result[i][j]=c4[k][l++];
k++;
}
return result;
}
int main()
{
int** a,**b,**c,i,j,n=8;
double time;
clock_t s,e;
s=clock();
a=(int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++)
a[i]=(int*)malloc(sizeof(int)*n);
b=(int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++)
b[i]=(int*)malloc(sizeof(int)*n);
c=(int**)malloc(sizeof(int*)*n);
for(i=0;i<n;i++)
c[i]=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
a[i][j]=rand()%bound,b[i][j]=rand()%bound;
c=strassen(a,b,n,0,0);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%3d ",a[i][j]);
printf("\n");
}
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%3d ",b[i][j]);
printf("\n");
}
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%3d ",c[i][j]);
printf("\n");
}
e=clock();
time=(double)(e-s)/CLOCKS_PER_SEC;
printf("\n\ntime=%f",time);
return 0;
}
Output:
1 4 2 0 3 2 3 1
3 2 2 2 3 4 1 0
3 0 4 1 0 0 1 3
4 4 1 1 1 4 1 2
3 4 3 4 4 2 3 1
4 0 2 2 1 1 0 0
2 0 4 3 4 0 2 1
2 1 4 3 2 1 0 1
3 2 4 4 1 3 4 1
3 4 2 4 1 3 0 2
1 2 3 0 4 0 1 3
3 4 0 1 3 4 3 3
2 1 0 3 0 1 1 0
2 4 3 2 0 1 2 4
37 33 51 48 27 27 23 25
28 31 28 32 18 20 16 24
33 29 40 36 32 22 24 26
39 37 41 38 44 31 30 32
29 25 39 38 17 19 29 21
41 37 47 43 42 29 30 34
53 49 63 62 49 37 33 40
24 18 31 29 27 16 20 16
time=0.018000
#include <stdio.h>
#include<time.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
double time;
clock_t s,e;
s=clock();
int first[10][10], second[10][10], multiply[10][10];
if (n != p)
printf("The matrices can't be multiplied with each other.\n");
else
{
printf("Enter elements of second matrix\n");
multiply[c][d] = sum;
sum = 0;
}
}
printf("\n");
}
}
e=clock();
time=(double)(e-s)/CLOCKS_PER_SEC;
printf("\n\ntime=%f",time);
return 0;
}
Output:
Enter number of rows and columns of first matrix
4
4
Enter elements of first matrix
2346
9 34 5 7
1256
0 45 2 7
Enter number of rows and columns of second matrix
4
4
Enter elements of second matrix
45 2 12 89
66 82 1 6
4 90 24 64
25 78 2 8
Product of the matrices:
454 1078 135 500
2844 3802 276 1381
347 1084 146 469
3153 4416 107 454
time=59.339000
6.Sort an array of structures with name,gpa(float) in decreasing oreder using merge sort
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define MAX 50
typedef struct {
int regno;
char name[20];
float gpa;
}student;
void mt(student *a,int l,int mid,int u)
{
Output:
After sorting
time=0.008000
12.09.2019
1. Knapsack Algorithm.
#include<stdio.h>
#include<stdlib.h>
int main()
{
float weight[25],profit[25],ratio[25],totalvalue,temp,capacity,amount;
int n,i,j;
printf("Enter the no.of items :");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("Enter weight & profit [%d] :\n",i);
scanf("%f %f",&weight[i],&profit[i]);
}
printf("Enter the capacity:\n");
scanf("%f",&capacity);
for(i=0;i<n;i++)
ratio[i]=profit[i]/weight[i];
for (i=0;i<n;i++)
for (j=i+1;j<n;j++)
if (ratio[i]<ratio[j])
{
temp=ratio[j];
ratio[j]=ratio[i];
ratio[i]=temp;
temp=weight[j];
weight[j]=weight[i];
weight[i]=temp;
temp=profit[j];
profit[j]=profit[i];
profit[i]=temp;
}
for (i=0;i<n;i++)
{
if (weight[i]>capacity)
break;
else
{
totalvalue=totalvalue+profit[i];
capacity=capacity-weight[i];
}
}
if(i<n)
totalvalue=totalvalue+(ratio[i]*capacity);
printf("\nThe max value is:%f\n",totalvalue);
return 0;
}
Output:
#include<stdio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,z=1,c,d,l,s,w;
int min,minicost=0,cost[200][200],parent[200],visited[200]={0};
int find(int);
int uni(int,int);
int main()
{
printf("Kruskal's algorithm");
printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
cost[i][j]=rand()%10;
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%2d\n",cost[i][j]);
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(cost[i][j]==0)
cost[i][j]=700;
}
}
printf("Prim's algorithm\n");
printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\n");
for(k=1;k<=n;k++)
{
for(l=1;l<=n;l++)
{
cost[k][l]=rand()%10;
}
}
for(k=1;k<=n;k++)
{
for(l=1;l<=n;l++)
{
printf("%2d\n",cost[k][l]);
}
}
visited[1]=1;
printf("\n");
while(z < n)
{
for(k=1,min=700;k<=n;k++)
for(l=1;l<=n;l++)
if(cost[k][l]< min)
if(visited[k]!=0)
{
min=cost[k][l];
c=w=k;
d=s=l;
}
if(visited[w]==0 || visited[s]==0)
{
printf("\n Edge %d:(%d %d) Cost:%d",z++,c,d,min);
minicost+=min;
visited[d]=1;
}
cost[c][d]=cost[d][c]=700;
}
printf("\n Minimun Cost=%d\n",minicost);
return 0;
}
int find(int i)
{
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}
Output:
Kruskal's algorithm
Enter the no. of vertices:7
Minimum cost = 6
Prim's algorithm
Minimun Cost=6
3. Djikstra’s Algorithm.
#include<stdio.h>
#include<time.h>
#define size 500
#define def 10
void sp(int d[def][def],int n,int beginnode);
int main()
{
int d[def][def],i,j,n,u;
double time;
clock_t s,e;
s=clock();
printf("Enter no. of vertices:");
scanf("%d",&n);
printf("\nEnter the adjacency matrix:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
d[i][j]=rand()%100;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",d[i][j]);
}
printf("\n");
}
printf("\nEnter the beginning node:");
scanf("%d",&u);
sp(d,n,u);
e=clock();
time=(double)(e-s)/CLOCKS_PER_SEC;
printf("\n\ntime=%f",time);
return 0;
}
void sp(int d[def][def],int n,int beginnode)
{
int cost[def][def],distance[def],prev[def];
int visited[def],count,mindistance,nextnode,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(d[i][j]==0)
cost[i][j]=size;
else
cost[i][j]=d[i][j];
for(i=0;i<n;i++)
{
distance[i]=cost[beginnode][i];
prev[i]=beginnode;
visited[i]=0;
}
distance[beginnode]=0;
visited[beginnode]=1;
count=1;
while(count<n-1)
{
mindistance=size;
for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
prev[i]=nextnode;
}
count++;
}
for(i=0;i<n;i++)
if(i!=beginnode)
{
printf("\ndistance of node%d=%d",i,distance[i]);
printf("\n\npath=%d",i);
j=i;
do
{
j=prev[j];
printf("--->%d",j);
}while(j!=beginnode);
}
}
Output:
distance of node0=12
path=0--->5
distance of node1=37
path=1--->3--->5
distance of node2=46
path=2--->0--->5
distance of node3=35
path=3--->5
distance of node4=52
path=4--->6--->5
distance of node6=11
path=6--->5
time=9.764000
19.09.2019
1. Heap Sort.
#include<stdio.h>
void create(int []);
void heap_sort(int [],int);
void main()
{
int heap[30],n,i,last,temp;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("\nEnter elements:");
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);
heap[0]=n;
create(heap);
while(heap[0] > 1)
{
last=heap[0];
temp=heap[1];
heap[1]=heap[last];
heap[last]=temp;
heap[0]--;
heap_sort(heap,1);
}
printf("\n\tArray after heap sort:\n");
for(i=1;i<=n;i++)
printf("%d ",heap[i]);
}
void create(int heap[])
{
int i,n;
n=heap[0];
for(i=n/2;i>=1;i--)
heap_sort(heap,i);
}
void heap_sort(int heap[],int i)
{
int j,temp,n,flag=1;
n=heap[0];
while(2*i<=n && flag==1)
{
j=2*i;
if(j+1<=n && heap[j+1] > heap[j])
j=j+1;
if(heap[i] > heap[j])
flag=0;
else
{
temp=heap[i];
heap[i]=heap[j];
heap[j]=temp;
i=j;
}
}
}
Output:
Enter elements:356
34
61
97
#include <stdio.h>
#include<limits.h>
#define MAX 999999999
long int m[20][20];
int s[20][20];
int p[20],i,j,n;
void multiply(void)
{
long int q;
int k;
for(i=n;i>0;i--)
{
for(j=i;j<=n;j++)
{
if(i==j)
m[i][j]=0;
else
{
for(k=i;k<j;k++)
{
q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
if(q<m[i][j])
{
m[i][j]=q;
s[i][j]=k;
}
}
}
}
}
}
void main()
{
int k;
printf("Enter the no. of elements: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
m[i][i]=0;
m[i][j]=MAX;
s[i][j]=0;
}
printf("\nEnter the dimensions: \n");
for(k=0;k<=n;k++)
{
printf("P%d: ",k);
scanf("%d",&p[k]);
}
multiply();
printf("\nCost Matrix M:\n");
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
printf("m[%d][%d]: %ld\n",i,j,m[i][j]);
printf("\n\tMinimum number of multiplications is : %d ",
MCO(p, 1, n));
}
Output:
Cost Matrix M:
m[1][1]: 0
m[1][2]: 18816
m[1][3]: 52332
m[1][4]: 126936
m[1][5]: 67329
m[2][2]: 0
m[2][3]: 21888
m[2][4]: 90780
m[2][5]: 53217
m[3][3]: 0
m[3][4]: 58140
m[3][5]: 49761
m[4][4]: 0
m[4][5]: 43605
m[5][5]: 0
Output:
Enter 1st sequence:ancbsp
Enter 2nd sequence:hdamcrb
26.09.2019
1.Priority queue
#include<stdio.h>
int size=20;
int heap_size=0;
const int INF=100000;
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
int get_right_child(int A[],int i)
{
if((((2*i)+1<size)&&(i>=1)))
return (2*i)+1;
return -1;
}
int get_left_child(int A[],int i)
{
if((((2*i)<size)&&(i>=1)))
return 2*i;
return -1;
}
int get_parent(int A[],int i)
{
if((i>1)&&(i<size))
{
return i/2;
}
return -1;
}
void max_heapify(int A[],int i)
{
int left_child_i=get_left_child(A,i);
int right_child_i=get_right_child(A,i);
int largest=i;
if((left_child_i<=heap_size) && (left_child_i>0))
{
if(A[left_child_i]>A[largest])
{
largest=left_child_i;
}
}
if((right_child_i<=heap_size && (right_child_i>0)))
{
if(A[right_child_i]>A[largest])
{
largest=right_child_i;
}
}
if(largest!=i)
{
swap(&A[i],&A[largest]);
max_heapify(A,largest);
}
}
void build_max_heap(int A[])
{
int i;
for(i=heap_size/2;i>=1;i++)
{
max_heapify(A,i);
}
}
int maximum(int A[])
{
return A[1];
}
int extract_max(int A[])
{
int maxm=A[1];
A[1]=A[heap_size];
heap_size--;
max_heapify(A,1);
return maxm;
}
void increase_key(int A[],int i,int key)
{
A[i]=key;
while((i>1)&&(A[get_parent(A,i)]<A[i]))
{
swap(&A[i],&A[get_parent(A,i)]);
i=get_parent(A,i);
}
}
void decrease_key(int A[],int i,int key)
{
A[i]=key;
max_heapify(A,i);
}
void insert(int A[],int key)
{
heap_size++;
A[heap_size]= -1*INF;
increase_key(A,heap_size,key);
}
void print_heap(int A[])
{
int i;
for(i=1;i<=heap_size;i++)
{
printf("%4d",A[i]);
}
printf("\n");
}
int main()
{
int n;
printf("\n enter the no. of elements to be in the array:");
scanf("%d",&n);
int arr[size];
int i,data;
for(i=0;i<n;i++)
{
scanf("%d",&data);
insert(arr,data);
}
printf("\n the heap is:\n");
print_heap(arr);
printf("%d\n\n",extract_max(arr));
return 0;
}
Output:
77
77 43 18 6 32
for(i=1;i<=n;++i)
{
printf("\n\n%d",i);
for(j=1;j<=n;++j)
{
if(board[i]==j)
printf("\tQ");
else
printf("\t*");
}
}
}
int place(int row,int column)
{
int i;
for(i=1;i<=row-1;++i)
{
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
}
return 1;
}
void queen(int row,int n)
{
int column;
for(column=1;column<=n;++column)
{
if(place(row,column))
{
board[row]=column;
if(row==n)
print(n);
else
queen(row+1,n);
}
}
}
Output:
Solution 1:
1 2 3 4
1 * Q * *
2 * * * Q
3 Q * * *
4 * * Q *
Solution 2:
1 2 3 4
1 * * Q *
2 Q * * *
3 * * * Q
4 * Q * *
3. Floyd’s Algorithm.
#include<stdio.h>
int min(int,int);
void floyds(int p[10][10],int n)
{
int i,j,k;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if(i==j)
p[i][j]=0;
else
p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}
int min(int a,int b)
{
if(a<b)
return(a);
else
return(b);
}
void main()
{
int p[10][10],w,n,e,u,v,i,j;;
printf("\n Enter the no. of vertices:");
scanf("%d",&n);
printf("\n Enter the no. of edges:\n");
scanf("%d",&e);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
p[i][j]=999;
}
for(i=1;i<=e;i++)
{
printf("\n Enter the end vertices of edge%d with its weight \n",i);
scanf("%d%d%d",&u,&v,&w);
p[u][v]=w;
}
printf("\n Matrix of input data:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d \t",p[i][j]);
printf("\n");
}
floyds(p,n);
printf("\n Transitive closure:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
printf("%d \t",p[i][j]);
printf("\n");
}
printf("\n The shortest paths are:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i!=j)
printf("\n <%d,%d>=%d",i,j,p[i][j]);
}
}
Output:
Transitive closure:
0 5 4 999
7 0 11 999
8 1 0 999
999 999 999 0
<1,2>=5
<1,3>=4
<1,4>=999
<2,1>=7
<2,3>=11
<2,4>=999
<3,1>=8
<3,2>=1
<3,4>=999
<4,1>=999
<4,2>=999
<4,3>=999
4.Sum of subsets.
#include<stdio.h>
#define TRUE 1
#define FALSE 0
int inc[50],w[50],sum,n;
int promising(int i,int wt,int total)
{
return(((wt+total)>=sum)&&((wt==sum)||(wt+w[i+1]<=sum)));
}
void main()
{
int i,j,n,temp,total=0;
printf("\n Enter how many numbers:\n");
scanf("%d",&n);
printf("\n Enter %d numbers to the set:\n",n);
for (i=0;i<n;i++) {
scanf("%d",&w[i]);
total+=w[i];
}
printf("\n Input the sum value to create sub set:\n");
scanf("%d",&sum);
for (i=0;i<=n;i++)
for (j=0;j<n-1;j++)
if(w[j]>w[j+1])
{
temp=w[j];
w[j]=w[j+1];
w[j+1]=temp;
}
printf("\n The given %d numbers in ascending order:\n",n);
for (i=0;i<n;i++)
printf("%d \t",w[i]);
if((total<sum))
printf("\n Subset construction is not possible"); else
{
for (i=0;i<n;i++)
inc[i]=0;
printf("\n The solution is:\n");
sumset(-1,0,total);
}
}
void sumset(int i,int wt,int total)
{
int j;
if(promising(i,wt,total))
{
if(wt==sum)
{
printf("\n{\t");
for (j=0;j<=i;j++)
if(inc[j])
printf("%d\t",w[j]);
printf("}\n");
} else
{
inc[i+1]=TRUE;
sumset(i+1,wt+w[i+1],total-w[i+1]);
inc[i+1]=FALSE;
sumset(i+1,wt,total-w[i+1]);
}
}
}
Output:
{ 34 54 }
#include <stdio.h>
int main()
{
int num, guess, tries = 0;
num = rand() % 100;
do
{
printf("Enter a guess between 1 and 100 : ");
scanf("%d", &guess);
tries++;
if (guess > num)
{
printf("Guess is too high\n\n");
}
else if (guess < num)
{
printf("Guess is too low\n\n");
}
else
{
printf("\nCorrect,You got it in %d guesses\n", tries);
}
}while (guess != num);
return 0;
}
Output:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<limits.h>
#include<time.h>
typedef struct{int x;int y;}comp;
void multiply(comp** a,comp ** b,comp ** c,int n)
{
int i,j,k;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i][j].x=0;
c[i][j].y=0;
for(k=0;k<n;k++)
{
c[i][j].x+=a[i][k].x*b[k][j].x;
c[i][j].y+=a[i][k].y*b[k][j].y;
}
}
}
int main()
{
comp **a,**b,**c;int i,j,n;
double time;
clock_t s,e;
s=clock();
printf("enter no. of elements");
scanf("%d",&n);
a=(comp**)malloc(sizeof(comp*)*n);
for(i=0;i<n;i++)
a[i]=(comp*)malloc(sizeof(comp)*n);
b=(comp**)malloc(sizeof(comp*)*n);
for(i=0;i<n;i++)
b[i]=(comp*)malloc(sizeof(comp)*n);
c=(comp**)malloc(sizeof(comp*)*n);
for(i=0;i<n;i++)
c[i]=(comp *)malloc(sizeof(comp)*n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
a[i][j].x=rand()%10;
a[i][j].y=rand()%10;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
b[i][j].x=rand()%10;
b[i][j].y=rand()%10;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d+i%d\t ",a[i][j].x,a[i][j].y);
}
printf("\n");
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d+i%d ",b[i][j].x,b[i][j].y);
}
printf("\n");
}
multiply(a,b,c,n);
printf("\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("%d+i%d ",c[i][j].x,c[i][j].y);
}
printf("\n");
}
e=clock();
time=(double)(e-s)/CLOCKS_PER_SEC;
printf("time=%f",time);
return 0;
}
Output:
6+i28 9+i35
26+i72 53+i28
time=2.797000