You are on page 1of 99

EXPERIMENT-1

Task : WAP to find the sum of n natural numbers where n is entered by the
user.

Purpose By implementing this program we are capable of finding out the sum of
n natural numbers where n is entered by the user .

Source Code : //The program showing sum of natural number under a limit. #include<iostream.h> #include<conio.h> int main() { int i,n,sum=0; start://label cout<<"Enter any number: "; cin>>n; if(n>0) { cout<<"numbers are: \n"; for (i=1;i<=n;i++) { cout<<i<<" \n"; sum=sum+i; } } else { cout<<"You entered a wrong choice. Please enter the natural number again.\n"; goto start;//move to label } cout<<"Sum of "<<n<<"'s natural no.'s are: "<<sum; getch(); return 0;
JESSICA E101048 Page 1

} Output :

Conclusion This program efficiently find the sum of n natural numbers


where n is entered by the user.

JESSICA E101048

Page 2

EXPERIMENT-2
Task : WAP to fetch a number from the user and find the factorial of the number. Purpose By implementing this program we are capable of finding out the
factorial of the number & the number is fetch from user.

Source Code : //The program showing factorial of a number. #include<iostream.h> #include<conio.h> int main() { int i,n,mul=1; start://label cout<<"Enter any number: "; cin>>n; if(n==0) { cout<<"Factorial is: 1"; getch(); exit(1);//terminate the program } else if(n<0) { cout<<"You entered a wrong number & its Factorial doesn't exist.\n"; goto start;//move to label } else { for (i=1;i<=n;i++) { mul=mul*i; } }
JESSICA E101048 Page 3

cout<<"Factorial is: "<<mul; getch(); return 0; } Output :

Conclusion This program efficiently calculates and displays the factorial of the
number & the number is fetch from user.

JESSICA E101048

Page 4

EXPERIMENT-3
Task : WAP to solve the given expression z=xn/(n!). Purpose By implementing this program we are capable of finding out the given
expression z=xn/(n!).

Source Code : //The program satisfying the condition z=x(power n)/n! #include<iostream.h> #include<math.h> #include<conio.h> //header file for power function int main() { float t,z; int n,x; cout<<"Enter any power number 'n': "; cin>>n; cout<<"Enter the base number 'x': "; cin>>x; int factorial(int);//prototype declaration if(n>=0) { t= pow(x,n);//calling the function defined in the complier z=t/factorial(n);//calling an undefined function cout<<"\nResult is: "<<z; } else { cout<<"Result doesn't exist."; } getch(); return 0; } int factorial(int n) {
JESSICA E101048 Page 5

int i,fact=1; if(n==0) { return(fact); } { for (i=1;i<=n;i++) { fact=fact*i; } return(fact); } } Output :

Conclusion This program efficiently calculates and displays the given


expression z=xn/(n!).

JESSICA E101048

Page 6

EXPERIMENT-4
Task : WAP to sort an array in ascending order. Purpose By implementing this program we are capable of sorting an array in
ascending order.

Source Code : #include<iostream.h> #include<conio.h> #define MAX 100 void output (int a[], int n); int sort (int a[], int n); int main() { int small,large,a[MAX],i,n; cout<<"No. of elements in array:"<<endl; cin>>n; cout<<"enter "<<n<<" elements"<<endl; for(i=0;i<n;i++) { cin>>a[i]; } small=a[0]; large=a[0]; for(i=1;i<n;i++) { if (small>a[i]) { small=a[i]; } if (large<a[i]) {
JESSICA E101048 Page 7

large = a[i]; } } cout<<"smallest no. entered is "<<small<<endl; cout<<"largest no. entered is "<<large<<endl; cout<<"unsorted output: "<<endl; output(a,n); sort(a,n); cout<<endl; cout<<"sorted output: "<<endl; output(a,n); getch(); return 0; } void output(int a[],int n) { cout<<endl; for (int i=0;i<=n-1;i++) { cout<<a[i]<<'\t'; } } int sort(int a[],int n) { int temp; for (int i=0;i<=n-1;i++) { for(int j=0;j<=n-2;j++) { if (a[i]<a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } }
JESSICA E101048 Page 8

} } Output :

Conclusion This program efficiently sorts an array in ascending order.

JESSICA E101048

Page 9

EXPERIMENT-5
Task : WAP to fetch the record of n students and print them. Purpose By implementing this program we are capable of printing record of n
students.

Source Code : //The program containing record of students of a class #include<iostream.h> #include<stdio.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<ctype.h> #define max 100 #define ma 100 struct student { char name[max]; char na[max]; char nam[ma]; char n[ma]; int roll; float per; }s[70]; int main() { start: int i,n; cout<<"Enter total no of students: "; cin>>n; if(n>0 && n<=70)
JESSICA E101048 Page 10

{ for(i=0;i<=n-1;i++) { cout<<"Student "<<i+1<<" :"; cout<<"\nStudent's name: "; cout<<"\nEnter First name: "; cin>>s[i].name; cout<<"Enter Last name: "; cin>>s[i].na; cout<<"\nFather's name: "; cout<<"\nEnter First name: "; cin>>s[i].nam; cout<<"Enter Last name: "; cin>>s[i].n; cout<<endl<<"Enter roll number: "; cin>>s[i].roll; cout<<"Enter Percentage: "; cin>>s[i].per; } cout<<"Details are:\n"; cout<<"Student's name\t\tFather's name\t\tRoll number\tPercentage\n"; for(i=0;i<=n-1;i++) { cout<<s[i].name; cout<<" "<<s[i].na; cout<<"\t\t"; cout<<s[i].nam; cout<<" "<<s[i].n; cout<<"\t\t"; cout<<s[i].roll; cout<<"\t\t"; cout<<s[i].per; cout<<"\n"; } } else {
JESSICA E101048 Page 11

cout<<"You exceed the limit of students.\n"; goto start; } getch(); return 0; } Output :

Conclusion This program efficiently print the record of n students.

JESSICA E101048

Page 12

EXPERIMENT-6
Task : WAP to implement insertion , deletion & updation in an existing array. Purpose By implementing this program we are capable of implementing
insertion , deletion & updation in an existing array.

