Vous êtes sur la page 1sur 31

1.

Using circular representation for a polynomial, design, develop, and execute a program in C
to accept two polynomial, add them, and then print the resulting polynomial.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<malloc.h>
struct node
{
int coeff,exp;
struct node *link;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf(Out of memory);
exit(0);
}
return x;
}
int COMPARE(int x,int y)
{
if(x==y) return 0;
else
if(x>y) return 1;
else
return -1;
}
NODE
NODE
NODE
void

read_poly(NODE head);
attach(int coeff, int exp, NODE head);
poly_add(NODE head1,NODE head2,NODE head3);
display(NODE head);

int main()
{
NODE head1,head2,head3;
head1=getnode();
head2=getnode();
head3=getnode();
head1->link=head1;
head2->link=head2;
head3->link=head3;

printf("Enter 1st polynomial\n");


head1=read_poly(head1);
printf("Enter the 2nd polynomial\n");
head2=read_poly(head2);
head3=poly_add(head1,head2,head3);
printf("Polynomial 1:\n");
display(head1);
printf("Polynomial 2:\n");
display(head2);
printf("Polynomial 3:\n");
display(head3);
return 0;
}
NODE read_poly(NODE head)
{
int coeff,exp,i=1;
printf("Enter the coeff as -999 to end Polynomial\n");
while(1)
{
printf("Enter the %d term",i++);
printf("Coeff = ");
scanf("%d",&coeff);
if(coeff==-999)break;
printf("Power X = ");
scanf("%d",&exp);
head=attach(coeff,exp,head);
}
return head;
}
NODE attach(int coeff,int exp,NODE head)
{
NODE temp,cur;
temp=getnode();
temp->coeff=coeff;
temp->exp=exp;
cur=head->link;
while(cur->link!=head)
{
cur=cur->link;
}
cur->link=temp;
temp->link=head;
return head;

}
NODE poly_add(NODE head1,NODE head2,NODE head3)
{
NODE a,b;
int coeff;
a=head1->link;
b=head2->link;
while(a!=head1 && b!=head2)
{
switch(COMPARE(a->exp,b->exp))
{
case 0: coeff=a->coeff +b->coeff;
if(coeff!=0)
{
head3=attach(coeff,a->exp,head3);
a=a->link;
b=b->link;
}
break;
case 1: head3=attach(a->coeff,a->exp,head3);
a=a->link;
break;
default: head3=attach(b->coeff,b->exp,head3);
b=b->link;
}
}
while(a!=head1)
{
head3=attach(a->coeff,a->exp,head3);
a=a->link;
}
while(b!=head2)
{
head3=attach(b->coeff,b->exp,head3);
b=b->link;
}
return head3;
}
void display(NODE head)
{
NODE temp;
if(head->link==head)
{
printf("Polynomial does not exist");
return;
}
temp =head->link;
while(temp!=head)

{
if(temp->coeff<0)
{
printf("%2dx^%2d",temp->coeff,temp->exp);
}
else
{
printf("+%2dx^%2d",temp->coeff,temp->exp);
}
temp=temp->link;
}
}

2. Design, develop, and execute a program in C to convert a given valid parenthesized


