Sunteți pe pagina 1din 42

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

BAHUBALI COLLEGE OF ENGINEERING SHRAVANABELAGOLA.

DATA STRUCTURES IN C LABORATORY MANUAL THIRD SEMESTER 2011

FACULTY: Mr. NAGARAJU S

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Program 1. 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_DEGREE 101 #define COMPARE((a<b) ? -1: (a==b)? 0:1) typedef struct { int degree; float coef[MAX_DEGREE]; }polynomial; polynomial a,b,d; int main() { int i,FALSE=0,TRUE=1; int poly=0; clrscr(); /*if(poly) return FALSE; else return TRUE;*/ printf("enter value for degree to invok exponent") scanf("%d",&a.degree,&b.degree); printf("enter coefficiants"); for(i=0;i<=n;i++) scanf("%d",&a.coef[i],&b.coef[i]); d=Zero(a,b); while(!IsZero(a) && !IsZero(b)) do { case -1: }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Program

2.

Write a C program to convert & print a valid parenthesized infix expression to postfix. The expression should consist of single character operands and binary operators. #include<stdio.h> #include<stdlib.h> #include<string.h> int f(char symbol) { switch(symbol) { case '+': case case case case case case case '-': '*' : '/': '^' : '$' : '(' : '#' : : return 2; return 2; return 4; return 4; return 5; return 5; return 0; return -1; return 8;

default } }

int g(char symbol) { switch(symbol) { case '+' case case case case case '-' '*' '/' '^' '$' : : return 6; : : : : return 3; return 1;

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

case

'('

: :

return 9; return 0; return 7;

case ')' default } } :

int infixtoposfix(char infix[],char postfix[]) { int top,i,j; char s[30],symbol; top=-1; s[++top]='#'; j=0; for(i=0;i<strlen(infix);i++) { symbol=infix[i]; while(f(s[top])>g(symbol)) { postfix[j]=s[top--]; j++; } if(f(s[top]!=g(symbol))) s[++top]=symbol; else top--; } while(s[top]!='#') { } postfix[j]='\0'; postfix[j++]=s[top--];

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

int main() { char infix[30],postfix[30]; printf("enter the valid expression"); scanf("%s",infix); infixtoposfix(infix,postfix); printf("the infix expression is %s",infix); printf("the postfix expression is %s",postfix); return 0; }

---------------------------------------------------------------OUTPUT-------------------------------------------------enter the valid expressiona*b+c*d the infix expression is a*b+c*d the postfix expression is ab*cd*+

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Program

3.

Wrie a C program to evaluate a valid postfix expression using a stack. Assume that the postfix expression is read as a single line consisting of non negative singe digit operands and binary arithmatic operators. The operators are +,,*,/. #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> int compute(char symbol,float op1,float op2) { switch(symbol) { case '+' : case '-': return op1+op2; return op1-op2; return op1*op2; return op1/op2;

case '*' : case '/':

case '^': case'$': } } int main() { float res,op1,op2; char postfix[20],symbol,s[20]; int i,top; printf("enter the valid postfix expression"); scanf("%s",postfix); top=-1; for(i=0;i<strlen(postfix);i++) { return pow(op1,op2);

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

symbol=postfix[i]; if(isdigit(symbol)) s[++top]=symbol-'0'; else { op2=s[top--]; op1=s[top--]; res=compute(symbol,op1,op2); s[++top]=res; } } res=s[top--]; printf("evaluated expression is %2f",res); return 0; } ---------------------------------------------------------------OUTPUT------------------------------------------------enter the valid postfix expression 23+3* evaluated expression is 15.0000

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Program 4. Write a program to simulate the working of queue of integers using an array. Provide the following operations: a] Insert b] Delete c] Display

#include<stdio.h> #include<stdlib.h> # define queuesize 5 int item,i,r=-1,f=0,q[5]; int insert() { if(r==queuesize-1) { printf("queue overflow\n"); return; } r=r+1; q[r]=item; return 0; } int delete() { if(f>r) { printf("queue is empty"); return; } printf("item deleted is %d\n",q[f++]); if(f>r) { f=0; r=-1;

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

} return 0; }

