You are on page 1of 22

1. Convert a prefix notation to postfix notation.

include<iostream.h>
#include<conio.h>
char infix[50],postfix[50];
char stack[50];
int top=-1;
void main()
{
char is;
int i,k=0;
void push(char);
char pop();
int pre(char);
clrscr();
cout<<"Enter any infix expression\n";
cin>>infix;
push('#');
for(i=0;infix[i];i++)
{
is=infix[i];
if(is==' ')
continue;
while(pre(is)<=pre(stack[top]))
{
if(stack[top]=='(')
break;
postfix[k]=pop();
k++;
}
if(stack[top]=='('&& is==')')
pop();
else
push(is);
}
while(stack[top]!='#')
{
postfix[k]=pop();
k++;
}
postfix[k]='\0';
cout<<"postfix expression is\n"<<postfix;
getch();
}
void push(char ch)
{
top++;
stack[top]=ch;
}
char pop()
{
char ch;
ch=stack[top];
top--;
return(ch);
}
int pre(char ch)
{
switch(ch)
{
case '+':
case '-':return(1);
case '*':
case '/':return(2);
case '^':
case '$':return(3);
case '#':return(-1);
case '(':return(5);
case ')':return(0);
default:return(4);
}
}

2. Evaluate a given postfix expression and its values for the


variables.
#include<iostream.h>
#inclucinde<conio.h>
#include<math.h>
float stack[10];
int top=-1;
void main()
{
void push(float);
float pop();
float cal(float,float,char);
char postfix[20];
char is;
int i;
float op1,op2,res;
clrscr();
cout<<"Postfix expression\n";
cin>>postfix;
for(i=0;postfix[i];i++)
{
is=postfix[i];
if(is>='0'&&is<='9')
push(is-'0');
else
{
op2=pop();
op1=pop();
res=cal(op1,op2,is);
push(res);
}
}
cout<<"Result is="<<res;
getch();
}
void push(float num)
{
top++;
stack[top]=num;
}
float pop()
{
float res;
res=stack[top];
top--;
return(res);
}
float cal(float op1,float op2,char ch)
{
float res;
switch(ch)
{
case '+':res=op1+op2;
break;
case '-':res=op1-op2;
break;
case '*':res=op1*op2;
break;
case '/':res=op1/op2;
break;
case '^':
case '$':res=pow(op1,op2);
break;
}
return(res);
}

3. Simulate the working of circular queue providing the


following operations–Insert, Delete and Display.
#include<iostream.h>
#include<conio.h>
int a[5];
int front=-1,rear=-1,count=0;
void main()
{
int choice;
void insert();
void delete1();
void display();
do
{
clrscr();
cout<<"1.insert \n 2.delete \n 3.display \n 4.exit \n";
cout<<"enter your choice \n";
cin>>choice;
switch(choice)
{
case 1: insert();
break;
case 2: delete1();
break;
case 3: display();
break;
}
}
while(choice!=4);
}
void insert()
{
if(count==5)
{
cout<<"queue overflow \n";
getch();
return;
}
if(rear==-1)
front=0;
rear=(rear+1)%5;
count++;
cout<<"enter the number \n";
cin>>a[rear];
}
void delete1()
{
if(count==0)
{
cout<<"queue underflow \n";
getch();
return;
}
cout<<"deleted item is \n"<<a[front];
getch();
front=(front+1)%5;
count--;
if(count==0)
front=rear=-1;
}
void display()
{
int i,j;
if(count==0)
{
cout<<"queue is empty \n";
getch();
return;
}
cout<<"contents of queue is \n";
for(i=front,j=0;j<count;j++)
{
cout<<a[i];
i=(i+1)%5;
}
getch();
return;
}

4. Demonstrate recursion –
a. Calculate GCD and LCM of 3 integer numbers
b. Solve Towers of Hanoi problem
c. Calculate the sum for a given number ‘n’ from 1 to n.

A.Program to calculate GCD and LCM of 3 integer numbers.

#include<iostream.h>
#include<conio.h>
int GCD(int m, int n)
{
if (n==0)
return m;
return GCD(n,m%n);
}
int LCM(int m, int n)
{
return m*n/GCD(m,n);
}
void main()
{
int m,n,p,gcd,lcm;
cout<<"Enter m, n and p”;
cin>>m>>n>>p;
gcd=GCD(m,GCD(n,p));
lcm=LCM(m,LCM(n,p));
cout<<”GCD=”<<gcd;
cout<<”lcm=”<<lcm;
getch();
}

B. Program to solve Towers of Hanoi problem


