Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Data Structures With C/C++ Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 45

Data Structures with C/C++ Lab

/*PROGRAM 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.*/ //POLYNOMIAL ADDITION #include <stdio.h> typedef struct { int exp; float coef; }poly; int read_poly(poly p[]) { int i,n; printf("\nEnter the no of terms : "); scanf("%d",&n); for(i=0;i < n;i++) { printf("\nEnter coef : "); scanf("%f",&p[i].coef); printf("\nEnter exp : "); scanf("%d",&p[i].exp); } return(n); } void print_poly(poly p[], int n) { int i; for(i=0;i < n;i++) printf("+%fX^%d ",p[i].coef,p[i].exp); printf("\n"); } int add_poly( poly a[],poly b[],poly c[],int max1,int max2) { int i,j,k; i = j = k = 0; while ( i < max1 && j < max2) { if( a[i].exp > b[j].exp) { c[k] = a[i]; k++; i++; } else if( a[i].exp < b[j].exp) { c[k] = b[j];

BITM

Page 1

Data Structures with C/C++ Lab


k++; j++; } else { c[k].exp = a[i].exp; c[k].coef = a[i].coef + b[j].coef; i++; j++; k++; } } while( i < { c[k] = k++; i++; } while( j < { c[k] = k++; j++; } return(k); } void main() { poly a[20],b[20],c[20]; int max1,max2,max3; printf("\nEnter first poly : "); max1 = read_poly(a); printf("\nEnter second poly : "); max2 = read_poly(b); max3 = add_poly(a,b,c,max1,max2); printf("\nFirst poly is "); print_poly(a,max1); printf("\nSecond poly is "); print_poly(b,max2); printf("\nThe resultant poly after addition is "); print_poly(c,max3); } /******************************OUTPUT***************************** [bitm@dslab Desktop]$ vi poly.c [bitm@dslab Desktop]$ cc poly.c [bitm@dslab Desktop]$ ./a.out Enter first poly : Enter the no of terms : 2 max1 )//for(;i<max1;i++) a[i];

max2 )//for(;j<max2;j++) b[j];

BITM

Page 2

Data Structures with C/C++ Lab


Enter coef : 2 Enter exp : 1000 Enter coef : 1 Enter exp : 0 Enter second poly : Enter the no of terms : 4 Enter coef : 1 Enter exp : 4 Enter coef : 10 Enter exp : 3 Enter coef : 3 Enter exp : 2 Enter coef : 1 Enter exp : 0 First poly is +2.000000X^1000 +1.000000X^0 Second poly is +1.000000X^4 +10.000000X^3 +3.000000X^2 +1.000000X^0 The resultant poly after addition is +2.000000X^1000 +1.000000X^4 +10.000000X^3 +3.000000X^2 +2.000000X^0 ************************************************************************/

BITM

Page 3

Data Structures with C/C++ Lab


/*PROGRAM 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).*/ //INFIX TO POSTFIX //HEADER FILES #include<stdio.h>//GETS()-PUTS() //#include<ctype.h>//ISALNUM() #include<string.h>//STRLEN() //GLOBAL VARIABLES char stack[100]; int top=-1; //PUSH=INSERT void push(char x) { stack[++top]=x; } //POP=DELETE char pop() { return(stack[top--]); } //PRIORITY OF OPERATORS int prior (char x) { int p; if(x=='('||x=='#')p=1; if(x=='+'||x=='-')p=2; if(x=='*'||x=='/')p=3; if(x=='^'||x=='$')p=4; return p; } //MAIN() int main() { //LOCAL VARIABLES char pf[100],inf[100]; int i,j=0; printf("enter the infix expration\n"); //INPUT=INFIX gets(inf); push('#');//BOTTOM OF STACK IS #

BITM

Page 4

Data Structures with C/C++ Lab


for(i=0;i<strlen(inf);i++) { //OPERAND if(isalnum(inf[i]))//CHECKS FOR AN ALPHANUMERIC CHARACTER pf[j++]=inf[i]; //LEFT PARENTHESIS else if(inf[i]=='(') push(inf[i]); //RIGHT PARENTHESIS else if(inf[i]==')') { while(stack[top]!='(') pf[j++]=pop(); pop(); } //OPERATOR else { while(prior(stack[top])>=prior(inf[i])) pf[j++]=pop(); push(inf[i]); } } //END OF STRING while(stack[top]!='#') pf[j++]=pop(); pf[j]='\0'; //OUTPUT=POSTFIX puts(pf); } /**************************************OUTPUT************************* $vi p1.c $cc p1.c $./a.out enter the infix expration a+(b-c)*d abc-d*+ *********************************************************************/

BITM

Page 5

Data Structures with C/C++ Lab


/*PROGRAM 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 nonnegative single digit operands and binary arithmetic operators. The arithmetic operators are + (add), (subtract), * (multiply) and / (divide).*/ //EVALUATION OF POSTFIX EXP //HEADER FILES #include<stdio.h>//GETS()-PUTS() #include<ctype.h>//ISDIGIT() #include<stdlib.h>//EXIT() #include<math.h>//POW() #include<string.h>//STRLEN() #define MAX_SIZE 50 //GLOBAL VARIABLES int stack[MAX_SIZE],top=-1; //PUSH=INSERT void push(int x) { if(top == MAX_SIZE-1 ) printf("stack overflow"); else stack[++top]=x; } //POP=DELETE int pop() { if(top == -1) { printf("invalid expression\n"); exit(0); } else return(stack[top--]); } //MAIN() int main() { //LOCAL VARIABLES char pf[MAX_SIZE]; int x,y,res,i; printf("enter the postfix expration\n");

BITM

Page 6

Data Structures with C/C++ Lab


//INPUT=POSTFIX gets(pf); for(i=0;i<strlen(pf);i++) { //OPERAND if(isdigit(pf[i]))//checks for a digit (0 through 9) push(pf[i]-'0'); //OPERATOR else { y=pop();//OPERAND2 x=pop();//OPERAND1 //SWITCH FOR OPERATOR switch(pf[i]) { //ADD case'+':push(x+y); break; //SUB case'-':push(x-y); break; //MUL case'*':push(x*y); break; //DIV case'/':push(x/y); break; //EXP case'^': case'$':push(pow(x,y)); break; } } } //OUTPUT=EVALUATION OF POSTFIX EXPRESSION res=pop(); printf("the result is %d",res); } /**************************************OUTPUT************************* $vi p2.c $cc p2.c -lm $./a.out enter the postfix expration 632-5*+ the result is 11 *********************************************************************/

BITM

Page 7

Data Structures with C/C++ Lab


/*PROGRAM 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*/ //SIMPLE QUEUE //HEADER FILES #include<stdio.h>//SCANF()-PRINTF() #include<stdlib.h>//EXIT() //GLOBAL VARIABLES int queue_size; //FUNCTION DECLARATION void insert_rear(int elem,int *rear,int queue[]); void delete_front(int *front,int *rear,int queue[]); void display(int front,int rear,int queue[]); //MAIN() void main() { int queue[20],rear=-1,front=0; int elem,ch; printf("enter size of queue\n"); scanf("%d",&queue_size); for(;;) { printf("\n\t*********Main menu*********\n\t"); printf("\n1.insert\n2.delete\n3.display\n4.exit"); printf("\nenter the choice\n"); scanf("%d",&ch); switch(ch) { case 1:printf("\n enter the element to be inserted\n"); scanf("%d",&elem); insert_rear(elem,&rear,queue); break; case 2:delete_front(&front,&rear,queue); break; case 3:display(front,rear,queue); break; case 4:exit(0); default:printf("invalid choice:"); } } } //FUNCTION DEFINATION

BITM

Page 8

Data Structures with C/C++ Lab


void insert_rear(int elem,int *rear,int queue[]) { if(*rear==queue_size-1) printf("\n queue is full"); else queue[++(*rear)]=elem; } void delete_front(int *front,int *rear,int queue[]) { if(*front>*rear) printf("\n queue is empty"); else printf(" deleted element is %d",queue[(*front)++]); } void display(int front,int rear,int queue[]) { int i; if(front>rear) printf("\n queue is empty"); else { printf("contents of queue are"); for(i=front;i<=rear;i++) printf("\n%d",queue[i]); } } /**************************************OUTPUT************************* [root@dslab ayesha]# ./a.out enter size of queue 3 *********Main menu********* 1.insert 2.delete 3.display 4.exit enter the choice 1 enter the element to be inserted 10 *********Main menu********* 1.insert 2.delete 3.display 4.exit

BITM

Page 9

Data Structures with C/C++ Lab


enter the choice 1 enter the element to be inserted 20 *********Main menu********* 1.insert 2.delete 3.display 4.exit enter the choice 1 enter the element to be inserted 30 *********Main menu********* 1.insert 2.delete 3.display 4.exit enter the choice 1 enter the element to be inserted 40 queue is full *********Main menu********* 1.insert 2.delete 3.display 4.exit enter the choice 3 contents of queue are 10 20 30 *********Main menu********* 1.insert 2.delete 3.display 4.exit enter the choice 2 deleted element is 10

BITM

Page 10

Data Structures with C/C++ Lab


*********Main menu********* 1.insert 2.delete 3.display 4.exit enter the choice 2 deleted element is 20 *********Main menu********* 1.insert 2.delete 3.display 4.exit enter the choice 2 deleted element is 30 *********Main menu********* 1.insert 2.delete 3.display 4.exit enter the choice 2 queue is empty *********Main menu********* 1.insert 2.delete 3.display 4.exit enter the choice 3 queue is empty *********Main menu********* 1.insert 2.delete 3.display 4.exit enter the choice 4 *********************************************************************/

BITM

Page 11

Data Structures with C/C++ Lab


/*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)*/ //EMPLOYEE DETAILS //HEADER FILES #include<iostream> #include<stdio.h> using namespace std; //EMPLOYEE CLASS class employee { int eno,basic,al,it,net,gs; char ename[10]; public: //READ EMPLOYEE DETAILS void read() { cout<<"\nenter employee name "; cin>>ename; cout<<"\nenter employee no "; cin>>eno; cout<<"\nenter basic salary"; cin>>basic; } //CALCULATE NET SALARY void cal() { al=1.23*basic; gs=basic+al; it=0.3*gs; net=gs-it; } //DISPLAY EMPLOYEE DETAILS void output() {

BITM

Page 12

Data Structures with C/C++ Lab


cout<<"\nemployee name: "<<ename; cout<<"\nemployee no: "<<eno; cout<<"\nnet salary: "<<net; } }; //MAIN int main() { int i,n; employee e[10]; cout<<"\nenter the no of employees "; cin>>n; for(i=0;i<n;i++) { e[i].read(); e[i].cal(); e[i].output(); } } /****************************************OUTPUT**************************** [bitm@dslab dsc lab programs]$ vi emp.cpp [bitm@dslab dsc lab programs]$ c++ emp.cpp [bitm@dslab dsc lab programs]$ ./a.out enter the no of employees 2 enter employee name a enter employee no 01 enter basic salary10000 employee name: a employee no: 1 net salary: 15610 enter employee name b enter employee no 02 enter basic salary20000 employee name: b employee no: 2 net salary: 31220 ***************************************************************************/