Source Code : #include<iostream.h> #include<conio.h> #include<stdlib.h> #define max 100 int main() { int a[max],limit,i,loc,elem,ch; while (1) { lets: cout<<"Press\n 1 for Entering the data.\n 2 for Insertion.\n 3 for Deletion.\n 4 for Updation.\n 5 To exit.\n";//menu cout<<"Enter your choice: "; cin>>ch; switch(ch) { case 1: { start: cout<<"Enter the limit of an array: "; cin>>limit; cout<<"Enter the elements in an array: "; if(limit<=max) { for(i=1;i<=limit;i++) { cin>>a[i]; } cout<<"\nHence the elements in array are:\n";
JESSICA E101048 Page 13

for(i=1;i<=limit;i++) { cout<<a[i]<<"\t"; } } else { cout<<"It is out of range."; goto start; } cout<<"\n"; break; } case 2: { begin: cout<<"Enter the location: "; cin>>loc; cout<<"Number to be inserted: "; cin>>elem; if(loc<=limit) { limit++; for(i=limit;i>loc;i--) { a[i]=a[i-1]; } a[i]=elem; cout<<"\nHence the elements in array are:\n"; for(i=1;i<=limit;i++) { cout<<a[i]<<"\t"; } } else {
JESSICA E101048 Page 14

cout<<"No insertion as location exceed the value of limit. So enter with in the limit."; goto begin; } cout<<"\n"; break; } case 3: { now: cout<<"Enter the location for deletion: "; cin>>loc; if(loc<=limit) { for(i=loc;i<limit;i++) { a[i]=a[i+1]; } limit--; cout<<"\nHence the elements in array are:\n"; for(i=1;i<=limit;i++) { cout<<a[i]<<"\t"; } } else { cout<<"Since location exceed the value of limit. So enter within the limit."; goto now; } cout<<"\n"; break; } case 4: { cout<<"Enter the location to be updated: "; cin>>loc;
JESSICA E101048 Page 15

cout<<"Enter the new element: "; cin>>elem; a[loc]=elem; cout<<"\nHence the elements in array are:\n"; for(i=1;i<=limit;i++) { cout<<a[i]<<"\t"; } cout<<"\n"; break; } case 5: { cout<<"ok bye\n"; getch(); exit(1); } default: cout<<"You entered a wrong choice.\n"; goto lets; }//closing switch }//closing the true loop getch(); return 0; }//end of main Output :

JESSICA E101048

Page 16

Conclusion This program efficiently implement insertion , deletion & updation


in an existing array.

JESSICA E101048

Page 17

EXPERIMENT-7
Task : WAP to implement linear search on array with redundant integer elements. Purpose By implementing this program we are capable of implementing linear
search on array with redundant integer elements.

Source Code : #include<iostream.h> #include<conio.h> #define MAX 100 int main() { int a[MAX],i,j,k,n,loc,elem; cout<<"Enter the limit\n"; cin>>n; if(n>MAX) cout<<"Enter value below the limit\n"; else { cout<<"Enter the elements\n"; for(i=0;i<n;i++) { cin>>a[i]; } cout<<endl; cout<<"Array is \n"; for(i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(i==j)
JESSICA E101048 Page 18

continue; else { if(a[i]==a[j]) { for(k=j;k<n;k++) a[k]=a[k+1]; n=n-1; j=0; } } } } cout<<"Elements in normalized array is= \n"; for(i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; } cout<<"Enter element to search using linear search\n"; cin>>elem; loc=-1; for(i=0;i<n;i++) { if(a[i]==elem) { loc=i; break; } } if(loc==-1) cout<<"Element not in the array\n"; else { cout<<"Location = "<<loc<<endl; cout<<"Element = "<<elem<<endl; }
JESSICA E101048 Page 19

getch(); } Output :

Conclusion This program efficiently implement linear search on array with


redundant integer elements.

JESSICA E101048

Page 20

EXPERIMENT-8
Task : WAP to implement linear search on array without redundant integer
elements.

Purpose By implementing this program we are capable of implementing linear


search on array without redundant integer elements.

Source Code : #include<iostream.h> #include<conio.h> #define max 100 int main() { int a[max],i,n,loc,elem,j; cout<<"Enter the limit\n"; cin>>n; cout<<"Enter the elements of array\n"; for(i=0;i<n;i++) cin>>a[i]; cout<<endl; cout<<"Array is = \n"; for(i=0;i<n;i++) cout<<a[i]<<" "; cout<<endl; cout<<"Enter element to search\n"; cin>>elem; cout<<"Element= "<<elem<<endl; loc=-1; for(i=0;i<n;i++) { if(a[i]==elem) { loc=i; cout<<"Location= "<<loc<<endl; }
JESSICA E101048 Page 21

} if(loc==-1) cout<<"Element not present"; getch(); } Output :

Conclusion This program efficiently implement linear search on array without


redundant integer elements.

JESSICA E101048

Page 22

EXPERIMENT-9
Task : WAP to implement binary search on integer elements. Purpose By implementing this program we are capable of implementing binary
search on integer elements.

Source Code : #include<iostream.h> #include<conio.h> #define MAX 100 void output (int a[], int n); void sort (int a[], int n); int main() { int a[MAX],i,n,elem,loc,mid,end,beg; cout<<"No. of elements in array:"<<endl; cin>>n; if(n>MAX) { cout<<"You exceed the limit."; } else { cout<<"enter "<<n<<" elements"<<endl; for(i=0;i<n;i++) { cin>>a[i]; } cout<<"unsorted output: "<<endl; output(a,n); sort(a,n); cout<<endl; cout<<"sorted output: "<<endl; output(a,n); cout<<"\nEnter element to search using binary search\n"; cin>>elem;
JESSICA E101048 Page 23

{ beg=0; end=n-1; mid=((beg+end)/2); while(beg<=end && a[mid]!=elem) { if(elem>a[mid]) { beg=mid+1; } else { end=mid-1; } mid= ((beg+end)/2); } if(elem==a[mid]) { cout<<"Found at loc: "<<mid+1; } else { cout<<"Not found."; } } } getch(); return 0; } void output(int a[],int n) { cout<<endl; for (int i=0;i<=n-1;i++) { cout<<a[i]<<'\t'; } }
JESSICA E101048 Page 24

