Вы находитесь на странице: 1из 50

DS WITH C/C++ LAB

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

MANOHAR V NELLI

_______________________________________________________________________________ Dept. of CSE -1NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/*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; }

_______________________________________________________________________________ Dept. of CSE -2NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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); } }

_______________________________________________________________________________ Dept. of CSE -3NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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(); }

_______________________________________________________________________________ Dept. of CSE -4NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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> #include<process.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; } void display(struct node *a) { struct node *temp; _______________________________________________________________________________ Dept. of CSE -5NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

int i; if(a==NULL) { printf("empty polynomial equation\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; temp->link=c; } }

_______________________________________________________________________________ Dept. of CSE -6NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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); t2=b; b=b->link; free(t2); c2--; _______________________________________________________________________________ Dept. of CSE -7NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

} }

void main() { clrscr(); a=NULL; b=NULL; c=NULL; printf("enter the no of terms for 1st polynomial equation\n"); scanf("%d",&n); printf("enter the no of terms for 2nd polynomial equation\n"); scanf("%d",&m); if (n==0||m==0) { printf("empty polynomial equation cant be added"); getch(); exit(0); } printf("enter the elements for first poly\n"); a=read(n); printf("enter the elements for second poly\n"); b=read(m); printf("\t polynomial A(X)= "); display(a); printf("\n\t polynomial B(X)= "); display(b); padd(); printf("\n____________________________"); printf("\nresultant polynomial D(X)= "); display(c); getch(); }

_______________________________________________________________________________ Dept. of CSE -8NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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); //prototype //prototype

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); } int F(char symbol) { switch(symbol) { case '+': _______________________________________________________________________________ Dept. of CSE -9NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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(); }

_______________________________________________________________________________ Dept. of CSE - 10 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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> #include<ctype.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); } int F(char symbol) { switch(symbol) _______________________________________________________________________________ Dept. of CSE - 11 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

{ 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,j,k; char s[20]; for(i=0;i<strlen(infix);i++) { if (!isdigit(infix[i])) { if(isalpha(infix[i])) count1++; else if(infix[i]=='('|| infix[i]==')'); else count2++; } else { printf("invalid infix expression\n"); getch(); exit(0); } if(infix[i]=='(') _______________________________________________________________________________ Dept. of CSE - 12 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

s[++top]='('; if(infix[i]==')') top--; } for (j=0;j<strlen(infix);j++) { if (!isdigit(infix[j])) { if (isalpha(infix[j])&&(isalpha(infix[j+1]))) { printf("invalid infix expression\n"); getch(); exit(0); } } } 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(); }

_______________________________________________________________________________ Dept. of CSE - 13 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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=0,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]); }

_______________________________________________________________________________ Dept. of CSE - 14 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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(); }

_______________________________________________________________________________ Dept. of CSE - 15 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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]); }

_______________________________________________________________________________ Dept. of CSE - 16 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

void validate() { int count1=0,count2=0,i,j; for(i=0;i<strlen(postfix);i++) { if(!isalpha(postfix[i])) { if(isdigit(postfix[i])) count1++; else count2++; } else { printf("\n entered invalid postfix expression\n"); getch(); exit(0); } } 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(); }

_______________________________________________________________________________ Dept. of CSE - 17 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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 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 underflow\n"); } else { printf("deleted element=%d\n",q[f]); f=f+1; if(f>r) { f=0; r=-1; } } } void display() { int i; if(r==-1) { printf("Queue underflow\n"); } _______________________________________________________________________________ Dept. of CSE - 18 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

else { printf("Queue elements are\n"); for(i=f;i<=r;i++) printf("%d\t",q[i]); } printf("\n"); } void main() { int choice; clrscr(); printf("Enter the size of Queue\n"); scanf("%d",&size); q=(int*)malloc(sizeof(int)*size); while(1) { printf("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"); } } }

_______________________________________________________________________________ Dept. of CSE - 19 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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"; }

void main() _______________________________________________________________________________ Dept. of CSE - 20 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

{ 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(); }

_______________________________________________________________________________ Dept. of CSE - 21 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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; cout<<setw(10)<<setfill(' ')<<basic_salary<<setw(10)<<setfill(' ')<<allowance; cout<<setw(10)<<setfill(' ')<<gross_salary<<setw(10)<<setfill(' ')<<IT<<setw(10)<<setfill(' ')<<net_salary<<"\n"; }

_______________________________________________________________________________ Dept. of CSE - 22 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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(); }

_______________________________________________________________________________ Dept. of CSE - 23 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* ASSIGNMENT 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<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; } void main() { clrscr(); STRING s1="VTU"; _______________________________________________________________________________ Dept. of CSE - 24 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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(); }

_______________________________________________________________________________ Dept. of CSE - 25 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* ASSIGNMENT 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<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; } STRING STRING::operator+(STRING s) { STRING t; int len1,len2; len1=strlen(str); len2=strlen(s.str); t.str=new char[len1+len2+2]; _______________________________________________________________________________ Dept. of CSE - 26 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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(); }

_______________________________________________________________________________ Dept. of CSE - 27 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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 { _______________________________________________________________________________ Dept. of CSE - 28 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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<<"1->push\n2->pop\n3->display\n4->exit\n"; cin>>choice; switch(choice) { case 1: cout<<"enter 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; } } }

_______________________________________________________________________________ Dept. of CSE - 29 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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"; _______________________________________________________________________________ Dept. of CSE - 30 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

} else if(first->link==NULL) { cout<<"deleted node is: "<<first->data<<endl; delete first; first=NULL; } else { temp=first; cout<<"deleted node is: "<<first->data<<endl; 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<<"1->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(); _______________________________________________________________________________ Dept. of CSE - 31 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

break; case 3:l.display(); break; case 4:exit(0); default:cout<<"invalid choice\n"; } } }

_______________________________________________________________________________ Dept. of CSE - 32 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* ASSIGNMENT 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> #include<process.h> typedef struct { int r,c,v; }sm; sm *a;

void read() { int i,j,r1,c1,ele,k=1,m,flag=0; printf("enter the row size\n"); scanf("%d",&r1); printf("enter the column size\n"); scanf("%d",&c1); m=r1*c1; if (m%2==0) { a=(sm*)malloc(sizeof(sm)*(m/2)); flag=1; } else a=(sm*)malloc(sizeof(sm)*(m/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>=(m/2))&&flag==1) { _______________________________________________________________________________ Dept. of CSE - 33 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

printf("entered is not a sparse matrix\n"); getch(); exit(0); } if ((a[0].v>=(m/2+1))&&flag==0) { printf("entered is not a sparse matrix\n"); getch(); exit(0); } } 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(); } _______________________________________________________________________________ Dept. of CSE - 34 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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,j; if(count==n) { printf("Heap is full\n"); } else { for (j=1;j<=count;j++) { if (ele==heap[j]) { printf("element already present\n"); return; } } 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++) { _______________________________________________________________________________ Dept. of CSE - 35 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

printf("%d\t",heap[i]); } printf("\n"); } } 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("1->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\n"); scanf("%d",&ele); insert(ele); display(); break; case 2: display(); break; case 3: exit(0); default: printf("invalid choice\n"); } } }

_______________________________________________________________________________ Dept. of CSE - 36 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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; }; //typedef can also be used struct DLL *first=NULL; 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=NULL; if (first==NULL) first=temp; else { temp->rlink=first; first->llink=temp; first=temp; } } } void insertkey() { int key,ele; struct DLL *temp,*prev,*cur; if (first==NULL) _______________________________________________________________________________ Dept. of CSE - 37 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

{ printf("empty linked list\n"); return; } temp=(struct DLL*)malloc(sizeof(struct DLL)); printf("enter the key \n"); scanf("%d",&key); printf("enter the element \n"); scanf("%d",&ele); temp->data=ele; temp->llink=temp->rlink=NULL; 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("key not found\n"); return; } prev->rlink=temp; temp->llink=prev; temp->rlink=cur; cur->llink=temp; } } void deletekey() { int key; struct DLL *temp,*prev,*cur,*next; if(first==NULL) { printf("Empty Doubly Linked List \n"); return; } printf("enter the key to be deleted\n"); scanf("%d",&key); if(key==first->data) { _______________________________________________________________________________ Dept. of CSE - 38 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

temp=first; first=first->rlink; first->llink=NULL; printf("key found! and deleted i.e=%d\n",temp->data); free(temp); } else { prev=NULL; cur=first; while(key!=cur->data&&cur!=NULL) { prev=cur; cur=cur->rlink; } if(cur==NULL) { printf("key not found \n"); return; } next=cur->rlink; prev->rlink=next; next->llink=prev; printf("key found! and deleted i.e = %d\n",cur->data); free(cur); } } void display() { struct DLL *temp; if(first==NULL) { printf("Empty Doublu Linked List \n"); } else { printf("contents of DLL\n"); printf("NULL<=>"); for(temp=first;temp!=NULL;temp=temp->rlink) { printf("%d<=>",temp->data); } printf("NULL \n"); } } void main() { int ch,n; clrscr(); _______________________________________________________________________________ Dept. of CSE - 39 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

printf("enter the number of nodes\n"); scanf("%d",&n); create(n); while(1) { printf("1->insert left \n2->delete key \n3->display \n4->exit \n"); printf("Enter your choice\n"); scanf("%d",&ch); switch(ch) { case 1: insertkey(); break; case 2: deletekey(); break; case 3: display(); break; case 4:exit(0); default: printf("invalid choice\n"); } } }

_______________________________________________________________________________ Dept. of CSE - 40 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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<stdio.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)) { _______________________________________________________________________________ Dept. of CSE - 41 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

for(int i = 0; i<D1.month; i++) 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) { if ((D1.year%4==0)) { x=x+a1-a2; x++; } 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) { _______________________________________________________________________________ Dept. of CSE - 42 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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; } void DATE :: getdate() { int f=0; cout <<"enter the date in the format dd mm yyyy\n"; scanf("%d/%d/%d",&day,&month,&year); 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 { _______________________________________________________________________________ Dept. of CSE - 43 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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; 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; cout<<"\n-----------------------------------------------------------"; rt=0; } else _______________________________________________________________________________ Dept. of CSE - 44 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

{ cout<<"first date is to be less than or equal to second date"<<endl; } } getch(); }

_______________________________________________________________________________ Dept. of CSE - 45 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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 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 { 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; } int OCTAL::octtodec(int x) { _______________________________________________________________________________ Dept. of CSE - 46 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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\n"; cin>>x; OCTAL h=x; cout<<"X value in OCTAL is "; cout<<h<<endl; cout<<"enter integer value of K\n"; cin>>k; int y=h+k; cout<<"y value is="<<y<<endl; getch(); }

_______________________________________________________________________________ Dept. of CSE - 47 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

/* 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; if(direction[i]=='L' || direction[i]=='l') _______________________________________________________________________________ Dept. of CSE - 48 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

cur=cur->lchild; else if (direction[i]=='R' || direction[i]=='r') cur=cur->rchild; else { cout<<"invalid direction\n"; return; } } 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"; } } _______________________________________________________________________________ Dept. of CSE - 49 NARENDRA KUMAR S AND MANOHAR V NELLI

DS WITH C/C++ LAB

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<<"enter 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"; } } }

_______________________________________________________________________________ Dept. of CSE - 50 NARENDRA KUMAR S AND MANOHAR V NELLI

Вам также может понравиться