#include<iostream.h>
#include<conio.h>
int count=0;
void tower(int n, char s, char t, char d)
{
if(n==1)
{
Cout<<“Move disc 1 from”<<s<<d;
count ++;
return;
}
tower(n-1, s, d, t);
cout<<“Move disc from”<<n<<s<<d;
count ++;
tower(n-1, t, s, d);
}
void main()
{
int n;
clrscr();
cout<<“Enter the number of discs”;
cin>>n;
tower(n, ‘A’,’B’,’C’);
cout<<“Total number of moves”<<count;
getch();
}

C.Program to calculate the sum for a given number ‘n’ from 1


to n.
#include<iostream.h>
#include<conio.h>
int sum(int n)
{
if (n==1)
return n;
return n+sum(n-1);
}
void main()
{
int n, s;
cout<<"Enter n”;
cin>>n;
s=sum(n);
cout<<”s”;
getch();
}
5. Simulate the working of a linked list providing the
following operations.
a. Insert at the beginning
b. Insert at the end
c. Insert before a given element
d. Insert at the position
e. Display
#include<iostream.h>
#include<alloc.h>
#include<stdlib.h>
struct node
{
int num;
struct node*next;
};
typedef struct node *Node;
Node getnode()
{
Node x;
x=(Node)malloc(sizeof(struct node));
if(x==NULL)
{
cout<<"no memory in heap";
exit(0);
}
return x;
}
Node insert_front(int item,Node first)
{
Node nn;
nn=getnode();
nn->num=item;
nn->next=first;
return nn;
}
void display(Node first)
{
Node temp;
if(first==NULL)
{
cout<<"no elements";
return;
}
cout<<"list is";
temp=first;
while(temp!=NULL)
{
cout<<temp->num;
temp=temp->next;
}
}
Node insert_rear(int item,Node first)
{
Node nn,cur;
nn=getnode();
nn->num=item;
nn->next=NULL;
if(first==NULL)
{
return nn;
}
cur=first;
while(cur->next!=NULL)
{
cur=cur->next;
}
cur->next=nn;
return first;
}
Node insert_pos(int item,int pos,Node first)
{
Node nn,cur,prev;
int p;
nn=getnode();
nn->num=item;
nn->next=NULL;
if(pos==1)
{
nn->next=first;
return nn;
}
cur=first;
prev=NULL;
p=1;
while(cur!=NULL && p!=pos)
{
prev=cur;
cur=cur->next;
p++;
}
if(p==pos)
{
prev->next=nn;
nn->next=cur;
return first;
}
else
{
cout<<"Invalid position";
return first;
}
}
Node insert_before(int key,int item,Node first)
{
Node nn,prev,cur;
nn=getnode();
nn->num=item;
nn->next=NULL;
if(first==NULL)
{
cout<<"Invalid request";
return first;
}
if(key==first->num)
{
nn->next=first;
return nn;
}
prev=NULL;
cur=first;
while(key!=cur->num && cur!=NULL)
{
prev=cur;
cur=cur->next;
}
if(cur==NULL)
{
cout<<"key not found";
return first;
}
else
{
prev->next=nn;
nn->next=cur;
return first;
}
}
void main()
{
int choice,item,key,pos;
Node first=NULL;
for(;;)
{
cout<<"1.insert_front\n 2.insert_rear\n 3.display\n
4.insert_pos\n 5.insert_before\n";
cout<<"enter your choice";
cin>>choice;
switch(choice)
{
case 1:cout<<"enter item";
cin>>item;
first=insert_front(item,first);
break;
case 2:cout<<"enter item";
cin>>item;
first=insert_rear(item,first);
break;
case 3:display(first);
break;
case 4:cout<<"enter item";
cin>>item;
cout<<"enter the pos";
cin>>pos;
if(pos<=0)
cout<<"invalid position";
else
first=insert_pos(item,pos,first);
break;
case 5:cout<<"enter item";
cin>>item;
cout<<"enter key,before which new item
inserted";
cin>>key;
first=insert_before(key,item,first);
break;
default:exit(0);
}
}
}
6. Simulate the working of a circular linked list providing
the following operations. a. Delete from the beginning
b. Delete from the end
c. Delete a given element
d. Delete every alternate element. e.display
#include<iostream.h>
#include<alloc.h>
#include<stdlib.h>
struct node
{
int num;
struct node*next;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
cout<<"no memory in heap";
exit(0);
}
return x;
}
NODE insert_front(int item,NODE first)
{
NODE nn;
nn=getnode();
nn->num=item;
nn->next=nn;
if(first==NULL)
return nn;
nn->next=first->next;
first->next=nn;
return first;
}
void display(NODE first)
{
NODE temp;
if(first==NULL)
{
cout<<"No elements to display";
return;
}
temp=first->next;
cout<<"The contents of the list"<<endl;
while(temp!=first)
{
cout<<temp->num;
temp=temp->next;
}
cout<<temp->num;
}
NODE delete_front(NODE first)
{
NODE nn;
if(first==NULL)
{
cout<<"no elements to delete"<<endl;
return NULL;
}
if(first->next==first)
{
cout<<"The item deleted is"<<first->num;
free(first);
return NULL;
}
nn=first->next;
first->next=nn->next;
cout<<"item deleted is"<<nn->num;
free(nn);
return first;
}
NODE delete_rear(NODE first)
{
NODE prev;
if(first==NULL)
{
cout<<"no elements to delete"<<endl;
return NULL;
}
if(first->next==first)
{
cout<<"the item deleted is"<<first->num;
free(first);
return NULL;
}
prev=first->next;;
while(prev->next!=first)
prev=prev->next;
prev->next=first->next;
cout<<"\n deleted element is"<<first->num;
free(first);
return prev;
}
NODE del_item(int key,NODE first)
{
NODE prev,cur;
if(first==NULL)
{
cout<<"\nlist is empty\n";
return first;
}
if(first==first->next && key==first->num)
{
cout<<"\n deleteditem is"<<first->num;
free(first);
return NULL;
}
prev=first;
cur=first->next;
while(cur!=first && key!=cur->num)
{
prev=cur;
cur=cur->next;
}
if(key==cur->num)
{
prev->next=cur->next;
cout<<"\n deleted item is"<<cur->num;
if(cur==first)
first=prev;
free(cur);
}
else
cout<<"\n Key not found\n";
return first;
}
NODE del_alternate(NODE first)
{
NODE cur,prev;
int count=1,pos=1;
if(first==NULL)
{
cout<<"\nList is empty";
return first;
}
if(first==first->next)
{
cout<<"\ndeleted item is"<<first->num;
free(first);
return NULL;
}
prev=first;
cur=first->next;
while(cur!=first)
{
if(pos%2!=0)
{
NODE nn=cur;
cout<<"\n deleted item is"<<nn->num;
prev->next=cur->next;
prev=cur=cur->next;
free(nn);
}
else
cur=cur->next;
pos++;
}
if(cur==first&&pos%2!=0)
{
cout<<"\n deleted item is"<<cur->num;
prev->next=cur->next;
free(cur);
return prev;
}
else
return first;
}
void main()
{
int opt,item,key;
NODE first=NULL;
for(;;)
{
cout<<"\n1.insert_front\n2.delete_front\n3.delete_rear\n4.display\n
5.delete a given element\n6.delete alternative element\n";
cout<<"enter your option";
cin>>opt;
switch(opt)
{
case 1:cout<<"\nenter item";
cin>>item;
first=insert_front(item,first);
break;
case 2:first=delete_front(first);
break;
case 3:first=delete_rear(first);
break;
case 4:display(first);
break;
case 5:cout<<"enter key element to be deleted";
cin>>key;
first=del_item(key,first);
break;
case 6:first=del_alternate(first);
break;
default:exit(0);
}
}
}

