You are on page 1of 56

www.mcdtu.wordpress.

com

www.mcdtu.wordpress.com 1
www.mcdtu.wordpress.com

PROGRAM 1:

WAP to find length of a string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()

{clrscr();
char string[20];
int len;
printf("Enter the string:\n");
gets(string);
len=strlen(string);
printf("\nThe length of the given string is %d",len);
getch();
}

OUTPUT
Enter the string:
Student
The length of the given string is 7

www.mcdtu.wordpress.com 2
www.mcdtu.wordpress.com

PROGRAM 2:

WAP to compare two strings.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{clrscr();
char string1[20],string2[20];
printf("\nEnter string1:\n");
gets(string1);
printf("\nEnter string2:\n");
gets(string2);
int l=strcmp(string1,string2);
if(l==0)
printf("\nThe given strings are equal");
else
printf("\nThe given strings are not equal");
getch();
}

OUTPUT
Enter string1:
What
Enter string2:
Perpendicular
The given strings are not equal

Enter string1:
Data
Enter string2:
Data
The given strings are equal

www.mcdtu.wordpress.com 3
www.mcdtu.wordpress.com

PROGRAM 3:

WAP for concatenation of two strings.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{clrscr();
char a[10],b[10];
printf("\NEnter string1:\n ");
gets(a);
printf("\nEnter string2:\n ");
gets(b);
strcat(a,b);
printf("\nThe concatenated string is %s",a);
getch();
}

OUTPUT
Enter string1:
Naughty
Enter string2:
Student
The concatenated string is NaughtyStudent

www.mcdtu.wordpress.com 4
www.mcdtu.wordpress.com

PROGRAM 4:

WAP for finding a character in a string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[20],ch,*ptr;
printf("Enter string:\n");
gets(string);
printf("\nEnter the character to be found:\n");
scanf("%c",&ch);
ptr=strchr(string,ch);
if(ptr)
printf("\nThe character %c is at position %d\n",ch,(ptr-string+1));
else
printf("\nThe given string does not contain the character %c",ch);
getch();
}

OUTPUT
Enter string:
Computers
Enter character to be found:
u
The character u is at position 5

Enter string:
Computers
Enter character to be found:
A
The given string does not contain the character A

www.mcdtu.wordpress.com 5
www.mcdtu.wordpress.com

PROGRAM 5:

WAP for Finding a substring in a given string.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[20],sub[20],*ptr;
printf("Enter string: ");
gets(string);
printf("\nEnter the substring to be found: ");
gets(sub);
ptr=strstr(string,sub);
if(ptr!=NULL)
{printf("\nThe given string contains the substring");
printf("\nThe substring is %s\n", ptr);
}
else
printf("\nThe given string does not contain the substring");
getch();
}

OUTPUT
Enter string: student
Enter the substring to be found: den
The given string contains the substring
The substring is
den

www.mcdtu.wordpress.com 6
www.mcdtu.wordpress.com

PROGRAM 6:

WAP for implementing stack using array.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 10
typedef struct
{int arr[MAX];
int top;
}stack;
stack st= {{0}, -1};
void push(stack*,int);
int pop(stack*);
void show(stack*);
void push(stack *ptr,int d)
{
if(ptr->top==MAX-1)
{
printf("Stack is full, while pushing %d\n",d);
return;
}
ptr->top++;
ptr->arr[ptr->top]=d;
}
int pop(stack *ptr)
{int d;
if(ptr->top==-1)
{
printf("\n Stack is empty\n");
return -1;
}
d=ptr->arr[ptr->top];
ptr->arr[ptr->top]=0;
ptr->top--;
return d;
}
void show(stack *ptr)
{printf("\nThe elements of stack are ");
for(int i=0;i<=ptr->top;i++)
printf("\n%d",ptr->arr[i]);

www.mcdtu.wordpress.com 7
www.mcdtu.wordpress.com

}
void main()
{
int item,ch,del;
do
{printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{case 1: printf("\nEnter element to be inserted:");
scanf("%d",&item);
push(&st,item);
break;
case 2: del=pop(&st);
if(del!=-1)
printf("\nThe deleted element is %d",del);
break;
case 3: show(&st);
break;
case 4: printf(“\mGOOD BYE!!”);
getch();
exit(0);
break;
default: printf("\n Wrong choice");
}
}while(ch>=1&&ch<=4);
getch();
}

