Top Banner
DS WITH C/C++ LAB _______________________________________________________________________________ Dept.of CSE - 1 - NARENDRA KUMAR S AND SHARATH JAWAHARLAL NEHRU NATIONAL COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING LAB MANUAL ON DATA STRUCTURES WITH C/C++ LABORATORY (10CSL37) PREPARED BY: NARENDRA KUMAR S SHARATH
47

DS with C C++ lab manual VTU 2010

Oct 28, 2015

Download

Documents

Manohar Nelli

Data Structures with c/c++ VTU lab manual
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 1 - NARENDRA KUMAR S AND SHARATH

JAWAHARLAL NEHRU NATIONAL COLLEGE OF

ENGINEERING

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

LAB MANUAL

ON

DATA STRUCTURES WITH C/C++ LABORATORY (10CSL37)

PREPARED BY:

NARENDRA KUMAR S

SHARATH

Page 2: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 2 - NARENDRA KUMAR S AND SHARATH

/*ASSIGNMENT 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<conio.h>

#include<stdio.h>

#include<stdlib.h>

typedef struct

{

int c, e;

}poly;

poly *term;

int av=0,sa=-1,fa=-1,sb=-1,fb=-1,sd=-1,fd=-1,sum=0;

void attach(int coef,int exp)

{

term[av].c=coef;

term[av++].e=exp;

}

void read(int s,int f,int n)

{

int i=0,j;

if(n==0)

{

printf("zero polynomial\n");

}

else

{

for(j=s;j<=f;j++)

{

printf("enter %d term coef and exp\n",++i);

scanf("%d%d",&term[j].c,&term[j].e);

}

}

}

int compare(int x,int y)

{

if(x==y)

return 0;

else if(x>y)

return 1;

else

return -1;

}

Page 3: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 3 - NARENDRA KUMAR S AND SHARATH

void padd()

{

sd=av;

while(sa<=fa&&sb<=fb)

{

switch(compare(term[sa].e,term[sb].e))

{

case 1:attach(term[sa].c,term[sa].e);

sa++;

break;

case -1:attach(term[sb].c,term[sb].e);

sb++;

break;

case 0:sum=(term[sa].c+term[sb].c);

if(sum)

{

attach(sum,term[sa].e);

}

sa++;

sb++;

break;

}

}

for(;sa<=fa;sa++)

attach(term[sa].c,term[sa].e);

for(;sb<=fb;sb++)

attach(term[sb].c,term[sb].e);

fd=av-1;

}

void display(int s,int f)

{

int j;

if(s==-1 && f==-1)

{

printf("no polynomial\n");

}

else

{

for(j=s;j<f;j++)

{

printf("%dx^%d+",term[j].c,term[j].e);

}

printf("%dx^%d",term[j].c,term[j].e);

}

}

Page 4: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 4 - NARENDRA KUMAR S AND SHARATH

void main()

{

int n,m;

clrscr();

printf("enter the number of terms for first polynomial\n");

scanf("%d",&n);

printf("enter the number of terms for second polynomial\n");

scanf("%d",&m);

term=(poly*)malloc(sizeof(poly)*((n+m)*2));

sa=0;

fa=sa+n-1;

sb=fa+1;

fb=sb+m-1;

av=fb+1;

printf("enter the coef and exp for first polynomial\n");

read(sa,fa,n);

printf("enter the coef and exp for second polynomial\n");

read(sb,fb,m);

printf(" first polynomial A(x) =");

display(sa,fa);

printf("\n second polynomial B(x) =");

display(sb,fb);

printf("\n______________________________________________________\n");

padd();

printf("\nResultant polynomial D(x)=");

display(sd,fd);

getch();

}

Page 5: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 5 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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>

#include<stdlib.h>

struct node

{

int coef,exp;

struct node *link;

};

struct node *a,*b,*c;

int n,m,sum=0;

struct node* read(int n)

{

struct node *temp,*x=NULL,*last;

int e,c,i;

for(i=0;i<n;i++)

{

printf("enter the coeffeint and exponenet\n");

scanf("%d%d",&c,&e);

temp=(struct node*)malloc(sizeof(struct node));

temp->coef=c;

temp->exp=e;

temp->link=NULL;

if(x==NULL)

{

x=temp;

x->link=x;

}

else

{

last=x;

while(last->link!=x)

{

last=last->link;

}

temp->link=x;

last->link=temp;

}

}

return x;

}

Page 6: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 6 - NARENDRA KUMAR S AND SHARATH

void display(struct node *a)

{

struct node *temp;

int i;

if(a==NULL)

{

printf("link is empty\n");

}

else

{

for(temp=a;temp->link!=a;temp=temp->link)

{

printf("%dx^%d+",temp->coef,temp->exp);

}

printf("%dx^%d",temp->coef,temp->exp);

}

}

int compare(int x,int y)

{

if(x>y)

return 1;

else if(x<y)

return -1;

else

return 0;

}

void attach(int co,int ex)

{

struct node *temp,*last;

temp=(struct node*)malloc(sizeof(struct node));

temp->coef=co;

temp->exp=ex;

temp->link=NULL;

if(c==NULL)

{

c=temp;

c->link=c;

}

else

{

last=c;

while(last->link!=c)

{

last=last->link;

}

last->link=temp;

Page 7: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 7 - NARENDRA KUMAR S AND SHARATH

temp->link=c;

}

}

void padd()

{ int c1=n,c2=m;

struct node *t1,*t2;

while(c1!=0&&c2!=0)

{

switch(compare(a->exp,b->exp))

{

case 1:attach(a->coef,a->exp);

t1=a;

a=a->link;

free(t1);

c1--;

break;

case -1:attach(b->coef,b->exp);

t2=b;

b=b->link;

free(t2);

c2--;

break;

case 0:sum=a->coef+b->coef;

if(sum)

{

attach(sum,a->exp);

}

t1=a;

t2=b;

a=a->link;

b=b->link;

free(t1);

free(t2);

c1--;

c2--;

break;

}

}

for(;c1!=0;)

{

attach(a->coef,a->exp);

t1=a;

a=a->link;

free(t1);

c1--;

}

for(;c2!=0;)

{

attach(b->coef,b->exp);

Page 8: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 8 - NARENDRA KUMAR S AND SHARATH

t2=b;

b=b->link;

free(t2);

c2--;

}

}

void main()

{

clrscr();

a=NULL;

b=NULL;

c=NULL;

printf("enter the no of terms fa 1st pol\n");

scanf("%d",&n);

printf("enter the no of terms fa 1st pol\n");

scanf("%d",&m);

printf("enter the elements for first poly\n");

a=read(n);

printf("enter the elements for second poly\n");

b=read(m);

printf("polynomial A(X)= ");

display(a);

printf("\npolynomial B(X)= ");

display(b);

padd();

printf("\n____________________________");

printf("\nresultant polynomial D(X) = ");

display(c);

getch();

}

Page 9: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 9 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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).*/

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<string.h>