7. Simulate the working of a dequeue


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#define size 5
int queue[size];
int f=-1;
int r=-1;
int count=0;
void insert_rear(int x)
{
if(f==(r+1)%size)
{
cout<<"Queue overflow";
}
else if(r==-1)
{
f=0;
r=0;
queue[r]=x;
}
else
{
r=(r+1)%size;
queue[r]=x;
count++;
}
}
void insert_front(int x)
{
if(f==(r+1)%size)
{
cout<<"Queue overflow";
}
else if(r==-1)
{
f=0;
r=0;
queue[r]=x;
}
else
{
f=(f+(size-1))%size;
queue[f]=x;
count++;
}
}
int delete_rear()
{
int x;
if(f==-1)
{
cout<<"Queue underflow";
}
else if(f==r)
{
x=queue[f];
f=-1;
r=-1;
count=0;
}
else
{
r=queue[r];
r=(r+(size-1))%size;
count--;
}
return x;
}
int delete_front()
{
int x;
if(f==-1)
{
cout<<"Queue underflow";
}
else if(f==r)
{
x=queue[f];
f=-1;
r=-1;
count=0;
}
else
{
x=queue[f];
f=(f+1)%size;
count--;
}
return x;
}
void display()
{
int i,j;
i=f;
j=0;
cout<<"Queue is";
while(j<=count)
{
cout<<queue[i];
j++;
i=(i+1)%size;
}
}
void main()
{
int choice;
int x;
clrscr();
while(1)
{
cout<<"\n1.insert_rear\n 2.insert_front\n 3.delete_rear\n
4.delete_front\n .5.display\n 6.exit";
cout<<"\n Enter your choice";
cin>>choice;
switch(choice)
{
case 1:cout<<"Enter integer data";
cin>>x;
insert_rear(x);
break;
case 2:cout<<"Enter integer data";
cin>>x;
insert_front(x);
break;
case 3:cout<<"Delete data from rear";
delete_rear();
break;
case 4:cout<<"Delete data from front";
delete_front();
break;
case 5:display();
break;
case 6:exit(0);
}
}
}