BITM

Page 13

Data Structures with C/C++ Lab


/*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) */ //HEADER FILES #include<iostream> #include<string.h> using namespace std; //STRING CLASS class String { char str[20]; public: //DEFAULT CONSTRUCTOR String() { strcpy(str,""); } //PARAMETERIZED CONSTRUCTOR String(const char *ptr) { strcpy(str,ptr); } friend ostream & operator<<(ostream &print,String &s); //OPERATOR+ OVERLOADING String operator+(String ss) { String temp(str); strcat(temp.str,ss.str); return temp; } //COPY CONSTRUCTOR String(String &s) { strcpy(str,s.str); } }; //OPERATOR<< OVERLOADING ,FRIEND FUNCTION ,DISPLAY ostream & operator<<(ostream &print,String &s) { print<<s.str; return(print); }

BITM

Page 14

Data Structures with C/C++ Lab


//MAIN int main() { String s1("VTU "),s2("BELGAUM"),s3; cout<<"\nFirst String "<<s1; cout<<"\nSecond String "<<s2; s3=s1+s2; cout<<"\nResult after concatenation "<<s3; } /**********************************OUTPUT****************************** [bitm@dslab dsc lab programs]$ c++ string.cpp [bitm@dslab dsc lab programs]$ ./a.out First String VTU Second String BELGAUM Result after concatenation VTU BELGAUM ***********************************************************************/

