You are on page 1of 41

DATA STRUCTURES WITH C/C++ LAB MANUAL

DATA STRUCTURES WITH C/C++


LABORATORY

DATA STRUCTURES WITH C/C++ LABORATORY


Dept.of CSE

Page 1

DATA STRUCTURES WITH C/C++ LAB MANUAL


(Common to CSE & ISE)
Subject Code: 10CSL37
Hours/Week : 03
Total Hours : 42

1.

I.A. Marks : 25
Exam Hours: 03
Exam Marks: 50

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

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

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

5.

Design,develop and execute a program in C++ based on the following requirements. An


EMPLOYEE class contains following members: Data members : Employee_Number(an
integer), Employee Name(string of characters), Basic_salary(an integer),
All_allowances(an integer),IT(an integer) and Net_Salary (an integer).
Member
functions : to read the data, to calculate Net_Salary and to print the values of all data
members.
(All_Allowance (AA) = 123% of Basic , Income Tax (IT) = 30% of the
gross salary and Net_Salary = Basic + AA - IT).

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)

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 elem
Handle the STACK Empty and STACK Full conditions. Also display the contents of the
stack after each operation, by overloading the operator <<.

Dept.of CSE

Page 2

DATA STRUCTURES WITH C/C++ LAB MANUAL


8.

9.

10.

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

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)

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.

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.

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.

Note: In the examination each student picks one question from a lot of all the 14 questions.

Dept.of CSE

Page 3

DATA STRUCTURES WITH C/C++ LAB MANUAL

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 <malloc.h>
#include<conio.h>
struct node
{
float coef;
int expo;
struct node *link;
};
struct node *poly_add(struct node *,struct node *);
struct node *enter(struct node *,int);
struct node *insert(struct node *,float,int);
void display(struct node *);
int p,q; //variables to strore the number of terms
void main( )
{
struct node *p1_start,*p2_start,*p3_start; //pointer to each list
p1_start=NULL;
p2_start=NULL;
p3_start=NULL;
clrscr();
printf("Enter the No of terms for Polynomial 1: ");
scanf("%d",&p);
printf("Polynomial 1 :\n");
p1_start=enter(p1_start,p);
printf("\nEnter the No of terms for Polynomial 2: ");
scanf("%d",&q);
printf("Polynomial 2 : \n");
p2_start=enter(p2_start,q);
p3_start=poly_add(p1_start,p2_start);
printf("\n Polynomial 1 is : ");
display(p1_start);
printf("\n Polynomial 2 is : ");
display(p2_start);
printf("\n Added polynomial is : ");
display(p3_start);
Dept.of CSE

Page 4

DATA STRUCTURES WITH C/C++ LAB MANUAL


}
struct node *enter(struct node *start,int n)
{
int i,exponent;
float coeficient;
for(i=1;i<=n;i++)
{
printf("Enter coeficient and exponent for term %d : ",i);
scanf("%f%d",&coeficient,&exponent);
start=insert(start,coeficient,exponent);
}
return start;
}
struct node *insert(struct node *start,float co,int ex)
{
struct node *temp;
temp= malloc(sizeof(struct node));
temp->coef=co;
temp->expo=ex;
temp->link=NULL;
if(start==NULL)
{
start=temp;
temp->link=temp;
}
else{
temp->link=start->link;
start->link=temp;
start=temp;
}
return start;
}
struct node *poly_add(struct node *p1,struct node *p2)
{
int i=1,j=1;
struct node *p3_start,*temp,*startp1,*startp2;
p3_start=NULL;
p1=p1->link;
p2=p2->link;
startp1=p1;
startp2=p2;
Dept.of CSE

Page 5

DATA STRUCTURES WITH C/C++ LAB MANUAL


do
{
if(p1->expo > p2->expo)
{
p3_start=insert(p3_start,p1->coef,p1->expo);
p1=p1->link;i++;
}
else if(p2->expo > p1->expo)
{
p3_start=insert(p3_start,p2->coef,p2->expo);
p2=p2->link;j++;
}
else if(p1->expo == p2->expo)
{
p3_start=insert(p3_start,(p1->coef+p2->coef),p1->expo);
p1=p1->link;
p2=p2->link;i++,j++;
}
}while(i<=p && j<=q);
while(p1!=startp1)
{
p3_start=insert(p3_start,p1->coef,p1->expo);
p1=p1->link;
}
while(p2!=startp2)
{
p3_start=insert(p3_start,p2->coef,p2->expo);
p2=p2->link;
}
return p3_start;
}
void display(struct node *ptr)
{
struct node *startp3=ptr->link;
if(ptr==NULL)
{
printf("Empty\n");
return;
}
ptr=ptr->link;
Dept.of CSE

Page 6

DATA STRUCTURES WITH C/C++ LAB MANUAL


do
{
printf("%.1f+x^%d ",ptr->coef,ptr->expo);
ptr=ptr->link;
} while(ptr!=startp3);
}