int F(char);

int G(char);

int top=-1,i,j;

char *s,*postfix,*infix,symbol;

void infix_postfix()

{

j=0;

s[++top]='#';

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--];

j++;

}

postfix[j]='\0';

printf("postfix expression is %s \n ",postfix);

}

Page 10: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 10 - NARENDRA KUMAR S AND SHARATH

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 main()

{

int n;

clrscr();

printf("enter the size of the infix expression\n");

scanf("%d",&n);

infix=(char *)malloc(sizeof(char)*n+1);

postfix=(char *)malloc(sizeof(char)*n+1);

s=(char *)malloc(sizeof(char)*n+1);

printf("enter the infix expression\n");

scanf("%s",infix);

infix_postfix();

getch();

}

Page 11: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 11 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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).*/

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#include<string.h>

#include<process.h>

int F(char);

int G(char);

int top=-1,i,j;

char *s,*postfix,*infix,symbol;

void infix_postfix()

{

j=0;

s[++top]='#';

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--];

j++;

}

postfix[j]='\0';

printf("postfix expression is %s \n ",postfix);

}

Page 12: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 12 - NARENDRA KUMAR S AND SHARATH

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 validate()

{

int count1=0,count2=0,i,flag1=0,flag2=0,top=-1;

char s[20];

for(i=0;i<strlen(infix);i++)

{

{

if(isalpha(infix[i]))

count1++;

else if(isdigit(infix[i]))

count1++;

else if(infix[i]=='('|| infix[i]==')');

else

count2++;

}

if(infix[i]=='(')

s[++top]='(';

Page 13: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 13 - NARENDRA KUMAR S AND SHARATH

if(infix[i]==')')

top--;

}

if(count2==count1-1)

flag1=1;

if(top==-1)

flag2=1;

if(flag1==1 && flag2==1)

printf("\n entered infix expression is valid\n");

else

{

printf("\n entered invalid infix expression\n");

getch();

exit(0);

}

}

void main()

{

int n;

clrscr();

printf("enter the size of the infix expression\n");

scanf("%d",&n);

infix=(char *)malloc(sizeof(char)*n+1);

postfix=(char *)malloc(sizeof(char)*n+1);

s=(char *)malloc(sizeof(char)*n+1);

printf("enter the infix expression\n");

scanf("%s",infix);

validate();

infix_postfix();

getch();

}

