Sie sind auf Seite 1von 44

# Data Structure Lab Manual

## Dept. of CSE Page 1

Data Structure Lab Manual
(Using C and C++)
3
rd
Semister CSE

By,
Dhananjaya.M,
Asso.Professor,Dept. of CSE,
SJBIT, BANGLORE.

Data Structure Lab Manual

Dept. of CSE Page 2

/*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<conio.h>
#define MAX_TERMS 100
typedef struct
{
int coef;
int expon;
}polynomial;
int avail=0,startA,finishA,startB,finishB,startD,finishD;
polynomial term[MAX_TERMS];
int compare(int expona,int exponb)
{
if(expona==exponb)
return(0);
else
if(expona>exponb)
return(1);
else
return(-1);
}
void attach(int coefficient,int exponent)
{
term[avail].coef=coefficient;
term[avail++].expon=exponent;
}

Data Structure Lab Manual

Dept. of CSE Page 3

{
int coefficient;
startD=avail;
while(startA<=finishA && startB<=finishB)
{
switch(compare(term[startA].expon,term[startB].expon))
{
case -1:attach(term[startB].coef,term[startB].expon);
startB++;
break;
case 0:coefficient=term[startA].coef+term[startB].coef;
attach(coefficient,term[startA].expon);
startA++;
startB++;
break;
case 1:attach(term[startA].coef,term[startA].expon);
startA++;
}
}
for(;startA<=finishA;startA++)
attach(term[startA].coef,term[startA].expon);
for(;startB<=finishB;startB++)
attach(term[startB].coef,term[startB].expon);
finishD=avail-1;
}

Data Structure Lab Manual

Dept. of CSE Page 4

void main()
{
int na,nb,i;
clrscr();
printf("enter number of terms of polynomial A & coef & expon\n");
startA=0;
scanf("%d",&na);
for(i=0;i<na;i++)
{
scanf("%d%d",&term[i].coef,&term[i].expon);
avail++;
}
finishA=avail-1;
printf("enter number of terms of polynomial b & coef & expon\n");
startB=avail;
scanf("%d",&nb);
for(i=startB;i<nb+startB;i++)
{ scanf("%d%d",&term[i].coef,&term[i].expon);
avail++; }
finishB=avail-1;
printf("resultant polynomial\n D(x)=");
for(i=startD;i<=finishD;i++)
{ printf("%dx^%d",term[i].coef,term[i].expon);
if(i!=finishD) printf("+"); }
getch();
}

Data Structure Lab Manual

Dept. of CSE Page 5

/*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 single character operands and the binary operators + (plus),
- (minus), * (multiply) and / (divide).*/
//CONVERSION OF INFIX EXPRESSION TO POSTFIX EXPRESSION
#include<stdio.h>
#include<conio.h>
typedef enum{lparen,rparen,plus,minus,times,divide,mod,eos,operand}precedence;
precedence stack[10];
int isp[]={0,19,12,12,13,13,13,0};
int icp[]={20,19,12,12,13,13,13,0};
int top=0;
char expr[20];
void printToken(precedence token)
{
switch(token)
{
case plus : printf("+");
break;
case minus : printf("-");
break;
case times : printf("*");
break;
case divide : printf("/");
break;
case mod : printf("%");
}
}
Data Structure Lab Manual

Dept. of CSE Page 6

void push(precedence token)
{
stack[++top]=token;
}
precedence pop()
{
return(stack[top--]);
}
precedence getToken(char *symbol,int *n)
{
*symbol=expr[(*n)++];
switch(*symbol)
{
case '(' : return(lparen);
case ')' : return(rparen);
case '+' : return(plus);
case '-' : return(minus);
case '*' : return(times);
case '/' : return(divide);
case '%' : return(mod);
case '\0' : return(eos);
default : return(operand);
}
}
void postfix()
{
char symbol;
int n=0;
Data Structure Lab Manual

Dept. of CSE Page 7

precedence token;
stack[0]=eos;
printf("the converted postfix string:");
for(token=getToken(&symbol,&n);token!=eos;token=getToken(&symbol,&n))
{
if(token==operand)
printf("%c",symbol);
else
if(token==rparen)
{
while(stack[top]!=lparen)
printToken(pop());
pop();
}
else
{
while(isp[stack[top]]>=icp[token])
printToken(pop());
push(token);
}
}
token=pop();
while(token!=eos)
{
printToken(token);
token=pop();
}
}
Data Structure Lab Manual