BITM

Page 15

Data Structures with C/C++ Lab


/*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 the stack. ii) s1=s1- ; where s1 is an object of the class STACK and operator pops off the top element. Handle the STACK Empty and STACK Full conditions. Also display the contents of the stack after each operation, by overloading the operator <<.*/ //STACK USING OPERATOR OVERLOADING //HEADER FILES #include<iostream> #include<stdlib.h> #define max 5 //NAMESPACE STD using namespace std; //CLASS STACK class stack { int s[max],top; public: stack() { top=-1; } stack operator+(int); void operator-(); friend ostream & operator<<(ostream &,stack); }; //PUSH=OVERLODING OPERATOR+ stack stack::operator+(int e) { if(top==max-1) cout<<"stack full\n"; else { s[++top]=e; return *this; } } //POP=OVERLODING OPERATORvoid stack::operator-() { if(top==-1)

BITM

Page 16

Data Structures with C/C++ Lab


cout<<"stack empty\n"; else { int x=s[top]; top--; cout<<"deleted element is"<<x; } } //DISPLAY=OVERLODING OPERATOR<< ostream & operator<<(ostream &out,stack st) { if(st.top==-1) cout<<"stack empty\n"; else for(int i=st.top;i>=0;i--) cout<<st.s[i]<<" "; return out; } //MAIN int main() { stack st; int ch; while(1) { cout<<"\n1.insert\n2.delete\n3.display\n4.exit\nenter your choice:\n"; cin>>ch; int e; switch(ch) { case 1:cout<<"enter element\n"; cin>>e; st=st+e; break; case 2:-st; break; case 3:cout<<"contents of stack are\n"; cout<<st; break; case 4:exit(0); default:cout<<"wrong choice\n"; break; } } } /*********************************************OUTPUT********************* ******* [bitm@dslab dsc lab programs]$ ./a.out