void sort(int a[],int n) { int temp; for (int i=0;i<=n-1;i++) { for(int j=0;j<=n-2;j++) { if (a[i]<a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } Output :

Conclusion This program efficiently implement binary search on integer


elements.
JESSICA E101048 Page 25

EXPERIMENT-10
Task : WAP to implement stacks and various operations on stacks.. Purpose By implementing this program we are capable of implementing stacks
and various operations on stacks.

Source Code : #include<iostream.h> #include<conio.h> #include<math.h> #include<stdlib.h> #define MAXSTK 100 struct stack { int item[MAXSTK]; int top; }; void push(struct stack*,int); int pop(struct stack*); void display(struct stack*); int main() { cout<<"Program showing push & pop operation."; int ch,n; struct stack s; s.top = -1; while(1) { cout<<"\nPress\n 1 For PUSH.\n 2 For POP.\n 3 To EXIT."; cout<<"\nEnter your choice: "; cin>>ch; switch(ch) { case 1: { cout<<"Enter the elements you want to push: ";
JESSICA E101048 Page 26

cin>>n; push(&s,n); cout<<"\nStack after push operation:\n"; display(&s); getch(); break; } case 2: { n=pop(&s); cout<<"Element poped is: "<<n; cout<<"\nStack after pop operation:\n"; display(&s); getch(); break; } case 3: { cout<<"\n Ok Bye."; getch(); exit(1); } default: cout<<"\nYou entered a wrong choice."; } } getch(); return 0; } void push(struct stack *ps,int x) { if(ps->top==(MAXSTK-1)) { cout<<"\nStack Overflow."; exit(1); } ps->top++;
JESSICA E101048 Page 27

ps->item[ps->top]=x; } int pop(struct stack *ps) { int x; if(ps->top==(-1)) { cout<<"\nStack underflow"; getch(); exit(1); } x=ps->item[ps->top]; ps->top--; return (x); } void display(struct stack *ps) { int i; if(ps->top==(-1)) { cout<<"\nStack is empty."; } else { for(i=ps->top;i>=0;i--) cout<<ps->item[i]<<"\n"; } } Output :

JESSICA E101048

Page 28

Conclusion This program efficiently implement stacks and various operations


on stacks.

JESSICA E101048

Page 29

EXPERIMENT-11
Task : WAP to implement sorting using insertion sort. Purpose By implementing this program we are capable of implementing sorting
using insertion sort.

Source Code : #include<iostream.h> #include<conio.h> #define MAX 100 int main() { int a[MAX],i,j,k,n,loc,elem; cout<<"Enter the limit: "; cin>>n; if(n>MAX) cout<<"Enter value below the limit.\n"; else { cout<<"Enter the elements:\n"; for(i=0;i<n;i++) { cin>>a[i]; } for(k=0;k<n;k++) { j=a[k]; for(i=k-1;i>=0 && j<a[i];i--) a[i+1]=a[i]; a[i+1]=j; } cout<<"\nElements after Insertion sort:\n"; for(i=0;i<n;i++) { cout<<a[i]<<"\t"; }
JESSICA E101048 Page 30

} getch(); return 0; } Output :

Conclusion This program efficiently implement sorting using insertion sort.

JESSICA E101048

Page 31

EXPERIMENT-12
Task : WAP to implement sorting using bubble sort. Purpose By implementing this program we are capable of implementing sorting
using bubble sort.

Source Code : #include<iostream.h> #include<conio.h> #define MAX 100 int main() { int a[MAX],i,j,limit,temp; cout<<"Enter no. of elements: "; cin>>limit; if(limit>MAX) { cout<<"You exceed the limit."; } else { cout<<"Enter elements: "; for(i=0;i<limit;i++) { cin>>a[i]; } for(i=0;i<limit-1;i++) { for(j=0;j<=(limit-i-1);j++) { if(a[j]>a[j+1]) { temp = a[j]; a[j]=a[j+1]; a[j+1]=temp;
JESSICA E101048 Page 32

} } } cout<<"Elements after sorting: "; for(i=0;i<limit;i++) { cout<<a[i]<<"\t"; } } getch(); return 0; } Output :

Conclusion This program efficiently implement sorting using bubble sort.

JESSICA E101048

Page 33

EXPERIMENT-13
Task : WAP to implement sorting using selection sort. Purpose By implementing this program we are capable of implementing sorting
using selection sort.

Source Code : #include<iostream.h> #include<conio.h> #define MAX 100 void ascend(int a[],int limit); void desend(int a[],int limit); int min(int a[], int k, int limit); int max(int a[], int k, int limit); int main() { int a[MAX],i,limit,n,loc,temp; cout<<"Enter no. of elements: "; cin>>limit; if(limit>MAX) { cout<<"You exceed the limit."; } else { cout<<"Enter elements: "; for(i=0;i<limit;i++) { cin>>a[i]; } cout<<"Press\n 1 for ascending the nos.\n 2 for descending the nos."; cout<<"\nEnter ur choice: "; cin>>n; switch(n) {
JESSICA E101048 Page 34

case 1: { ascend(a,limit); break; } case 2: { desend(a,limit); break; } default: cout<<"You entered the wrong choice."; } } getch(); return 0; } void ascend(int a[],int limit) { int temp,loc,i; for(i=0;i<limit;i++) { loc=min(a,i,limit); temp=a[loc]; a[loc]=a[i]; a[i]=temp; } cout<<"Elements after sorting in ascending order: \n"; for(i=0;i<limit;i++) { cout<<a[i]<<"\t"; } } void desend(int a[],int limit) { int temp,loc,i; for(i=0;i<limit;i++)
JESSICA E101048 Page 35

{ loc=max(a,i,limit); temp=a[loc]; a[loc]=a[i]; a[i]=temp; } cout<<"Elements after sorting in descending order: \n"; for(i=0;i<limit;i++) { cout<<a[i]<<"\t"; } } int min(int a[],int k,int limit) { int mini,loc,i; mini=a[k]; for(i=k;i<=limit;i++) { if(a[i]<mini) { mini=a[i]; loc=i; } } return(loc); } int max(int a[],int k,int limit) { int maxi,loc,i; maxi=a[k]; for(i=k;i<=limit;i++) { if(a[i]>maxi) { maxi=a[i]; loc=i; }
JESSICA E101048 Page 36

} return(loc); } Output :