Dept. of CSE Page 8

void main()
{
clrscr();
printf("enter the valid infix expression\n");
gets(expr);
postfix();
getch();
}

/*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<conio.h>
typedef enum
{lparen,rparen,plus,minus,times,divide,mod,eos,operand } precedence;
int stack[10],top=-1;
char expr[30];
void push(int x)
{
stack[++top]=x;
}
int pop()
{
return(stack[top--]);
}
precedence gettoken(char *symbol, int *n)
Data Structure Lab Manual

Dept. of CSE Page 9

{
*symbol=expr[(*n)++];
switch(*symbol)
{
case '(':return(lparen);
case ')':return(rparen);
case '+':return(plus);
case '-':return(minus);
case '*':return(times);
case '/':return(divide);
case '%':return(mod);
case '\0':return(eos);
default:return(operand);
}
}
int eval()
{
precedence token;
char symbol;
int op1,op2,n=0;
token=gettoken(&symbol,&n);
while(token!=eos)
{
if(token==operand)
push(symbol-'0');
else
{
op2=pop();
Data Structure Lab Manual

Dept. of CSE Page 10

op1=pop();
switch(token)
{
case plus:push(op1+op2);
break;
case minus:push(op1-op2);
break;
case times:push(op1*op2);
break;
case divide:push(op1/op2);
break;
case mod:push(op1%op2);
}
}
token=gettoken(&symbol,&n);
}
return(pop());
}
void main()
{
int res;
clrscr();
printf("enter valid postfix expression\n");
gets(expr);
res=eval();
printf("Result=%d",res);
getch();
}
Data Structure Lab Manual

Dept. of CSE Page 11

/*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 */
//QUEUE PROGRAM
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX 5
typedef struct
{
int item[MAX];
int front,rear;
}QUEUE;
int full(QUEUE *pq)
{
return(pq->rear==MAX-1?1:0);
}
void insert(QUEUE *pq, int x)
{
if (full(pq))
printf("queue is full");
else
pq->item[++pq->rear]=x;
}
int empty(QUEUE *pq)
{
return(pq->rear<pq->front?1:0);
}
Data Structure Lab Manual

Dept. of CSE Page 12

int delete1(QUEUE *pq)
{
if(empty(pq))
{
printf("QUEUE is empty\n");
return(0);
}
else
return(pq->item[pq->front++]);
}
void disp(QUEUE *pq)
{
int i;
if (empty(pq))
printf("queue is empty\n");
else
for(i=pq->front;i<=pq->rear;i++)
printf("%d\n",pq->item[i]);
}
void main()
{
QUEUE q;
int ch,ele;
q.front=0;
q.rear=-1;
while(1)
{
clrscr();
Data Structure Lab Manual

Dept. of CSE Page 13

printf("1.INSERT\n 2.DELETE\n 3.DISP\n 4.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter the element to be insert\n");
scanf("%d",&ele);
insert(&q,ele);
break;
case 2:ele=delete1(&q);
printf("delet ele=%d\n",ele);
break;
case 3:printf("status of QUEUE\n");
disp(&q);
break;
case 4:exit(0);
default:printf("enter a proper choice\n");
}
getch();
}
}