8. Simulate the working of a double linked list to implement


stack and queue
#include<iostream.h>
#include<alloc.h>
#include<stdlib.h>
struct node
{
struct node*llink;
int num;
struct node *rlink;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
cout<<"no memory in heap";
exit(0);
}
return x;
}
NODE insert_front(int item,NODE first)
{
NODE nn;
nn=getnode();
nn->llink=NULL;
nn->num=item;
nn->rlink=first;
first->llink=nn;
return nn;
}
void display(NODE first)
{
NODE temp;
if(first==NULL)
{
cout<<"list is empty\n";
return;
}
temp=first;
cout<<"\nthe contents of list:\n";
while(temp!=NULL)
{
cout<<"\n"<<temp->num;
temp=temp->rlink;
}
}
NODE delete_front(NODE first)
{
NODE nn;
if(first==NULL)
{
cout<<"\nlist is empty";
return first;
}
nn=first;
cout<<"\ndeleted item is"<<nn->num;
first=first->rlink;
first->llink=NULL;
free(nn);
return first;
}
NODE insert_rear(int item,NODE first)
{
NODE nn,cur;
nn=getnode();
nn->llink=NULL;
nn->num=item;
nn->rlink=NULL;
if(first==NULL)
return nn;
cur=first;
while(cur->rlink!=NULL)
cur=cur->rlink;
cur->rlink=nn;
nn->llink=cur;
return first;
}
void main()
{
int opt,ch,item;
NODE first=NULL;
cout<<"\n1.stack implementation\n2.Queue implementation\n 3.exit\n";
cout<<"enter your choice";
cin>>ch;
switch(ch)
{
case 1:cout<<"\n***Stack using double linked list***\n";
for(;;)
{
cout<<"\n1.push\n2.pop\n3.display\n4.exit";
cout<<"\nEnter your option";
cin>>opt;
switch(opt)
{
case 1:cout<<"\nEnter item to be inserted";
cin>>item;
first=insert_front(item,first);
break;
case 2:first=delete_front(first);
break;
case 3:display(first);
break;
default:exit(0);
break;
}
}
case 2:cout<<"\n***Queue using Double linked list***\n";
for(;;)
{
cout<<"1.insert\n2.delete\n3.display\n4.exit";
cout<<"\nEnter your option:";
cin>>opt;
switch(opt)
{
case 1:cout<<"\nEnter item to be inserted";
cin>>item;
first=insert_rear(item,first);
break;
case 2:first=delete_front(first);
break;
case 3:display(first);
break;
default:exit(0);
break;
}
}
default:exit(0);
}
}

9. Create a binary tree and implement the tree traversal


techniques of inorder,preorder and postorder.
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int num;
struct node *left,*right;
};
typedef struct node NODE;
NODE *root=NULL;
void main()
{
clrscr();
void insert();
void display();
void inorder(NODE *);
void preorder(NODE *);
void postorder(NODE *);
int choice;
do
{
cout<<"\n1.insert\n 2.display\n 3.Exit";
cout<<"\nEnter your choice";
cin>>choice;
switch(choice)
{
case 1:insert();
break;
case 2:if(root==NULL)
cout<<"Tree is empty";
else
{
cout<<"Inorder Display is\n";
inorder(root);
cout<<"\npreorder Display is\n";
preorder(root);
cout<<"\npostorder Display is\n";
postorder(root);
}
getch();
break;
}
}
while(choice!=3);
}
void insert()
{
NODE *nn,*temp,*prev;
nn=(NODE *)malloc(sizeof(NODE));
nn->left=nn->right=NULL;
cout<<"Enter the no";
cin>>nn->num;
if(root==NULL)
{
root=nn;
return;
}
temp=root;
while(temp!=NULL)
{
prev=temp;
if(nn->num>temp->num)
temp=temp->right;
else
temp=temp->left;
}
if(nn->num>prev->num)
prev->right=nn;
else
prev->left=nn;
}
void inorder(NODE *root1)
{
if(root1!=NULL)
{
inorder(root1->left);
cout<<root1->num;
inorder(root1->right);
}
}
void preorder(NODE *root1)
{
if(root1!=NULL)
{
cout<<root1->num;
preorder(root1->left);
preorder(root1->right);
}
}
void postorder(NODE *root1)
{
if(root1!=NULL)
{
postorder(root1->left);
postorder(root1->right);
cout<<root1->num;
}
}