OUTPUT:
Enter the No of terms for Polynomial 1:2
Enter coefficient and exponent for term 1: 2 3
Enter coeficient and exponent for term 2:4 2
Enter the No of terms for Polynomial 2:3
Enter coefficient and exponent for term 1: 5 2
Enter coeficient and exponent for term 2:3 3
Enter coefficient and exponent for term 3: 4 1
Polynomial 1 is:2 x^3+4 x^2
Polynomial 2 is:3x^3+5 x^2+4 x^1
Added polynomial is:5 x^3+9 x^2+4 x^1

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
Dept.of CSE

Page 7

DATA STRUCTURES WITH C/C++ LAB MANUAL

expressions.The expression consists of single character operands and the binary


operators+(plus),-(minus),*(multiply)and /(divide).
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>
char infix[50],postfix[50];
char s[10];
int top=-1;
void main()
{
clrscr();
void push(char);
char pop();
void convert();
int pri(char ch);
printf("enter the infix expression\n");
scanf("%s",infix);
convert();
getch();
}
void push(char ch)
{
s[++top]=ch;
}
char pop()
{
return(s[top--]);
}
int pri(char ch)
{
if(ch=='(' || ch=='#')
return 1;
if(ch=='+' || ch=='-')
return 2;

Dept.of CSE

Page 8

DATA STRUCTURES WITH C/C++ LAB MANUAL


if(ch=='*' || ch=='/')
return 3;
if(ch=='$')
return 4;
return 0;
}
void convert()
{
int i,j=0;
push('#');
for(i=0;infix[i]!='\0';i++)
{
if(isalnum(infix[i]))
postfix[j++]=infix[i];
else if(infix[i]=='(')
push(infix[i]);
else if(infix[i]==')')
{
while(s[top]!='(')
postfix[j++]=pop();
pop();
}
else
{
while(pri(s[top])>=pri(infix[i]))
postfix[j++]=pop();
push(infix[i]);
}
}
while(s[top]!='#')
postfix[j++]=pop();
postfix[j]='\0';
printf("postfix expression====> %s",postfix);
}

OUTPUT:
Enter the infix expression: (((a+b)/c)*(a-b))
Dept.of CSE

Page 9

DATA STRUCTURES WITH C/C++ LAB MANUAL


Postfix expression isab+c/ab-*
Enter the infix expression: (a-b)/(c*d$e)
Postfix expression isab-cde$*/

3. Design, develop and execute a program in C Program to evaluate a valid


suffix/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<math.h>
#define TRUE 1
#define FALSE 0
#include<conio.h>
int top=-1;
double stack[25];
char postfix[25];
void main()
{
double eval();
double result;
clrscr();
printf("enter a valid postfix expression");
scanf("%s",postfix);
result=eval();
printf("\n the result is %f",result);
getch();
}
double eval()
{
void push(double);
double pop();
int isdigit(char);
double oper(char,double,double);
double opnd1,opnd2,value;
int i=0;
while(postfix[i]!='\0')
Dept.of CSE

Page 10

DATA STRUCTURES WITH C/C++ LAB MANUAL


{
if(isdigit(postfix[i]))
push(postfix[i]-'0');
else
{
opnd2=pop();
opnd1=pop();
value=oper(postfix[i],opnd1,opnd2);
push(value);
}
i++;
}
return(pop());
}
double oper(char opr,double opnd1,double opnd2)
{
double res=1;
switch(opr)
{
case '+':return(opnd1+opnd2);
case '-':return(opnd1-opnd2);
case '*':return(opnd1*opnd2);
case '/':return(opnd1/opnd2);
case '$':return(pow(opnd1,opnd2));
default:printf("illegal operation");
exit(1);
}
}
void push(double item)
{
stack[++top]=item;
}
double pop()
{
double item;
item=stack[top--];
return(item);
}
Dept.of CSE