Page 14: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 14 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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>

#include<ctype.h>

#include<string.h>

#include<stdlib.h>

int *s,top=-1,res,op1,op2;

char *postfix, symbol;

int compute(char symbol,int op1,int op2)

{

int result;

switch(symbol)

{

case '+':result=op1+op2;

break;

case '-':result=op1-op2;

break;

case '*':result=op1*op2;

break;

case '/':result=op1/op2;

break;

}

return result;

}

void evaluate()

{

int i;

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;

}

}

printf("Result=%d",s[top]);

}

Page 15: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 15 - NARENDRA KUMAR S AND SHARATH

void main()

{

int n;

clrscr();

printf("Enter the size of postfix expression\n");

scanf("%d",&n);

postfix=(char*)malloc((sizeof(char)*n)+1);

s=(int*)malloc(sizeof(int)*n);

printf("Enter the valid postfix expression\n");

scanf("%s",postfix);

evaluate();

getch();

}

Page 16: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 16 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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>

#include<ctype.h>

#include<string.h>

#include<stdlib.h>

int *s,top=-1,res,op1,op2;

char *postfix, symbol;

int compute(char symbol,int op1,int op2)

{

int result;

switch(symbol)

{

case '+':result=op1+op2;

break;

case '-':result=op1-op2;

break;

case '*':result=op1*op2;

break;

case '/':result=op1/op2;

break;

}

return result;

}

void evaluate()

{

int i;

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;

}

}

printf("Result=%d",s[top]);

}

Page 17: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 17 - NARENDRA KUMAR S AND SHARATH

void validate()

{

int count1=0,count2=0,i;

for(i=0;i<strlen(postfix);i++)

{

if(isdigit(postfix[i]))

count1++;

else

count2++;

}

if(count2==count1-1)

printf("\n entered postfix expression is valid\n");

else

{

printf("\n entered invalid postfix expression\n");

getch();

exit(0);

}

}

void main()

{

int n;

clrscr();

printf("Enter the size of postfix expression\n");

scanf("%d",&n);

postfix=(char*)malloc((sizeof(char)*n)+1);

s=(int*)malloc(sizeof(int)*n);

printf("Enter the valid postfix expression\n");

scanf("%s",postfix);

validate();

evaluate();

getch();

}

Page 18: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 18 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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<conio.h>

#include<process.h>

#include<stdlib.h>

int *q, size, f=0,r=-1;

void insert()

{

int ele;

if(r==(size-1))

{

printf("Queue is overflow\n");

}

else

{

printf("enter the elements to be inserted\n");

scanf("%d",&ele);

r=r+1;

q[r]=ele;

}

}

void deletq()

{

if(r==-1)

{

printf("Queue is underflow\n");

}

else

{

printf("deleted ele=%d",q[f]);

f=f+1;

if(f>r)

{

f=0;

r=-1;

}

}

}

void display()

{

int i;

if(r==-1)

{

printf("Queue is underflow\n");

Page 19: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 19 - NARENDRA KUMAR S AND SHARATH

}

else

{

printf("Queue elements are\n");

for(i=f;i<=r;i++)

printf("%d\t",q[i]);

}

}

void main()

{

int choice;

clrscr();

printf("Enter the size of Queue\n");

scanf("%d",&size);

q=(int*)malloc(sizeof(int)*size);

while(1)

{

printf("\n 1 for insert\n");

printf("2 for deletion\n");

printf("3 for display\n");

printf("4 for exit\n");

printf("enter your choice\n");

scanf("%d",&choice);

switch(choice)

{

case 1: insert();

break;

case 2: deletq();

break;

case 3: display();

break;

case 4: exit(0);

default: printf("Invalid choice\n");

}

}

}

Page 20: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 20 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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<iostream.h>

#include<conio.h>

class EMPLOYEE

{

char employee_name[20];

int employee_number;

int basic_salary;

int allowance;

int gross_salary;

int IT;

int net_salary;

public:

void getdata();

void compute();

void display();

};

void EMPLOYEE::getdata()

{

cin>>employee_name>>employee_number>>basic_salary;

}

void EMPLOYEE::compute()

{

allowance=basic_salary*1.23;

gross_salary=basic_salary+allowance;

IT=gross_salary*0.3;

net_salary=gross_salary-IT;

}

void EMPLOYEE::display()

{

cout<<employee_name<<"\t"<<employee_number<<"\t"<<basic_salary<<"\t"<<allowance

<<"\t\t"<<gross_salary<<"\t"<<IT<<"\t"<<net_salary<<"\n";

}