Conclusion This program efficiently implement sorting using selection sort.

JESSICA E101048

Page 37

EXPERIMENT-14
Task : WAP to convert infix expression to postfix expression. Purpose By implementing this program we are capable of converting infix
expression to postfix expression.

Source Code : #include<iostream.h> #include<conio.h> #include<math.h> #include<stdlib.h> #define M 100 struct stack { char item[M]; int top; }; void push(struct stack *s,char c); char pop(struct stack *s); void infix_postfix(char infix[M],char postfix[M]); int priority(char c); int main() { cout<<"WRITE THE INFIX STARTING WITH ""("" & CLOSE IT WITH "")"; start: char in[M],po[M]; int l=0; cout<<"\nEnter the infix expression: "; fflush(stdin); gets(in); while(in[l]!='\0') { l++; } if(in[0]=='(' && in[l-1]==')')
JESSICA E101048 Page 38

{ infix_postfix(in,po); } else { cout<<"USE PARANTHESIS.\n"; goto start; } getch(); return 0; } void push(struct stack *s,char c) { if(s->top==(M-1)) { cout<<"\nStack Overflow."; } else { s->top++; s->item[s->top]=c; } } char pop(struct stack *s) { char c; if(s->top==(-1)) { cout<<"\nStack underflow"; } else { c=s->item[s->top]; s->top--; } return (c); }
JESSICA E101048 Page 39

int priority(char c) { if(c=='^') { return 3; } else if(c=='*'||c=='/'||c=='%') { return 2; } else if(c=='+'||c=='-') { return 1; } else { return 0; } } void infix_postfix(char infix[],char postfix[]) { struct stack s; s.top=(-1); char ch; int i=0,j=0; while(infix[i]!='\0') { if(infix[i]=='(') { push(&s,'('); } else if(infix[i]=='+'||infix[i]==''||infix[i]=='*'||infix[i]=='/'||infix[i]=='%'||infix[i]=='^') { while(priority(s.item[s.top])>=priority(infix[i])) { ch=pop(&s);
JESSICA E101048 Page 40

postfix[j]=ch; j++; } push(&s,infix[i]); } else if(infix[i]==')') { ch=pop(&s); while(ch!='(') { postfix[j]=ch; j++; ch=pop(&s); } } else { postfix[j]=infix[i]; j++; } i++; } postfix[j]='\0'; cout<<"Postfix expression is: "; puts(postfix); } Output :

JESSICA E101048

Page 41

Conclusion This program efficiently convert infix expression to postfix


expression.

JESSICA E101048

Page 42

EXPERIMENT-15
Task : WAP to evaluate the postfix expression. Purpose By implementing this program we are capable of evaluating the postfix
expression.

Source Code : #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<math.h> #define MXSTK 100 struct stack { float item[MXSTK]; int top; }; void push(struct stack *,float); float pop(struct stack *); int main() { float a,b; int i=0; float value,result,c; char exp[50]; struct stack s; s.top = -1; cout<<"Enter expression in postfix order: "<<endl; gets(exp); while(exp[i]!='\0') { if(exp[i]=='+') { a=pop(&s); b=pop(&s);
JESSICA E101048 Page 43

c=b+a; push(&s,c); } else if(exp[i]=='-') { a=pop(&s); b=pop(&s); c=b-a; push(&s,c); } else if(exp[i]=='/') { a=pop(&s); b=pop(&s); c=b/a; push(&s,c); } else if(exp[i]=='*') { a=pop(&s); b=pop(&s); c=b*a; push(&s,c); } /* else if(exp[i]=='%') { a=pop(&s); b=pop(&s); c=b%a; push(&s,c); }*/ else if(exp[i]=='^') { a=pop(&s); b=pop(&s); c=pow(b,a);
JESSICA E101048 Page 44

push(&s,c); } else { push(&s,exp[i]-'0'); } i++; } value=pop(&s); cout<<"Result after evaluation: "<<value<<endl; getch(); return 0; } void push(struct stack *ps,float x) { if(ps->top==MXSTK-1) { cout<<"stack is full"<<endl; exit(1); } ps->top++; ps->item[ps->top]=x; } float pop(struct stack *ps) { float x; if(ps->top==-1) { cout<<"Stack underflows."<<endl; getch(); exit(1); } x=ps->item[ps->top]; ps->top--; return x; }
JESSICA E101048 Page 45

Output :

Conclusion This program efficiently evaluate the postfix expression.

JESSICA E101048

Page 46

EXPERIMENT-16
Task : WAP to convert infix expression to postfix expression and evaluate the
postfix expression.

Purpose By implementing this program we are capable of converting infix


expression to postfix expression and evaluate the postfix expression.

Source Code : #include<iostream.h> #include<conio.h> #include<math.h> #include<stdlib.h> #define M 100 struct stack { char item[M]; int top; }; struct stacke { float iteme[M]; int tope; }; void pushe(struct stacke *,float); float pope(struct stacke *); void push(struct stack *s,char c); char pop(struct stack *s); void infix_postfix(char infix[M],char postfix[M]); int priority(char c); int main() { cout<<"WRITE THE INFIX STARTING WITH ""("" & CLOSE IT WITH "")"; start: char in[M],po[M]; int l=0;
JESSICA E101048 Page 47