/*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;
Data Structure Lab Manual

Dept. of CSE Page 14

gross salary= basic_Salary + All_Allowance; Net_Salary =Basic_Salary + All_Allowances - IT*/
#include<iostream.h>
#include<conio.h>
class EMPLOYEE
{
private:int empno,basicsal,all_allowance,grosssal,it,netsal;
char empname[20];
void calculate();
void dispemp();
};
{
cout<<"enter employee no,name,basic sal"<<endl;
cin>>empno>>empname>>basicsal;
}
void EMPLOYEE::calculate()
{
all_allowance=basicsal*1.23;
grosssal=basicsal+all_allowance;
it=grosssal*0.3;
netsal=grosssal-it;
}
void EMPLOYEE::dispemp()
{
cout<<"EMPLOYEE INFORMATION:"<<endl;
cout<<"EMPLOYEE NUMBER :"<<empno<<endl;
cout<<"EMPLOYEE NAME :"<<empname<<endl;
Data Structure Lab Manual

Dept. of CSE Page 15

cout<<"BASIC SALARY :"<<basicsal<<endl;
cout<<"ALL ALLOWANCES :"<<all_allowance<<endl;
cout<<"INCOME TAX :"<<it<<endl;
cout<<"GROSS SALARY :"<<grosssal<<endl;
cout<<"NET SALARY :"<<netsal<<endl;
}
void main()
{
EMPLOYEE e;
clrscr();
e.calculate();
e.dispemp();
getch();
}

/*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)*/
//STRING PROGRAM
#include<iostream.h>
#include<string.h>
#include<conio.h>
class STRING
{
private : char str[30];
Data Structure Lab Manual

Dept. of CSE Page 16

public : STRING(){}
STRING(char *);
STRING(STRING &);
friend ostream & operator <<(ostream &,STRING &);
STRING operator +(STRING &);
};
STRING :: STRING(char *s)
{
strcpy(str,s);
}
STRING :: STRING(STRING &t)
{
strcpy(str,t.str);
}
ostream & operator <<(ostream &p,STRING &T)
{
p<<T.str;
return p;
}
STRING STRING :: operator +(STRING &s)
{
STRING T;
strcpy(T.str,str);
strcat(T.str,s.str);
return T;
}

Data Structure Lab Manual

Dept. of CSE Page 17

void main()
{
clrscr();
STRING S1="VTU";
cout<<"S1="<<S1<<endl;
STRING S2="BELGAUM";
cout<<"S2="<<S2<<endl;
STRING S3=S1+S2;
cout<<"S3="<<S3;
getch();
}

/*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.*/
//STACK PROGRAM
#include<process.h>
#include<iostream.h>
#include<conio.h>
#define MAX 10
class STACK
{
private:int item[MAX],top;
public:STACK()
{
top=-1; }
Data Structure Lab Manual

Dept. of CSE Page 18

STACK operator+(int);
STACK operator--(int);
int full();
int empty();
void disp();
};
STACK STACK::operator+(int x)
{
if(full())
cout<<"stack is full"<<endl;
else
item[++top]=x;
return *this;
}
STACK STACK::operator--(int)
{
if(empty())
cout<<"stack is empty"<<endl;
else
top--;
return *this;
}
int STACK::full()
{
if(top>=MAX-1)
return 1;
return 0;
}
Data Structure Lab Manual

Dept. of CSE Page 19

int STACK::empty()
{
if(top==-1)
return 1;
return 0;
}
void STACK::disp()
{
if(empty())
cout<<"stack is empty"<<endl;
else
for(int i=top;i>=0;i--)
cout<<item[i]<<endl;
}
void main()
{
STACK s1;
int ch,ele;
while(1)
{
clrscr();
cout<<"1.PUSH\n 2.pop\n 3.DISP\n 4.EXIT\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"enter the element to be pushed"<<endl;
Data Structure Lab Manual

Dept. of CSE Page 20

cin>>ele;
s1=s1+ele;
break;
case 2:s1=s1--;
break;
case 3:cout<<"status of stack"<<endl;
s1.disp();
break;
case 4:exit(0);
default:cout<<"enter proper choice"<<endl;
}
getch();
}
}