BITM

Page 17

Data Structures with C/C++ Lab


1.insert 2.delete 3.display 4.exit enter your choice: 1 enter element 10 1.insert 2.delete 3.display 4.exit enter your choice: 1 enter element 20 1.insert 2.delete 3.display 4.exit enter your choice: 1 enter element 30 1.insert 2.delete 3.display 4.exit enter your choice: 3 contents of stack are 30 20 10 1.insert 2.delete 3.display 4.exit enter your choice: 2 deleted element is30 1.insert 2.delete 3.display 4.exit enter your choice: 2 deleted element is20 1.insert 2.delete 3.display

BITM

Page 18

Data Structures with C/C++ Lab


4.exit enter your choice: 2 deleted element is10 1.insert 2.delete 3.display 4.exit enter your choice: 2 stack empty 1.insert 2.delete 3.display 4.exit enter your choice: 3 contents of stack are stack empty 1.insert 2.delete 3.display 4.exit enter your choice: 4 [bitm@dslab dsc lab programs]$ ************************************************************************/

BITM

Page 19

Data Structures with C/C++ Lab


/*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.*/ //SINGLY LINKED LIST //HEADER FILES #include<iostream> #include<stdlib.h> using namespace std; //NESTED CLASSES class list { class node { public:int info; node *link; }*first,*temp; public: list() { first=NULL; } void insert_front(); void del(); void show(); }; //INSERT AT FRONT void list::insert_front() { int item; temp=new node; cout<<"enter element to be inserted\n"; cin>>item; temp->info=item; temp->link=first; first=temp; } //DELETE AT FRONT void list::del() { if(first==NULL) cout<<"list empty\n"; else { temp=first; cout<<"deleted element is"<<temp->info<<"\n";

BITM

Page 20

Data Structures with C/C++ Lab


first=first->link; delete(temp); } } //DISPLAY void list::show() { temp=first; cout<<"contents of list are\n"; while(temp!=NULL) { cout<<temp->info<<"->"; temp=temp->link; } cout<<"NULL\n"; } //MAIN int main() { list l1; int ch; while(1) { cout<<"\n1.insert\n2.delete\n3.display\n4.exit\nenter your choice:\n"; cin>>ch; switch(ch) { case 1:l1.insert_front(); break; case 2:l1.del(); break; case 3:l1.show(); break; case 4:exit(0); default:cout<<"wrong choice\n"; break; } } } /*****************************OUTPUT********************************** [bitm@dslab dsc lab programs]$ c++ list.cpp [bitm@dslab dsc lab programs]$ ./a.out 1.insert 2.delete 3.display 4.exit enter your choice: 1 enter element to be inserted 10

BITM

Page 21

Data Structures with C/C++ Lab