infix arithmetic expression to postfix expression and then to print both the expression.
The expression consists of single character operands and the binary operators + (plus), (minus), * (multiply) and / (divide).
#include<stdio.h>
#include<string.h>
int F(char symbol)
{
switch(symbol)
{
case +:
case -:return 2;
case *:
case /:return 4;
case ^:
case $:return 5;
case (:return 0;
case #:return -1;
default:return 8;
}
}
int G(char symbol)
{
switch(symbol)
{
case +:
case -:return 1;
case *:
case /:return 3;
case ^:
case $:return 6;
case (:return 9;
case ):return 0;
default:return 7;
}
}
void infix_postfix(char infix[], char postfix[])
{
int i,j,top;
char s[30],symbol;
top=-1;
s[++top]=#;
j=0;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(F(s[top])>G(symbol))
{
postfix[j]=s[top--];
j++;
}

if(F(s[top])!=G(symbol))
{
s[++top]=symbol;
}
else
{
top--;
}
}
while(s[top]!=#)
{
postfix[j++]=s[top--];
}
postfix[j]=\0;
}
int main()
{
char infix[20],postfix[20];
printf(Enter a valid infix expression \n);
scanf(%s,infix);
infix_postfix(infix,postfix);
printf(The postfix expression is:%s\n,postfix);
return 0;
}

3.Design, develop, and execute a program in C to evaluate a valid postfix expression using stack.
Assume that the postfix expression is read as a single line consisting of non-negative single digit
operands and binary arithmetic operators. The arithmetic operators are + (add), - (subract), *
(multiply) and / (divide).
#include<stdio.h>
#include<string.h>

#include<math.h>
#include<ctype.h>
double compute(char symbol, double op1, double op2)
{
swithch(symbol)
{
case +:return op1+op2;
case -:return op1-op2;
case *:return op1*op2;
case /:return op1/op2;
case $:
case ^:return pow(op1,op2);
}
}
int main ()
{
double s[20],res,op1,op2;
int I,top;
char symbol,postfix[20];
printf(\n Enter the postfix expression\n);
scanf(%s,postfix);
top=-1;
for(i=0;i<strlen(postfix);i++)
{
symbol=postfix[i];
if(isdigit(symbol))
{
s[++top]=symbol-0;
}
else
{
op2=s[top--];
op1=s[top--];
res=compute(symbol,op1,op2);
s[++top]=res;
}
}
res=s[top--];
printf(The result is %f\n,res);
return 0;
}

4. Design, develop, and execute a program in C to simulate the working of a queue of integers
using an array. Provide the following operations:
a. Insert b. Delete c. Display
#include<stdio.h>
#include<stdlib.h>
#define queue_size 5
int choice,i,item,f,r,q[10];
int insert_rear()
{
if(r==queue_size-1)
{
printf("Queue Overflow\n");
return;
}
printf(Enter the item to be inserted\n);
scanf(%d,&item);
r=r+1;
q[r]=item;
}
int delete_front()
{
if(f>r)
{
printf("Queue Underflow\n");
return 0;
}
printf("The element delete is %d\n",q[f++]);
if(f>r)
{
f=0;
r=-1;
}
}
int display()
{
int i;
if(f>r)
{
printf("Queue is empty\n");
return 0;
}
printf("Content of the Queue\n");
for(i=f;i<=r;i++)
{
printf("%d\n",q[i]);
}
}
int main()
{
f=0;
r=-1;

for(;;)
{
printf("1:INSERT 2:DELETE 3:DISPLAY 4:EXIT\n");
printf("Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
insert_rear();
break;
case 2:
delete_front();
break;
case 3:
display();
break;
default:return 0;
}
}
}

5. Design, develop, and execute a program in C++ based on the following requirments :
An EMPLOYEE class is to contain the following data members and member fuction
Data members: Employee_Number (an integer), Employee_Name (a string of charectors),
Basic_Salary (an integer), All_Allowances (an integer), IT (an integer), Net_Salary (an integer).
Member function: to read the data of an employee, to calculate Net_Salary and to print the
value of all the data members.
(All_Allowances=123% of Basic; Income tax (IT)=30% of the gross_salary
(=Basic_Salary_All_Allowance);Net_Salary=Basic_Salary+All_Allowances-IT)
#include<iostream>
using namespace std;
class EMPLOYEE
{
private:
char employee_name[10];
int employee_number;
float basic,AL,IT,net_sal;
public:
void read_data();
void calculate_net_salary();
void display_data();
};
void EMPLOYEE::read_data()
{
cout<<"Enter the employee name and number"<<endl;
cin>>employee_name>>employee_number;
cout<<"Enter the basic salary"<<endl;
cin>>basic;
}
void EMPLOYEE::calculate_net_salary()
{
float gross_sal;
AL=(123*basic)/100;
gross_sal=basic+AL;
IT=(30*gross_sal)/100;
net_sal=gross_sal-IT;
}
void EMPLOYEE::display_data()
{
cout<<"EMPLOYEE NAME:"<<employee_name<<endl;
cout<<"EMPLOYEE NUMBER:"<<employee_number<<endl;
cout<<"NET SALARY:"<<net_sal<<endl;
}
int main()
{
int n,i;
cout<<"Enter the number of employee:";
cin>>n;
cout<<endl;
EMPLOYEE emp[10];
cout<<"Enter employee data"<<endl;
for(i=0;i<n;i++)
emp[i].read_data();
for(i=0;i<n;i++)

{
emp[i].calculate_net_salary();
emp[i].display_data();
}
}

6. Design, develop, and execute a program in C++ to create a class called STRING and
implement the following operations. Display the results after every operation by overloading
operator <<.
i. STRING s1=VTU
ii. STRING s2=BELGUAM
iii. STRING s3=s1+s2;(use copy constructor)
#include<iostream>
#include<string.h>
using namespace std;
class STRING
{
char str[20];
public:
STRING()
{
}
STRING(char *str1)
{
strcpy(str,str1);
}
STRING operator+(STRING s2);
friend ostream&operator<<(ostream&fout,STRING s);
};
STRING STRING::operator+(STRING s2)
{
strcat(str,s2.str);
return str;
}
ostream&operator<<(ostream&fout,STRING s)
{
cout<<"\n String is: \n";
fout<<s.str;
return fout;
}
int main()
{
STRING s1="VTU";
STRING s2="BELGUAM";
cout<<"\n Before concatination \n";
cout<<s1<<endl;
cout<<s2<<endl;
cout<<"\n\n After concatination \n";
STRING s3=s1+s2;
cout<<s3<<endl;
}
NOTE: SINCE STRING IS A CLASS NAME EVERYWHERE STRING WORD SHOULD BE IN CAPITAL
LETTERS

7. Design, develop, and execute a program in C++ to create a class called STACK using an array
of interger and to integers and to implement the following operations by overloading the
operator + and - :
i. s1=s1+element; where s1 is an object of the class STACK and elements is an integer to be
pushed on the top of the stack.
ii. s1=s1-;where s1 is a object of the class STACK and - operator pops off the top element.
Handle the STACK Empty and STACK full conditions. Also display the contents of the stack
after each operations, by overloading the operator <<.
#include<iostream>
#include<stdlib.h>
using namespace std;
int x;
class stack
{
private:
int s[10];
int size,top;
public:
stack(int n)
{
top=-1;
size=n;
}
void operator+(int x);
void operator-();
friend ostream & operator <<(ostream &out,stack st);
};
void stack::operator+(int x)
{
if(top==size-1)
{
cout<<"Stack is overflow"<<endl;
}
else
{
cout<<Enter the item bo be pushed\n;
cin>>x;
s[++top]=x;
}
}
void stack::operator-()
{
if(top==-1)
cout<<"Stack is underflow"<<endl;
else
cout<<"The popped element is "<<s[top--]<<endl;
}

ostream & operator <<(ostream & out,stack st)


{
if(st.top==-1)
cout<<"Stack is underflow"<<endl;
else
{
cout<<"The content of stack:";
for(int i=st.top;i>=0;i--)
out<<st.s[i]<<"->";
cout<<endl;
}
return out;
}
int main()
{
int n,ch,x;
cout<<"Enter the size of stack:"<<endl;
cin>>n;
stack st(n);
while(1)
{
cout<<"1:push\t"<<"2:pop\t"<<"3:Display\t"<<"4:Exit"<<end
l;
cout<<"Enter ur choice:";
cin>>ch;
switch(ch)
{
case 1: st+x;
break;
case 2: -st;
break;
case 3: cout<<st;
break;
default: cout<<"Program is terminated\n";
return 0;
}
}
return 0;
}

8. Design, develop, and execute a program in C++ to create a class called LIST (linked list) with
member function to insert an element at the front of the link as well as to delete an element from
the front of the list. Demonstrate all the function after creating a list objects.
#include<iostream>
#include<string.h>
using namespace std;
class LIST
{
public:
int info;
LIST *link;
};
class LINKED_LIST
{
LIST *first;
public:
LINKED_LIST()
{
first=NULL;
}
void insertf();
void deletef();
void display_list();
};
void LINKED_LIST::insertf()
{
LIST *temp;
int item;
cout<<"Enter the data:";
cin>>item;
cout<<endl;
temp=new LIST;
temp->info=item;
temp->link=NULL;
if(first==NULL)
{
first=temp;
}
else
{
temp->link=first;
first=temp;
}
}
void LINKED_LIST::deletef()
{
LIST *temp;
if(first==NULL)
cout<<"no data is present "<<endl;
else
{

temp=first;
first=first->link;
cout<<"The deleted data is "<<temp->info<<endl;
delete temp;
}
}
void LINKED_LIST::display_list()
{
LIST *temp;
if(first==NULL)
cout<<"NO data is present"<<endl;
else
{
cout<<"THE CONTENTS OF LIST ARE"<<endl;
temp=first;
while(temp!=NULL)
{
cout<<temp->info<<->;
temp=temp->link;
}
}
}
int main()
{
LINKED_LIST s1;
int ch=1;
while(ch)
{
cout<<"1:=Insert_front 2:delete_front 3:display_list
4:exit "<<endl;
cout<<Enter your choice\n;
cin>>ch;
switch(ch)
{
case 1:s1.insertf();
break;
case 2:s1.deletef();
break;
case 3:s1.display_list();
break;
default:return 0;
}
}
}

9. Design, develop, and execute a program in C to read a sparse matrix of integer values
and to search the sparse matrix for an element specified by the user. Print the result of
the search appropriately. Use the triple <row, column,value> to represent an element in
the sparse matrix.
#include<stdio.h>
int main()
{
int a[10][10],b[20][3],row,col;
int m=0,i,j,flag=0,s;
printf("Enter the row and column number\n");
scanf("%d%d",&row,&col);
printf("Enter the array element\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(a[i][j]!=0)
{
b[m][0]=i;
b[m][1]=j;
b[m][2]=a[i][j];
m++;
}
}
}
printf("The sparse matrix is :\n");
for(i=0;i<m;i++)
{
printf("ROW:%d\tCOLUMN:%d\tVALUE:%d\n",b[i][0],b[i][1],b[i][2]);
}
printf("\n");
printf("Enter the search element:");
scanf("%d",&s);
for(i=0;i<m;i++)
{
if(s==b[i][2])
{
printf("Search successful!\n");
printf("ROW:%d\tCOLUMN:%d\tVALUE:%d\n",b[i][0],b[i][1],b[i][2]);
flag=1;
}
if(flag)
break;
}

if(!flag)
printf("Search unsucessful\n");
}