/*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<conio.h>
#include<iostream.h>
#include<process.h>
struct node
{
int data;
};
typedef struct node *NPTR;

Data Structure Lab Manual

Dept. of CSE Page 21

class LIST
{
private:NPTR list;
public:LIST()
{
list=NULL;
}
void insertfront(int);
int deletefront();
void disp();
};
void LIST::insertfront(int x)
{
NPTR p;
p=new node;
p->data=x;
list=p;
}
int LIST::deletefront()
{
int x;NPTR p;
if(list==NULL)
return-1;
else
{
p=list;
x=p->data;
Data Structure Lab Manual

Dept. of CSE Page 22

delete p;
return x;
}
}
void LIST::disp()
{
NPTR p;
if(list==NULL)
cout<<"empty list"<<endl;
else
cout<<p->data<<endl;
}
void main()
{
LIST l;int ch,ele;
while(1)
{
clrscr();
cout<<" 1.insert front\n 2.delete front\n 3display\n 4.exit\n";
cin>>ch;
switch(ch)
{
case 1:cout<<"enter the elment to be inserted"<<endl;
cin>>ele;
l.insertfront(ele);
Data Structure Lab Manual

Dept. of CSE Page 23

break;
case 2:ele=l.deletefront();
if(ele==-1)
cout<<"empty list"<<endl;
else
cout<<"delete element="<<ele;
break;
case 3:cout<<"status of list"<<endl;
l.disp();
break;
case 4:exit(0);
}
getch();
}
}
/*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>
#include<conio.h>
#define MAX_TERMS 101
typedef struct
{
int row;
int col;
int value;
}term;
Data Structure Lab Manual

Dept. of CSE Page 24

term a[MAX_TERMS];
int n,i;
{
printf("Enter the non-zero terms in a given sparsematrix\n");
scanf("%d",&n);
printf("enter the sparse matrix\n");
for(i=0;i<=n;i++)
scanf("%d%d%d",&a[i].row,&a[i].col,&a[i].value);
}
void disp()
{
printf("SPARSE MATRIX\n\trow\tcol\tvalue\n");
for(i=0;i<=n;i++)
printf("a[%d]: %d\t%d\t%d\n",i,a[i].row,a[i].col,a[i].value);
}
int search()
{
int k;
printf("Enter the key element to be searched in the sparse matrix\n");
scanf("%d",&k);
for(i=1;i<=n;i++)
{
if(k==a[i].value)
return (i);
}
return (-1);
}
Data Structure Lab Manual

Dept. of CSE Page 25

void main()
{
int loc;
clrscr();
disp();
if((loc=search())==-1)
printf("the key value is not present in the sparse matrix\n");
else
printf("The key value is present in %d row %d col",a[loc].row,a[loc].col);
getch();
}

/*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 representationfor the heap. Display the array at the end of insertion phase.*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAX_ELEMENTS 200
#define HEAP_FULL(n) (n==MAX_ELEMENTS-1)
#define HEAP_EMPTY(n) (!n)
int n=0,heap[MAX_ELEMENTS];
void insert_MAX_HEAP(int item)
{
int i;
if(HEAP_FULL(n))
printf("the heap is full\n");
Data Structure Lab Manual

Dept. of CSE Page 26

else
{
i=++n;
while((i!=1)&&(item>heap[i/2]))
{
heap[i]=heap[i/2];
i/=2;
}
heap[i]=item;
}
}
void disp()
{
int i;
if(HEAP_EMPTY(n))
printf("the heapis empty\n");
else
for(i=1;i<=n;i++)
printf("%d\t",heap[i]);
}
void main()
{
int ch,item;
while(1)
{
clrscr();
printf("1.insert\n2.disp\n3.exit\n");
printf("enter ur choise\n");
Data Structure Lab Manual

Dept. of CSE Page 27

scanf("%d",&ch);
switch(ch)
{
case 1: printf("enter one element\n");
scanf("%d",&item);
insert_MAX_HEAP(item);
break;
case 2:printf("status of heap\n");
disp();
break;
case 3:exit(0);
}
getch();
}
}

/*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<conio.h>
#include<alloc.h>
#include<process.h>

Data Structure Lab Manual

Dept. of CSE Page 28

struct node
{
struct node * llink;
int data;
};
typedef struct node * NPTR;
void insertfront(NPTR *pdlist,int x)
{
NPTR p;
p=(NPTR)malloc(sizeof(struct node));
p->data=x;
if(*pdlist!=NULL)
*pdlist=p;
}
void insertleft(NPTR *pdlist,int x,int k)
{
NPTR p,q;
{
if(k==q->data)
break;
}
if(q==NULL)
Data Structure Lab Manual

Dept. of CSE Page 29

else
{
if(q==*pdlist)
insertfront(pdlist,x);
else
{
p=(NPTR)malloc(sizeof(struct node));
p->data=x;
}
}
}
void disp(NPTR *pdlist)
{
NPTR p;
if(*pdlist==NULL)
printf("empty list\n");
else
printf("%d\n",p->data);
}
void main()
{
NPTR pdlist=NULL;
int ch,ele,key;
Data Structure Lab Manual

Dept. of CSE Page 30

while(1)
{
clrscr();
printf("1:insert front\n2:insertleft\n3:disp\n4:exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter the element:");
scanf("%d",&ele);
insertfront(&pdlist,ele);
break;
case 2:printf("enter the element and key element\n");
scanf("%d%d",&ele,&key);
insertleft(&pdlist,ele,key);
break;
case 3:printf("enter the status of doubbly linked list\n");
disp(&pdlist);
break;
case 4: exit(0);
}
getch();
}
}

/*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.
Data Structure Lab Manual

Dept. of CSE Page 31

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<conio.h>
#include<alloc.h>
#include<process.h>
struct node
{
struct node * llink;
int data;
};
typedef struct node * NPTR;
void insertfront(NPTR *pdlist,int x)
{
NPTR p;
p=(NPTR)malloc(sizeof(struct node));
p->data=x;
if(*pdlist!=NULL)
*pdlist=p;
}

Data Structure Lab Manual

Dept. of CSE Page 32

void deletek(NPTR *pdlist,int k)
{
NPTR q;
{
if(k==q->data)
break;
}
if(q==NULL)
else
{
if(*pdlist==q)
{
}
else
{
}
free(q);
}
}

Data Structure Lab Manual

Dept. of CSE Page 33

void disp(NPTR *pdlist)
{
NPTR p;
if(*pdlist==NULL)
printf("empty list\n");
else
printf("%d\n",p->data);
}
void main()
{
NPTR pdlist=NULL;
int ch,ele,key;
while(1)
{
clrscr();
printf("1:insert front\n2:delete\n3:disp\n4:exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("enter the element:");
scanf("%d",&ele);
insertfront(&pdlist,ele);
break;

case 2:printf("enter the key ele\n");
scanf("%d",&key);
Data Structure Lab Manual

Dept. of CSE Page 34

deletek(&pdlist,key);
break;
case 3:printf("enter the status of doubbly linked list\n");
disp(&pdlist);
break;
case 4: exit(0);
}
getch();
}
}
/*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
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.*/
//DATE PROGRAM
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class DATE
{
private:int dd,mm,yy;
public:
void getdate();
int operator-(DATE &);
DATE operator+(int);
friend ostream & operator<<(ostream&,DATE &);
void isleapyear(int); };
Data Structure Lab Manual