Page 11

DATA STRUCTURES WITH C/C++ LAB MANUAL

int isdigit(char symb)


{
if(symb>='0' && symb<='9')
return(TRUE);
else
return(FALSE);

OUTPUT:
Enter a valid postfix expression:234*+
The result is: 14.000000
Enter a valid postfix expression:68321$*/The result is: 4.666667

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>
# define QUEUE_SIZE 5
int choice,item,rear,front,q[10];
void insert();
void del();
void display();
void main()
{
front=0,rear=-1;
clrscr();
for(;;)
{
printf("\n1: insert 2: delete 3: display 4: exit");
printf("\nenter your choice\n");
scanf("%d",&choice);
switch(choice)
Dept.of CSE

Page 12

DATA STRUCTURES WITH C/C++ LAB MANUAL


{
case 1: printf("enter the item to be inserted\n");
scanf("%d",&item);
insert();
break;
case 2: del();
break;
case 3: display();
break;
default: return;
}
}
}
void insert()
{
if(rear==QUEUE_SIZE-1)
{
printf("queue over flow\n");
return;
}
q[++rear]=item;
}
void del()
{
if(front>rear)
{
printf("queue underflow\n");
return;
}
printf("%d is deleted\n",q[front++]);
if(front>rear)
{
front=0;rear=-1;
}
}
void display()
{
int i;
Dept.of CSE

Page 13

DATA STRUCTURES WITH C/C++ LAB MANUAL


if(front>rear)
{
printf("queue is empty\n");
return;
}
printf("contents of queue\n");
for(i=front;i<=rear;i++)
printf("%d\n",q[i]);
}

OUTPUT:
1. Insert 2.delete
3.display
Enter your choice:1
Enter the item to be inserted:10
1. Insert 2.delete
3.display
Enter your choice:1
Enter the item to be inserted:20

4.exit

1. Insert 2.delete
3.display
Enter your choice:1
Enter the item to be inserted:30

4.exit

1. Insert 2.delete
3.display
Enter your choice:3
contents of the queue is:
10
20
30
1. Insert 2.delete
3.display
Enter your choice:2
10 is deleted

4.exit

4.exit

4.exit

5. Design,develop and execute a program in C++ based on the following requirements.


An EMPLOYEE class contains following members:
Data members : Employee_Number(an integer), Employee_Name(string of
characters), Basic_salary(an integer), All_allowances(an integer),IT(an integer) and
Net_Salary (an integer).
Dept.of CSE

Page 14

DATA STRUCTURES WITH C/C++ LAB MANUAL

Member functions : to read the data, to calculate Net_Salary and to print the values
of all data members.
(All_Allowance (AA) = 123% of Basic , Income Tax (IT) = 30% of the gross salary
and Net_Salary = Basic + AA - IT).
#include<iostream.h>
#include<conio.h>
class Emp
{
char emp_name[10];
int emp_num;
float AA,IT,net_sal,basic;
public:
void read_data();
void emp_sal();
void print_data();
};
void Emp::read_data()
{
cout<<"\nEnter the employee num:";
cin>>emp_num;
cout<<"\nEnter the employee's name:";
cin>>emp_name;
cout<<"\nEnter the employee salary:";
cin>>basic;
}
void Emp::emp_sal()
{
float gross_sal;
AA=(123*basic)/100;
gross_sal=basic+AA;
IT=(30*gross_sal)/100;
net_sal=gross_sal-IT;
}

void Emp::print_data()
{
cout<<"*******************************";
cout<<"\nEname = "<<emp_name;
cout<<"\nEmp_num= "<<emp_num;
Dept.of CSE

Page 15

DATA STRUCTURES WITH C/C++ LAB MANUAL


cout<<"\nIT = "<<IT;
cout<<"\nAA = "<<AA;
cout<<"\nnet_sal= "<<net_sal;
cout<<"\n*******************************\n";
}
Void main()
{
Emp emp[10];
int n,i;
cout<<"Enter number of emp:";
cin>>n;
cout<<"\nEnter the employee data";
for(i=0;i<n;i++)
{
cout<<"\nEnter the Details of a employee: "<<i+1;
emp[i].read_data();
emp[i].emp_sal();
emp[i].print_data();
}
getch();
}

OUTPUT:
Enter number of emp:2
Enter the employee data
Enter the Details of a employee: 1
Enter the employee num:11
Enter the employee's name:kiran
Enter the employee salary:6000
*******************************
Ename = kiran
Emp_num= 11
IT = 2736
DA = 3120
net_sal= 6384
*******************************
Enter the Details of a employee: 2
Enter the employee num:12
Enter the employee's name:babu
Enter the employee salary:9000

Dept.of CSE

Page 16

DATA STRUCTURES WITH C/C++ LAB MANUAL


*******************************
Ename = babu
Emp_num= 12
IT = 4104
DA = 4680
net_sal= 9576
*******************************

6. Design,develop and execute a program in C++ to create a class called string and
implement the follwing operations. Display the results after every operation by
overloading the operator <<.
i. STRING s1=VTU
ii.STRING s2=BELGAUM
iii.STRING s3=s1+s2;(Use copy constructor).
#include<iostream.h>
#include<conio.h>
#include<string.h>
class STRING
{
char str[50];
public: STRING()
{
strcpy(str,"\0");
}
STRING(const char s[50])
{
strcpy(str,s);
}
STRING(const STRING & s);
STRING operator +(STRING s);
friend ostream & operator <<(ostream &,STRING);
};
STRING :: STRING(const STRING & s)
{
strcpy(str,s.str);
}
STRING STRING :: operator +(STRING s)
{
STRING temp;
Dept.of CSE

Page 17

DATA STRUCTURES WITH C/C++ LAB MANUAL


strcpy(temp.str,str);
strcat(temp.str,s.str);
return(temp);
}
ostream & operator<<(ostream & os,STRING s)
{
os<<s.str;
return(os);
}
Void main ()
{
clrscr ()
STRING s1 = "VTU";
cout<<"the first string is:"<<s1;
STRING s2= "BELUGM";
cout<<"the second string is:"<<s2;
STRING s4(s1);
STRING s5(s2);
STRING s3 = s4 + s5;
cout<<"the concatenated dtringis:"<<s3;
getch();
}

OUTPUT:
The first string is:VTU
The second string is:BELGAUM
The concatenated string is:VTUBELGAUM

7. Design, develop and execute a program in C++ to create a class called STACK
using array of integers and to implement the following operations by overloading
the operators + and -:
Dept.of CSE

Page 18

DATA STRUCTURES WITH C/C++ LAB MANUAL

i. S1=S1+element; where S1 is an object of the class STACK and element is an


integer to be pushed onto 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 <<.
#include<iostream>
#include<conio.h>
#include<stdlib.h>
const int SIZE=3;
class stack
{
int items[SIZE],top;
public: stack()
{
top=-1;
}
stack operator+(int info)
{
if(top==SIZE-1)
{
cout<<"stack overflow...\n";
return *this;
}
else
items[++top]=info;
return *this;
}
stack operator --(int)
{
if(top==-1)
{
cout<<"stack underflow\n";
return *this;
}
else
cout<<"the ele popped out is"<<items[top--];
return *this;
Dept.of CSE

Page 19

DATA STRUCTURES WITH C/C++ LAB MANUAL


}
friend ostream & operator<<(ostream &,stack);
};
ostream & operator <<(ostream & os,stack s)
{
if(s.top==-1)
{
os<<"the stack is empty \n";
return os;
}
else
{
os<<"the contents of the stack are\n";
for(int i=s.top;i>-1;i--)
os<<s.items[i]<<"\n";
}
}
Void main()
{
int option,element;
stack s1;
clrscr();
while(1)
{
cout<<"\n1 to push \n 2 to pop \n 3 to exit\n";
cout<<"enter your option\n";
cin>>option;
switch(option)
{
case 1:cout<<"enter the element to push";
cin>>element;
s1=s1+element;
cout<<s1;
break;
case 2:s1=s1--;
cout<<s1;
Dept.of CSE

Page 20

DATA STRUCTURES WITH C/C++ LAB MANUAL


break;
case 3:exit(0);
}
}
}

OUTPUT:
1. push 2.pop 3.exit
Enter your option:1
Enter the element to push:10
The contents of the stack are:10
1.push 2 .pop 3.exit
Enter your option:1
Enter the element to push:20
The contents of the stack are:
20
10
1.push 2 .pop 3.exit
Enter your option:2
The element popped out is 20
1.push 2 .pop 3.exit
Enter your option:2
The element popped out is 10
The stack is empty

8. Design, develop and execute a program in C++ to create a class called


LIST(linked list) with member functions to insert an elements at the front of the list
Dept.of CSE

Page 21

DATA STRUCTURES WITH C/C++ LAB MANUAL

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>
class list
{
private: struct llist
{
int info;
struct llist *next;
};
typedef struct llist node;
node *start,*temp;
public: list()
{
start=NULL;
}
void insert()
{
temp=new node;
cout<<"enter the node to insert\n";
cin>>temp->info;
temp->next=start;
start=temp;
}
void remove()
{
if(start)
{
temp=start;
start=start->next;
cout<<"the node deleted is"<<temp->info;
}
else
cout<<"the list is empty";
}
void display();
Dept.of CSE

Page 22

DATA STRUCTURES WITH C/C++ LAB MANUAL


};
void list::display()
{
if(start)
{
cout<<"the contenets of the list are\n";
for(temp=start;temp;temp=temp->next)
cout<<temp->info<<"\n";
}
else
cout<<"list is empty";
}
Void main()
{
list ll;
int option;
clrscr();
while(1)
{
cout<< "1 to insert 2 to delete 3 to display 4 to exit\n";
cout<<" enter your option please:";
cin>>option;
switch(option)
{
case 1:ll.insert();
break;
case 2:ll.remove();
break;
case 3: ll.display();
break;
case 4:exit(0);
}
}
getch();
}

OUTPUT:
Dept.of CSE

Page 23

DATA STRUCTURES WITH C/C++ LAB MANUAL


1.insert
2.delete
3.display
Enter your option:1
Enter the node to insert:12

4.exit

1.insert
2.delete
3.display
Enter your option:1
Enter the node to insert:13

4.exit

1.insert
2.delete
3.display
Enter your option:1
Enter the node to insert:14

4.exit

1.insert
2.delete
Enter your option:3
Contents of the list are:
14 13 12

3.display

4.exit

1.insert
2.delete
Enter your option:2
The node deleted is 14

3.display

4.exit

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, col, value> to represent an
element in the sparse matrix.
#include<stdio.h>
#include<conio.h>
struct sparse
{
int row,col,data;
}s[10];
void main()
{
int z,p,m,n,i,j,k=0,element;
clrscr();
printf("\n enter the row and col");
scanf("%d%d",&m,&n);
printf("\n enter the matrix elements");
for(i=0;i<m;i++)
Dept.of CSE

Page 24

DATA STRUCTURES WITH C/C++ LAB MANUAL


{
for(j=0;j<n;j++)
{
scanf("%d",&p);
if(p!=0)
{
s[k].row=i;
s[k].col=j;
s[k].data=p;
k++;
}
}
}
Printf(elements of the matrix\n);
Printf(ROW
COL
VALUE\n);
for(z=0;z<k;z++)
printf("%d\t %d\t %d\t \n ",s[z].row, s[z].col, s[z].data);
printf("\n enter an element to search: ");
scanf("%d",&element);
for(z=0;z<k;z++)
{
if(s[z].data==element)
{
printf("element found in matrix %d %d",s[z].row,s[z].col);
exit(0);
}
}
printf("element not found");
getch();

OUTPUT:
Enter the row and col no: 4 4
Dept.of CSE

Page 25

DATA STRUCTURES WITH C/C++ LAB MANUAL


Enter the elements of the matrix
0002
0012
3000
5000
Elements of the sparse matrix are:
ROWNO
COLNO
VALUE
0
3
2
1
2
1
2
0
3
3
0
5
Enter an element to search:5
Element found in row=3 col=0

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 into a heap. Use the
array representation for the heap. Display the array at the end of insertion phase.
#include<stdio.h>
#include<conio.h>
int m=0,heap[20];
void insert(int);
void main()
{
int i,a,n;
clrscr();
printf("Enter the number of items insert into heap array:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\enter the item %d to insert into heap: ",i);
scanf("%d",&a);
insert(a);
}
printf("\nThe heap array is=");
for(i=1;i<=n;i++)
printf("%d ",heap[i]);
getch();
}
void insert(int item)
Dept.of CSE

Page 26

DATA STRUCTURES WITH C/C++ LAB MANUAL


{
int i;
i=++m;
while((i!=1) && (item>heap[i/2]))
{
heap[i]=heap[i/2];
i=i/2;
}
heap[i]=item;
}

OUTPUT:
Enter the number of items insert into heap array: 5
Enter the item 1 to insert into heap: 10
Enter the item 2 to insert into heap: 90
Enter the item 3 to insert into heap: 40
Enter the item 4 to insert into heap: 50
Enter the item 5 to insert into heap: 100
The heap array is: 100 90 40 10 50

Dept.of CSE

Page 27

DATA STRUCTURES WITH C/C++ LAB MANUAL

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.
#include<stdio.h>
#include<conio.h>
struct node1
{
struct node1 *left;
int info;
struct node1 *right;
};
typedef struct node1 node;
node *first,*temp;
void main()
{
int choice,item,key;
clrscr();
void create(int);
void insert(int,int);
void del(int);
void display();
first=NULL;
while(1)
{
printf("\n 1-> create\n 2-> insert left\n 3->delete \n 4->display \n 5 exit");
printf("\n enter your choice");
scanf("%d",&choice);
switch(choice)
{
Dept.of CSE

Page 28

DATA STRUCTURES WITH C/C++ LAB MANUAL


case 1: printf("\n enter the elements of the list to insert(-1 to terminate");
scanf("%d",&item);
while(item!=-1)
{
create(item);
scanf("%d",&item);
}
break;
case 2:printf("enter the elment to insert left\n");
scanf("%d",&item);
printf("enter the key\n");
scanf("%d",&key);
insert(item,key);
break;
case 3:printf("\n enter the key value of the element to be deleted");
scanf("%d",&key);
del(key);
break;
case 4: display();
break;
case 5: exit(0);
break;
}
}
}
void create(int item)
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->info=item;
temp->left=temp->right=NULL;
if(first==NULL)
first=temp;
else
{
temp->right=first;
first->left=temp;
first=temp;
}
}
void insert(int item, int key)
Dept.of CSE

