Vous êtes sur la page 1sur 42

Data Structures with C/C++ Laboratory

CANARA ENGINEERING
COLLEGE
(Affiliated to Visvesvaraya Technological University, Belgaum, Karnataka)
Benjanpadavu 574 219, Mangalore, Bantwal Taluk, D.K. District, Karnataka

Department of Computer Science and


Engineering

Data Structures with C/C++ Laboratory


10CSL37
(Commom to CSE & ISE)

Laboratory Manual

Name
USN
Staff Incharge

Canara Engineering College, Benjanapadavu

Data Structures with C/C++ Laboratory


DATA STRUCTURES WITH C/C++ LABORATORY
(Common to CSE & ISE)
Subject Code: 10CSL37
Hours/Week: 03
Total Hours: 42

I.A. Marks: 25
Exam Hours: 03
Exam Marks: 50

1. Using circular representation for a polynomial, design, develop, and execute a program in C to
accept two polynomials, add them, and then print the resulting polynomial.
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
struct link
{
int coeff;
int exp;
struct link *next;
};
struct link *poly1=NULL,*poly2=NULL,*poly=NULL;
void create(struct link *node)
{
char ch;
do
{
printf("\nEnter Coeff : ");
scanf("%d",&node->coeff);
printf("\nEnter power ");
scanf("%d",&node->exp);
node->next=(struct link*)malloc(sizeof(struct link));
node=node->next;
node->next=NULL;
printf("\nContinue (y/n) ");
ch=getch();
}while(ch=='y'||ch=='Y');
}
void show(struct link *node)
{
while(node->next!=NULL)
{
printf("%dx^%d",node->coeff,node->exp);
node=node->next;
if(node->next!=NULL)
printf("+");
}
}
void polyadd(struct link *poly1,struct link *poly2,struct link *poly)
{
while(poly1->next && poly2->next)
{
if(poly1->exp > poly2->exp)
{
poly->exp=poly1->exp;
poly->coeff=poly1->coeff;

Canara Engineering College, Benjanapadavu

Data Structures with C/C++ Laboratory


poly1=poly1->next;
}
else if(poly1->exp < poly2->exp)
{
poly->exp=poly2->exp;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
else
{
poly->exp=poly1->exp;
poly->coeff=poly1->coeff+poly2->coeff;
poly1=poly1->next;
poly2=poly2->next;
}
poly->next=(struct link*)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}
while(poly1->next || poly2->next)
{
if(poly1->next)
{
poly->exp=poly1->exp;
poly->coeff=poly1->coeff;
poly1=poly1->next;
}
if(poly2->next)
{
poly->exp=poly2->exp;
poly->coeff=poly2->coeff;
poly2=poly2->next;
}
poly->next=(struct link*)malloc(sizeof(struct link));
poly=poly->next;
poly->next=NULL;
}

void main()
{
char ch;
clrscr();
do
{
poly1=(struct link*)malloc(sizeof(struct link));
poly2=(struct link*)malloc(sizeof(struct link));
poly=(struct link*)malloc(sizeof(struct link));
printf("\nEnter 1st number: ");
create(poly1);
printf("\nEnter 2nd number: ");
create(poly2);
printf("\n1st number: ");
show(poly1);
printf("\n2nd number: ");
show(poly2);
polyadd(poly1,poly2,poly);

Canara Engineering College, Benjanapadavu

Data Structures with C/C++ Laboratory

printf("\nAdded Polynomial : ");


show(poly);
printf("\nAdd two more numbers (y/n)");
ch=getch();
}while(ch=='y'||ch=='Y');

Canara Engineering College, Benjanapadavu

Data Structures with C/C++ Laboratory


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 expressions. The expression consists of 13
single character operands and the binary operators + (plus), - (minus), * (multiply) and / (divide).
#include<stdio.h>
void evaluate();
void push(char);
char pop();
int prec(char);
char infix[30], postfix[30],stack[30];
int top=-1;
void main()
{
clrscr();
printf("Enter the valid infix expresion\n");
scanf("%s",infix);
evaluate();
printf("\nThe entered infix expression is : %s",infix);
printf("\nThe coresponding postfix expression is : %s",postfix);
getch();
}
void evaluate()
{
int i=0,j=0;
char symb,temp;
push('#');
for(i=0;infix[i]!='\0';i++)
{
symb=infix[i];
switch(symb)
{
case '(': push(symb);
break;
case ')': temp=pop();
while(temp!='(')
{
postfix[j]=temp;
j++;
temp=pop();
}
break;
case'+':
case'-':
case'*':
case'/':
case'^':
case'$': while(prec(stack[top])>=prec(symb))
{
temp=pop();
postfix[j]=temp;
j++;
}
push(symb);
break;
default:

postfix[j]=symb;

Canara Engineering College, Benjanapadavu

Data Structures with C/C++ Laboratory


}

j++;

}
while(top>0)
{
temp=pop();
postfix[j]=temp;
j++;
}
postfix[j]='\0';
}
void push(char item)
{
top=top+1;
stack[top]=item;
}
char pop()
{
char item;
item=stack[top];
top=top-1;
return item;
}
int prec(char symb)
{
int p;
switch(symb)
{
case'+':
case'-':
case'*':
case'/':
case'^':
case'$':
case'(':
case')':
case'#':
}
return p;
}

p=1; break;
p=2; break;
p=3; break;
p=0; break;
p=-1;

Canara Engineering College, Benjanapadavu

Data Structures with C/C++ Laboratory

Canara Engineering College, Benjanapadavu

Data Structures with C/C++ Laboratory


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), - (subtract), *
(multiply) and / (divide).
#include<stdio.h>
#include<math.h>
#include<process.h>
void push(int a);
int pop();
int stack[20];
int top=-1;
void main()
{
int op1,op2,i;
char postfix[20],symb;
float res;
clrscr();
printf("Enter a valid postfix expression\n");
scanf("%s",postfix);
for(i=0;postfix[i]!='\0';i++)
{
symb=postfix[i];
if(isdigit(symb))
push(symb-'0');
switch(symb)
{
case '+':op2=pop();
op1=pop();
res=op1+op2;
push(res);
break;
case '-':op2=pop();
op1=pop();
res=op1-op2;
push(res);
break;
case '*':op2=pop();
op1=pop();
res=op1*op2;
push(res);
break;
case '/':
op2=pop();
op1=pop();
if(op2==0)
{
printf("Error\n");
getch();
Canara Engineering College, Benjanapadavu
7

Data Structures with C/C++ Laboratory

exit(0);
}
res=op1/op2;
push(res);
break;

res=pop();
printf("\n\nResult\t%f",res);
getch();

void push(int item)


{
top=top+1;
stack[top]=item;
}
int pop()
{
int item;
item=stack[top];
top=top-1;
return item;
}

Canara Engineering College, Benjanapadavu

Data Structures with C/C++ Laboratory


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>
#define size 5
int queue[size];
int front=0,rear=-1;
void insert();
void delete();
void display();
void main()
{
int choice;
clrscr();
for(;;)
{
printf("\n1.Insert\n2.Delete\n3.Display\n");
printf("\nEnter your choice\t");
scanf("%d",&choice);
switch(choice)
{
case 1:insert();
break;
case 2: delete();
break;
case 3:
display();
break;
default:exit(0);
}
getch();
}
}
void insert()
{
int item;
if(rear==(size-1))
{
printf("\nQueue is full");
return;
}
printf("\nEnter an element to be inserted\t");
scanf("%d",&item);
rear=rear+1;
queue[rear]=item;
}
void delete()
{
int item;
if(front>rear)
{
printf("\nQueue is Empty\n");
return;

Canara Engineering College, Benjanapadavu

Data Structures with C/C++ Laboratory


}
item=queue[front];
printf("The Deleted item is %d",item);
front++;
if(front>rear)
{
front=0;
rear=-1;
}
}
void display()
{
int i;
if(front>rear)
{
printf("\n\nQueue is empty\n");
return;
}
printf("Elements of the queue are\n");
for(i=front;i<=rear;i++)
printf("%d\n",queue[i]);
}

Canara Engineering College, Benjanapadavu

10

Data Structures with C/C++ Laboratory

Canara Engineering College, Benjanapadavu

11

Data Structures with C/C++ Laboratory


5. Design, develop, and execute a program in C++ based on the following requirements:
An EMPLOYEE class is to contain the following data members and member functions:
Data members: Employee_Number (an integer), Employee_Name (a string of characters),
Basic_Salary (an integer), All_Allowances (an integer), IT (an integer), Net_Salary (an integer).
Member functions: to read the data of an employee, to calculate Net_Salary and to print the values 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<stdio.h>
#include<string.h>
struct employee
{
int num;
char name[20];
float basic;
float gross;
float allow;
float it;
float net;
};
struct employee e1;
void read()
{
printf("\nEnter Employee Number :");
scanf("%d",&e1.num);
printf("\nEnter Name\n");
scanf("%s",e1.name);
printf("\nEnter Basic Pay\n");
scanf("%f",&e1.basic);
}
void calculate()
{
e1.allow=(123*e1.basic)/100;
e1.gross=e1.allow+e1.basic;
e1.it=(30*e1.gross)/100;
e1.net=e1.basic+e1.allow-e1.it;
}
void write()
{
printf("\nEmployee # : %d",e1.num);
printf("\nEmployee Name :");
puts(e1.name);
printf("\nBasic : %d",e1.basic);
printf("\nAll Allowances : %.2f",e1.allow);
printf("\nIncome Tax : %.2f",e1.it);
printf("\nNet Salary : %.2f",e1.net);
return;
}
void main()
{
clrscr();
read();
calculate();

Canara Engineering College, Benjanapadavu

12

Data Structures with C/C++ Laboratory


write();
getch();
}

Canara Engineering College, Benjanapadavu

13

Data Structures with C/C++ Laboratory


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 the operator <<.
i. STRING s1 = VTU
ii. STRING s2 = BELGAUM
iii. STIRNG s3 = s1 + s2; (Use copy constructor)
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
private:
char str[20];
public:
string()
{
strcpy(str," ");
}
string(char *p)
{
strcpy(str,p);
}

};

string operator +(string);


friend ostream& operator << (ostream&, string);

string string :: operator +(string s2)


{
strcat(str,s2.str);
return *this;
}
ostream& operator << (ostream& os, string s1)
{
os<<s1.str<<endl;
return os;
}
void main()
{
clrscr();
string s1="VTU";
cout<<"S1= "<<s1<<endl;
string s2=" Belgaum";
cout<<"S2= "<<s2<<endl;
string s3= s1+ s2;
cout<<"S3 = "<<s3<<endl;
getch();
}

Canara Engineering College, Benjanapadavu

14

Data Structures with C/C++ Laboratory

Canara Engineering College, Benjanapadavu

15

Data Structures with C/C++ Laboratory


7. Design, develop, and execute a program in C++ to create a class called STACK using an array of
integers and to implement the following operations by overloading the operators + and - :
i. s1=s1 + element; where s1 is an object of the class STACK and element is an integer to be
pushed on to top of the stack.
ii. s1=s1- ; where s1 is an 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 operation, by overloading the operator <<.

#include<iostream.h>
#include<conio.h>
#include<process.h>
#define size 5
class stack
{
private:
int top;
int st[size];
public:
stack operator +(int);
stack operator --(int);
friend void operator <<(ostream&,stack);
stack()
{
top=-1;
}
};
stack stack :: operator +(int item)
{
if(top==size-1)
{
cout<<"\nStack Overflow\n";
}
else
{
top=top+1;
st[top]=item;
}
return *this;
}
stack stack :: operator --(int)
{
int item;
if(top==-1)
{
cout<<"\nStack Underflow\n";
}
else
{
item=st[top];
cout<<"Deleted element ="<<item<<endl;
top=top-1;
}
return *this;
}

Canara Engineering College, Benjanapadavu

16

Data Structures with C/C++ Laboratory


void operator << (ostream& os,stack s)
{
int i;
if(s.top==-1)
os<<"\nStack is empty\n";
else
{
for(i=s.top;i>=0;i--)
os<<s.st[i]<<endl;
}
return ;
}
void main()
{
int choice,item;
stack s;
clrscr();
for(;;)
{
cout<<"\n1.Insert\n2.Delete\n3.Display\n";
cout<<"Enter your choice\t";
cin>>choice;
switch(choice)
{
case 1: cout<<"Enter the element to be inserted\t" ;
cin>>item;
s=s+item;
break;
case 2: s=s--;
break;
case 3: cout<<s;
break;
default:exit(0);
}
}

Canara Engineering College, Benjanapadavu

17

Data Structures with C/C++ Laboratory

Canara Engineering College, Benjanapadavu

18

Data Structures with C/C++ Laboratory

Canara Engineering College, Benjanapadavu

19

Data Structures with C/C++ Laboratory


8. Design, develop, and execute a program in C++ to create a class called LIST (linked list) with
member functions to insert an element at the front of the list as well as to delete an element from the
front of the list. Demonstrate all the functions after creating a list object.
#include<iostream.h>
#include<process.h>
#include<conio.h>
class node
{
public:
int data;
node *link;
};
class list
{
private:
node *first;
public:
void insert_front();
void delete_front();
void display();
list()
{
first=NULL;
}
};
void list :: insert_front()
{
node * temp;
temp=new node;
cout<<"Enter the element to be inserted : ";
cin>>temp->data;
temp->link=first;
first=temp;
}
void list :: delete_front()
{
node *temp;
if(first==NULL)
{
cout<<"List empty\n";
return;
}
temp=first;
cout<<"The deleted element = "<<temp->data;
first=temp->link;
delete temp;
}
void list :: display()
{
node *temp;
if(first==NULL)
{

Canara Engineering College, Benjanapadavu

20

Data Structures with C/C++ Laboratory


cout<<"List empty\n";
return;
}
temp=first;
while(temp!=NULL)
{
cout<<temp->data<<"\t";
temp=temp->link;
}
}
void main()
{
int choice;
list l;
clrscr();
for(;;)
{
cout<<"\n1.Insert\n2.Delete\n3.Display\n";
cout<<"Enter your choice\t";
cin>>choice;
switch(choice)
{
case 1:
l.insert_front();
break;
case 2:
l.delete_front();
break;
case 3:
l.display();
break;
default:exit(0);
}
}
getch();
}

Canara Engineering College, Benjanapadavu

21

Data Structures with C/C++ Laboratory

Canara Engineering College, Benjanapadavu

22

Data Structures with C/C++ Laboratory


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>
#define maxterms 101
typedef struct
{
int row;
int col;
int value;
}term;
term a[maxterms];
void main()
{
int i,j,key;
clrscr();
printf("\nEnter the number of rows : ");
scanf("%d",&a[0].row);
printf("Enter the number of column : ");
scanf("%d",&a[0].col);
printf("Enter the number of elements : ");
scanf("%d",&a[0].value);
if(a[0].value>a[0].row*a[0].col)
{
printf("\nInvalid Input");
getch();
exit(0);
}
for(i=1;i<=a[0].value;i++)
{
printf("Enter the row value : ");
scanf("%d",&a[i].row);
printf("Enter the col value : ");
scanf("%d",&a[i].col);
printf("Enter the element : ");
scanf("%d",&a[i].value);
}
printf("the entered sparse matrix is");
printf("\nRows\tColumn\tValue\n");
for(i=0;i<=a[0].value;i++)
{
printf("%d\t",a[i].row);
printf("%d\t",a[i].col);
printf("%d\n",a[i].value);
}
printf("Enter the element to be searched : ");
scanf("%d",&key);

Canara Engineering College, Benjanapadavu

23

Data Structures with C/C++ Laboratory


for(i=1;i<=a[0].value;i++)
{
if(key==a[i].value)
{
printf("Element found at row %d and column %d" ,
a[i].row,a[i].col);
getch();
exit(0);
}
}

printf("Element not found");


getch();

Canara Engineering College, Benjanapadavu

24

Data Structures with C/C++ Laboratory

Canara Engineering College, Benjanapadavu

25

Data Structures with C/C++ Laboratory


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<math.h>
void heapify (int a[],int n)
{
int i,j,k,item;
for(k=1;k<n;k++)
{
item=a[k];
i=k;
while(i!=1 && item>=a[i/2])
{
a[i]=a[i/2];
i=i/2;

}
a[i]=item;

}
void main()
{
int a[20],i,n;
clrscr();
printf("Enter the no. of elements ");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
heapify(a,n);
printf("The elements of the array after heapify\n");
for(i=1;i<=n;i++)
printf("%d\n",a[i]);
getch();
}

Canara Engineering College, Benjanapadavu

26

Data Structures with C/C++ Laboratory

Canara Engineering College, Benjanapadavu

27

Data Structures with C/C++ Laboratory


11. Design, develop, and execute a program in C to implement a doubly 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 an 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<alloc.h>
#include<process.h>
struct node
{
int data;
struct node* llink;
struct node* rlink;
};
typedef struct node * NODE;
NODE
void
void
void
void

getnode();
insert_front();
insert_left();
delete();
display();

NODE first = NULL;


NODE getnode()
{
NODE temp;
temp=(NODE)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("Error\n");
getch();
exit(0);
//return;
}
printf("enter the element to be inserted:");
scanf("%d",&temp->data);
temp->rlink=temp->llink=NULL;
return temp;
}
void insert_front()
{
NODE temp;
temp=getnode();
if(first==NULL)
{
first=temp;
return;
}
temp->rlink=first;
first->llink=temp;
first=temp;
}

Canara Engineering College, Benjanapadavu

28

Data Structures with C/C++ Laboratory


void delete()
{
int key;
NODE temp,prev;
if(first==NULL)
{
printf("List is Empty\n");
return;
}
printf("Enter the element to be deleted \n");
scanf("%d",&key);
temp=first;
while(temp!=NULL && key!=temp->data)
temp=temp->rlink;
if(temp==NULL)
{
printf("Element not found\n");
return;
}
prev=temp->llink;

if(prev==NULL)
{
first=temp->rlink;
first->llink=prev;
printf("Deleting %d\n",temp->data);
free(temp);
return;
}
prev->rlink=temp->rlink;
temp->rlink->llink=temp->llink;
printf("Deleting %d",temp->data);
free(temp);

void display()
{
NODE temp;
if(first==NULL)
{
printf("No List exists\n");
return;
}
temp=first;
printf("The elements of the list are\n");

while(temp!=NULL)
{
printf("%d\t",temp->data);
temp=temp->rlink;
}
printf("\n");
return;

Canara Engineering College, Benjanapadavu

29

Data Structures with C/C++ Laboratory


void insert_left()
{
NODE temp,cur,prev;
int data;
if(first==NULL)
{
printf("Illegal Operations\n");
return;
}
cur=first;
printf("Enter the element
");
scanf("%d",&data);
cur=first;
while(data!=cur->data && cur!=NULL)
cur=cur->rlink;
if(cur==NULL)
{
printf("Element not found\n");
return;
}
temp=getnode();
prev=cur->llink;
if(prev==NULL)
{
temp->rlink=first;
first->llink=temp;
first=temp;
return;
}
prev->rlink=temp;
temp->llink=prev;
temp->rlink=cur;
cur->llink=temp;
}
void main()
{
int choice;
clrscr();
for(;;)
{
printf("1.Insert_front\n2.insert_left\n3.delete\n4.display\n");
printf("enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:insert_front();
break;
case 2:insert_left();
break;
case 3:delete();
break;
case 4:display();
break;
default:exit(0);
}

Canara Engineering College, Benjanapadavu

30

Data Structures with C/C++ Laboratory


getch();
}
}

Canara Engineering College, Benjanapadavu

31

Data Structures with C/C++ Laboratory

Canara Engineering College, Benjanapadavu

32

Data Structures with C/C++ Laboratory


12. Design, develop, and execute a program in C++ to create a class called DATE with methods 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 results 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.h>
#include<conio.h>
#include<process.h>
class date
{
private:
int d,m,y;
int a[20];
public:
date()
{
a[1]=31;
a[3]=31;
a[5]=31;
a[7]=31;
a[8]=31;
a[10]=31;
a[12]=31;
a[4]=30;
a[6]=30;
a[9]=30;
a[11]=30;
a[2]=28;
}
friend date operator + (date,int);
friend int operator - (date,date);
friend ostream& operator << (ostream &, date);
void read();
};
date operator + (date d1, int x)
{
int i;
for(i=1;i<=x;i++)
{
d1.d++;
if(d1.y % 4==0)
d1.a[2]=29;
else
d1.a[2]=28;
if(d1.d > d1.a[d1.m])
{
d1.d=1;
d1.m++;
}
if(d1.m > 12)
{
d1.m=1;
d1.y++;
}
}

Canara Engineering College, Benjanapadavu

33

Data Structures with C/C++ Laboratory


}

return d1;

int operator - (date d1,date d2)


{
int count=0;
if(d1.m==d2.m && d1.y==d2.y)
{
count = d1.d-d2.d;
return count;
}
while(!((d1.d==d2.d)&&(d1.m==d2.m)&&(d1.y==d2.y)))
{
count++;
d2.d++;
if(d2.y % 4==0)
d2.a[2]=29;
else
d2.a[2]=28;
if(d2.d > d2.a[d2.m])
{
d2.d=1;
d2.m++;
}
if(d2.m > 12)
{
d2.m=1;
d2.y++;
}
}
return

count;
}
ostream& operator << (ostream& os,date d1)
{
os<<d1.d<<"/"<<d1.m<<"/"<<d1.y<<endl;
return os;
}
void date :: read()
{
cin>>d>>m>>y;
}
void main()
{
date d1,d2;
int choice, x;
clrscr();
for(;;)
{
cout<<"1.Subtraction\n2. Addition\n";
cout<<"Enter your choice
";
cin>>choice;
switch(choice)
{

Canara Engineering College, Benjanapadavu

34

Data Structures with C/C++ Laboratory

}
}

case 1: cout<<"Enter Valid date 1: ";


d1.read();
cout<<"Enter valid date 2: ";
d2.read();
x=d1-d2;
cout<<"No. of days between d1 & d2 : "<<x<<endl;
break;
case 2:cout<<"Enter valid date : ";
d1.read();
cout<<"Enter the no. of days to be added : ";
cin>>x;
d2=d1+x;
cout<<"New date : "<<d2<<endl;
break;
default: exit(0);

Canara Engineering College, Benjanapadavu

35

Data Structures with C/C++ Laboratory


13. Design, develop, and execute a program in C++ to create a class called OCTAL, which has the
characteristics of an octal number. Implement the following operations by writing an appropriate
constructor and an overloaded 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 values of h and y.
#include<iostream.h>
#include<conio.h>
class octal
{
private:
public:

int oct;
octal(int);
int operator + (int);
friend ostream& operator << (ostream&,octal);

};
octal :: octal (int x)
{
int base=1,rem=0;
oct=0;
while(x!=0)
{
rem=x%8;
oct+=rem*base;
x=x/8;
base=base*10;
}
}
int octal :: operator + (int k)
{
int base=1,rem=0,dec=0;
while(oct!=0)
{
rem=oct%10;
dec+=rem*base;
oct=oct/10;
base=base*8;
}
return (dec+k);
}
ostream & operator <<(ostream& os, octal x)
{
os<<x.oct;
return os;
}
void main()
{
int x,k;
clrscr();
cout<<"Enter the value of x ";
cin>>x;
octal h=x;

Canara Engineering College, Benjanapadavu

36

Data Structures with C/C++ Laboratory


cout<<"The octal number is "<<h<<endl;
cout<<"Enter the value of k ";
cin>>k;
int y = h+k;

cout<<"The sum is "<<y;


getch();

Canara Engineering College, Benjanapadavu

37

Data Structures with C/C++ Laboratory


14. Design, develop, and execute a program in C++ to create a class called BIN_TREE that represents
a Binary Tree, with member functions to perform inorder, preorder and postorder traversals.
Create a BIN_TREE object and demonstrate the traversals
#include<iostream.h>
#include<process.h>
#include<conio.h>
class node
{
public:
int data;
node *lchild;
node *rchild;
};
class bintree
{
public:
node *root;
void create();
void preorder(node*);
void inorder(node*);
void postorder(node*);
bintree()
{
root=NULL;
}
};
void bintree :: preorder(node* root)
{
node *t;
t=root;
if(t)
{
cout<<t->data<<"\t";
preorder(t->lchild);
preorder(t->rchild);
}
}
void bintree :: inorder ( node* root)
{
node *t;
t=root;
if(t)
{
inorder(t->lchild);
cout<<t->data<<"\t";
inorder(t->rchild);
}
}
void bintree :: postorder( node* root)
{
node *t;
t=root;
if(t)
{

Canara Engineering College, Benjanapadavu

38

Data Structures with C/C++ Laboratory

postorder(t->lchild);
postorder(t->rchild);
cout<<t->data<<"\t";

}
void bintree :: create()
{
node *temp,*t;
temp=new node;
cout<<"Enter the element to be inserted\n";
cin>>temp->data;
temp->lchild=NULL;
temp->rchild=NULL;
t=root;
if(root==NULL)
{
root=temp;
return;
}

while(1)
{
if(temp->data < t->data)
{
if(t->lchild==NULL)
{
t->lchild=temp;
break;
}
t=t->lchild;
}
else
{
if(t->rchild==NULL)
{
t->rchild=temp;
break;
}
t=t->rchild;
}
}

void main()
{
bintree b;
int choice;
clrscr();
for(;;)
{
cout<<"1. Create\n2. Preorder\n3. Inorder\n4. Postorder\n";
cout<<"Enter your choice\n";
cin>>choice;
switch(choice)
{

Canara Engineering College, Benjanapadavu

39

Data Structures with C/C++ Laboratory


case 1:b.create();
break;
case 2:cout<<\nPreorder\t;
b.preorder(b.root);
break;
case 3:cout<<\nInorder\t;
b.inorder(b.root);
break;
case 4:cout<<\nPostorder\t;
b.postorder(b.root);
break;
default:exit(0);
}
}

getch();

Canara Engineering College, Benjanapadavu

40

Data Structures with C/C++ Laboratory

Canara Engineering College, Benjanapadavu

41

Vous aimerez peut-être aussi