cout<<"\nEnter the infix expression: "; fflush(stdin); gets(in); while(in[l]!='\0') { l++; } if(in[0]=='(' && in[l-1]==')') { infix_postfix(in,po); } else { cout<<"USE PARANTHESIS.\n"; goto start; } getch(); return 0; } void push(struct stack *s,char c) { if(s->top==(M-1)) { cout<<"\nStack Overflow."; } else { s->top++; s->item[s->top]=c; } } char pop(struct stack *s) { char c; if(s->top==(-1)) { cout<<"\nStack underflow";
JESSICA E101048 Page 48

} else { c=s->item[s->top]; s->top--; } return (c); } int priority(char c) { if(c=='^') { return 3; } else if(c=='*'||c=='/'||c=='%') { return 2; } else if(c=='+'||c=='-') { return 1; } else { return 0; } } void infix_postfix(char infix[],char postfix[]) { struct stack s; s.top=(-1); char ch; int i=0,j=0; while(infix[i]!='\0') { if(infix[i]=='(') {
JESSICA E101048 Page 49

push(&s,'('); } else if(infix[i]=='+'||infix[i]==''||infix[i]=='*'||infix[i]=='/'||infix[i]=='%'||infix[i]=='^') { while(priority(s.item[s.top])>=priority(infix[i])) { ch=pop(&s); postfix[j]=ch; j++; } push(&s,infix[i]); } else if(infix[i]==')') { ch=pop(&s); while(ch!='(') { postfix[j]=ch; j++; ch=pop(&s); } } else { postfix[j]=infix[i]; j++; } i++; } postfix[j]='\0'; cout<<"Postfix expression is: "; puts(postfix); struct stacke e; e.tope = -1; float a,b; int k=0;
JESSICA E101048 Page 50

float value,result,c; while(postfix[k]!='\0') { if(postfix[k]=='+') { a=pope(&e); b=pope(&e); c=b+a; pushe(&e,c); } else if(postfix[k]=='-') { a=pope(&e); b=pope(&e); c=b-a; pushe(&e,c); } else if(postfix[k]=='/') { a=pope(&e); b=pope(&e); c=b/a; pushe(&e,c); } else if(postfix[k]=='*') { a=pope(&e); b=pope(&e); c=b*a; pushe(&e,c); } else if(postfix[k]=='^') { a=pope(&e); b=pope(&e); c=pow(b,a);
JESSICA E101048 Page 51

pushe(&e,c); } else { pushe(&e,postfix[k]-'0'); } k++; } value=pope(&e); cout<<"Result after evaluation: "<<value<<endl; } void pushe(struct stacke *ps,float x) { if(ps->tope==M-1) { cout<<"stack is full"<<endl; exit(1); } ps->tope++; ps->iteme[ps->tope]=x; } float pope(struct stacke *ps) { float x; if(ps->tope==-1) { cout<<"Stack underflows."<<endl; getch(); exit(1); } x=ps->iteme[ps->tope]; ps->tope--; return x; }

JESSICA E101048

Page 52

Output :

Conclusion This program efficiently evaluate the postfix expression.

JESSICA E101048

Page 53

EXPERIMENT-17
Task : WAP to implement Quicksort using recursion. Purpose By implementing this program we are capable of implementing
Quicksort using recursion.

Source Code : #include<iostream.h> #include<conio.h> #define MAX 100 int partition(int a[],int beg,int end); void quicksort(int a[],int beg,int end); void display(int a[],int n); int main() { int a[MAX],n,i; cout<<"Enter the total no. of element: "; cin>>n; cout<<"Enter elements: "; for(i=0;i<n;i++) { cin>>a[i]; } cout<<"Unsorted elements are: "; for(i=0;i<n;i++) { cout<<a[i]<<"\t"; } quicksort(a,0,n-1); display(a,n); getch(); return 0; } int partition(int a[],int beg,int end) { int left,right,loc,temp,flag=0;
JESSICA E101048 Page 54

left=loc=beg; right=end; while(flag!=1) { left=beg; right=end; while((a[loc])<=(a[right]) &&(loc!=right)) { right--; } if(loc==right) { flag=1; } else if((a[loc])>(a[right])) { temp=a[loc]; a[loc]=a[right]; a[right]=temp; loc=right; } while((a[loc])>=(a[left]) && (loc!=left)) { left++; } if(loc==left) { flag=1; } else if(a[loc]<a[left]) { temp=a[loc]; a[loc]=a[left]; a[left]=temp; loc=left; } }
JESSICA E101048 Page 55

return(loc); } void quicksort(int a[],int beg,int end) { int loc; if(beg<end) { loc=partition(a,beg,end); quicksort(a,beg,loc-1); quicksort(a,loc+1,end); } } void display(int a[],int n) { int i; cout<<"\nSorted elements are:\n"; for(i=0;i<n;i++) { cout<<a[i]<<"\t"; } } Output :

JESSICA E101048

Page 56

Conclusion This program efficiently implement Quicksort using recursion.

JESSICA E101048

Page 57

EXPERIMENT-18
Task : WAP to implement queues and various operations on queues. Purpose By implementing this program we are capable of implementing queues
and various operations on queues.

Source Code : #include<iostream.h> #include<conio.h> #include<stdlib.h> #define max 5 struct queue { int item[max]; int front; int rear; }; int deletion(struct queue *q); void display(struct queue *q); void insert(struct queue *q,int e,int n); void insert(struct queue *q,int e,int n) { if((q->front==0 && q->rear==(n-1))||(q->front==q->rear+1)) { cout<<"\nQueue overflow"; return; } else if(q->front==-1 && q->rear==-1) { q->front=q->rear=0; } else if(q->rear==(max-1)) { q->rear=0; } else
JESSICA E101048 Page 58

{ q->rear++; } q->item[q->rear]=e; } int deletion(struct queue *q) { int e; if(q->front==(-1)) { cout<<"\nQueue underflow."; } e=q->item[q->front]; if(q->front==q->rear) { q->front=q->rear=-1; } else if(q->front==(max-1)) { q->front=0; } else { q->front++; } return (e); } void display(struct queue *q) { int i; cout<<"\nElements in queues are: "; if(q->rear<q->front) { for(i=q->front;i<max;i++) { cout<<q->item[i]<<"\t"; }
JESSICA E101048 Page 59

for(i=0;i<=q->rear;i++) { cout<<q->item[i]<<"\t"; } } else { for(i=q->front;i<=q->rear;i++) { cout<<q->item[i]<<"\t"; } } } int main() { start: int ch,n,e; struct queue q; q.front=(-1); q.rear=(-1); cout<<"Enter the limit: "; cin>>n; if(n>max) { cout<<"Out of limit.\n"; goto start; } else { while(1) { cout<<"\nPRESS\n 1. For insertion.\n 2. For Deletion.\n 3. For exit."; cout<<"\nEnter your choice: "; cin>>ch; switch(ch) { case 1:
JESSICA E101048 Page 60

{ cout<<"\nEnter the element you want to insert: "; cin>>e; insert(&q,e,n); display(&q); getch(); break; } case 2: { e=deletion(&q); cout<<"\nDeleted element is: "<<e; display(&q); getch(); break; } case 3: cout<<"Good bye."; getch(); exit(1); break; default: cout<<"You entered a wrong choice."; } } } getch(); return 0; } Output :

