Академический Документы
Профессиональный Документы
Культура Документы
1. Write a program in “c “ language that accepts two matrices as input and print
their product.
Sol :
#include<stdio.h>
#include<conio.h>
int main()
{
int x[10][10],y[10][10],z[10][10],i,j,k,m,n,p,q;
clrscr();
input_mat(x,m,n);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
z[i][j]=0;
for(k=0;k<p;k++)
{
z[i][j]+=x[i][k]*y[k][j];
}
}
}
getch();
return 0;
}
if(x[i]>y[i])
return 2;
else
return 1;
}
}
int main()
{
int i,j,k;
char s[10][10],t[10];
clrscr();
for(i=0;i<3;i++)
{
for(j=i+1;j<3;j++)
{
k=strcomp(s[i],s[j]);
if(k==2)
{
t[0]='\0';
strcat(t,s[i]);
s[i][0]='\0';
strcat(s[i],s[j]);
s[j][0]='\0';
strcat(s[j],t);
}
}
}
getch();
return 0;
}
4
int main()
{
char s1[100],s2[100];
int i,j,k,f;
clrscr();
for(i=0;s1[i]!='\0';i++)
{
f=0;
for(j=0,k=i;s2[j]!='\0';j++,k++)
{
if(s1[k]!=s2[j])
{
f++;
break;
}
}
if(f==0)
{
printf("\n\nThe second string(S2) presents in first string(S1) at location\n Strat=
%d\nEnd=%d",i+1,i+j);
}
}
printf("\nend");
getch();
return 0;
int main()
{
char s1[100],s2[100];
int i,j,k,f;
clrscr();
printf("\nEnter the first strig: ");
gets(s1);
printf("\nEnter the second strig: ");
gets(s2);
for(i=strlen(s1),j=0;s2[j]!='\0';i++,j++)
{
s1[i]=s2[j];
}
printf("\nAfter concatenation the first string becomes: %s",s1);
getch();
return 0;
}
5. Write a program in “c” language to accept atwo singly linked lists A and B as
input. Now, print a singly linked list that consists of only those elements,
Which are common to both A and B
Sol :
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int d;
struct node *next;
};
struct node *create()
{
int i,n;
struct node *f,*x,*l;
printf("\nEnter no. of node to be created: ");
scanf("%d",&n);
x=(node *)malloc(sizeof(node));
printf("\nEnter a no. ");
scanf("%d",&x->d);
x->next=NULL;
f=l=x;
for(i=2;i<=n;i++)
{
x=(node *)malloc(sizeof(node));
printf("\nEnter a no. ");
scanf("%d",&x->d);
x->next=NULL;
l->next=x;
l=x;
6
}
return f;
}
int main()
{
clrscr();
struct node *a,*b;
a=create();
b=create();
printf("\nThe first linked list is: ");
display(a);
printf("\nThe second linked list is: ");
display(B);
commonNode(a,B);
getch();
return 0;
}
7
struct node
{
int d;
struct node *next;
};
for(;x!=NULL;x=x->next)
{
for(q=x->next;q!=NULL;q=q->next)
{
if(x->d>q->d)
{
t=x->d;
x->d=q->d;
q->d=t;
}
}
}
return p;
}
int main()
{
int f=0;
struct node *a,*x,*p,*b;
clrscr();
a=create();
printf("\nThe first linked list is: ");
display(a);
a=sortList(a);
x=(node *)malloc(sizeof(node));
printf("\nEnter a number to be inserted im the linked list: ");
scanf("%d",&x->d);
x->next=NULL;
p=a;
if(p->next==NULL)
if(p->d<x->d)
p->next=x;
else
{
x->next=p;
a=x;
}
else
{
if(p->d>x->d)
{
x->next=p;
a=x;
}
9
else
{
b=a->next;
while(b!=NULL)
{
if((p->d < x->d) && (b->d > x->d))
{
x->next=b;
p->next=x;
f++;
break;
}
p=b;
b=b->next;
}
if(f==0)
{
p->next=x;
}
}
}
getch();
return 0;
}
struct node
{
int d;
struct node *next;
};
f=l=x;
for(i=2;i<=n;i++)
{
x=(node *)malloc(sizeof(node));
printf("\nEnter a no. ");
scanf("%d",&x->d);
x->next=NULL;
l->next=x;
l=x;
}
return f;
}
int main()
{
int f=0;
struct node *a,*x,*p,*b;
clrscr();
a=create();
printf("\nThe first linked list is: ");
display(a);
11
a=sortList(a);
x=(node *)malloc(sizeof(node));
printf("\nEnter a number to be inserted im the linked list: ");
scanf("%d",&x->d);
x->next=NULL;
p=a;
if(p->next==NULL)
if(p->d<x->d)
p->next=x;
else
{
x->next=p;
a=x;
}
else
{
if(p->d>x->d)
{
x->next=p;
a=x;
}
else
{
b=a->next;
while(b!=NULL)
{
if((p->d < x->d) && (b->d > x->d))
{
x->next=b;
p->next=x;
f++;
break;
}
p=b;
b=b->next;
}
if(f==0)
{
p->next=x;
}
}
}
getch();
return 0;
}
int main()
{
char *s;
int len;
clrscr();
len=strlen(s);
getch();
return 0;
}
struct node
{
13
int d;
struct node *next;
};
for(i=1;i<=n;i++)
{
x=(node *)malloc(sizeof(node));
printf("Enter a no. ");
scanf("%d",&x->d);
x->next=NULL;
if(f==NULL)
f=r=x;
else
{
r->next=x;
r=x;
}
}
return f;
}
{
q->next=start;
start=q;
}
p=p->next;
}
return start;
}
x=(node *)malloc(sizeof(node));
printf("Enter a no. ");
scanf("%d",&x->d);
x->next=a;
a=x;
return a;
}
x=(node *)malloc(sizeof(node));
printf("Enter a no. ");
scanf("%d",&x->d);
x->next=NULL;
if(a==NULL)
{
a=x;
return a;
}
p=a;
while(p->next!=NULL)
p=p->next;
p->next=x;
return a;
}
struct node *del_r(struct node *a)
{
int item;
struct node *x,*p;
if(a==NULL)
{
printf("Underflow");
getch();
return a;
15
}
else if(a->next==NULL)
{
item=a->d;
a=NULL;
}
else
{
p=a;
while((p->next)->next!=NULL)
p=p->next;
p->next=NULL;
item=(p->next)->d;
//free((p->next)->d);
}
if(a==NULL)
{
printf("Underflow");
getch();
return a;
}
else if(a->next==NULL)
{
item=a->d;
a=NULL;
}
else
{
p=a;
a=a->next;
p->next=NULL;
item=p->d;
}
printf("%d is deleted from the rear end oif the queue",item);
getch();
return a;
}
int main()
16
{
struct node *a,*rev,*x;
int ch;
clrscr();
a=createQ();
while(1)
{
clrscr();;
gotoxy(35,3);
printf("MENU");
gotoxy(25,5);
printf("1. Insertion");
gotoxy(25,6);
printf("2. Deletion");
gotoxy(25,7);
printf("3. Display");
gotoxy(25,8);
printf("4. Exit");
gotoxy(20,15);
printf("Enetr your choice(1-4): ");
scanf("%d",&ch);
switch(ch)
{
case 1:
do
{
clrscr();
gotoxy(35,3);
printf("INSERTION MENU");
gotoxy(25,5);
printf("1. Insertion at front");
gotoxy(25,6);
printf("2. Insertion at rear");
gotoxy(25,7);
printf("3. Retrun to main menu");
gotoxy(20,15);
printf("Enetr your choice(1-3): ");
scanf("%d",&ch);
switch(ch)
{
case 1: a=ins_f(a);
break;
case 2: a=ins_r(a);
break;
case 3: break;
default: printf("\nWrong choice. Try again");
}
}while(ch!=3);
break;
17
case 2:
do
{
clrscr();
gotoxy(35,3);
printf("DELETION MENU");
gotoxy(25,5);
printf("1. deletion at front");
gotoxy(25,6);
printf("2. Deletion at rear");
gotoxy(25,7);
printf("3. Retrun to main menu");
gotoxy(20,15);
printf("Enetr your choice(1-3): ");
scanf("%d",&ch);
switch(ch)
{
case 1: a=del_f(a);
break;
case 2: a=del_r(a);
//if(a==NULL)
break;
case 3: break;
default: printf("\nWrong choice. Try again");
}
}while(ch!=3);
break;
case 3: clrscr();
gotoxy(10,15);
printf("The queue elements are: ");
display(a); getch();
break;
case 4:
exit(0);
}
}
return 0;
}
struct node
{
int d;
18
start=q;
}
p=p->next;
}
return start;
}
int main()
{
struct node *a,*rev;
clrscr();
a=create();
printf("\nThe element of the queue is: ");
display(a);
rev=ReverseQ(a);
getch();
return 0;
}
11. Write a program in “c” language to implement a queue using two stacks.
Sol :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int d;
struct node *next;
};
x=(node *)malloc(sizeof(node));
printf("Enter a no. ");
scanf("%d",&x->d);
x->next=NULL;
if(f==NULL)
f=r=x;
else
{
20
r->next=x;
r=x;
}
}
return f;
}
int main()
{
int ch;
struct node *r,*f;
r=f=NULL;
clrscr();
while(1)
{
printf("\n\n\t\tMENU");
printf("\n\n\t1. Insert");
printf("\n\t2. Delete");
printf("\n\t3. Display");
printf("\n\t4. Exit");
printf("\n\nEnter your choice(1-4): ");
fflush(stdin);
scanf("%d",&ch);
switch(ch)
{
case 1: insert(
break;
case 2:
break;
case 3:
break;
case 4: exit(0);
break;
default: printf("\n\nWrong input. Try again!!!");
}
}
getch();
return 0;
}
21
12. Write a program in “c” language for the creation of a binary tree. Also,
provide for insertion and deletion operations.
Sol :
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include<ctype.h>
struct node
{
int d;
struct node *l,*r;
};
int main()
{
struct node *root=NULL,*p,*q;
char ch='y';
22
clrscr();
while(tolower(ch)=='y')
{
printf("\nInorder is : ");
inorder(root);
23
printf("\nPreorder is : ");
preorder(root);
printf("\nPostorder is : ");
postorder(root);
getch();
return 0;
}
13. Write a program in “c” language for pre-order, post-order and in-
ordertraversals of a Binary tree. Don’t use Recursion.
Sol :
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include<ctype.h>
struct node
{
int d;
struct node *l,*r;
};
int main()
{
struct node *root=NULL,*p,*q;
char ch='y';
25
clrscr();
while(tolower(ch)=='y')
{
printf("\nInorder is : ");
inorder(root);
26
/*printf("\nPreorder is : ");
preorder(root);
printf("\nPostorder is : ");
postorder(root);*/
getch();
return 0;
}
14. Write a program in “c” language to accept a tree as input and converts it into
binary tree. Print the resultant binary tree.
Sol :
#include<stdio.h>
#include<conio.h>
struct tree
{
int d;
struct tree *l;
struct tree *r;
};
return x;
if(n<=h->d)
h->l=ins_tree(h->l,n)
else
h->r=ins_tree(h->r,n);
return h;
int main()
{
clrscr();
getch();
return 0;
}
#define FALSE 0
#define TRUE 1
struct AVLNode
{
int data ;
int balfact ;
struct AVLNode *left ;
struct AVLNode *right ;
};
void main( )
{
struct AVLNode *avl = NULL ;
int h ;
28
deltree ( avl ) ;
getch( ) ;
}
if ( !root )
{
root = ( struct AVLNode * ) malloc ( sizeof ( struct AVLNode ) ) ;
root -> data = data ;
root -> left = NULL ;
root -> right = NULL ;
root -> balfact = 0 ;
*h = TRUE ;
return ( root ) ;
}
case 0:
root -> balfact = 1 ;
break ;
case -1:
root -> balfact = 0 ;
*h = FALSE ;
}
}
}
case 0:
root -> balfact = -1 ;
break;
case -1:
node1 = root -> right ;
if ( node1 -> balfact == -1 )
{
printf ( "\nLeft rotation along %d.", root -> data ) ;
root -> right = node1 -> left ;
node1 -> left = root ;
root -> balfact = 0 ;
root = node1 ;
}
else
{
printf ( "\nDouble rotation, right along %d",
node1 -> data ) ;
node2 = node1 -> left ;
node1 -> left = node2 -> right ;
node2 -> right = node1 ;
printf ( " then left along %d.\n", root -> data ) ;
root -> right = node2 -> left ;
node2 -> left = root ;
if ( !root )
{
printf ( "\nNo such data." ) ;
return ( root ) ;
31
}
else
{
if ( data < root -> data )
{
root -> left = deldata ( root -> left, data, h ) ;
if ( *h )
root = balright ( root, h ) ;
}
else
{
if ( data > root -> data )
{
root -> right = deldata ( root -> right, data, h ) ;
if ( *h )
root = balleft ( root, h ) ;
}
else
{
node = root ;
if ( node -> right == NULL )
{
root = node -> left ;
*h = TRUE ;
free ( node ) ;
}
else
{
if ( node -> left == NULL )
{
root = node -> right ;
*h = TRUE ;
free ( node ) ;
}
else
{
node -> right = del ( node -> right, node, h ) ;
if ( *h )
root = balleft ( root, h ) ;
}
}
}
}
}
return ( root ) ;
}
struct AVLNode * del ( struct AVLNode *succ, struct AVLNode *node, int *h )
{
struct AVLNode *temp = succ ;
if ( succ -> left != NULL )
{
32
case 0:
root -> balfact = -1 ;
*h = FALSE ;
break;
case -1:
node1 = root -> right ;
if ( node1 -> balfact <= 0 )
{
printf ( "\nLeft rotation along %d.", root -> data ) ;
root -> right = node1 -> left ;
node1 -> left = root ;
if ( node1 -> balfact == 0 )
{
root -> balfact = -1 ;
node1 -> balfact = 1 ;
*h = FALSE ;
}
else
{
root -> balfact = node1 -> balfact = 0 ;
}
root = node1 ;
}
else
{
33
case 0:
root -> balfact = 1 ;
*h = FALSE ;
break ;
case 1:
node1 = root -> left ;
if ( node1 -> balfact >= 0 )
{
printf ( "\nRight rotation along %d.", root -> data ) ;
root -> left = node1 -> right ;
node1 -> right = root ;
if ( node1 -> balfact == 0 )
{
root -> balfact = 1 ;
node1 -> balfact = -1 ;
*h = FALSE ;
}
34
else
{
root -> balfact = node1 -> balfact = 0 ;
}
root = node1 ;
}
else
{
printf ( "\nDouble rotation, left along %d", node1 -> data ) ;
node2 = node1 -> right ;
node1 -> right = node2 -> left ;
node2 -> left = node1 ;
printf ( " then right along %d.\n", root -> data ) ;
root -> left = node2 -> right ;
node2 -> right = root ;
k=a;
for(l=0;l<ltidx;++l)A[k++]=leftarr[l];
A[k++]=pivot;
for(l=0;l<rtidx;++l)A[k++]=rtarr[l];
if(ltidx>0)quicksort(a,a+ltidx-1);
if(rtidx>0)quicksort(b-rtidx+1,B);
}
void printarr(int a)
{
int i;
for(i=0;i<a;i++)
{
printf("%d",A[i]);
printf("\n");
}
}
main()
{
int i,s;
printf("enter the number of numbers to be entered \n");
scanf("%d",&s);
for(i=0;i<s;i++)
{
printf("enter the number \n" );
scanf("%d",&A[i]);
}
printf("array before sorting ");
printarr(s);
quicksort(0,s-1);
printf("array after sorting");
printarr(s);
}
37
#include <stdlib.h>
#include "fatal.h"
/* START*/
#ifndef _Splay_H
#define _Splay_H
struct SplayNode;
typedef struct SplayNode *SplayTree;
#endif /* _Splay_H */
/* END */
/*fatal.h*/
#include <stdio.h>
#include <stdlib.h>
splay.c
#include "splay.h"
#include <stdlib.h>
#include "fatal.h"
struct SplayNode
{
ElementType Element;
SplayTree Left;
SplayTree Right;
};
38
SplayTree
Initialize( void )
{
if( NullNode == NULL )
{
NullNode = malloc( sizeof( struct SplayNode ) );
if( NullNode == NULL )
FatalError( "Out of space!!!" );
NullNode->Left = NullNode->Right = NullNode;
}
return NullNode;
}
SplayTree
MakeEmpty( SplayTree T )
{
if( T != NullNode )
{
MakeEmpty( T->Left );
MakeEmpty( T->Right );
free( T );
}
return NullNode;
}
void
PrintTree( SplayTree T )
{
if( T != NullNode )
{
PrintTree( T->Left );
printf( "%d ", T->Element );
PrintTree( T->Right );
}
}
SplayTree
Find( ElementType X, SplayTree T )
{
return Splay( X, T );
}
SplayTree
FindMin( SplayTree T )
{
return Splay( NegInfinity, T );
}
39
SplayTree
FindMax( SplayTree T )
{
return Splay( Infinity, T );
}
static Position
SingleRotateWithLeft( Position K2 )
{
Position K1;
K1 = K2->Left;
K2->Left = K1->Right;
K1->Right = K2;
static Position
SingleRotateWithRight( Position K1 )
{
Position K2;
K2 = K1->Right;
K1->Right = K2->Left;
K2->Left = K1;
/* START: fig12_6.txt */
/* Top-down splay procedure, */
/* not requiring Item to be in tree */
SplayTree
Splay( ElementType Item, Position X )
{
static struct SplayNode Header;
Position LeftTreeMax, RightTreeMin;
/* Reassemble */
LeftTreeMax->Right = X->Left;
RightTreeMin->Left = X->Right;
X->Left = Header.Right;
X->Right = Header.Left;
return X;
}
/* END */
/* START: fig12_7.txt */
SplayTree
Insert( ElementType Item, SplayTree T )
{
static Position NewNode = NULL;
if( T == NullNode )
41
{
NewNode->Left = NewNode->Right = NullNode;
T = NewNode;
}
else
{
T = Splay( Item, T );
if( Item < T->Element )
{
NewNode->Left = T->Left;
NewNode->Right = T;
T->Left = NullNode;
T = NewNode;
}
else
if( T->Element < Item )
{
NewNode->Right = T->Right;
NewNode->Left = T;
T->Right = NullNode;
T = NewNode;
}
else
return T; /* Already in the tree */
}
/* START */
SplayTree
Remove( ElementType Item, SplayTree T )
{
Position NewTree;
if( T != NullNode )
{
T = Splay( Item, T );
if( Item == T->Element )
{
/* Found it! */
if( T->Left == NullNode )
NewTree = T->Right;
else
{
NewTree = T->Left;
NewTree = Splay( Item, NewTree );
NewTree->Right = T->Right;
}
free( T );
42
T = NewTree;
}
}
return T;
}
/* END */
ElementType
Retrieve( SplayTree T )
{
return T->Element;
}
main prog
#include "splay.h"
#include <stdio.h>
#define NumItems 500
main( )
{
SplayTree T;
SplayTree P;
int i;
int j = 0;
T = Initialize( );
T = MakeEmpty( T );
for( i = 0; i < NumItems; i++, j = ( j + 7 ) % NumItems )
T = Insert( j, T );
for( j = 0; j < 2; j++ )
for( i = 0; i < NumItems; i++ )
{
T = Find( i, T );
if( Retrieve( T ) != i )
printf( "Error1 at %d\n", i );
}
return 0;
}