1.insert 2.delete 3.display 4.exit enter your choice: 1 enter element to be inserted 20 1.insert 2.delete 3.display 4.exit enter your choice: 3 contents of list are 20->10->NULL 1.insert 2.delete 3.display 4.exit enter your choice: 2 deleted element is20 1.insert 2.delete 3.display 4.exit enter your choice: 2 deleted element is10 1.insert 2.delete 3.display 4.exit enter your choice: 2 list empty 1.insert 2.delete 3.display 4.exit enter your choice: 3 contents of list are NULL 1.insert

BITM

Page 22

Data Structures with C/C++ Lab


2.delete 3.display 4.exit enter your choice: 4 ************************************************************************/

BITM

Page 23

Data Structures with C/C++ Lab


/*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.*/ //SPARSE MATRIX //HEADER FILES #include<stdio.h> //SPARSE STUCTURE struct sparse { int row,col,val; }; //MAIN void main() { struct sparse a[20]; int i,n,key,flag=0; printf("enter no of non-zero elements\n"); scanf("%d",&n); printf("enter sparse matrix <row,colonm,value>\n"); for(i=0;i<=n;i++) scanf("%d%d%d",&a[i].row,&a[i].col,&a[i].val); printf("enter the key\n"); scanf("%d",&key); for(i=1;i<=n;i++) { if(key==a[i].val) { printf("key found(%d,%d)",a[i].row,a[i].col); flag=1; } } if(!flag) printf("key not found"); } /*****************OUTPUT******************** SPARSE MATRIX 1 0 5 0 9 2 0 0 0 [root@dslab ayesha]# cc p5.c [bitm@dslab dsc lab programs]$ ./a.out enter no of non-zero elements 4 enter sparse matrix <row,colonm,value> 3 3 4 0 0 1

BITM

Page 24

Data Structures with C/C++ Lab


0 2 5 1 1 9 1 2 2 enter the key 9 key found(1,1) [root@dslab ayesha]# ./a.out enter no of non-zero elements 4 enter sparse matrix <row,colonm,value> 3 3 4 0 0 1 0 2 5 1 1 9 1 2 2 enter the key 4 key not found ***********************************************/

BITM

Page 25

Data Structures with C/C++ Lab


/*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.*/ //HEAP //HEADER FILES #include<stdio.h> #include<stdlib.h> int a[1000],n; //HEAPIFICATION void heapify(int a[],int n) { int i,j,k,item; for(i=n/2;i>=1;i--) { k=i; item=a[k]; while((2*k)<=n) { j=2*k; if(j<n&&a[j]<a[j+1])j=j+1; if(item>=a[j])break; else { a[k]=a[j]; k=j; } a[k]=item; } } } //MAIN void main() { int i,j,temp; printf("enter the no of terms\n"); scanf("%d",&n); printf("enter the array elements\n"); //scanf("%d",&a[1]); for(i=1;i<=n;i++) { scanf("%d",&a[i]); heapify(a,i); printf("\nAfter inserting "); for(j=1;j<=i;j++) printf("%d ",a[j]); } } /***************************************OUTPUT****************************** [bitm@dslab dsc lab programs]$ cc heap.c [bitm@dslab dsc lab programs]$ ./a.out

BITM

Page 26

Data Structures with C/C++ Lab


enter the no of terms 5 enter the array elements 30 After inserting 30 40 After inserting 40 30 50 After inserting 50 30 40 70 After inserting 70 50 40 30 80 After inserting 80 70 40 30 50 ***************************************************************************/

BITM

Page 27

Data Structures with C/C++ Lab


/*PROGRAM 11. Design, develop, and execute a program in C to implement a doubly linked list where each node consists of integers. The program should support the following operations; i) Create a doubly linked list by adding each node at the front. ii) Insert a new node to the left of the node whose key value is read as an input. iii) Delete the node of a given data if it is found, otherwise display appropriate message. iv) Display the contents of the list. (Note; Only either (a,b and d) or (a, c and d) may be asked in the examination) */ //DOUBLY LINKEDLIST //HEADER FILES #include<stdio.h> #include<stdlib.h> #include<malloc.h> //STRUCTURE FOR NODE struct node { int info; struct node *llink; struct node *rlink; }; typedef struct node *NODE; //CREATE A NODE NODE getnode() { NODE x; x=(NODE)malloc(sizeof(struct node)); if(x==NULL) { printf("memory is full"); exit(0); } return x; } //INSERT AT FRONT FUNCTION NODE insert_front(int item,NODE first) { NODE temp; temp=getnode(); temp->info=item; //INSERTING FIRST NODE if(first==NULL) return temp;