Page 29

DATA STRUCTURES WITH C/C++ LAB MANUAL


{
node *scan,*temp;
temp=(node *)malloc(sizeof(node));
temp->info=item;
temp->left=temp->right=NULL;
scan=first;
if(scan==NULL)
printf("\n list is empty");
else
{
if(scan->info==key)
{
scan->left=temp;
temp->right=scan;
first=temp;
}
else {
scan=scan->right;
while(scan->info!=key && scan->right!=NULL)
scan=scan->right;
if(scan->info==key)
{
temp->left=scan->left;
(scan->left)->right=temp;
temp->right=scan;
scan->left=temp;
}
else
printf("\n key element not in the list");
}
} }
void del(int key)
{
node *scan,*prev;
scan=first;
if(scan==NULL)
printf("\n list is empty");
else
if(scan->info==key)
{
Dept.of CSE

Page 30

DATA STRUCTURES WITH C/C++ LAB MANUAL


if(scan->right==NULL)
first=NULL;
else
{ (scan->right)->left=NULL;
first=scan->right;
}
printf("\n the elemnet deleted is: %d", scan->info);
free(scan);
}
else {
prev=scan;
scan=scan->right;
while(scan->info!=key && scan->right!=NULL)
{
prev=scan;
scan=scan->right;
}
if(scan->info==key)
{
prev->right=scan->right;
if(scan->right!=NULL)
(scan->right)->left=prev;
printf("\n the element deleted is %d",scan->info);
free(scan);
}
else
printf("key not found\n");
}
}
void display()
{
node *temp;
temp=first;
if(temp==NULL)
printf("list is empty\n");
else
{
printf(" the elements are:");
while(temp)
{
printf("%d\t",temp->info);
Dept.of CSE

Page 31

DATA STRUCTURES WITH C/C++ LAB MANUAL


temp=temp->right;
}
} }