int display() {

if(f>r) { printf("queue empty"); return ; } printf("the contents of queue are\n"); for(i=0;i<=r;i++) printf("%d\t",q[i]); return 0; } int main() { int choice; for(;;) { printf("1:insert 2: delete 3: display 4:exit\n"); printf("enter the choice"); scanf("%d",&choice); switch(choice) { case 1: printf("enter the item\n"); scanf("%d",&item);

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

insert(); break; case 2: delete(); break; case 3: display(); break; default: } } return 0; } exit(0);

-----------------------------------------------------------------OUTPUT------------------------------------------------1:insert 2: delete 3: display 4:exit enter the choice1 enter the item 11 1:insert 2: delete 3: display 4:exit enter the choice1 enter the item 12 1:insert 2: delete 3: display 4:exit enter the choice1 enter the item 13 1:insert 2: delete 3: display 4:exit enter the choice1 enter the item 14

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

1:insert 2: delete 3: display 4:exit enter the choice1 enter the item 15 1:insert 2: delete 3: display 4:exit enter the choice1 enter the item 16 queue overflow 1:insert 2: delete 3: display 4:exit enter the choice3 the contents of queue are 11 12 13 14 15 1:insert 2: delete 3: display 4:exit

enter the choice2 item deleted is 11 1:insert 2: delete 3: display 4:exit enter the choice2 item deleted is 12 1:insert 2: delete 3: display 4:exit enter the choice2 item deleted is 13 1:insert 2: delete 3: display 4:exit enter the choice2 item deleted is 14 1:insert 2: delete 3: display 4:exit enter the choice2 item deleted is 15 1:insert 2: delete 3: display 4:exit enter the choice2

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

queue is empty1:insert 2: delete 3: display 4:exit enter the choice4

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Program 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) //1st :To calculate employee net salry #include<iostream.h> #include<conio.h> #define SIZE 5 class emp { float basic,da,it,netsal; char name[20],num[10]; public: void getdata(); void net_sal(); void dispdata(); }; void emp::getdata() { cout<<"\n Enter employee number: " ; cin>>name; cout<<" Enter employee name: " ; cin>>num; cout<<"Enter employee basic salary in Rs: " ; cin>>basic; } void emp::net_sal() { da=((0.52)*basic ); float gsal=da+basic; it=((0.3)*gsal); netsal=gsal-it; } void emp::dispdata() { cout

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

<<"\n <<"\n <<"\n }

Employee number: "<<name Employee name: "<<num Employee netsalary: "<<netsal<<" Rs.";

void main() { clrscr(); emp ob[SIZE]; int n; cout<<"\n\n**************************************************" <<"\n Calculation of Employees Net Salray" <<"\n**************************************************" <<"\n Enter the number of employees"; cin>>n; for(int i=0;i<n;i++) { ob[i].getdata(); ob[i].net_sal(); } clrscr(); cout<<"\n-----------------" <<"\nEmployee Detail::" <<"\n-----------------"; for( i=0;i<n;i++) { cout<<"\n\n Employee:"<<i+1 <<"\n ----------"; ob[i].dispdata(); } }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Program 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 { char name[23]; public :string() { name[23]='\0'; } string(char s[]) { strcpy(name,s); } string(string &s) { strcpy(name,s.name); } friend string operator +(string s1, string s2); friend ostream &operator <<(ostream &out, string &s); }; ostream &operator <<(ostream &out , string &s) { out <<"\t"<<s.name<<endl; return(out); } string operator +(string s1, string s2) { string temp(s1); //strcat(temp.name,""); strcat(temp.name, s2.name); return(temp); } void main() { clrscr(); string s1("vtu"); string s2("belgaum"); string s3=s1+s2; cout<<"\nFIRST STRING ="<<s1<<"\nSECOND STRING ="<<s2 <<"\nCONCATENATAD THIRD STRING ="<<s3;

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

getch(); }