OUTPUT
1.Push
2.Pop
3.Display
4.Exit

Enter your choice:1

www.mcdtu.wordpress.com 8
www.mcdtu.wordpress.com

Enter element to be inserted:16

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:1

Enter element to be inserted:08

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:1

Enter element to be inserted:93

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:3

The elements of stack are


16
08
93

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:2

The deleted element is 93

1.Push
2.Pop
www.mcdtu.wordpress.com 9
www.mcdtu.wordpress.com

3.Display
4.Exit

Enter your choice:4


GOOD BYE!!

www.mcdtu.wordpress.com 10
www.mcdtu.wordpress.com

PROGRAM 7:

Write a program to implement stack using linked list.

#include<stdio.h>
#include<conio.h>
struct stack
{
int data;
struct stack *next;
};
typedef struct stack stack;
stack *alloc()
{
return (stack*)malloc(sizeof(stack));
}
void push(stack**, int);
int pop(stack**);
void display(stack*);
void main()
{int ch,item,delitem;
stack *top;
top=NULL;
do
{
printf("\n1.Push");
printf("\n2.Pop");
printf("\n3.Display");
printf("\n4.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter element to be inserted:");
scanf("%d",&item);
push(&top,item);
break;
case 2: delitem=pop(&top);
if(delitem!=-1)
printf("\nThe deleted element is %d",delitem);
break;

www.mcdtu.wordpress.com 11
www.mcdtu.wordpress.com

case 3:
printf("\nThe elements of the stack are\n\n");
printf("\t");
display(top);
break;
case 4:printf(“\nGOOD BYE!!”);
getch();
exit(0);
break;
default: printf("\nWrong Choice!!");
}
}while(ch>=1&&ch<=4);
getch();
}
void push(stack**T, int d)
{
stack *temp;
if(*T==NULL)
{
(*T)=alloc();
(*T)->data=d;
(*T)->next=NULL;
}
else
{
temp=alloc();
temp->data=d;
temp->next=(*T);
(*T)=temp;
}
}
int pop(stack**T)
{
stack*temp;
int d;
if(*T==NULL)
{
printf("\nStack is empty\n");
return -1;
}
d=(*T)->data;
temp=*T;
(*T)=(*T)->next;
free(temp);
www.mcdtu.wordpress.com 12
www.mcdtu.wordpress.com

return d;
}
void display(stack *S)
{
while(S!=NULL)
{
printf("%d->",S->data);
S=S->next;
}
printf("\n");
}

OUTPUT
1.Push
2.Pop
3.Display
4.Exit

Enter your choice:1

Enter element to be inserted:16

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:1

Enter element to be inserted:08

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:3

The elements of the stack are


16->08->
www.mcdtu.wordpress.com 13
www.mcdtu.wordpress.com

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:2

The deleted element is 16

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:3

The elements of the stack are


08->

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:2


The deleted element is 08

1.Push
2.Pop
3.Display
4.Exit

Enter your choice:2

Stack is empty

1.Push
2.Pop
3.Display
4.Exit

www.mcdtu.wordpress.com 14
www.mcdtu.wordpress.com

Enter your choice:4


GOOD BYE!!

www.mcdtu.wordpress.com 15
www.mcdtu.wordpress.com

PROGRAM 8:

WAP to evaluate postfix expression.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#define MAX 30
int stack[MAX/2];
int top=-1;
void push(int*,int*,int);
int pop(int*,int*);
void eval(char*);
void main()
{
char expression[20];
printf("\nEnter the expression:");
scanf("%s",&expression);
eval(expression);
printf("\nThe result is %d",stack[0]);
getch();
}
void eval(char*exp)
{
int val1,val2,ans;
while(*exp)
{
if(*exp==' ' || *exp=='\t')
{
exp++;
continue;
}
else if(isdigit(*exp))
push(stack,&top,(*exp)-48);
else
{
val1=pop(stack,&top);
val2=pop(stack,&top);
switch(*exp)
{

www.mcdtu.wordpress.com 16
www.mcdtu.wordpress.com

case '+':
ans=val2+val1;
break;
case '-':
ans=val2-val1;
break;
case '/':
ans=val2/val1;
break;
case '*':
ans=val2*val1;
break;
case '%':
ans=val2%val1;
break;
case '^':
ans=pow(val2,val1);
break;
default:
printf("\nUnknown Operator");
exit(0);
}
push(stack,&top,ans);
}
exp++;
}}
void push(int *st,int *sp,int item)
{
if(*sp==MAX-1)
printf("\nStack is full");
else{
(*sp)++;
st[*sp]=item;}}
int pop(int *st, int *sp)
{int item;
if(*sp==-1)
{printf("\nStack is empty");
return -1;}
else
{item=st[*sp];
(*sp)--;
return item;
}}