JESSICA E101048

Page 61

Conclusion This program efficiently implement queues and various operations


on queues.

JESSICA E101048

Page 62

EXPERIMENT-19
Task : WAP to implement One Way Linked List and perform various operations. Purpose By implementing this program we are capable of implementing One
Way Linked List and perform various operations.

Source Code : #include<iostream.h> #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> #include<process.h> #define max 100 struct node { int info; struct node *next; }; void insertb(struct node**,int item); void inserte(struct node**,int item); void inserta(struct node**,int item); void deleteb(struct node**); void deletee(struct node**); void deletea(struct node**); void traverse(struct node**); struct node *searching(struct node*,int); struct node *searching(struct node *start,int item) { int count=0; struct node *ptr; ptr=start; while(ptr!=NULL) { if(ptr->info== item) {
JESSICA E101048 Page 63

return(ptr); count++; } ptr = ptr->next; } if(count==0) { cout<<"\nItem is not present in the list.\n"; } return(ptr); } void traverse(struct node **start) { struct node *ptr; if(*start == NULL) { cout<<"\nList is empty.\n"; } else { ptr = *start; cout<<"List is: \n"; while(ptr!=NULL) { cout<<ptr->info<<"\t"; ptr=ptr->next; } } } void insertb(struct node **start,int item) { struct node *ptr; ptr=(struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { cout<<"\nLink Overflow.\n"; getch();
JESSICA E101048 Page 64

exit(1); } else { ptr->info=item; if(*start == NULL) { ptr->next = NULL; *start=ptr; } else { ptr->next = *start; *start = ptr; } } } void inserte(struct node **start,int item) { struct node *ptr,*ptr1; ptr=(struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { cout<<"\nLink Overflow.\n"; getch(); exit(1); } else { ptr->info=item; ptr1=*start; if(*start == NULL) { ptr->next = NULL; *start=ptr; } else
JESSICA E101048 Page 65

{ while((ptr1->next)!=NULL) { ptr1=ptr1->next; } ptr1->next=ptr; ptr->next=NULL; } } } void inserta(struct node **start,int item) { int info1; struct node *ptr,*ptr1,*loc; ptr=(struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { cout<<"\nLink Overflow.\n"; getch(); exit(1); } else { ptr->info=item; ptr1 = *start; cout<<"Enter the node after which node is to be insert: "; cin>>info1; loc=searching(ptr1,info1); if(loc == NULL) { cout<<"\nNo node is present.\n"; } else { ptr->next = loc->next; loc->next = ptr; }
JESSICA E101048 Page 66

} } void deleteb(struct node **start) { struct node *ptr; ptr = *start; if(*start == NULL) { cout<<"\nList is empty. So, no deletion is possible.\n"; } else { *start = (*start)->next; delete(ptr); } } void deletee(struct node **start) { struct node *ptr,*ptr1; ptr = *start; if(*start == NULL) { cout<<"\nList is empty. So, no deletion is possible.\n"; } else if(ptr->next == NULL) { *start = NULL; delete(ptr); } else { while((ptr->next)!=NULL) { ptr1 = ptr; ptr = ptr->next; } ptr1->next= NULL;
JESSICA E101048 Page 67

delete(ptr); } } void deletea(struct node **start) { struct node *ptr,*loc; int info1; ptr = *start; cout<<"Enter the node infon after which you wanna perform deletion: "; cin>>info1; loc=searching(ptr,info1); if(*start == NULL) { cout<<"\nList is empty. So, no deletion is possible.\n"; } else if(loc == NULL) { cout<<"\nNo node present. So, no deletion.\n"; } else if(loc->next == NULL) { cout<<"\nThis is the last node. So, no deletion possible.\n"; } else { ptr = loc->next; loc->next = ptr->next; delete(ptr); } } int main() { struct node *start; int c,item; start=NULL; while(1) {
JESSICA E101048 Page 68

cout<<"\nPRESS\n 1. To insert at beginning.\n 2. To insert at end.\n 3. To insert after a node.\n"; cout<<" 4. To delete node from beginning.\n 5. To delete node from end.\n 6. To delete node after a given node.\n 7. To exit.\n"; cout<<"Enter your choice: "; cin>>c; switch(c) { case 1: { cout<<"Enter item to insert: "; cin>>item; insertb(&start,item); traverse(&start); getch(); break; } case 2: { cout<<"Enter item to insert: "; cin>>item; inserte(&start,item); traverse(&start); getch(); break; } case 3: { cout<<"Enter item to insert: "; cin>>item; inserta(&start,item); traverse(&start); getch(); break; } case 4: {
JESSICA E101048 Page 69

deleteb(&start); traverse(&start); getch(); break; } case 5: { deletee(&start); traverse(&start); getch(); break; } case 6: { deletea(&start); traverse(&start); getch(); break; } case 7: cout<<"GOOD BYE."; getch(); exit(1); break; default: cout<<"You entered a wrong choice."; } } getch(); return 0; } Output :

JESSICA E101048

Page 70

JESSICA E101048

Page 71

Conclusion This program efficiently implement One Way Linked List and
perform various operations.

JESSICA E101048

Page 72

EXPERIMENT-20
Task : WAP to implement Two Way Linked List and perform various operations. Purpose By implementing this program we are capable of implementing Two
Way Linked List and perform various operations.

Source Code : #include<iostream.h> #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> #include<process.h> #define max 100 struct node { int info; struct node *next; struct node *prev; }; void insertb(struct node **,int item); void inserte(struct node **,int item); void inserta(struct node **,int item); void deleteb(struct node **); void deletee(struct node **); void deletea(struct node **); void traverse(struct node **); struct node *searching(struct node *,int); struct node *searching(struct node *start,int item) { int count=0; struct node *ptr; ptr=start; while(ptr!=NULL) { if(ptr->info== item)
JESSICA E101048 Page 73

{ return(ptr); count++; } ptr = ptr->next; } if(count==0) { cout<<"\nItem is not present in the list.\n"; } return(ptr); } void traverse(struct node **start) { char c; struct node *ptr; if(*start == NULL) { cout<<"\nList is empty.\n"; } else { ptr = *start; while(ptr->next!=NULL) { cout<<"Adressing PREVIOUS NODE: "<<ptr->prev<<"\nAdressing NEXT NODE: "<<ptr->next<<"\nAdressing of NODE: "<<ptr<<"\nDATA: "<<ptr->info<<"\n"; ptr=ptr->next; } cout<<"Adressing PREVIOUS NODE: "<<ptr->prev<<"\nAdressing NEXT NODE: "<<ptr->next<<" \nAdressing of NODE: "<<ptr<<"\nDATA: "<<ptr->info<<"\n"; cout<<"\n\nEnter Y if traverse in reverse order: "; cin>>c; if( c=='Y' || c=='y') {
JESSICA E101048 Page 74

cout<<"List is: \n"; while(ptr!=NULL) { cout<<ptr->info<<"\t"; ptr=ptr->prev; } } } } void insertb(struct node **start,int item) { struct node *ptr,newp; ptr=(struct node *)malloc(sizeof(struct node)); ptr->info=item; if(ptr == NULL) { cout<<"\nLink Overflow.\n"; getch(); exit(1); } else if(*start==NULL) { ptr->next = ptr->prev = NULL; *start=ptr; } else { ptr->prev = NULL; ptr->next = *start; (*start)->prev = ptr; *start = ptr; } } void inserte(struct node **start,int item) { struct node *ptr,*ptr1; ptr=(struct node *)malloc(sizeof(struct node));
JESSICA E101048 Page 75

if(ptr == NULL) { cout<<"\nLink Overflow.\n"; getch(); exit(1); } else { ptr->info=item; ptr1=*start; if(*start == NULL) { ptr->prev = NULL; ptr->next = *start; (*start)->prev = ptr; *start = ptr; } else { while((ptr1->next)!=NULL) { ptr1=ptr1->next; } ptr1->next=ptr; ptr->next=NULL; ptr->prev=ptr1; } } } void inserta(struct node **start,int item) { int info1; struct node *ptr,*ptr1,*loc; ptr=(struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { cout<<"\nLink Overflow.\n";
JESSICA E101048 Page 76

getch(); exit(1); } else { ptr->info=item; ptr1 = *start; cout<<"Enter the node after which node is to be insert: "; cin>>info1; loc=searching(ptr1,info1); if(loc == NULL) { cout<<"\nNo node is present.\n"; } else if(loc->next==NULL) { ptr->prev = loc; ptr->next = loc->next; loc->next = ptr; } else { ptr->prev = loc; ptr->next = loc->next; loc->next->prev = ptr; loc->next = ptr; } } } void deleteb(struct node **start) { struct node *ptr; ptr = *start; if(*start == NULL) { cout<<"\nList is empty. So, no deletion is possible.\n";
JESSICA E101048 Page 77

} else { (*start)->next->prev= NULL; *start = (*start)->next; delete(ptr); } } void deletee(struct node **start) { struct node *ptr; ptr = *start; if(*start == NULL) { cout<<"\nList is empty. So, no deletion is possible.\n"; } else { while((ptr->next)!=NULL) { ptr = ptr->next; } ptr->prev->next=NULL; delete(ptr); } } void deletea(struct node **start) { struct node *ptr,*loc; int info1; ptr = *start; cout<<"Enter the node infon after which you wanna perform deletion: "; cin>>info1; loc=searching(ptr,info1); if(*start == NULL) {
JESSICA E101048 Page 78

cout<<"\nList is empty. So, no deletion is possible.\n"; } else if(loc == NULL) { cout<<"\nNo node present. So, no deletion.\n"; } else if(loc->next == NULL) { cout<<"\nThis is the last node. So, no deletion possible.\n"; } else { ptr = loc->next; if(ptr->next==NULL) { loc->next = ptr->next; delete(ptr); } else { ptr->next->prev=loc; loc->next=ptr->next; delete(ptr); } } } int main() { struct node *start; int c,item; start=NULL; while(1) { cout<<"\nPRESS\n 1. To insert at beginning.\n 2. To insert at end.\n 3. To insert after a node.\n";
JESSICA E101048 Page 79

cout<<" 4. To delete node from beginning.\n 5. To delete node from end.\n 6. To delete node after a given node.\n 7. To exit.\n"; cout<<"Enter your choice: "; cin>>c; switch(c) { case 1: { cout<<"Enter item to insert: "; cin>>item; insertb(&start,item); traverse(&start); getch(); break; } case 2: { cout<<"Enter item to insert: "; cin>>item; inserte(&start,item); traverse(&start); getch(); break; } case 3: { cout<<"Enter item to insert: "; cin>>item; inserta(&start,item); traverse(&start); getch(); break; } case 4: { deleteb(&start); traverse(&start);
JESSICA E101048 Page 80

getch(); break; } case 5: { deletee(&start); traverse(&start); getch(); break; } case 6: { deletea(&start); traverse(&start); getch(); break; } case 7: cout<<"GOOD BYE."; getch(); exit(1); break; default: cout<<"You entered a wrong choice."; } } getch(); return 0; } Output :

JESSICA E101048

Page 81

JESSICA E101048

Page 82

JESSICA E101048

Page 83

JESSICA E101048

Page 84

Conclusion This program efficiently implement Two Way Linked List and
perform various operations.

JESSICA E101048

Page 85

EXPERIMENT-21
Task : WAP to check the equality between two link lists via entering integer in
each node of the link list. Purpose By implementing this program we are capable of checking the equality between two link lists via entering integer in each node of the link list.

Source Code : #include<iostream.h> #include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> #include<process.h> #define max 100 struct node { int info; struct node *next; }; void inserte(struct node**,int item); void traverse(struct node**); void equality( struct node **, struct node **); void equality( struct node **start1, struct node **start2) { int c1=0,c2=0,c3=0; struct node *ptr1,*ptr2; ptr1=*start1; ptr2=*start2; while(ptr1!=NULL) { c1++; ptr1=ptr1->next; } while(ptr2!=NULL) {
JESSICA E101048 Page 86

c2++; ptr2=ptr2->next; } if(c1!=c2) { cout<<"\n Both the list are not equal."; } else { while(ptr1!=NULL) { if(ptr1->info==ptr2->info) { c3++; } ptr1=ptr1->next; ptr2=ptr2->next; } if(c3==c1) { cout<<"\nList are equal.\n"; } else { cout<<"\nList are not equal.\n"; } } } void traverse(struct node **start) { struct node *ptr; if(*start == NULL) { cout<<"\nList is empty.\n"; } else {
JESSICA E101048 Page 87

ptr = *start; cout<<"List is: \n"; while(ptr!=NULL) { cout<<ptr->info<<"\t"; ptr=ptr->next; } } } void inserte(struct node **start,int item) { struct node *ptr,*ptr1; ptr=(struct node *)malloc(sizeof(struct node)); if(ptr == NULL) { cout<<"\nLink Overflow.\n"; getch(); exit(1); } else { ptr->info=item; ptr1=*start; if(*start == NULL) { ptr->next = NULL; *start=ptr; } else { while((ptr1->next)!=NULL) { ptr1=ptr1->next; } ptr1->next=ptr; ptr->next=NULL; }
JESSICA E101048 Page 88

} } int main() { struct node *start1,*start2; int item,c,n,i; start1=start2=NULL; while(1) { cout<<"\nPRESS\n 1. To insert elements in First Link List.\n 2. To insert elements in Second Link List.\n 3. To check the EQUALITY.\n 4. To Exit.\n"; cout<<"Enter your choice: "; cin>>c; switch(c) { case 1: { cout<<"Enter the total no of element you want to enter: "; cin>>n; cout<<"Enter elements:\n"; for(i=0;i<n;i++) { cin>>item; inserte(&start1,item); } traverse(&start1); getch(); break; } case 2: { cout<<"Enter the total no of element you want to enter: "; cin>>n; cout<<"Enter elements:\n"; for(i=0;i<n;i++) {
JESSICA E101048 Page 89

cin>>item; inserte(&start2,item); } traverse(&start2); getch(); break; } case 3: { equality(&start1,&start2); break; } case 4: cout<<"GOOD BYE."; getch(); exit(1); break; default: cout<<"You entered a wrong choice."; } } getch(); return 0; }