Program 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 thestack. ii. s1=s1- ; where s1 is an object of the class STACK and operator pops off the top element.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

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<stdlib.h> class stack { int a[100],top,size; public: stack(int n) { top=-1; size=n; } friend stack operator+(stack,int); friend stack operator--(stack); friend int empty(stack); friend int overflow(stack); friend ostream & operator<<(ostream &,stack); }; stack operator+(stack s1,int num) { s1.a[++s1.top]=num; return s1; } stack operator--(stack s1) { clrscr(); cout<<"\n The deleted element is:"<<s1.a[s1.top--]; return s1; } int empty(stack s1) { if(s1.top==-1) return 1; else return 0; } int overflow(stack s1) {

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

if(s1.top==s1.size-1) return 1; else return 0; } ostream & operator<<(ostream &print,stack s1) { if(empty(s1)) print<<"\n The stack is empty"; else { print<<"\n The element in stack are:\n"; for(int i=s1.top;i>=0;i--) { print<<" | "<<s1.a[i]<<" } print<<" |_______|"; } return print; } void main() { int num,ch=1,n; cout<<"\nEnter the size of stack:"; cin>>n; stack s1(n); clrscr(); while(ch) { cout<<"\n\n\n\n ************** Stack************** \n" <<"\n-------- Menu ---------" <<"\n Enter 1 to pushed " <<"\n Enter 2 to popped " <<"\n Enter 3 to display " <<"\n Enter 4 to exit " <<"\n Enter your choice: "; cin>>ch; switch(ch) { case 1:clrscr(); if(overflow(s1)) cout<<"\n Stack overflow"; else { cout<<"\n Enter the number to be inserted "; cin>>num; s1=s1+num; } cout<<s1;

|\n";

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

break; case 2:clrscr(); if(empty(s1)) cout<<"\n Stack empty"; else s1=--s1; cout<<s1; break; case 3:clrscr(); cout<<s1; case 4:exit(0); default : cout<<"\nInvalid choice"; } } getch(); }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Program

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<stdlib.h> struct NOD { int info; struct NOD *next; }; typedef struct NOD node; class linklist { node *f; public: linklist()

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

{ f=NULL; } void insert(int); void del(); void disp(); }; void linklist::insert(int num) { node *p=new node; p->info=num; p->next=f; f=p; } void linklist::del() { clrscr(); node *temp=f; if(f==NULL) cout<<"\n The list is empty"; else { cout<<"\n The deleted element is :"<<f->info; f=f->next; delete temp; cout<<"\n Deletion successful"; } return; } void linklist::disp() { node *temp=f; if(f==NULL) cout<<"\n The list is empty"; else { cout<<"\n The element in list are:"; while(temp!=NULL) { cout<<" "<<temp->info; temp=temp->next; } } } void main() {

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

int num,ch=1; linklist ob; clrscr(); while(ch) { cout<<"\n\n\n\n ************** Linked List ************** \n" <<"\n-------- Menu ---------" <<"\n Enter 1 to pushed " <<"\n Enter 2 to popped " <<"\n Enter 3 to display " <<"\n Enter 4 to exit " <<"\n Enter your choice: "; cin>>ch; switch(ch) { case 1:clrscr(); cout<<"\n Enter the number to be inserted "; cin>>num; ob.insert(num); ob.disp(); break; case 2:clrscr(); ob.del(); ob.disp();break; case 3:clrscr(); ob.disp();break; case 4:exit(0); default : cout<<"\nInvalid choice"; } } getch(); }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Program 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 col; int row; int value; }term; void main() { term a[MAX_TERMS]; int i,j,m,n,r[50][50],k,c,item,flag=0; clrscr(); printf("enter the size of matrix\n"); scanf("%d %d",&m,&n); printf("%d %d",m,n); printf("\n Enter the %d elements of sparse matrix A\n",m*n); for(i=0;i<m;i++) { for(j=0;j<n;j++) { scanf("%d",&r[i][j]); } } k=1;

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

c=0; for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(r[i][j]!=0) { a[k].row=i; a[k].col=j; a[k].value=r[i][j]; k++; c++; } } } a[0].row=m; a[0].col=n; a[0].value=c; printf("\n\n"); for(i=0;i<k;i++) { if(i==0) { printf("%d\t %d\t %d\n",a[i].row,a[i].col,a[i].value); printf("-------------------------------\n"); } else printf("%d\t %d\t %d\n",a[i].row+1,a[i].col+1,a[i].value); } printf("Enter the number to be searched\n"); scanf("%d",&item); for(i=1;i<=k;i++) { if(a[i].value==item) { flag=1; printf("Row=%d\n col=%d\n value=%d\n",a[i].row+1,a[i].col+1,a[i].value); } } if(flag==0) printf("Unsuccessful Search\n"); getch(); }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Program 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.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Program 11: Write a C program to support the following operations on a where each node consists of integers. a] Create a doubly linked list by adding each node at b] Insert new nodes to the left of the node whose key input c] Delete the node of the given data, if it is found, appropriate message. d] Display the contents of the file. #include<stdio.h> #include<stdlib.h> doubly linked list the front. value is read as an otherwise display

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