www.mcdtu.wordpress.com 17
www.mcdtu.wordpress.com

OUTPUT
Enter the expression: 234*+52^-

The result is -11

www.mcdtu.wordpress.com 18
www.mcdtu.wordpress.com

PROGRAM 9:

WAP to convert infix expression to postfix.

#include<iostream.hP>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
class expression
{
char infix[100];
char stack[200];
int top;
int r;
char postfix[100];
public:
void convert();
int input_p(char);
int stack_p(char);
int rank(char);
};
int expression::input_p(char c)
{
if(c==’+’ || c==’-')
return 1;
else if(c==’*’ || c==’/')
return 3;
else if(c==’^')
return 6;
else if(isalpha(c)!=0)
return 7;
else if(c==’(‘)
return 9;
else if(c==’)')
return 0;
else
{
cout<<”Invalid expression ::input error\n”;
exit(0);
}}
int expression::stack_p(char c)
{

www.mcdtu.wordpress.com 19
www.mcdtu.wordpress.com

if(c==’+’ || c==’-')
return 2;
else if(c==’*’ || c==’/')
return 4;
else if(c==’^')
return 5;
else if(isalpha(c)!=0)
return 8;
else if(c==’(‘)
return 0;
else
{
cout<<”Invalid expression ::stack error\n”;
exit(0);
}}
int expression::rank(char c)
{
if(c==’+’ || c==’-')
return -1;
else if(c==’*’ || c==’/')
return -1;
else if(c==’^')
return -1;
else if(isalpha(c)!=0)
return 1;
else
{
cout<<”Invalid expression ::in rank\n”;
exit(0);
}}
void expression::convert()
{
cout<<”Enter an infix expression ::\n”;
cin>>infix;
int l=strlen(infix);
infix*l+=’)';
infix*l+1+=”;
top=1;
stack*top+=’(‘;
r=0;
int x=-1;
int i=0;
char next=infix[i];

www.mcdtu.wordpress.com 20
www.mcdtu.wordpress.com

while(next!=”)
{
//Pop all the elements to output in stack which have higher precedence
while( input_p(next) < stack_p(stack[top]) )
{
if(top<1)
{
cout<<”invalid expression ::stack error\n”;
exit(0);
}
postfix[++x]=stack[top];
top–;
r=r+rank(postfix[x]);

if(r<1)
{
cout<<”Invalid expression ::r<1\n”;
exit(0);
}}
if(input_p( next ) != stack_p( stack[top]))
stack[++top]=next;
else
top–;
i++;
next=infix[i];
}
postfix*++x+=”;
if(r!=1 || top!=0)
{
cout<<”Invalid expression ::error in rank or stack\n”;
exit(0);
}
cout<<”\n\nThe corresponding postfix expression is ::\n”;
cout<<postfix<<endl;
}
int main()
{
expression obj;
obj.convert();
return 0;}

www.mcdtu.wordpress.com 21
www.mcdtu.wordpress.com

OUTPUT
Enter an infix expression ::
(a+b^c^d)*(c+d)
The corresponding postfix expression is ::
abcd^^+cd+*

www.mcdtu.wordpress.com 22
www.mcdtu.wordpress.com

PROGRAM 10:

Write a program for checking balanced parenthesis.

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
const int maxstack = 100;
struct stack
{
char data[maxstack] ; // array where data is stored
int stacktop ; // when it is -1, stack is empty
}s;
void initstack(stack & s) // empty the stack
{
s.stacktop = -1; // set stacktop to be empty
}
bool isEmpty(const stack& s) // returns true or false
{
return s.stacktop==-1; // function is true if stacktop has value of -1
}
void pop ( stack & s, char & x) // removes the top item
{
if (isEmpty(s)) // if stack is empty, cannot remove data
{
cout<<"Stack underflow\n";
getch();
exit(1); //terminate the program
}
else
{
x = s.data[s.stacktop]; // retrieve data first
s.stacktop--; // remove from stacktop
}}
// ************ FUNCTION PUSH ****************
void push( stack & s, const char & x)
// places element x on the stack
{
if (s.stacktop == maxstack -1) // not more than maxstack-1 is allowed
{
cout<<"Stack overflow"; // items on the stack.
exit(1); //terminate the program

www.mcdtu.wordpress.com 23
www.mcdtu.wordpress.com

}
else
{
s.stacktop++; // move the top of the stack up
s.data[s.stacktop] = x; // add new item,x
}
}
// ************ FUNCTION TOP ****************
char top(const stack& s)
//returns the top item from the stack in parameter x
{
if (isEmpty(s))
{
cout<<"Stack underflow"<<endl;
exit(1);}
else
return s.data[s.stacktop] ; }
// ************ FUNCTION CHECK ****************
void check()
{
bool error = false;
char ch;
cout << "Please enter an algebraic expression: (\n";
cin.get(ch);
cout<<")";
while (( ch != '\n') && (!error) )
{
if (ch == '(')
push(s,ch);
if (ch == ')')
if (isEmpty(s))
error = true;
else
pop(s,ch);
if (ch == '{')
push(s,ch);
if (ch == '}')
if (isEmpty(s))
error = true;
else
pop(s,ch);
if (ch == '[')
push(s,ch);
if (ch == ']')
www.mcdtu.wordpress.com 24
www.mcdtu.wordpress.com

if (isEmpty(s))
error = true;
else
pop(s,ch);
cin.get(ch);
}
if ((!error) && isEmpty(s))
cout<<"\nResult: This expression is a balanced parenthesis. \n"<<endl;
else
cout <<"\nResult: This expression is not a balanced parenthesis. \n"<<endl;
}
// ************ FUNCTION MAIN ***************
int main ()
{
initstack(s);// initialising stack to -1; i.e. it is empty
check();
getch();
return 0;}

OUTPUT
Please enter an algebraic expression: (
A+B (G*I)/{H}^[k]
)
Result: This expression is a balanced parenthesis
Please enter an algebraic expression: (
A+B (G*I/{H^[k]
)
Result: This expression is not a balanced parenthesis

www.mcdtu.wordpress.com 25
www.mcdtu.wordpress.com

PROGRAM 11:

WAP to implement queue using linked list.

#include<iostream>
#include<conio.h>
#include<stdlib.h>
class cqueue
{
int q[5],front,rare;
public:
cqueue()
{
front=-1;
rare=-1;
}
void push(int x)
{
if(front ==-1 && rare == -1)
{
q[++rare]=x;
front=rare;
return;
}
else if(front == (rare+1)%5 )
{
cout <<" Circular Queue over flow";
return;
}
rare= (rare+1)%5;
q[rare]=x;
}
void pop()
{
if(front==-1 && rare== -1)
{
cout <<"under flow";
return;
}
else if( front== rare )
{
front=rare=-1;

www.mcdtu.wordpress.com 26
www.mcdtu.wordpress.com

return;
}
front= (front+1)%5;
}
void display()
{
int i;
if( front <= rare)
{
for(i=front; i<=rare;i++)
cout << q[i]<<" ";
}
else
{
for(i=front;i<=4;i++)
{
cout <<q[i] << " ";
}
for(i=0;i<=rare;i++)
{
cout << q[i]<< " ";
}
}
}
};
main()
{
int ch;
cqueue q1;
while( 1)
{
cout<<"\n1.INSERT 2.DELETE 3.DISPLAY 4.EXIT\nEnter your choice";
cin >> ch;
switch(ch)
{
case 1: cout<<"enter element";
cin >> ch;
q1.push(ch); break;

case 2: q1.pop(); break;


case 3: q1.display(); break;
case 4:
cout<<”\nGOOD BYE!!”;
getch();
www.mcdtu.wordpress.com 27
www.mcdtu.wordpress.com

exit(0);
}}
getch();
return(0);}

OUTPUT
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your choice1
enter element4
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your choice1
enter element5
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your choice1
enter element3
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your choice3
453
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your choice2
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your choice3
53
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT
Enter your choice4

www.mcdtu.wordpress.com 28
www.mcdtu.wordpress.com

PROGRAM 12:

WAP to implement queue using array.

#include <stdio.h>
#include<ctype.h>
#include<stdlib.h>
#define MAXSIZE 5
int cq[MAXSIZE];
int front,rear;
void main()
{
void add(int,int);
void del(int);
int will=1,i,num;
front = -1;
rear = -1;
clrscr();
printf("\nProgram for Circular Queue demonstration through array");
while(1)
{
printf("\n\nMAIN MENU\n1.INSERTION\n2.DELETION\n3.EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&will);
switch(will)
{
case 1:
printf("\n\nENTER THE QUEUE ELEMENT : ");
scanf("%d",&num);
add(num,MAXSIZE);
break;
case 2:
del(MAXSIZE);
break;
case 3:
exit(0);
default: printf("\n\nInvalid Choice . ");
}

} //end of outer while


} //end of main
void add(int item,int MAX)

www.mcdtu.wordpress.com 29
www.mcdtu.wordpress.com

{
//rear++;
//rear= (rear%MAX);
if(front ==(rear+1)%MAX)
{
printf("\n\nCIRCULAR QUEUE IS OVERFLOW");
}
else
{
if(front==-1)
front=rear=0;
else
rear=(rear+1)%MAX;
cq[rear]=item;
printf("\n\nRear = %d Front = %d ",rear,front);
}
}
void del(int MAX)
{
int a;
if(front == -1)
{
printf("\n\nCIRCULAR QUEUE IS UNDERFLOW");
}
else
{
a=cq[front];
if(front==rear)
front=rear=-1;
else
front = (front+1)%MAX;
printf("\n\nDELETED ELEMENT FROM QUEUE IS : %d ",a);
printf("\n\nRear = %d Front = %d ",rear,front);
}}

OUTPUT
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
www.mcdtu.wordpress.com 30
www.mcdtu.wordpress.com

ENTER YOUR CHOICE : 1


ENTER THE QUEUE ELEMENT : 10
Rear=0 Front=0
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 1
ENTER THE QUEUE ELEMENT : 20
Rear=1 Front=0
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 1
ENTER THE QUEUE ELEMENT : 30
Rear=2 Front=0
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 1
ENTER THE QUEUE ELEMENT : 40
Rear=3 Front=0
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 1
ENTER THE QUEUE ELEMENT : 50
Rear=4 Front=0
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 1
ENTER THE QUEUE ELEMENT : 60
CIRCULAR QUEUE IS OVERFLOW.
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 2
DELETED ELEMENT FROM QUEUE IS : 10
www.mcdtu.wordpress.com 31
www.mcdtu.wordpress.com

Rear =4 Front=1
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 2
DELETED ELEMENT FROM QUEUE IS : 20
Rear =4 Front=2
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 2
DELETED ELEMENT FROM QUEUE IS : 30
Rear =4 Front=3
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 2
DELETED ELEMENT FROM QUEUE IS : 40
Rear =4 Front=4
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 2
DELETED ELEMENT FROM QUEUE IS : 50
Rear =-1 Front=-1
MAIN MENU
1. INSERTION
2.DELETION
3.EXIT
ENTER YOUR CHOICE : 2
CIRCULAR QUEUE IS UNDERFLOW

www.mcdtu.wordpress.com 32
www.mcdtu.wordpress.com

PROGRAM 13:

WAP to implement binary search tree.

#include<iostream>
#include<conio.h>
#include<stdlib.h>
void insert(int,int );
void delte(int);
void display(int);
int search(int);
int search1(int,int);
int tree[40],t=1,s,x,i;
main()
{
int ch,y;
for(i=1;i<40;i++)
tree[i]=-1;
while(1)
{
cout <<"1.INSERT\n2.DELETE\n3.DISPLAY\n4.SEARCH\n5.EXIT\nEnter your choice:";
cin >> ch;
switch(ch)
{
case 1:
cout <<"enter the element to insert";
cin >> ch;
insert(1,ch);
break;
case 2:
cout <<"enter the element to delete";
cin >>x;
y=search(1);
if(y!=-1) delte(y);
else cout<<"no such element in tree";
break;
case 3:
display(1);
cout<<"\n";
for(int i=0;i<=32;i++)
cout <<i;
cout <<"\n";

www.mcdtu.wordpress.com 33
www.mcdtu.wordpress.com

break;
case 4:
cout <<"enter the element to search:";
cin >> x;
y=search(1);
if(y == -1) cout <<"no such element in tree";
else cout <<x << "is in" <<y <<"position";
break;
case 5:
exit(0);
}
}
}

void insert(int s,int ch )


{
int x;
if(t==1)
{
tree[t++]=ch;
return;
}
x=search1(s,ch);
if(tree[x]>ch)
tree[2*x]=ch;
else
tree[2*x+1]=ch;
t++;
}
void delte(int x)
{
if( tree[2*x]==-1 && tree[2*x+1]==-1)
tree[x]=-1;
else if(tree[2*x]==-1)
{ tree[x]=tree[2*x+1];
tree[2*x+1]=-1;
}
else if(tree[2*x+1]==-1)
{ tree[x]=tree[2*x];
tree[2*x]=-1;
}
else
{
tree[x]=tree[2*x];
www.mcdtu.wordpress.com 34
www.mcdtu.wordpress.com

delte(2*x);
}
t--;
}

int search(int s)
{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return tree[s];
if(tree[s]>x)
search(2*s);
else if(tree[s]<x)
search(2*s+1);
else
return s;
}

void display(int s)
{
if(t==1)
{cout <<"no element in tree:";
return;}
for(int i=1;i<40;i++)
if(tree[i]==-1)
cout <<" ";
else cout <<tree[i];
return ;
}

int search1(int s,int ch)


{
if(t==1)
{
cout <<"no element in tree";
return -1;
}
if(tree[s]==-1)
return s/2;
if(tree[s] > ch)
www.mcdtu.wordpress.com 35
www.mcdtu.wordpress.com

search1(2*s,ch);
else search1(2*s+1,ch);
}

OUTPUT
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:3
no element in tree:
0123456789011121314151617181920212223242526272829303132
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:1
Enter the element to insert 10
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:4
Enter the element to search: 10
10 is in 1 position
1.INSERT
2.DELETE
3.DISPLAY
4.SEARCH
5.EXIT
Enter your choice:5

www.mcdtu.wordpress.com 36
www.mcdtu.wordpress.com

PROGRAM 14:

WAP to implement linear search array.

#include < stdio.h>


#include < conio.h>
void main()
{
int a[10],n,i,item,loc;
clrscr();
printf("Enter the size of array: ");
scanf("%d",&n);
printf("\nEnter the elements of array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the item to be searched: ");
scanf("%d",&item);
a[n]=item;
loc=0;
while(a[loc]!=item)
{
loc=loc+1;
}
if(loc==n)
printf("\nItem %d is not present in the array",item);
else
{printf("\nItem %d is present at %d location in the array",item,loc+1);}
getch();}

OUTPUT
Enter the size of the array: 10
Enter elements of array:
1
2
34
5

www.mcdtu.wordpress.com 37
www.mcdtu.wordpress.com

67
23
12
45
4
6
Enter the item to be searched: 12
Item 12 is present at 7 location in the array

www.mcdtu.wordpress.com 38
www.mcdtu.wordpress.com

PROGRAM 15:

WAP to implement binary search in an array.

#include<stdio.h>
main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d",&array[c]);
printf("Enter value to find\n");
scanf("%d",&search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while( first <= last )
{
if ( array[middle] < search )
first = middle + 1;
else if ( array[middle] == search )
{ printf("%d found at location %d.\n", search, middle+1);
break;}
else
last = middle - 1;
middle = (first + last)/2;}
if ( first > last )
printf("Not found! %d is not present in the list.\n", search);
return 0; }

OUTPUT
Enter number of elements
10
Enter 10 integers
1
23

www.mcdtu.wordpress.com 39
www.mcdtu.wordpress.com

45
6
7
89
67
56
66
77
9
Enter value to find
23
23 found at location 2

www.mcdtu.wordpress.com 40
www.mcdtu.wordpress.com

PROGRAM 16:

WAP to implement linked list.

#include <stdio.h>
#include <conio.h>
#include <alloc.h>
void create();
void insert();
void delete();
void display();
struct node
{
int data;
struct node *link;
};
struct node *first=NULL,*last=NULL,*next,*prev,*cur;
void create()
{
cur=(struct node*)malloc(sizeof(struct node));
printf("\nENTER THE DATA: ");
scanf("%d",&cur->data);
cur->link=NULL;
first=cur;
last=cur;
}
void insert()
{
int pos,c=1;
cur=(struct node*)malloc(sizeof(struct node));
printf("\nENTER THE DATA: ");
scanf("%d",&cur->data);
printf("\nENTER THE POSITION: ");
scanf("%d",&pos);
if((pos==1) &&(first!=NULL))
{
cur->link = first;
first=cur;
}
else
{
next=first;

www.mcdtu.wordpress.com 41
www.mcdtu.wordpress.com

while(c<pos)
{
prev=next;
next=prev->link;
c++;
}
if(prev==NULL)
{
printf("\nINVALID POSITION\n");
}
else
{
cur->link=prev->link;
prev->link=cur;
}
}
}
void delete()
{
int pos,c=1;
printf("\nENTER THE POSITION : ");
scanf("%d",&pos);
if(first==NULL)
{
printf("\nLIST IS EMPTY\n");
}
else if(pos==1 && first->link==NULL)
{
printf("\n DELETED ELEMENT IS %d\n",first->data);
free(first);
first=NULL;
}
else if(pos==1 && first->link!=NULL)
{
cur=first;
first=first->link;
cur->link=NULL;
printf("\n DELETED ELEMENT IS %d\n",cur->data);
free(cur);
}
else
{
next=first;
while(c<pos)
www.mcdtu.wordpress.com 42
www.mcdtu.wordpress.com

{
cur=next;
next=next->link;
c++;
}
cur->link=next->link;
next->link=NULL;
if(next==NULL)
{
printf("\nINVALID POSITION\n");
}
else
{
printf("\n DELETED ELEMENT IS %d\n",next->data);
free(next);
}
}
}
void display()
{
cur=first;
while(cur!=NULL)
{
printf("\n %d",cur->data);
cur=cur->link;
}
}
void main()
{
int ch;
clrscr();
printf("\n\nSINGLY LINKED LIST");
do
{
printf("\n\n1.CREATE\n2.INSERT\n3.DELETE\n4.EXIT");
printf("\n\nENTER YOUR CHOICE : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
display();
break;
case 2:
www.mcdtu.wordpress.com 43
www.mcdtu.wordpress.com

insert();
display();
break;
case 3:
delete();
display();
break;
case 4:
exit(0);
default:
printf("Invalid choice...");
}
}while(1);
getch();
}

OUTPUT
SINGLY LINKED LIST
1.CREATE
2.INSERT
3.DELETE
4.EXIT
ENTER YOUR CHOICE : 1
ENTER THE DATA: 10
10
1.CREATE
2.INSERT
3.DELETE
4.EXIT
ENTER YOUR CHOICE : 2
ENTER THE DATA: 30
ENTER THE POSITION: 1
30
10
1.CREATE
2.INSERT
3.DELETE
4.EXIT
ENTER YOUR CHOICE : 3
ENTER THE POSITION : 2
LIST IS EMPTY

www.mcdtu.wordpress.com 44
www.mcdtu.wordpress.com

PROGRAM 17:

WAP to implement bubble sort.

#include <iostream.h>
#include<conio.h>
void bubbleSort(int *ar,int length)//Bubble sort function
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<i;j++)
{
if(ar[i]>ar[j])
{
int temp=ar[i]; //swap
ar[i]=ar[j];
ar[j]=temp;
}
}
}
}
void main()
{
clrscr();
int a[50],n;
cout<<”\nEnter number of elements of an array:\n”;
cin>>n;
cout<<”\nEnter elements of array:\n”;
for(int i=0; i<n; i++)
{
cin>>a[i];
}
bubbleSort(a,n); //call to bubble sort
cout<<”\nSorted array is: \n”;
for( i=0; i<n; i++)
{
cout<<”\na[i]\t”;
}
getch();
}

www.mcdtu.wordpress.com 45
www.mcdtu.wordpress.com

OUTPUT
Enter number of elements of an array:
5
Enter elements of array:
14
4
2
45
1
Sorted array is:

1 2 4 14 45

www.mcdtu.wordpress.com 46
www.mcdtu.wordpress.com

PROGRAM 18:

WAP to implement selection sort.

#include<stdio.h>
void selsort(int*a,int s)
{
Int i,temp,j;
for(i=0;i<s;i++){
for(j=i+1;j<s;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
int main()
{
int s,i,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("\nEnter %d elements:\n ",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
selsort(a,s);//selection sort function
printf("\nAfter sorting is: \n");
for(i=0;i<s;i++)
printf(" %d\t",a[i]);
return 0;
}

OUTPUT
Enter total elements: 5
Enter 5 elements:
4
5

www.mcdtu.wordpress.com 47
www.mcdtu.wordpress.com

0
21
7
After sorting is:
0 4 5 7 21

www.mcdtu.wordpress.com 48
www.mcdtu.wordpress.com

PROGRAM 19:

WAP to implement insertion sort.

#include<stdio.h>
void insort(int*a,int s)
{
Int i,j,temp;
for(i=1;i<s;i++){
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
}
int main()
{
int i,s,a[20];
printf("\nEnter total elements: ");
scanf("%d",&s);
printf("\nEnter %d elements: \n",s);
for(i=0;i<s;i++)
scanf("%d",&a[i]);
insort(a,s);//INSERTION SORT
printf("\nAfter sorting: \n");
for(i=0;i<s;i++)
printf(" %d\t",a[i]);

return 0;
}

OUTPUT
Enter total elements: 5
Enter 5 elements:
3

www.mcdtu.wordpress.com 49
www.mcdtu.wordpress.com

7
9
0
2
After sorting:
0 2 3 7 9

www.mcdtu.wordpress.com 50
www.mcdtu.wordpress.com

PROGRAM 20:

WAP to implement merge sort.

#include<iostream.h>
#include<conio.h>
const int size=50;
void merge(int*a,int*b,int*c,int m,int n)
{
int i,j,k;
for(i=0,j=0;i<m,j<n;)
{
if(a[i]<b[j])
{
c[k++]=a[i++];
}
else{
c[k++]=b[j++];
}
}
while(i<m)
{
c[k++]=a[i++];
}
while(j<n)
{
c[k++]=b[j];
}
}
main()
{
int a[size],b[size],c[2*size],n,m,k,i;
cout<<"\nEnter number of elements of array 1 :\n";
cin>>m;
cout<<"\nEnter elements of array in ascending order:\n";
for(i=0;i<m;i++)
{
cin>>a[i];
}
cout<<"\nEnter number of elements of array 2:\n";
cin>>n;
cout<<"\nEnter elements of array in ascending order:\n";

www.mcdtu.wordpress.com 51
www.mcdtu.wordpress.com

for(i=0;i<n;i++)
{
cin>>b[i];
}
k=m+n;
merge(a,b,c,m,n);
cout<<"\nMerged array in ascending order is :\n";//merging function
for(i=0;i<k;i++)
{
cout<<c[i]<<"\t";
}
getch();
return 0;
}

OUTPUT
Enter number of elements of array1:
5
Enter elements of array in ascending order:
1
2
3
4
5
Enter number of elements of array2:
6
Enter elements of array in ascending order:
2
4
5
6
7
8
Merged array in ascending order is:
1 2 2 3 4 5 5 6 7 8

www.mcdtu.wordpress.com 52
www.mcdtu.wordpress.com

PROGRAM 21:

WAP to implement shell sort.

#include <iostream.h>
#include <conio.h>
#define MAX 10
void shellsort(int*arr,int n){
int i,j,temp,increment;
for(increment=n/2; increment>0; increment /= 2){
for(i=increment; i<n; i++){
temp=arr[i];
for(j=i; j>=increment; j -= increment){
if(temp < arr[j-increment])
arr[j] = arr[j-increment];
elsebreak;
}
arr[j] = temp;
}
}
}
void main(){
clrscr();
int arr[MAX],n;
cout<<"\n*****Shell Sort*****\n";
cout<<"How many elements you require : ";
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
shellsort(arr,n);//shell sort
cout<<"\n--Display--\n";
for(int i=0;i<n;i++)
cout<<arr[i]<<" \t ";
getch();}

OUTPUT
How many elements you require : 5
10

www.mcdtu.wordpress.com 53
www.mcdtu.wordpress.com

35
45
32
23
--Display—
10 23 32 35 45

www.mcdtu.wordpress.com 54
www.mcdtu.wordpress.com

PROGRAM 22:

WAP to implement quick sort.

#include<stdio.h>
void quicksort(int [10],int,int);
int main(){
int x[20],size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&x[i]);
quicksort(x,0,size-1);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",x[i]);
return 0;
}
void quicksort(int x[10],int first,int last){
int pivot,j,temp,i;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(x[i]<=x[pivot]&&i<last)
i++;
while(x[j]>x[pivot])
j--;
if(i<j){
temp=x[i];
x[i]=x[j];
x[j]=temp;
}}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}}

www.mcdtu.wordpress.com 55
www.mcdtu.wordpress.com

OUTPUT
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8

www.mcdtu.wordpress.com 56

You might also like