Dept. of CSE Page 35

int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void DATE::getdate()
{
scanf("%d%d%d",&dd,&mm,&yy);
}
void DATE::isleapyear(int y)
{
if(y%4==0&&y%100!=0||y%400==0)
days[2]=29;
else
days[2]=28;
}
int DATE ::operator-(DATE &d2)
{
DATE t; int n=0;
t.dd=dd,t.mm=mm,t.yy=yy;
while(t.dd!=d2.dd||t.mm!=d2.mm||t.yy!=d2.yy)
{
isleapyear(t.yy);
if(t.dd>0)
{
t.dd--;
n++;
}
else
if(t.mm>0)
{
t.mm--; t.dd=days[t.mm]; }
Data Structure Lab Manual

Dept. of CSE Page 36

else
{
t.yy--; t.mm=12; t.dd=31;
}
}
return n;
}
DATE DATE::operator+(int n)
{
while(n!=0)
{
isleapyear(yy);
if(dd<days[mm])
dd++;
else
if(mm<12)
{
mm++; dd=1;
}
else
{
yy++; mm=1; dd=1;
}
n--;
}
return *this;
}

Data Structure Lab Manual

Dept. of CSE Page 37

ostream & operator<<(ostream &p,DATE &d)
{
p<<d.dd<<"/"<<d.mm<<"/"<<d.yy<<endl;
return p;
}
void main()
{
DATE d1,d2;
clrscr();
cout<<"enter valid date"<<endl;
d1.getdate();
cout<<"enter second date<d1"<<endl;
d2.getdate();
cout<<"d1="<<d1<<endl;
cout<<"d2="<<d2<<endl;
int n=d1-d2;
cout<<"d1-d2="<<n<<endl;
cout<<"enter no of days io be added"<<endl;
cin>>n;
d2=d1+n;
getch();
}