struct node { int info; struct node *llink,*rlink; }; typedef struct node *NODE; NODE getnode() { NODE x; x=(NODE)malloc(sizeof(struct node)); if(x==NULL) { printf("no memory"); exit(0); } return x; }

void freenode(NODE x) { free(x); } NODE insertfront(int item,NODE head) { NODE temp,curr; temp=getnode(); temp->info=item; curr=head->rlink; head->rlink=temp;

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

temp->llink=head; temp->rlink=curr; curr->llink=temp; return head; } NODE delete(int item,NODE head) { NODE prev,curr,next; if(head->rlink==head) { printf("list is empty"); return head; } curr=head->rlink; while(curr!=head) { if(item==curr->info) break; curr=curr->rlink; } if(curr==head) { printf("item not found"); return head; } prev=curr->llink; next=curr->rlink; prev->rlink=next; next->llink=prev;

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

freenode(curr); return head; } NODE linsert(int item,NODE head) { NODE temp,prev,curr; if(head->rlink==NULL) { printf("lists is empty"); return head; } curr=head->rlink; while(curr!=NULL) { if(item==curr->info) break; curr=curr->rlink; } if(curr==NULL) { printf("key not found"); return head; } prev=curr->llink; printf("enter the item to be inserted "); temp=getnode(); scanf("%d",&temp->info); prev->rlink=temp; temp->llink=prev;

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

curr->llink=temp; temp->rlink=curr; return head; }

void display(NODE head) { NODE temp; if(head->rlink==head) {printf("list is empty"); return; } printf("contents of list are"); temp=head->rlink; while(temp!=head) { printf("%d",temp->info); temp=temp->rlink; } } int main() { NODE head; int choice,item; head=getnode(); head->rlink=head; head->llink=head; for(;;) {

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

printf("1:insertfront 2:delete 3: linsert 4:display 5:exit\n"); printf("enter the choice\n"); scanf("%d",&choice); switch(choice) { case 1: printf("enter the item to be inserted\n"); scanf("%d",&item); head=insertfront(item,head); break; case 2: printf("enter the item to be deleted\n"); scanf("%d",&item); head=delete(item,head); break; case 3: printf("enter the key\n"); scanf("%d",&item); head=linsert(item,head); break; case 4: display(head); break; default: } } return(0); } ---------------------------------------------------------------OUTPUT-----------------------------------------------1:insertfront 2:delete 3: linsert 4:display 5:exit enter the choice 1 exit(0);

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

enter the item to be inserted 11 1:insertfront 2:delete 3: linsert 4:display 5:exit enter the choice 1 enter the item to be inserted 12 1:insertfront 2:delete 3: linsert 4:display 5:exit enter the choice 4 contents of list are 12 11 1:insertfront 2:delete 3: linsert 4:display 5:exit enter the choice 3 enter the key 11 enter the item to be inserted 10 1:insertfront 2:delete 3: linsert 4:display 5:exit enter the choice 4 contents of list are 12 10 11 1:insertfront 2:delete 3: linsert 4:display 5:exit enter the choice 2 enter the item to be deleted 10 1:insertfront 2:delete 3: linsert 4:display 5:exit enter the choice 4

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

contents of list are 12 11 1:insertfront 2:delete 3: linsert 4:display 5:exit enter the choice 5

Program 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> class date { int day,month, year; public: void getdata(); date operator +(int ); int operator -(date ); friend ostream & operator<<(ostream &, 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) { date res; int noly=0,nod,temp=d2.year; for(temp;temp<=year;temp++) if(temp%4==0 ) noly++; res.day=day-d2.day; if(res.day<0) { month--;

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

res.day=day+a[month]; } res.month=month-d2.month; if(res.month<0) { res.month=month+12; year--; } res.year=year-d2.year; if(res.year<0) { return -1; } nod=res.day+(res.year*365); for(int i=1;i<=res.month;i++) nod=nod+a[i]; nod+=noly; return nod; } 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]) { 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; }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