Page 21: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 21 - NARENDRA KUMAR S AND SHARATH

void main()

{

int n,i;

clrscr();

cout<<"enter the number of employees\n";

cin>>n;

EMPLOYEE *e=new EMPLOYEE [n];

for(i=0;i<n;i++)

{ cout<<"enter"<<i+1<<" employee details for name,employee number,basic salary\n"; e[i].getdata();

e[i].compute();

}

cout<<"name\tE_NO\tbasic\tallowance\tgross\tIT\tnetsalary\n";

for(i=0;i<n;i++)

{

e[i].display();

}

getch();

}

Page 22: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 22 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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<iostream.h>

#include<conio.h>

#include<iomanip.h>

class EMPLOYEE

{

char employee_name[20];

int employee_number;

int basic_salary;

int allowance;

int gross_salary;

int IT;

int net_salary;

public: void getdata();

void compute();

void display();

};

void EMPLOYEE::getdata()

{

cin>>employee_name>>employee_number>>basic_salary;

}

void EMPLOYEE::compute()

{

allowance=basic_salary*1.23;

gross_salary=basic_salary+allowance;

IT=gross_salary*0.3;

net_salary=gross_salary-IT;

}

void EMPLOYEE::display()

{

cout<<setw(10)<<setfill(' ')<<employee_name<<setw(10)<<setfill('

')<<employee_number<<setw(10)<<setfill(' ')<<basic_salary<<setw(10)<<setfill('

')<<allowance<<setw(10)<<setfill(' ')<<gross_salary<<setw(10)<<setfill('

')<<IT<<setw(10)<<setfill(' ')<<net_salary<<"\n";

}

Page 23: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 23 - NARENDRA KUMAR S AND SHARATH

void main()

{

int n,i;

clrscr();

cout<<"enter the number of employees\n";

cin>>n;

EMPLOYEE *e=new EMPLOYEE [n];

for(i=0;i<n;i++)

{

cout<<"enter"<<i+1<<" employee details for name,employee number,basic salary\n";

e[i].getdata();

e[i].compute();

}

cout<<setw(10)<<setfill(' ')<<"name"<<setw(10)<<setfill(' ')<<"number"<<setw(10)<<setfill('

')<<"basic"<<setw(10)<<setfill(' ')<<"allowance"<<setw(10)<<setfill('

')<<"gross"<<setw(10)<<setfill(' ')<<"IT"<<setw(10)<<setfill(' ')<<"net"<<"\n";

for(i=0;i<n;i++)

{

e[i].display();

}

getch();

}

Page 24: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 24 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 6: Design, develop, and execute a program in C++ to create a class called

STRING and implement the following operations. Displaythe 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<process.h>

#include<iostream.h>

#include<conio.h>

#include<string.h>

class STRING

{

char *str;

public: STRING()

{

}

STRING(char s[])

{

int len;

len=strlen(s);

str=new char[len+1];

strcpy(str,s);

}

STRING(STRING &s)

{

int len;

len=strlen(s.str);

str=new char[len+1];

strcpy(str,s.str);

}

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

STRING operator+(STRING);

};

ostream& operator<<(ostream& cse,STRING s)

{

cse<<s.str;

return cse;

}

Page 25: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 25 - NARENDRA KUMAR S AND SHARATH

STRING STRING::operator+(STRING s)

{

STRING t;

int len1,len2;

len1=strlen(str);

len2=strlen(s.str);

t.str=new char[len1+len2+2];

strcpy(t.str,str);

strcat(t.str," ");

strcat(t.str,s.str);

return t;

}

void main()

{ char a[100],b[100];

clrscr();

cout<<"enter the first string\n";

cin>>a;

cout<<"enter the second string\n";

cin>>b;

STRING s1=a;

STRING s2=b;

cout<<"Entered first string is\n";

cout<<s1<<endl;

cout<<"Entered second string is\n";

cout<<s2<<endl;

cout<<"Concatenated string is"<<endl;

STRING s3=s1+s2;

cout<<s3;

getch();

}

Page 26: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 26 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 6: Design, develop, and execute a program in C++ to create a class called

STRING and implement the following operations. Displaythe 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<process.h>

#include<iostream.h>

#include<conio.h>

#include<string.h>

class STRING

{

char str[20];

public: STRING()

{

}

STRING(char s[])

{

strcpy(str,s);

}

STRING(STRING &s)

{

strcpy(str,s.str);

}

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

STRING operator+(STRING);

};

ostream& operator<<(ostream& cse,STRING s)

{

cse<<s.str;

return cse;

}

STRING STRING::operator+(STRING s)

{

STRING t;

strcpy(t.str,str);

strcat(t.str," ");

strcat(t.str,s.str);

return t;

}

Page 27: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 27 - NARENDRA KUMAR S AND SHARATH

void main()

{

clrscr();

STRING s1="VTU";

STRING s2="BELGAUM";

cout<<"First string is\n";

cout<<s1<<endl;

cout<<"Second string is\n";

cout<<s2<<endl;

cout<<"Concatenated string is"<<endl;

STRING s3=s1+s2;

cout<<s3;

getch();

}

Page 28: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 28 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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-1 ; 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>

class STACK

{

int *s,top,size;

public:

STACK(int n)

{

size=n;

top=-1;

s=new int[size];

}

STACK operator+(int);

STACK operator-(int);

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

};

STACK STACK::operator+(int ele)

{

if(top==size-1)

{

cout<<"stack full"<<endl;

}

else

{

top=top+1;

s[top]=ele;

}

return *this;

}

STACK STACK::operator-(int one)

{

if(top==-1)

{

cout<<"stack empty"<<endl;

}

else

{

Page 29: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 29 - NARENDRA KUMAR S AND SHARATH

cout<<"poped "<<one<<" element : "<<s[top]<<endl;

top=top-1;

}

return *this;

}

ostream& operator<<(ostream& cse,STACK stk)

{

if(stk.top==-1)

{

cse<<"stack empty"<<endl;

}

else

{

cse<<"contents of stack are"<<endl;

for(int i=stk.top;i>=0;i--)

cse<<stk.s[i]<<endl;

}

return cse;

}

void main()

{

int element,choice,n;

clrscr();

cout<<"enter the size of stack"<<endl;

cin>>n;

STACK s1(n);

while(1)

{

cout<<"\n1->push\n2->pop\n3->display\n4->exit\n";

cin>>choice;

switch(choice)

{

case 1: cout<<"enetr the element to be pushed on to top of the stack"<<endl;

cin>>element;

s1=s1+element;

cout<<s1;

break;

case 2: s1=s1-1;

cout<<s1;

break;

case 3: cout<<s1;

break;

case 4: exit(0);

default: cout<<"invalid choice"<<endl;

}

}

}

Page 30: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 30 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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<conio.h>

#include<process.h>

struct node

{

int data;

struct node *link;

};

class LIST

{

struct node *first;

public: LIST()

{ first=NULL; }

void insert_front(int);

void delet_front();

void display();

};

void LIST::insert_front(int ele)

{

struct node *temp;

temp=new (struct node);

temp->data=ele;

temp->link=NULL;

if(first==NULL)

{ first=temp; }

else

{

temp->link=first;

first=temp;

}

}

void LIST::delet_front()

{

struct node *temp;

if(first==NULL)

{

cout<<"link is empty\n";

}

else if(first->link==NULL)

{

cout<<"deleted node is\n"<<first->data;

delete first;

first=NULL;

Page 31: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 31 - NARENDRA KUMAR S AND SHARATH

}

else

{

temp=first;

cout<<"deleted node is :"<<first->data;

first=first->link;

delete temp;

}

}

void LIST::display()

{

struct node *temp;

if(first==NULL)

{

cout<<"link is empty\n";

}

else

{

cout<<"contents of list are\n";

for(temp=first;temp!=NULL;temp=temp->link)

{

cout<<temp->data<<"->";

}

cout<<"NULL\n";

}

}

void main()

{

LIST l;

int ch,ele;

clrscr();

while(1)

{

cout<<"\n1->insert_front\n2->delete_front \n3->display\n4->exit\n";

cout<<"enter your choice\n";

cin>>ch;

switch(ch)

{ case 1:cout<<"enter the element to insert\n";

cin>>ele;

l.insert_front(ele);

break;

case 2:l.delet_front();

break;

case 3:l.display();

break;

case 4:exit(0);

default:cout<<"invalid choice\n"; }

}

}

Page 32: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 32 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 9: Design, develop, and execute a program in C to reada 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 anelement in the

sparse matrix. */

typedef struct

{

int r,c,v;

}sm;

sm *a;

void read()

{

int i,j,r1,c1,ele,k=1;

printf("enter the row size\n");

scanf("%d",&r1);

printf("enter the column size\n");

scanf("%d",&c1);

a=(sm*)malloc(sizeof(sm)*((r1*c1)/2)+1);

a[0].r=r1;

a[0].c=c1;

printf("enter the sparse matrix elements\n");

for(i=0;i<r1;i++)

{

for(j=0;j<c1;j++)

{

scanf("%d",&ele);

if(ele==0)

continue;

a[k].r=i;

a[k].c=j;

a[k].v=ele;

k++;

}

}

a[0].v=k-1;

if(a[0].v>=((r1*c1)/2+1))

{

printf("entered matrix is not a sparse matrix\n");

getch();

exit(0);

}

}

Page 33: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 33 - NARENDRA KUMAR S AND SHARATH

void display()

{ int i;

printf("sparse matrix is\n");

printf("\trow\tcol\tvalue\n");

printf("____________________________________\n");

for(i=0;i<=a[0].v;i++)

{

printf("a[%d]\t%d\t%d\t%d\n",i,a[i].r,a[i].c,a[i].v);

}

}

void search(int key)

{

int i,flag=0;

for(i=1;i<=a[0].v;i++)

{

if(key==a[i].v)

{

printf("key found at %d row and %d column\n",a[i].r,a[i].c);

flag=1;

}

}

if(flag==0)

printf("unsuccesfull search\n");

}

void main()

{

int key;

clrscr();

read();

display();

printf("enter the search key\n");

scanf("%d",&key);

search(key);

getch();

}

Page 34: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 34 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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<conio.h>

#include<stdlib.h>

#include<process.h>

int *heap,n,ele,count=0;

void insert(int ele)

{

int i;

if(count==n)

{

printf("Heap is full\n");

}

else

{

i=++count;

while((i!=1)&&(ele>heap[i/2]))

{

heap[i]=heap[i/2];

i=i/2;

}

heap[i]=ele;

}

}

void display()

{

int i;

if(count==0)

{

printf("Heap is empty\n");

}

else

{

printf("contents of the heap are\n");

for(i=1;i<=count;i++)

{

printf("%d\t",heap[i]);

}

printf("\n");

}

}

Page 35: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 35 - NARENDRA KUMAR S AND SHARATH

void main()

{

int ch;

clrscr();

printf("enter the size of heap\n");

scanf("%d",&n);

heap=(int*)malloc(sizeof(int)*(n+1));

while(1)

{

printf("\n1->insert\n2->display\n3->exit\n");

printf("enter your choice\n");

scanf("%d",&ch);

switch(ch)

{

case 1: printf("enter the item to be insert");

scanf("%d",&ele);

insert(ele);

display();

break;

case 2: display();

break;

case 3: exit(0);

default: printf("invalid choice\n");

}

}

}

Page 36: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 36 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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 keyvalue 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<stdlib.h>

#include<conio.h>

#include<conio.h>

struct DLL

{

int data;

struct DLL *llink;

struct DLL *rlink;

};

struct DLL *first;

void create(int n)

{

int ele,i;

struct DLL *temp;

for(i=0;i<n;i++)

{

temp=(struct DLL*)malloc(sizeof(struct DLL));

printf("enter the ele\n");

scanf("%d",&ele);

temp->data=ele;

temp->llink=NULL;

temp->rlink=first;

first=temp;

}

}

void insertkey()

{

int key,ele;

struct DLL *temp,*prev,*cur;

temp=(struct DLL*)malloc(sizeof(struct DLL));

printf("\n enter the key \n");

scanf("%d",&key);

printf("\n enter the element \n");

scanf("%d",&ele);

temp->data=ele;

temp->llink=temp->rlink=NULL;

if(first==NULL)

Page 37: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 37 - NARENDRA KUMAR S AND SHARATH

{

first=temp;

}

else if(key==first->data)

{

temp->rlink=first;

first->llink=temp;

first=temp;

}

else

{

prev=NULL;

cur=first;

while(key!=cur->data&&cur!=NULL)

{

prev=cur;

cur=cur->rlink;

}

if(key!=cur->data)

{

printf("\n key not found\n");

return;

}

temp->rlink=cur;

temp->llink=prev;

prev->rlink=temp;

cur->llink=temp;

}

}

void deletekey()

{

int key;

struct DLL *temp,*prev,*cur;

printf("\n enter the key to delete \n");

scanf("%d",&key);

if(first==NULL)

printf("\n Empty Doubly Linked List \n");

else if(key==first->data)

{

temp=first;

first=first->rlink;

printf("\n key found! and deleted i.e=%d",temp->data);

free(temp);

}

else

{

prev=NULL;

cur=first;

while(key!=cur->data&&cur!=NULL)

{

Page 38: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 38 - NARENDRA KUMAR S AND SHARATH

prev=cur;

cur=cur->rlink;

}

if(key!=cur->data)

{

printf("\n key not found \n");

return;

}

printf("\n key found! and deleted i.e = %d",cur->data);

prev->rlink=cur->rlink;

cur->rlink->llink=prev;

free(cur);

}

}

void display()

{

struct DLL *temp;

if(first==NULL)

{

printf("\n Empty Doublu Linked List \n");

}

else

{

printf("\n contents of DLL\n");

for(temp=first;temp!=NULL;temp=temp->rlink)

{

printf("%d<=>",temp->data);

}

printf("NULL \n");

}

}

void main()

{

int ch,n;

first=NULL;

clrscr();

while(1)

{

printf("\n 1-> create \n 2->insert key \n 3->delete key \n 4->display \n 5->exit \n");

printf("Enter your choice\n");

scanf("%d",&ch);

switch(ch)

{case 1: printf("\n enter no of nodes\n");

scanf("%d",&n);

create(n); break;

case 2:insertkey(); break;

case 3:deletekey(); break;

case 4:display(); break;

case 5:exit(0);

} } }

Page 39: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 39 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 12: Design, develop, and execute a program in C++ to create a class called

DATE with methods to accept two valid datesin 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 andno_of_days is an integer. */

#include<iostream.h>

#include<conio.h>

class DATE

{

int day,month,year,flag;

public:

void getdate();

DATE operator +(int);

int operator -(DATE);

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

int operator>=(DATE);

int return_integer_date(DATE);

};

int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};

int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int DATE::operator>=(DATE d2)

{

if(year==d2.year)

{

if(month<d2.month)

return 0;

else if(day<d2.day)

return 0;

else

return 1;

}

else if(year>d2.year)

{

return 1;

}

else

return 0;

}

int DATE::return_integer_date(DATE D1)

{

int int_value = D1.day;

if((D1.flag == 1)&&(D1.month>=2))

{

for(int i = 0; i<D1.month; i++)

Page 40: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 40 - NARENDRA KUMAR S AND SHARATH

int_value=int_value+b[i];

}

else

{

for(int i=0;i<D1.month;i++)

int_value=int_value+a[i];

}

return int_value;

}

int DATE::operator -(DATE D2)

{

int a1, a2,x = 0;

DATE D1;

D1.day=day, D1.month=month, D1.year=year;

if(D1.day == D2.day && D1.month == D2.month && D1.year == D2.year)

return x;

a1 = return_integer_date(D1);

a2 = return_integer_date(D2);

cout<<"a1="<<a1<<endl;

cout<<"a2="<<a2<<endl;

for(int i = D1.year-1; i > D2.year; i--)

{

if(i%4 == 0)

x = x+366;

else

x = x+365;

}

if(D1.year == D2.year)

x = x+a1-a2;

else

{

x = x+a1;

if(D2.year%4 == 0)

x = x+(366-a2);

else

x = x +(365-a2);

}

return x;

}

DATE DATE ::operator +(int ndays)

{

DATE d;

d.day=day;d.month=month;d.year=year;

for(int i=1;i<=ndays;i++)

{

d.day++;

if(d.year%4==0 )

{

if(d.day>b[d.month])

Page 41: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 41 - NARENDRA KUMAR S AND SHARATH

{

d.day=1;

d.month++;

}

}

else if(d.day>a[d.month])

{

d.day=1;

d.month++;

}

if(d.month>12)

{

d.month=1;

d.year++;

}

}

return d;

}

void DATE :: getdate()

{

int f=0;

cout <<"\n";

cout<<"Day:"; cin>>day;

cout<<"Month:";cin>>month;

cout<<"Year:";cin>>year;

cout <<"\n";

if(year%4==0)

{

flag=1;

if(month<=12)

{ if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day<=31) f=1;

else if((month==2)&&day<=29)

f=1;

else if((month==4||month==6||month==9||month==11)&&day<=30)

f=1;

else

f=0;

}

}

else

{

flag=0;

if(month<=12)

{ if((month==1||month==3||month==5||month==7||month==8||month==10||month==12)&&day<=31) f=1;

else if((month==2)&&day<=28)

f=1;

Page 42: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 42 - NARENDRA KUMAR S AND SHARATH

else if((month==4||month==6||month==9||month==11)&&day<=30)

f=1;

else

f=0;

}

}

if(f==0)

{

cout<<"entered date is invalid"<<endl;

getdate();

}

}