/*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
Data Structure Lab Manual

Dept. of CSE Page 38

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>
#include<math.h>
class OCTAL
{
private:int oval;
public:OCTAL(){ }
OCTAL(int);
int operator+(int);
friend ostream& operator<<(ostream&,OCTAL&);
};
OCTAL::OCTAL(int ival)
{
int i=0,sum=0,rem;
while(ival!=0)
{
rem=ival%8;
sum+=rem*pow(10,i++);
ival=ival/8;
}
oval=sum;
}
int OCTAL::operator+(int k)
{
int i=0,sum=0,rem;
while(oval!=0)
Data Structure Lab Manual

Dept. of CSE Page 39

{
rem=oval%10;
sum+=rem*pow(8,i++);
oval/=10;
}
return sum+k;
}
ostream& operator<<(ostream&p,OCTAL&s)
{
cout<<"h="<<s.oval<<endl;
return p;
}
void main()
{
clrscr();
int x;
cout<<"enter one number"<<endl;
cin>>x;
OCTAL h=x;
cout<<h;
cout<<"enter the value for k"<<endl;
int k;
cin>>k;
int y=h+k;
cout<<"y="<<y;
getch();
}

Data Structure Lab Manual

Dept. of CSE Page 40

/*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<stdio.h>
#include<conio.h>
struct node
{
struct node* left;
int info;
struct node* right;
};
typedef struct node* NDPTR;
class BINTREE
{
private:NDPTR tree;
public: BINTREE()
{
tree=NULL;
}
NDPTR root()
{
return tree;
}
NDPTR maketree(int x);
void preorder(NDPTR p);
void inorder(NDPTR p);
Data Structure Lab Manual

Dept. of CSE Page 41

void postorder(NDPTR p);
void create(int x);
};
NDPTR BINTREE::maketree(int x)
{
NDPTR p;
p=new node;
p->info=x;
p->left=NULL;
p->right=NULL;
return(p);
}
void BINTREE::preorder(NDPTR p)
{
if(p!=NULL)
{
cout<<p->info<<endl;
preorder(p->left);
preorder(p->right);
}
}
void BINTREE::inorder(NDPTR p)
{
if(p!=NULL)
{
inorder(p->left);
cout<<p->info<<endl;
inorder(p->right);
Data Structure Lab Manual

Dept. of CSE Page 42

}
}
void BINTREE::postorder(NDPTR p)
{
if(p!=NULL)
{
postorder(p->left);
postorder(p->right);
cout<<p->info<<endl;
}
}
void BINTREE::create(int x)
{
NDPTR p,q,r;
p=maketree(x);
if(tree==NULL)
tree=p;
else
{
q=tree;
while(q!=NULL)
{
r=q;
if(x<q->info)
q=q->left;
else
q=q->right;
}
Data Structure Lab Manual

Dept. of CSE Page 43

if(x<r->info)
r->left=p;
else
r->right=p;
}
}
void main()
{
int ch1,ch2,ele;
BINTREE b;
while(1)
{
clrscr();
cin>>ch1;
switch(ch1)
{
case 1:cout<<"enter the elements"<<endl;
cin>>ele;
b.create(ele);
break;
case 2:cout<<"1.preorder\n2.inorder\n3.postorder"<<endl<<"enter your choice:";
cin>>ch2;
switch(ch2)
{
case 1:cout<<"preorder traversel\n"<<endl;
b.preorder(b.root());
break;
Data Structure Lab Manual

Dept. of CSE Page 44

case 2:cout<<"inorder traversel"<<endl;
b.inorder(b.root());
break;
case 3:cout<<"postorder traversel"<<endl;
b.postorder(b.root());
}
break;
case 3:exit(1);
}
getch();
}
}