10.Design, develop, and execute a program in C to create a max heap of integers by


accepting one element at a time and by inserting it immediately in to the heap.
Use the array representation for the heap. Display the array at the end of
insertion phase.
#include<stdio.h>
#include<stdlib.h>
int n,a[30];
void create_heap(int a[],int n)
{
int i,c,root,temp;
for(i=1;i<n;i++)
{
c=i;
do
{
root=(int)(c-1)/2;
if(a[root]<a[c])
{
temp=a[root];
a[root]=a[c];
a[c]=temp;
}
c=root;
}while(c!=0);
}
}
int insert_heap(int item,int a[],int n)
{
int c,p;
c=n;
p=(int)(c-1)/2;
while(c!=0&&item>a[p])
{
a[c]=a[p];
c=p;
p=(int)(c-1)/2;
}
a[c]=item;
return n+1;
}

void display(int a[],int n)


{
int i;
printf(Max Heap is\n);
for(i=0;i<n;i++)
{
printf(%d\t,a[i]);
printf(\n);
}
}
int main()
{
int ch,i,k;
printf(Create a MAX heap\n);
printf(Enter the number of nodes\n);
scanf(%d,&n);
printf(Enter the nodes\n);
for(i=0;i<n;i++)
{
scanf(%d,&a[i]);
}
create_heap(a,n);
display(a,n);
while(1)
{
printf(Insert a new node to Max heap\n);
printf(1.YES\t 2.NO\n);
printf(Enter your choice\n);
scanf(%d,&ch);
switch(ch)
{
case 1: printf(Enter the node\n);
scanf(%d,&k);
n=insert_heap(k,a,n);
display(a,n);
break;
default: return 0;
}
}
}