BITM

Page 28

Data Structures with C/C++ Lab


//INSERT OTHER NODES AT FRONT else { temp->rlink=first; first->llink=temp; //temp->llink=NULL; return temp; } } //INSERT AN ELEMENT BEFORE KEY ELEMENT NODE insert_key(int key,NODE first) { int elem; NODE temp,curr,prev; int pos=1; //LIST IS EMPTY if(first==NULL) { printf("list is empty"); return first; } prev=NULL; curr=first; //TRAVERSE TILL YOU GET THE KEY ELEMENT POSITION while(curr->rlink !=NULL && curr->info!=key) { prev=curr; curr=curr->rlink; pos++; } //KEY ELEMENT NOT FOUND if(curr -> rlink == NULL && curr->info != key) { printf("key not found"); return first; } //INSERT ELEMENT TO BE INSERTED printf("insert ele\n"); scanf("%d",&elem); temp=getnode(); temp->info=elem; //INSERT AT FIRST if(pos==1) {

BITM

Page 29

Data Structures with C/C++ Lab


temp->rlink = first; first ->llink = temp; //temp->llink = NULL; return temp; } //INSERT AT OTHER POSITIONS prev->rlink = temp; temp->llink = prev; curr->llink = temp; temp->rlink = curr; return first; } //DELETE A KEY ELEMENT NODE delete_key(int key,NODE first) { NODE curr=NULL,prev,next; //LIST IS EMPTY if(first==NULL) { printf("list is empty"); return first; } //DELETE FIRST ELEMENT if(first->info==key) { curr=first; first=first->rlink; free(curr); return first; } prev=NULL; curr=first; //TRAVERING TO GET KEY ELEMENT POSITION while(curr->rlink!=NULL && curr->info!=key) { prev=curr; curr=curr->rlink; } //DELETE OTHER MIDDLE ELEMENTS if(curr->rlink !=NULL && curr->info==key) { next=curr->rlink;

BITM

Page 30

Data Structures with C/C++ Lab


prev->rlink=next; next->llink=prev; free(curr); return first; } //DELETE LAST ELEMENT else if(curr->rlink == NULL && curr->info==key) { prev->rlink = NULL; free(curr); return first; } //KEY ELEMENT NOT FOUND else { printf("key not found"); return first; } } //DISPLAY FUNCTION void display(NODE first) { NODE temp; //LIST IS EMPTY if(first==NULL) printf("list is empty"); //DISPLAY CONTENTS else { printf("contents of list are"); temp=first; while(temp!=NULL) { printf("%d ",temp->info); temp=temp->rlink; } } } void main() { NODE first=NULL; int ch,elem,key; for(;;) { printf("\n\t*********Main menu*********\n\t");

BITM

Page 31

Data Structures with C/C++ Lab


printf("\n1.insert_front\n2.insert_key\n3.delete\n4.display\n5.exit"); printf("\nenter the choice\n"); scanf("%d",&ch); switch(ch) { case 1:printf("\n enter the element to be inserted\n"); scanf("%d",&elem); first=insert_front(elem,first); break; case 2: printf("\n enter the key element\n"); scanf("%d",&key); first=insert_key(key,first); break; case 3:printf("enter the key element to be deleted"); scanf("%d",&key); first=delete_key(key,first); break; case 4:display(first); break; case 5:exit(0); default:printf("invalid choice:"); break; } } } /******************************OUTPUT************************** [root@dslab ayesha]# cc p4.c [root@dslab ayesha]# ./a.out *********Main menu********* 1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 1 enter the element to be inserted 10 *********Main menu********* 1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 1

BITM

Page 32

Data Structures with C/C++ Lab


enter the element to be inserted 20 *********Main menu********* 1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 2 enter the key element 10 insert ele 15 *********Main menu********* 1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 4 contents of list are20 15 10 *********Main menu********* 1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 3 enter the key element to be deleted15 *********Main menu********* 1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 4 contents of list are20 10 *********Main menu*********

BITM

Page 33

Data Structures with C/C++ Lab


1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 3 enter the key element to be deleted20 *********Main menu********* 1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 4 contents of list are10 *********Main menu********* 1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 3 enter the key element to be deleted10 *********Main menu********* 1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 4 list is empty *********Main menu********* 1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 3 enter the key element to be deleted10 list is empty *********Main menu*********

BITM

Page 34

Data Structures with C/C++ Lab


1.insert_front 2.insert_key 3.delete 4.display 5.exit enter the choice 5 ************************************************************************/