Output :

JESSICA E101048

Page 90

JESSICA E101048

Page 91

Conclusion This program efficiently check the equality between two link lists
via entering integer in each node of the link list.

JESSICA E101048

Page 92

EXPERIMENT-22
Task : WAP to implement BINARY SEARCH TREE. Purpose By implementing this program we are capable of implementing
BINARY SEARCH TREE.

Source Code : #include<alloc.h> #include<conio.h> #include<process.h> #include<iostream.h> struct bst { bst *left; bst *right; int info; }; struct bst *root=NULL; void bst_insert(struct bst ** ,int item); void bst_search(struct bst ** ,int item); void preorder(struct bst *ptr); void inorder(struct bst *ptr); void postorder(struct bst *ptr); void bst_insert(struct bst **root, int item) { struct bst *ptr,*ptr1,*save; ptr=(struct bst*) malloc(sizeof(struct bst)); ptr->info=item; ptr->left=ptr->right=NULL; if(ptr==NULL) { cout<<"OVERFLOW"; getch(); exit(1); } if(*root==NULL)
JESSICA E101048 Page 93

{ *root=ptr; } else { ptr1=*root; while(ptr1!=NULL) { if(ptr1->info==item) { cout<<"Redundancy Not allowed"; return; } else if(item < ptr1->info) { save=ptr1; ptr1=ptr1->left; } else { save=ptr1; ptr1=ptr1->right; } } if(item < save->info) save->left= ptr ; else save->right= ptr ; } } void bst_search(struct bst **root,int item) { int flag; struct bst *ptr; if(*root == NULL) { cout<<"Tree Empty";
JESSICA E101048 Page 94

return; } else { ptr = *root; while(ptr!= NULL) { if(ptr ->info == item) { cout<<"Element "<<ptr->info<<" is at location "<<ptr; break; } else if(item < ptr->info) ptr=ptr->left; else ptr=ptr->right; if(ptr==NULL) { cout<<"Search Unsuccessful\n"; cout<<"Press 1 to Insert it to the tree: "; cin>>flag; if(flag==1) { bst_insert(root,item); } } } } } void preorder(struct bst *ptr) { if(ptr == NULL) return; cout<<ptr->info<<"\t"; preorder(ptr->left); preorder(ptr->right); }
JESSICA E101048 Page 95