11. Design, develop, and execute a program in C to implement a doubly linked linked list where
each node consists of integers. The program should support the following operations:
i. Create a doubly linked list by adding each node at the front.
ii. Insert a new node to the left of the node whose key value is read as a input.
iii.Delete the node of a given data if it is found, otherwise display appropriate message.
iv.Display the contents of the list.
(Note:Only either (a,b and d) or (a,c and d) may be asked in the examination)
#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node *NODE;
NODE getnode()
{
NODE x;
x=(NODE)malloc(sizeof(struct node));
if(x==NULL)
{
printf("out of memory\n");
return 0;
}
return x;
}
NODE insert_front(int item,NODE head)
{
NODE temp,cur;
temp=getnode();
temp->info=item;
cur=head->rlink;
head->rlink=temp;
temp->llink=head;
temp->rlink=cur;
cur->llink=temp;
return head;
}
NODE insert_left(int item, NODE head)
{
NODE temp,cur,prev;
if(head->rlink==head)
{
printf("List if empty\n");
return head;
}
cur=head->rlink;
while(cur!=head)

{
if(item==cur->info) break;
cur=cur->rlink;
}
if(cur==head)
{
printf("key not found\n");
return head;
}
prev=cur->llink;
printf("Enter the item to be inserted towards left of
%d\n",item);
temp=getnode();
scanf("%d",&temp->info);
prev->rlink=temp;
temp->llink=prev;
cur->llink=temp;
temp->rlink=cur;
return head;
}
NODE delete_item(int item,NODE head)
{
NODE cur,prev,next;
if(head->rlink==head)
{
printf("LIst is empty");
return head;
}
cur=head->rlink;
while(cur!=head)
{
if(item==cur->info) break;
cur=cur->rlink;
}
if(cur==head)
{
printf("Item not found\n");
return head;
}
prev=cur->llink;
next=cur->rlink;
prev->rlink=next;
next->llink=prev;
printf("Delete item is =%d\n ",cur->info);
free(cur);
return head;
}
void display(NODE head)
{
NODE temp;
if(head->rlink==head)
{
printf("List is empty\n");
}

printf("content of the list\n");


temp=head->rlink;
while(temp!=head)
{
printf("%d\n",temp->info);
temp=temp->rlink;
}
}
int main()
{
NODE head;
int ch,item;
head=getnode();
head->rlink=head;
head->llink=head;
for(;;)
{
printf("1:insert \t 2:insert left of the key value\t 3:
delete node of given info \n");
printf("4:Display \t 5:exit\n");
printf("Enter your choice ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the item to insert \n");
scanf("%d",&item);
head=insert_front(item,head);
break;
case 2:
printf("Enter a key already existing in your
list \n");
scanf("%d",&item);
head=insert_left(item,head);
break;
case 3:
printf("Enter the item to delete \n");
scanf("%d",&item);
head=delete_item(item,head);
break;
case 4:
display(head);
break;
default:return 0;
}
}
}

12. Design, develop, and execute a program in C++ to create a class called DATE with
method to accept two valid dates in the form dd/mm/yy and to implement the following
operations by overloading the operators + and -. After every operation the result are to
be displayed by overloading the operator<<.
i. no_of_days=d1-d2; where d1 and d2 are DATE objects, d1>=d2 and no_of_days is an
integer.
ii. d2=d1+no_of_days; where d1 is a DATE object and no_of_days is an integer.
#include<iostream>
using namespace std;
class date
{
int dd,mm,yy;
int a[13];
long double dateno;
void getno();
public:
date()
{
a[1]=a[3]=a[5]=a[7]=a[8]=a[10]=a[12]=31;
a[4]=a[6]=a[9]=a[11]=30;
a[2]=28;
dd=mm=yy=1;
}
void getdate();
friend ostream& operator<<(ostream&,date&);
long double operator-(date&);
date operator+(long double);
};
void date::getdate()
{
cout<<"\n Enter date";
cout<<"\n\t day(dd):";
cin>>dd;
cout<<"\n\t month(mm):";
cin>>mm;
cout<<"\n\t year(yy):";
cin>>yy;
while((yy%4!=0&&dd>a[mm])||(yy%4==0 && mm==2 && dd>29)||(dd<=0)
||mm>12 || mm<=0 )
{
cout<<"\n Invlid entry";
getdate();
}
getno();
}

void date::getno()
{
int m=1;
dateno=(long double)yy*365+yy/4;
if(yy%4>0)
{
dateno++;
}
while(m!=mm)
{
dateno=dateno+a[m];
if(yy%4==0 && m==2)
{
dateno++;
}
m++;
}
dateno=dateno+dd;
}
ostream& operator<<(ostream &out,date &d1)
{
out<<d1.dd<<"/"<<d1.mm<<"/"<<d1.yy;
return out;
}
long double date::operator-(date &b)
{
return(dateno-b.dateno);
}
date date::operator+(long double b)
{
for(int i=1;i<=b;i++)
{
dd++;
if(dd>a[mm])
{
mm++;
dd=1;
}
if(mm>12)
{
yy++;
mm=1;
}
if(yy%4==0)

{
a[2]=29;
}
}
return *this;
}
int main()
{
date d1,d2,d3;
long double s;
d1.getdate();
cout<<"\n\td1="<<d1;
d2.getdate();
cout<<"\n\td2="<<d2;
s=d1-d2;
cout<<"\n\n The difference between the two dates = ";
cout<<s;
cout<<"\n\n Enter the no. of days to be added to the date "<<d1<<"
:";
cin>>s;
d3=d1+s;
cout<<"\n New date is..."<<d3;
}

13.Design, develop, and execute a program in C++ to create a class called OCTAL,
which has the characterstics of an octal number. Implement the following operations by
writing an appropriate constructor and an overloading operator +. \
i. OCTAL h=x ; where x is an integer.
ii. Int y=h+k ; where h is an OCTAL object and k is an integer.
Display the OCTAL result by overloading the operator << . Also display the value of h
and y.
#include<iostream>
#include<math.h>
using namespace std;
class octal
{
private:
int p;
public:
octal();
octal(int);
~octal();
int dectooct(int x);
int octtodec(int x);
friend ostream & operator <<(ostream&print,octal x);
int operator +(int);
};
octal::octal()
{
}
octal::octal(int x)
{
p=dectooct(x);
}
octal::~octal()
{
}
int octal::dectooct(int x)
{
int i=0,sum=0,rem;
while(x!=0)
{
rem=x%8;
sum=sum+rem*pow(10,i);
i++;
x=x/8;
}
return sum;
}
int octal::octtodec(int x)
{
int i=0,sum=0,rem;
while(x!=0)
{
rem=x%10;
sum=sum+rem*pow(8,i);

i++;
x=x/10;
}
return sum;
}
ostream & operator<<(ostream&print,octal x)
{
print<<x.p;
return print;
}
int octal::operator+(int x)
{
return octtodec(p)+x;
}
int main()
{
int x,y,k;
cout<<"Enter the value of x in decimal number:";
cin>>x;
octal h(x);
cout<<endl<<"Corresponding value of x in octal notation
h="<<h;
cout<<endl<<"Enter the value of k in decimal notation:";
cin>>k;
cout<<endl<<"The value of k="<<k;
y=h+k;
cout<<endl<<"The value of h+k in decimal notation k="<<y;
cout<<endl;
return 0;
}

14. Design, develop, and execute a program in C++ to create a class called BIN_TREE
that represents a Binary tree , with member function to perform inorder, preorder and
postorder traversals. Create a BIN_TREE object and demonstrate the traversals.
#include<iostream>
#include<stdlib.h>
#include<iomanip>
using namespace std;
struct node
{
int info;
struct node *llink;
struct node *rlink;
};
typedef struct node* NODE;
class btree
{
private:
NODE root;
public:
btree();
~btree();
NODE create();
void inorder(NODE root);
void postorder(NODE root);
void preorder(NODE root);
};
btree::btree()
{
root=NULL;
}
btree::~btree()
{
delete root;
}
NODE btree::create()
{
NODE temp,cur,prev;
int i,n;
cout<<endl<<"Enter members of elements in the tree";
cin>>n;
for(i=1;i<=n;i++)
{
temp=new node;
temp->llink=NULL;
temp->rlink=NULL;
cout<<endl<<"Enter the data";
cin>>temp->info;
if(root==NULL)
{

root=temp;
}
else
{
cur=root;
prev=NULL;
while(cur!=NULL)
{
prev=cur;
if(temp->info<cur->info)
cur=cur->llink;
else
cur=cur->rlink;
}
if(temp->info<prev->info)
prev->llink=temp;
else
prev->rlink=temp;
}
}
return root;
}
void btree::inorder(NODE root)
{
if(root!=NULL)
{
inorder(root->llink);
cout<<root->info<<"\n";
inorder(root->rlink);
}
}
void btree::preorder(NODE root)
{
if(root!=NULL)
{
cout<<root->info<<"\n";
preorder(root->llink);
preorder(root->rlink);
}
}
void btree::postorder(NODE root)
{
if(root!=NULL)
{
postorder(root->llink);
postorder(root->rlink);
cout<<root->info<<endl;
}
}

int main()
{
btree b;
NODE root;
root=b.create();
cout<<endl<<"Inorder travarsing:\n"<<endl;
b.inorder(root);
cout<<endl<<"preoreder traversing:\n"<<endl;
b.preorder(root);
cout<<endl<<"Postorder traversing:\n"<<endl;
b.postorder(root);
}

Vous aimerez peut-être aussi