BITM

Page 35

Data Structures with C/C++ Lab


/*PROGRAM 12. Design, develop, and execute a program in C++ to create a class called DATE with methods to accepttwo 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.*/ //HEADER FILES #include<iostream> #include<stdlib.h> using namespace std; //ARRAY FOR NO OF DAYS IN A MONTH int mon[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; //CLASS DATE class date { int day,month,year; public: date(); void input(); int isleapyear(int year); int no_of_days(int year,int month); long int operator-(date d2); date operator+(long int n); friend ostream & operator<<(ostream &,date &); }; //CONSTRUCTOR date::date() { day=0; month=0; year=0; } //INPUT DAY MONTH YEAR void date::input() { cout<<"\nenter day: "; cin>>day; cout<<"\nenter month: "; cin>>month; cout<<"\nenter year: "; cin>>year; } //CHECK IF LEAP YEAR int date::isleapyear(int year) { return(year%4==0);

BITM

Page 36

Data Structures with C/C++ Lab


} //CALCULATE NO OF DAYS int date::no_of_days(int year,int month) { if(isleapyear(year)&&(month==2)) return 29; else return mon[month]; } long int date::operator-(date d2) { long int diff=0; while(year!=d2.year||month!=d2.month||day!=d2.day) { diff++; if(d2.day==no_of_days(d2.year,d2.month)) { d2.day=1; if(d2.month==12) { d2.year++; d2.month=1; } else d2.month++; } else d2.day++; } return diff; } //DATE2=DATE1+NO OF DAYS date date::operator+(long int n) { while(n>0) { n--; if(day==no_of_days(year,month)) { day=1; if(month==12) { year++; month=1; } else month++; } else day++; } date d; d.year=year;

BITM

Page 37

Data Structures with C/C++ Lab


d.month=month; d.day=day; return d; } //DISPAY ostream & operator<<(ostream &print,date &d) { print<<"the new date is: "; print<<d.day<<"/"<<d.month<<"/"<<d.year; return print; } //MAIN int main() { date d1,d2; long int days,diff=0; int choice; for(;;) { cout<<"\npress 1 to determine diff=d1-d2 where d1>d2 "; cout<<"\npress 2 to determine d1=d1+ no. of days "; cout<<"\npress 3 to exit "; cout<<"\nenter ur choice "; cin>>choice; switch(choice) { case 1: d1.input(); d2.input(); diff=d1-d2; cout<<"\ndifference b/w 2 dates is: "<<diff<<" days\n"; break; case 2: d1.input(); cout<<"\nenter no. of days "; cin>>days; d1=d1+days; cout<<d1; break; default:exit(0); } } } /********************OUTPUT******************** [bitm@dslab dsc lab programs]$ vi date.cpp [bitm@dslab dsc lab programs]$ c++ date.cpp [bitm@dslab dsc lab programs]$ ./a.out press press press enter 1 to determine diff=d1-d2 where d1>d2 2 to determine d1=d1+ no. of days 3 to exit ur choice 1

enter day: 1

BITM

Page 38

Data Structures with C/C++ Lab


enter month: 1 enter year: 2013 enter day: 1 enter month: 1 enter year: 2012 difference b/w 2 dates is: 366 days press press press enter 1 to determine diff=d1-d2 where d1>d2 2 to determine d1=d1+ no. of days 3 to exit ur choice 2

enter day: 1 enter month: 1 enter year: 2012 enter no. of days 365 the new date is: 31/12/2012 press 1 to determine diff=d1-d2 where d1>d2 press 2 to determine d1=d1+ no. of days press 3 to exit enter ur choice 3 *************************************************/

/*PROGRAM 13. Design, develop, and execute a program in C++ to create a class called OCTAL, which has the

BITM

Page 39

Data Structures with C/C++ Lab


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. */ //OCTAL //HEADER FILES #include<iostream> #include<math.h> //NAMESPACE STD using namespace std; //CLASS OCTAL class octal { int p,i; public: octal() { p=0; } octal(int x); friend int operator+(octal h,int x); friend ostream & operator<<(ostream & out,octal h); }; //CONSTRUCTOR OVERLOADING=CONVERTS INT TO OCTAL octal::octal(int x) { int r,d=0,i=0; while(x!=0) { r=x%8; d=d+pow(10,i)*r; i++; x=x/8; } p=d; } //DISPLAY ostream & operator<<(ostream & out,octal h) { cout<<h.p; return out; }