ostream & operator<<(ostream &print, DATE &d)

{

print<<d.day<<"-"<<d.month<<"-"<<d.year;

return(print);

}

void main()

{

DATE d1,d2,d3;

int n,rt=1;

clrscr();

while(rt)

{

cout<<"\nEnter the valid first date\n";

d1.getdate();

cout<<"\nEnter the valid second date which has\nto be less than or equal to first\n";

d2.getdate();

if(d1>=d2)

{

n=d1-d2;

cout <<"\n\n\n d1:"<<d1<<"\n(-) d2:"<<d2<<"\n\nNo of days between the two days

is :"<<n<<"\n--------------------------------------------";

cout<<"\n\n Enter the no of days n:";

cin>>n;

d3=d1+n;

cout<<"\n First date: "<<d1

<<"\n after addition of "<<n <<" days is::"<<d3

<<"\n-----------------------------------------------------------";

rt=0;

}

else

{

cout<<"first date is to be less than or equal to second date"<<endl;

}

}

getch();

}

Page 43: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 43 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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 isan

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

{

int o;

public:

OCTAL(int x)

{

o=dectooct(x);

}

int dectooct(int);

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

int octtodec(int);

int operator+(int);

};

ostream& operator<<(ostream& cse,OCTAL h)

{

cse<<h.o;

return cse;

}