10. Implement quick sort.


#include<iostream.h>
#include<conio.h>
int partition(int a[],int low,int high);
void quick(int a[],int low,int high);
void main()
{
int a[100],n,i;
clrscr();
cout<<"Enter array size:";
cin>>n;
cout<<"\nEnter"<<n<<"nos\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nArray before sorting\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
quick(a,0,n-1);
cout<<"\nArray after sorting\n";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
getch();
}
void quick(int a[],int low,int high)
{
int j;
if(low<high)
{
j=partition(a,low,high);
quick(a,low,j-1);
quick(a,j+1,high);
}
}
int partition(int a[],int low,int high)
{
int i,j,num,temp;
num=a[low];
i=low+1;
j=high;
while(1)
{
while(i<high && num>a[i])
i++;
while(num<a[j])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
else
{
temp=a[low];
a[low]=a[j];
a[j]=temp;
return(j);
}
}
}

11. Implement a program to merge two doubly linked list.

#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
struct node
{
int data;
struct node *prev;
struct node *next;
};
struct node *list=NULL;
struct node *list_last=NULL;

struct node *even=NULL;


struct node *even_last=NULL;

struct node *odd=NULL;


struct node *odd_last=NULL;

struct node *current=NULL;

void insert(int data)


{
struct node *link=(struct node*)malloc(sizeof(struct node));
link->data=data;
link->prev=NULL;
link->next=NULL;
if(data%2==0)
{
if(even==NULL)
{
even=link;
return;
}
else
{
current=even;
while(current->next!=NULL)
current=current->next;
while(current->next!=NULL)
current=current->next;
current->next=link;
even_last=link;
link->prev=current;
}
}
else
{
if(odd==NULL)
{
odd=link;
return;
}
else
{
current=odd;
while(current->next!=NULL)
current=current->next;
current->next=link;
odd_last=link;
link->prev=current;
}
}
}
void print_backward(struct node *head)
{
struct node *ptr=head;
cout<<"\n[last]<=>";
while(ptr!=NULL)
{
cout<<"<=>"<<ptr->data;
ptr=ptr->prev;
}
cout<<"[head]\n";
}
void printList(struct node *head)
{
struct node *ptr=head;
cout<<"\n[head]<=>";
while(ptr!=NULL)
{
cout<<"<=>"<<ptr->data;
ptr=ptr->next;
}
cout<<"[last]\n";
}
void combine()
{
struct node *link;
list=even;
link=list;
while(link->next!=NULL)
{
link=link->next;
}
link->next=odd;
odd->prev=link;
while(link->next!=NULL)
{
link=link->next;
}
list_last=link;
}
int main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
insert(i);
cout<<"Even:";
printList(even);
cout<<"Even (R):";
print_backward(even_last);
cout<<"Odd:";
printList(odd);
cout<<"Odd (R)";
print_backward(odd_last);
combine();
cout<<"Combined List:\n";
printList(list);
cout<<"Combined List (R):\n";
print_backward(list_last);
getch();
return 0;
}

12. Implement the search techniques of


a. Linear search
b. Binary search

Program for Linear search


#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],key,i,n,flag=0;
clrscr();
cout<<"Enter the no of elements into the array";
cin>>n;
cout<<"Enter the elements";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the key no to be searched";
cin>>key;
for(i=0;i<n;i++)
{
if(a[i]==key)
{
flag=1;
break;
}
}
if(flag==1)
cout<<"Search is successfull";
else
cout<<"Search is failed";
getch();
}
B. Program for binary search
#include<iostream.h>
#include<conio.h>
void main()
{
int a[100],n,key,i,flag,low,high,mp;
clrscr();
cout<<"Enter the size of the array";
cin>>n;
cout<<"Enter the no in assending order";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"Enter the no to be searched";
cin>>key;
low=0,high=n-1,flag=0;
while(low<=high)
{
mp=(low+high)/2;
if(a[mp]==key)
{
flag=1;
break;
}
if(key>a[mp])
low=mp+1;
else
high=mp-1;
}
if(flag==1)
cout<<"Successfully searched the position"<<mp;
else
cout<<"Search failed";
getch();
}

You might also like