OUTPUT:
1->create 2->insert left
3->delete 4->display 5->exit
Enter your choice:1
Enter the elements of the list to insert and -1 to terminate
10 20 30 -1
1->create 2->insert left
3->delete 4->display 5->exit
Enter your choice:4
The elements are:30 20 10
1->create 2->insert left
3->delete 4->display 5->exit
Enter your choice:2
Enter the key value of the elements to be deleted:20
Item deleted is 20

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 overload 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>
#include <stdlib.h>
class octal
{
int octnum;
public: octal(int m=0)
{
int k,fact=1;
octnum=0;
while(m)
{
k=m%8;
octnum=octnum+k*fact;
fact*=10;
m=m/8;
Dept.of CSE

Page 32

DATA STRUCTURES WITH C/C++ LAB MANUAL


}
}
void input(int o)
{
octnum=o;
}
int to_int(int p)
{
int k=0,t,x,fact=1;
x=p;
while(x)
{
t=x%10;
k=k+t*fact;
fact=fact*8;
x=x/10;
}
return k;
}
int operator+(int i)
{
int k;
k=to_int(octnum)+i;
return k;
}
friend ostream & operator<<(ostream & os,octal oct);
};
ostream & operator<<(ostream & os,octal oct)
{
os<<"octal number is"<<oct.octnum;
return(os);
}
Void main()
{
int ch;
octal h;
int x,y,k;
clrscr();
while(1)
Dept.of CSE

Page 33

DATA STRUCTURES WITH C/C++ LAB MANUAL


{
cout<<"\n1 convert to octal\n 2 add int to octal\n 3 exit\n";
cout<<"enter the choice: ";
cin>>ch;
switch(ch)
{
case 1:cout<<"enter the integer value to be convert:";
cin>>x;
h=x;
cout<<h;
break;
case 2:cout<<"enter the octal number first: ";
int o;
cin>>o;
h.input(o);
cout<<"integer value is: "<<h.to_int(o)<<endl;
cout<<"now enter the intger to be added: ";
cin>>k;
y=h+k;
cout<<"the sum is: "<<y;
break;
case 3: exit(0);
break;
}
}
getch();
}