int OCTAL::operator+(int k)

{

return octtodec(o)+k;

}

int OCTAL::dectooct(int x)

{

int sum=0,i=0,r;

while(x!=0)

{

r=x%8;

sum=sum+(r*pow(10,i));

i++;

x=x/8;

}

return sum;

}

Page 44: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 44 - NARENDRA KUMAR S AND SHARATH

int OCTAL::octtodec(int x)

{

int sum=0,i=0,r;

while(x!=0)

{

r=x%10;

sum=sum+(r*pow(8,i));

i++;

x=x/10;

}

return sum;

}

void main()

{

int x,k;

clrscr();

cout<<"enter X value in DECIMAL";

cin>>x;

OCTAL h=x;

cout<<"X value in OCTAL is"<<endl;

cout<<h;

cout<<"\n enter integer value of K\n";

cin>>k;

int y=h+k;

cout<<" y value is="<<y;

getch();

}

Page 45: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 45 - NARENDRA KUMAR S AND SHARATH

/* ASSIGNMENT 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<conio.h>

#include<string.h>

#include<process.h>

struct NODE

{

int data;

struct NODE *lchild,*rchild;

};

class BIN_TREE

{

public: struct NODE *root;

BIN_TREE()

{

root=NULL;

}

void insert(int);

void inorder(struct NODE*);

void preorder(struct NODE*);

void postorder(struct NODE*);

};

void BIN_TREE::insert(int item)

{

int i;

char direction[10];

struct NODE *temp,*prev,*cur;

temp=new (struct NODE);

temp->data=item;

temp->lchild=temp->rchild=NULL;

if(root==NULL)

{

root=temp;

return;

}

cout<<"enter the direction to insert a node\n";

cin>>direction;

prev=NULL;

cur=root;

for(i=0;i<strlen(direction);i++)

{

if(cur==NULL)

break;

prev=cur;

Page 46: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 46 - NARENDRA KUMAR S AND SHARATH

if(direction[i]=='L' || direction[i]=='l')

cur=cur->lchild;

else

cur=cur->rchild;

}

if(cur!=NULL || i!=strlen(direction))

{

cout<<"invalid direction\n";

delete temp;

return;

}

if(direction[i-1]=='L' || direction[i-1]=='l')

prev->lchild=temp;

else

prev->rchild=temp;

}

void BIN_TREE::inorder(struct NODE * temp)

{

if(temp!=NULL)

{

inorder(temp->lchild);

cout<<temp->data<<"\t";

inorder(temp->rchild);

}

}

void BIN_TREE::preorder(struct NODE * temp)

{

if(temp!=NULL)

{

cout<<temp->data<<"\t";

preorder(temp->lchild);

preorder(temp->rchild);

}

}

void BIN_TREE::postorder(struct NODE * temp)

{

if(temp!=NULL)

{

postorder(temp->lchild);

postorder(temp->rchild);

cout<<temp->data<<"\t";

}

}

Page 47: DS with C C++ lab manual VTU 2010

DS WITH C/C++ LAB

_______________________________________________________________________________

Dept.of CSE - 47 - NARENDRA KUMAR S AND SHARATH

void main()

{

int ch,ele;

BIN_TREE bt;

clrscr();

while(1)

{

cout<<"\n1->insert\n2->inorder\n3->postorder\n4->preorder\n5->exit\n";

cout<<"Enter your choice\n";

cin>>ch;

switch(ch)

{

case 1: cout<<"ente the element to insert\n";

cin>>ele;

bt.insert(ele);

break;

case 2: bt.inorder(bt.root); break;

case 3: bt.postorder(bt.root); break;

case 4: bt.preorder(bt.root);break;

case 5: exit(0);

default: cout<<"wrong choice\n";

}

}

}