BITM

Page 40

Data Structures with C/C++ Lab


//ADD INT TO OCTALAND RESULT=INT int operator +(octal h,int x) { int r,d=0,i=0,k=h.p; while(k!=0) { r=k%10; d=d+pow(8,i)*r ; i++; k=k/10; } d=d+x; return d; } main() { octal h,n; int x,k,y; cout<<"enter the first decimal no\n"; cin>>x; h=x; cout<<"the octal no is: "<<h<<"\n"; cout<<"enter the senond decimal "; cin>>k; y=h+k; cout<<"result after addition is: "<<y; } /**************************OUTPUT*************************** [bitm@dslab dsc lab programs]$ c++ octal.cpp [bitm@dslab dsc lab programs]$ ./a.out enter the first decimal no 16 the octal no is: 20 enter the senond decimal 16 result after addition is: 32 **********************************************************/

/*PROGRAM 14. Design, develop, and execute a program in C++ to create a class called BIN_TREE that represents a

BITM

Page 41

Data Structures with C/C++ Lab


Binary Tree, with member functions to perform inorder, preorder and postorder traversals. Create a BIN_TREE object and demonstrate the traversals.*/ //BINTREE //HEADER FILES #include<iostream> #include<stdlib.h> using namespace std; //CLASS BINTREE class bintree { public: int num; bintree *lchild,*rchild,*head; bintree() { head=NULL; } void create(); void inorder(bintree*); void preorder(bintree*); void postorder(bintree*); //DISPLAY void display() { cout<<"inorder traversal:\n"; inorder(head); cout<<"postorder traversal:\n"; postorder(head); cout<<"preorder traversal:\n"; preorder(head); } }; //PREORDER void bintree::preorder(bintree *head) { if(head!=NULL) { cout<<head->num<<endl; preorder(head->lchild); preorder(head->rchild); } } //POSTORDER void bintree::postorder(bintree *head) { if(head!=NULL) { postorder(head->lchild); postorder(head->rchild);

BITM

Page 42

Data Structures with C/C++ Lab


cout<<head->num<<endl; } } //INORDER void bintree::inorder(bintree *head) { if(head!=NULL) { inorder(head->lchild); cout<<head->num<<endl; inorder(head->rchild); } } //CREATE void bintree::create() { bintree *t,*p,*n; int x; n=new bintree; cout<<"enter the value "; cin>>x; n->num=x; n->lchild=n->rchild=NULL; t=p=head; if(head==NULL) head=n; while(t!=NULL) { p=t; if(x<t->num) t=t->lchild; else t=t->rchild; } if(p!=NULL) { if(x<p->num) p->lchild=n; else p->rchild=n; } } //MAIN int main() { bintree b; int op; do { cout<<"\n1.create anode"; cout<<"\n2.display tree traversals"; cout<<"\n3.exit";

BITM

Page 43

Data Structures with C/C++ Lab


cout<<"\nenter ur choice "; cin>>op; switch(op) { case 1:b.create(); break; case 2:b.display(); break; case 3:exit(0); default:cout<<"\ninvalid choice\n"; break; } } while(op!=3); } /*****************************OUTPUT*********************************** [bitm@dslab dsc lab programs]$ ./a.out 1.create anode 2.display tree traversals 3.exit enter ur choice 1 enter the value 50 1.create anode 2.display tree traversals 3.exit enter ur choice 1 enter the value 30 1.create anode 2.display tree traversals 3.exit enter ur choice 1 enter the value 20 1.create anode 2.display tree traversals 3.exit enter ur choice 1 enter the value 70 1.create anode 2.display tree traversals 3.exit enter ur choice 1 enter the value 40 1.create anode 2.display tree traversals 3.exit enter ur choice 1 enter the value 80 1.create anode

BITM

Page 44

Data Structures with C/C++ Lab


2.display tree traversals 3.exit enter ur choice 2 inorder traversal: 20 30 40 50 70 80 postorder traversal: 20 40 30 80 70 50 preorder traversal: 50 30 20 40 70 80 1.create anode 2.display tree traversals 3.exit enter ur choice 3 [bitm@dslab dsc lab programs]$ ***************************************************************************/

BITM

Page 45

You might also like