OUTPUT:
1.convert to octal
2.add to octal
Enter your choice:1
Enter the integer value to convert:10
Octal number is:12
1.convert to octal
2.add to octal
Enter your choice:2
Enter the octal number first:12
Integer value is:10
Enter the integer to be added:5
Dept.of CSE

3.exit

3.exit

Page 34

DATA STRUCTURES WITH C/C++ LAB MANUAL


The sum is:15

12. Design, develop and execute a program in C++ to create a class called DATE
with methods to accepts 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 d1and d2 are DATE objects, d1>=d2 and no_of_days is
an integer.
ii.d2=d1+no_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:
date(int d,int m,int y)
{
day=d;
month=m;
year=y;
}
int operator-(date d2);
date operator+(int n);
friend ostream & operator<<(ostream & o,date d);};
int a[13]={0,31,28,31,30,31,30, 31,31,30,31,30,31};
int b[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int date::operator-(date d2)
{
int n=0;
date d1(day,month,year);
while(d1.day!=d2.day||d1.month!=d2.month||d1.year!=d2.year)
{
d2.day++;
n++;
if(d2.year%4==0)
{
if(d2.day>b[d2.month])
{
d2.day=1;
d2.month++;
}
}
else
Dept.of CSE

Page 35

DATA STRUCTURES WITH C/C++ LAB MANUAL


{
if(d2.day>a[d2.month])
{
d2.day=1;
d2.month++;
} }
if(d2.month>12)
{
d2.month=1;
d2.year++;
} }
return n;
}
date date::operator+(int n)
{
int i;
date d(day,month,year);
for(i=0;i<n;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;
}
ostream &operator<<(ostream & o,date d)
{
Dept.of CSE

Page 36

DATA STRUCTURES WITH C/C++ LAB MANUAL


o<<d.day<<":"<<d.month<<":"<<d.year;
return o;
}
void main()
{
int d,m,y,n;
clrscr();
x:
cout<<"enter 1st date"<<endl;
cin>>d>>m>>y;
if(y%4!=0&&m==2&&d>29)
{
cout<<"invalid date";
goto x;
}
date d1(d,m,y);
cout<<"enter 2nd date "<<endl;
cin>>d>>m>>y;
date d2(d,m,y);
cout<<"1st date is"<<d1<<endl;
cout<<"2nd date is"<<d2<<endl;
n=d1-d2;
cout<<"the difference between two dates is"<<n<<"days\n";
cout<<"enter number of days"<<endl;
cin>>n;
d1=d2+n;
cout<<"the resting date"<<d2<<"is"<<d1<<endl;
getch();
}

OUTPUT:
1 to subtract 2 to add
3 to exit
Enter your choice:1
Enter the date1 in the format dd/mm/yy
26 04 2013
Enter the date2 in the format dd/mm/yy
26 04 2014
Difference is:365

Dept.of CSE

Page 37

DATA STRUCTURES WITH C/C++ LAB MANUAL

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<stdlib.h>
class bin_tree
{
private: struct node
{
int info;
node *right,*left;
};
node *root,*temp,*scan,*father;
void inorder(node *ptr)
{
if(ptr)
{
inorder(ptr->left);
cout<<ptr->info<<" ";
inorder(ptr->right);
}
}
void preorder(node *ptr)
{
if(ptr)
{
cout<<ptr->info<<" ";
preorder(ptr->left);
preorder(ptr->right);
}
}
void postorder(node *ptr)
{
if(ptr)
{
Dept.of CSE

Page 38

DATA STRUCTURES WITH C/C++ LAB MANUAL


postorder(ptr->left);
postorder(ptr->right);
cout<<ptr->info<<" ";
}
}
public:bin_tree()
{
root=NULL;
}
void display()
{
int option;
if(root)
{
while(1)
{
cout<<"\n 1inorder\n"<< "2 preorder\n"<<" 3postorder\n"<< "4return to main program\n";
cout<<"enter your option:";
cin>>option;
switch(option)
{
case 1:cout<<"\n contenets of the tree in inorder is: ";
inorder(root);
break;
case 2:cout<<"\n contenets of the tree in preorder is:";
preorder(root);
break;
case 3:cout<<"\n contenets of the tree in postorder is ";
postorder(root);
break;
case 4: return;
}
}
}
else
cout<<"tree empty";
}
void create()
{
int info;
Dept.of CSE

Page 39

DATA STRUCTURES WITH C/C++ LAB MANUAL


while(1)
{
cin>>info;
if(info!=-1)
insert(info);
else
break;
}
}
void insert(int num)
{
temp=new node;
temp->left=temp->right=NULL;
temp->info=num;
if(root)
{
scan=father=root;
while(scan)
{
father=scan;
scan=temp->info<scan->info?scan->left:scan->right;
}
if(temp->info<father->info)
father->left=temp;
else
father->right=temp;
}
else
root=temp;
}
};
Void main()
{
bin_tree tree;
int option, info;
while(1)
{
cout<<"\n enter 1 to create binary tree\n";
cout<<"enter 2 to display\n";
Dept.of CSE

Page 40

DATA STRUCTURES WITH C/C++ LAB MANUAL


cout<<"enter 3 to exit\n";
cout<<"enter your option:";
cin>>option;
switch(option)
{
case 1:cout<<"\n enter the elements to create and enetr -1 to terminate\n";
tree.create();
break;
case 2:tree.display();
break;
case 3:exit(0);
}
}
getch();
}

OUTPUT:
Enter 1 to create binary tree
Enter 2 to display
Enter 3 to exit
Enter your choice:1
Enter the elements to create and -1 to terminate
40 50 100 -1
Enter 1 to create binary tree
Enter 2 to display
Enter 3 to exit
Enter your choice:2
1.in-order
2.pre-order
3.post-order 4.to return to main program
Enter your choice:1
Contents of the tree in in-order is:40 50 100
Enter your choice:3
Contents of the tree in post-order is:40 100 50

Dept.of CSE

Page 41

You might also like