void {

date :: getdata()

cout <<"\n"; cout<<"Date:"; cin>>day; cout<<"Month:";cin>>month; cout<<"Year:";cin>>year; cout <<"\n"; } ostream & operator<<(ostream &print, date &d) { print<<d.day<<"-"<<d.month<<"-"<<d.year; return(print); } void main() { date d1,d2,d3; int n; clrscr(); cout<<"\nEnter the valid first date\n"; d1.getdata(); cout<<"\nEnter the valid second date which has\nto be less than of equal to first\n"; d2.getdata(); 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 <<" after addition of "<<n <<" days is::"<<d3 <<"\n-----------------------------------------------------------"; getch(); }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Program 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> #include<stdlib.h> #include<math.h> class OCTAL { int oct[15],count; public: OCTAL(int x); int operator +(int); friend ostream &operator<< (ostream &,OCTAL &); }; OCTAL ::OCTAL(int x) { int i=0,rem,a[15]; while(x!=0) { rem=x%8; x=x/8; a[i++]=rem; } count=i; int n=count-1; for(i=0;i<count;i++) { oct[i]=a[n]; n--; } } int OCTAL::operator+(int k)

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

{ int x=0,i; int j=count-1; for(i=0;i<count;i++) { x=x+oct[j]*pow(8,i); j--; } return(x+k); } ostream & operator<<(ostream &print,OCTAL &o) { for(int i=0;i<o.count;i++) print<<o.oct[i]; return(print); } void main() { int x,k,y=0; clrscr(); cout<<"\nEnter the integer value in decimal: "; cin>>x; OCTAL h=OCTAL(x); cout<<"The corresponding octal value for ("<<x <<" )is: "<<h; cout<<"\n\nEnter the integer to be added to octal value:"; cin>>k; y=x+k; cout<<"\n "<<h<<" (Octal)+"<<k<<" (Decimal)="<<y<<" (Decimal)"; getch(); }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

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.

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

Create a BIN_TREE object and demonstrate the traversals. #include<conio.h> #include<stdlib.h> #include<iostream.h> struct node { int info; struct node *left; struct node *right; }; typedef struct node tree ; tree *root=NULL; class BIN { int num; tree *p,*prev,*temp; public: void insert(); void inorder(tree *); void postorder(tree *); void preorder(tree *); void display(); }; void BIN:: insert() { p=new(tree); cout<<"\n Enter number:"; cin>>num; p->info=num; p->left=p->right=NULL; if(root==NULL) { root=p; return; } temp=root; while(temp!=NULL) { if(num>=temp->info) { prev=temp; temp=temp->right; } else { prev=temp; temp=temp->left; } }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

if(num>=prev->info) prev->right=p; else prev->left=p; } void BIN::preorder(tree *temp) { if(temp!=NULL) { cout<<"\t"<<temp->info; preorder(temp->left); preorder(temp->right); } } void BIN:: inorder(tree *temp) { if(temp!=NULL) { inorder(temp->left); cout<<"\t"<<temp->info; inorder(temp->right); } } void { BIN::postorder(tree *temp) if(temp!=NULL) { postorder(temp->left); postorder(temp->right); cout<<"\t"<<temp->info; } } void BIN:: display() { if(root==NULL) { cout<<"\n ***EMPTY TREE**** \n"; return; } cout<<"\n\n THE PREORDER DISPLAY IS: "; preorder(root); cout<<"\n\n THE INORDER DISPLAY IS: "; inorder(root); cout<<"\n\n THE POSTORDER DISPLAY IS: ";

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

DATA STRUCTURES IN C LABORATORY MANUAL

10CSL37

postorder(root); } void main() { BIN o; int ch=1; int count=0; clrscr(); while(ch) { cout<<"\n\n\n\t\t\t***********MENU************\n"; cout<<"\n\t\t\t1:INSERT-IN-TREE\n\t\t\t2:DISPLAY\n\t\t\t3.QUIT\n"; cout<<"\nEnter your choice:\n"; cin>>ch; switch(ch) { case 1:clrscr(); count++; o.insert(); break; case 2:clrscr(); cout<<"\n\n THE NUMBER OF NODES IN THE BST is "<< count; o.display(); break; case 3:exit(0); } } getch(); }

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

BCE S.BELAGOLA

S-ar putea să vă placă și