void inorder(struct bst *ptr) { if(ptr == NULL) return; inorder(ptr->left); cout<<ptr->info<<"\t"; inorder(ptr->right); } void postorder(struct bst *ptr) { if(ptr == NULL) return; postorder(ptr->left); postorder(ptr->right); cout<<ptr->info<<"\t"; } int main() { int ch,item,temp; while(1) { cout<<"\nPRESS\n 1 for Insertion\n 2 for Traversing in Preorder\n 3 for Traversing in Inorder\n 4 for Traversing in Postorder"; cout<<"\n 5 for Searching an element\n 6 for Exit"; cout<<"\nEnter your choice: "; cin>>ch; switch(ch) { case 1: cout<<"Enter the item: "; cin>>item; bst_insert(&root,item); break; case 2: cout<<"\nPreorder is:\n"; preorder(root);
JESSICA E101048 Page 96

break; case 3: cout<<"\nInorder is:\n"; inorder(root); break; case 4: cout<<"\nPostorder is:\n"; postorder(root); break; case 5: cout<<"\nEnter item to be searched:\n"; cin>>item; bst_search(&root,item); break; case 6: cout<<"Good bye."; getch(); exit(1); break; default: cout<<"Wrong Choice"; } } }

Output :

JESSICA E101048

Page 97

JESSICA E101048

Page 98

Conclusion This program efficiently implement BINARY SEARCH TREE.

JESSICA E101048

Page 99

You might also like