You are on page 1of 79

SLIP 1

Q.1) Data Structures Using ‘C’ [Total Mark 40]


1. Sort a random array of n integers (accept the value of n from user) in ascending order by using bubble sort
algorithm. Accept & Display Function [10] Bubble Sort Function [5]
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void display(int a[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
void accept(int a[],int n)
{
int i;
printf("\n\t Enter numbers");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
}
void bsort(int a[],int n)
{
int i,j;
int temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{

temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
main()
{
int n,i;
int *a;
clrscr();
printf("\n\tEnter the number of elements : ");
scanf("%d",&n);
a = malloc(sizeof(int)*n);
accept(a,n);
display(a,n);
bsort(a,n);
printf("\n\tAfter bubble sort : ");
display(a,n);
}

2. Write a C program which uses Binary search tree library and implements following Functions:
a) Create Binary Tree [10] b) Insert a node in a binary tree [5] c) Search a node in binary tree [5]
d) Display a binary tree [5]
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct bnode
{
int data;
struct bnode *left,*right;
}bnode;
bnode *insert(bnode *,int);
bnode *search(bnode *,int);
bnode *create();
void inorder(bnode *T);
bnode *create()
{
int n,x,i;
bnode *root;
root=NULL;
printf("\nEnter no. of nodes :");
scanf("%d",&n);
printf("\nEnter tree values :");
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
return(root);
}
void inorder(bnode *T)
{
if(T!=NULL)
{
inorder(T->left);
printf("%d\t",T->data);
inorder(T->right);
}
}

bnode *insert(bnode *T,int x)


{

bnode *r;
if(T==NULL)
{
r=(bnode*)malloc(sizeof(bnode));
r->data=x;
r->left=NULL;
r->right=NULL;
return(r);
}
if(x>T->data)
{
T->right=insert(T->right,x);
return(T);
}
else

if(x<T->data)
{
T->left=insert(T->left,x);
return(T);
}
return(T);
}
bnode *search(bnode *root,int x)
{

while(root!=NULL)
{

if(x==root->data)
return(root);
if(x>root->data)
root=root->right;
else
root=root->left;
}
return(NULL);
}
void main()
{
bnode *root=NULL,*p;
int x,ch;
clrscr();
do
{
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Display");
printf("\n\t4.Search");
printf("\n\t5.Exit");
printf("\n enter your choice:-->");
scanf("%d",&ch);
switch(ch)
{
case 1: root=create();break;
case 2: printf("\n enter the key to thr inserted:-->");
scanf("%d",&x);
root=insert(root,x);
break;
case 3: inorder(root);
break;

case 4: printf("\nEnter the You Want To Search");


scanf("%d",&x);
p = search(root,x);
if(p==NULL)
{
printf("\n Element Not Found");
}
else
{
printf("\n Element Found");
}
break;
case 5: exit(0);
}
}while(ch!=5);
}

Q 2) Object Oriented Concepts and Programming in C++: [Total Marks 30]


1. Define a class Message with two data members one character pointer and an
integer storing length.
Implement following functions:
a) Default constructor and Parameterized constructor
b) Overload binary + operator to represent concatenation of messages
c) Overload [] operator to return a character at a specific position
d) Overload = operator to copy one Message object to another.
Write a main function that uses the above class and its member functions.

#include<iostream>
#include<string.h>
using namespace std;

class String
{
char *str;
int len;
public :
String()
{}
String(char nm[])
{
len=strlen(nm);
str=new char[len+1];
strcpy(str,nm);

}
void display()
{
cout<<"String = "<<str<<endl;
}
String operator+(String ob)
{
String temp;
temp.len=len+ob.len;
temp.str = new char[temp.len+1];
strcpy(temp.str,str);
strcat(temp.str,ob.str);
return temp;
}
char operator[](int &index)
{
if(index>=0 && index<len)
return str[index];
else
return '_';
}

void operator=(String ob)


{
len=ob.len;
str= new char[len+1];
strcpy(str,ob.str);
}

};

main()
{
char s1[20],s2[30];
cout<<"Enter 1st string : ";
cin>>s1;
cout<<"Enter 2nd string : ";
cin>>s2;
String ob1(s1),ob2(s2);
cout<<"1st string is ";
ob1.display();
cout<<"2nd string is ";
ob2.display();

cout<<"After concatinated String : ";


String ob3;
ob3=ob1+ob2;
ob3.display();

cout<<"Enter index to be search in 1st String : ";


int i;
cin>>i;
char ans=ob1[i];
if(ans!='_')
cout<<"Given index charcater is : "<<ans<<endl;
else
cout<<"Enter valid index"<<endl;

cout<<"Enter String to be copy :";


cin>>s2;
String ob4(s2);
ob1=ob4;
ob1.display();
}

SLIP3
Q.1) Data Structures Using ‘C’ [Total Marks 40]
1.Read the data from the file “employee.txt” and sort on age using insertion sort.Reading & Display the file [10]
Insertion sort Function [5]
#include<stdio.h>
typedef struct emp
{
char name[10];
int age;
}emp;
emp e[10];
void insert(emp a[],int n)
{
emp temp,b[10];
int i=0,j=0;
printf("-------");
b[0]=a[0];
for(j=1;j<n;j++)
{
temp=a[j];
for(i=j-1;i>=0 && strcmp(temp.name,b[i].name)<0;i--)
{

b[i+1]=b[i];
}
b[i+1]=temp;
}
for(i=0;i<n;i++)
{
a[i]=b[i];
}
}

main()
{
FILE *f;
emp e[10];
int j,i=0;
f=fopen("emp.txt","r");
for(i=0;!feof(f);i++)
{
fscanf(f,"%s %d",e[i].name,&e[i].age);
}
for(j=0;j<i-1;j++)
{
printf("\n%s %d",e[j].name,e[j].age);
}
printf("\n i=%d",i-1);
fclose(f);
insert(e,i-1);
printf("\n the asc order is \n");
f=fopen("sort.txt","w");
fprintf(f,"\n the asc order on age is ");
for(j=0;j<i-1;j++)
{
printf("\n%s \t%d",e[j].name,e[j].age);
fprintf(f,"\n%s \t%d",e[j].name,e[j].age);
}
fclose(f);
}

Write a C program that accepts the graph as an adjacency matrix and converts it to adjacency list
representation Implement functions to print in degree, out degree and to Display the adjacency matrix
Read a graph as adjacency Matrix [5]
Creation of adjacency List [10]
Calculate out degree & In degree from adjacency list [10]

#include<stdio.h>
#include<malloc.h>
struct n
{
int data;
struct n *link;
};
typedef struct n NODE;
NODE *getnode(int);
NODE *findlast(NODE *);
void display(NODE *[],int);
void main()
{
NODE *ptr,*temp,*h[10];
int n,a[10][10],i,j;
printf("\n Enter total number of vertices : ");
scanf("%d",&n);
printf("\n Enter entries of an adjacency matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\n Enter a[%d][%d] : ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\n Entered Adjacency matrix is … \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\t %d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
h[i]=NULL;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==1)
{
temp=getnode(j+1);
if(h[i]==NULL)
h[i]=temp;
else
{
ptr=findlast(h[i]);
ptr->link=temp;
}
}
}
}
printf("\n The Adjacency list looks like … \n");
display(h,n);
}
NODE *getnode(int j)
{
NODE * temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=j;
temp->link=NULL;
return(temp);
}
NODE *findlast(NODE *h)
{
NODE *ptr;
for(ptr=h;ptr->link!=NULL;ptr=ptr->link);
return(ptr);
}
void display(NODE *h[10],int n)
{
NODE *ptr;
int i;
for(i=0;i<n;i++)
{
printf(" V%d ",i+1);
ptr=h[i];
if(ptr==NULL)
printf(" NULL");
while(ptr!=NULL)
{
printf(" ->V%d",ptr->data);
ptr=ptr->link;
}
printf("\n");
}
}
Q 2) Object Oriented Concepts and Programming in C++: [Total Marks 30]

Implement a class "printdata” to overload "print" function as follows:


a) void print(int) - outputs value - <int>, that is, value followed by the value of the integer. eg.
print(10) output value -<10>
b) void print (int, int) – outputs value – [<int>, <int>], that is, value followed by the two integers separated
by comma in square brackets. eg print(4,6) output value-[<4>,<6>]
c) void print(char *) – outputs value –“char*”, that is, value followed by the string in double quotes. eg
print(“hi”) output value-“hi”
d) void print(int n, char *)- display first n characters from the given string. eg print(3,”Object”)- output –“Obj”
e) void print(char ch, char *)- count occurrences of a character from the given string. eg
print("P",”CPP Programming”)- output
Write a main function that uses the above class and its member functions.

#include<iostream>
#include<string.h>
using namespace std;
class printdata
{
public:
void print(int no)
{
cout<<"\n<"<<no<<">";
}

void print(int n1,int n2)


{
cout<<"\n[<"<<n1<<"> , <"<<n2<<">]";
}

void print(char *str)


{
cout<<"\""<<str<<"\"";
}
void print(int n,char *str)
{ char *str1=new char[20];int j;
for(int i=0;str[i]!='\0';i++)
{ j=i;
if(i==n)
break;
else {
str1[i]=str[i];

}
}
str1[j]='\0';
cout<<"\n\""<<str1<<"\"";
}
void print(char ch ,char *str)
{
int i,count=0;
for(int i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
count++;

}
cout<<"\n Count = "<<count;
}
};

int main()
{
printdata ob1;
int num,n1,n2;
char *str,ch;
cout<<"\n Enter num : ";
cin>>num;
ob1.print(num);
cout<<"\n Enter n1 and n2 : ";
cin>>n1>>n2;
ob1.print(n1,n2);
str=new char[30];

cout<<"\n Enter string : ";


cin>>str;
ob1.print(str);

cout<<"\n Enter string :";


cin>>str;
cout<<"\n Enter no of charcters to be display : ";
cin>>num;
ob1.print(num,str);

cout<<"\n Enter string :";


cin>>str;
cout<<"\n Enter character to display occurance : ";
cin>>ch;
ob1.print(ch,str);
return 0;
}

SLIP4
Q.1) Data Structures Using ‘C’ [Total Marks 40]

Read the data from the file “employee.txt” and sort on age using Bubble sort. Reading the contents of File &
Display Function [10]
Bubble sort Function [5]

#include<stdio.h>

typedef struct emp


{
char name[10];
int age;
}emp;
emp e[10];
void bubble(emp a[],int n);

main()
{
FILE *f;
int j,i=0;
f=fopen("employee","r");

for(i=0;!feof(f);i++)
{
fscanf(f,"%s %d",e[i].name,&e[i].age);
}

for(j=0;j<i-1;j++)
{ printf("\n%s %d",e[j].name,e[j].age);
}

bubble(e,i-1);

fclose(f);
printf("\n the asc order is \n");
f=fopen("employeee","w");
fprintf(f,"\n the asc order on age is ");
for(j=0;j<i-1;j++)
{
printf("\n%s \t%d",e[j].name,e[j].age);
fprintf(f,"\n%s \t%d",e[j].name,e[j].age);
}

fclose(f);
}

void bubble(emp e[],int n)


{
int i,j;
emp temp;
printf("\n n=%d",n);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(e[i].age>e[j].age)
{
temp=e[j];
e[j]=e[i];
e[i]=temp;
}
}
}
}

Write a C program that accepts the graph as an adjacency matrix and converts it to adjacency list
representation. Implement functions to print out degree of any vertex i (take i as parameter to the function)
from adjacency list a)Read a graph as adjacency Matrix [5]
b) Creation of adjacency List [10] c)Calculate out degree [10]

#include<stdio.h>
#include<malloc.h>
struct n
{
int data;
struct n *link;
};
typedef struct n NODE;
NODE *getnode(int);
NODE *findlast(NODE *);
void display(NODE *[],int);
void main()
{
NODE *ptr,*temp,*h[10];
int n,a[10][10],i,j;
printf("\n Enter total number of vertices : ");
scanf("%d",&n);
printf("\n Enter entries of an adjacency matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\n Enter a[%d][%d] : ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\n Entered Adjacency matrix is … \n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("\t %d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<n;i++)
h[i]=NULL;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==1)
{
temp=getnode(j+1);
if(h[i]==NULL)
h[i]=temp;
else
{
ptr=findlast(h[i]);
ptr->link=temp;
}
}
}
}
printf("\n The Adjacency list looks like … \n");
display(h,n);
}
NODE *getnode(int j)
{
NODE * temp;
temp=(NODE *)malloc(sizeof(NODE));
temp->data=j;
temp->link=NULL;
return(temp);
}
NODE *findlast(NODE *h)
{
NODE *ptr;
for(ptr=h;ptr->link!=NULL;ptr=ptr->link);
return(ptr);
}
void display(NODE *h[10],int n)
{
NODE *ptr;
int i;
for(i=0;i<n;i++)
{
printf(" V%d ",i+1);
ptr=h[i];
if(ptr==NULL)
printf(" NULL");
while(ptr!=NULL)
{
printf(" ->V%d",ptr->data);
ptr=ptr->link;
}
printf("\n");
}
}

Q 2) Object Oriented Concepts and Programming in C++: [Total Marks 30]


Student: id, name,
StudentExam (derived from Student): with n subjects (n can be variable)
StudentResult (derived from StudentExam): with percentage, grade
Define a parameterized constructor for each class and appropriate functions to accept and display details.
Create n objects of the StudentResult class and display the marklist using suitable manipulators.
a) Parameterized constructor, accept and display for each class-each function carries 2 mARK
b) Display marklist with Use of manipulators
Write main function that uses the above class and its member functions.

#include<iostream>
#include<string.h>

using namespace std;


class Student
{
protected: int rno;
char name[20];
public:
Student(int r, char name1[])
{
rno=r;
strcpy(name,name1);
}

void display()
{
cout<<"\n Rno : "<<rno;
cout<<"\t Name : "<<name;
}
};
class StudentExam : public Student
{
protected : int *ptr;
int no;
public :
StudentExam(int r,char nm[],int n):Student(r,nm)
{
no=n;
ptr = new int[no];
}

void accept()
{
for(int i=0;i<no;i++)
{ cout<<"\n Enter marks ";
cin>>ptr[i];
}
}
void display()
{ cout<<"\n========== Markslist============ \n ";
for(int i=0;i<no;i++)
cout<<"subject"<<i<<" : "<<ptr[i]<<"\n";
}

};
class StudentResult :public StudentExam
{
float per;char grade;
public :
StudentResult(int r,char nm[],int no):StudentExam(r,nm,no)
{
}
void cal()
{
int t=0;
for(int i=0;i<no;i++)
{ t=t+ptr[i];
}
per=t/no;
if(per>70)
grade ='A';
else if(per>=60 && per<70)
grade ='B';
else if(per>=50 && per<60)
grade='C';
else if(per>=40 && per<50)
grade ='D';
else grade ='E';
Student::display();
StudentExam::display();

cout<<"======================================================";
cout<<"\n Percentage = "<<per<<"%";
cout<<"\n Grade = "<<grade<<endl;
}

};

main()
{
int no,s;char name[20];
cout<<"\n enter roll no ";
cin>>no;
cout<<"\n Enter name :";
cin>>name;
cout<<"\n Enter no of subject :";
cin>>s;
StudentResult ob(no,name,s);
ob.accept();
cout<<"\n MARLKIST IS \n ";
ob.cal();
}

Slip 5
Read the data from the file “employee.txt” and sort on names in alphabetical order (use strcmp) using bubble
sort Reading the contents of File & Display Function [10] Bubble sort Function [5]

#include<stdio.h>
typedef struct emp
{
char name[10];
int age;
}record;
record emp[100];

int readfile(record *a)


{
int i=0;
FILE *fp;

if((fp=fopen("emp.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}
}
return(i-1);
}

void writefile(record *a,int n)


{
int i=0;
FILE *fp;
if( ( fp=fopen("sort.txt","w+") )!=NULL)
{
for(i=0;i<n;i++)

fprintf(fp,"%d%s\n",a[i].age,a[i].name);
}

}
void bsort(record *a,int n)
{
int i,j; record t;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(strcmp(a[j].name,a[j+1].name)>=0)
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}

main()
{
int n;
n=readfile(emp);
bsort(emp,n);
writefile(employee,n);
}

Write a C program that accepts the graph as an adjacency matrix and converts it to adjacency list
representation. Write a function to display the graph in adjacency list form.
Read a graph as adjacency Matrix [5]
Creation of adjacency List [10]
Display adjacency List & In degree of any vertex i (take I as parameter to the function) each carries
5 marks [10]

#include<stdio.h>

#include<malloc.h>

int n,g[10][10],v[10];

main()

{ int in,td=0,indegree=0,outdegree=0,n,i,j;

printf("\n Enter total number of vertices : ");

scanf("%d",&n);

printf("\n Enter entries of an adjacency matrix :\n");


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

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

printf("\n Enter a[%d][%d] : ",i+1,j+1);

scanf("%d",&g[i][j]);

printf("\n Entered Adjacency matrix is … \n");

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

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

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

printf("\n");

printf("\n Enter vertex to find indegree : ");

scanf("%d",&in);

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

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

if(i==in-1 && g[j][i]==1)

indegree++;

}
printf("node=%d\t indegree=%d",in,indegree);

Q 2) Object Oriented Concepts and Programming in C++: [Total Marks 30]


Define a class named Complex for representing complex numbers. A complex number has the general form a +
ib, where a- the real part, b - the imaginary part are both real numbers and i 2 =-1.
Define parameterized and default constructor. Overload +, - and * operators with usual meaning.
Default constructor & Parameterized constructor-each carries 3 marks [6]
Overloading of + operator & - operator- each carries 5 marks [10]
Overloading of * operator [9]
Write a main function that uses the above class and its member functions. [5]
*/
#include<iostream>
using namespace std;
class Complex
{
float a,b;
public :
Complex(){a=0;b=0;}
Complex(float a , float b)
{
this->a=a;
this->b=b;
}
void display()
{
cout<<"complex no:\t"<<a<<"\t+\t"<<"i"<<b<<endl;
}
Complex operator +(Complex &o)
{
Complex q;
q.a=(this -> a + o.a );
q.b=(this -> b + o.b );
return q;
}
Complex operator -(Complex &o)
{
Complex c;
c.a=(this->a - o.a);
c.b=(this->b - o.b);
return c;
}
//ac-bd + ad+bc
Complex operator*(Complex &o)
{
Complex c;
int r,i;
r=((this->a)*o.a) - (this->b * o.b);
i=(this->a * o.b) + (this->b * o.a);
c.a=r;
c.b=i;
return c;
}
};
main()
{
Complex o1,o2(3,4),o3(5,6),o4,o5(5,6),o6(7,8),o7,o8(7,8),o9(5,3),o10;
cout<<"\n\taddition\n\t "<<endl;
o2.display();
o3.display();
cout<<"\n\tanswer is"<<endl;
o4=o2+o3;
o4.display();
cout<<"\n\tsubtraction \n\t"<<endl;
o6.display();
o5.display();
cout<<"\n\tanswer is"<<endl;
o7=o6-o5;
o7.display();
cout<<"\n\tMUltiplication\n\t"<<endl;
o8.display();
o9.display();
cout<<"\n\tanswer is"<<endl;
o10=o8*o9;
o10.display();
}

Slip 6
Read the data from the file “employee.txt” and sort on names in alphabetical order (using Insertion sort
1.Reading the contents of File & Display Function [10]2.Insertion sort Function [5]

#include<stdio.h>
typedef struct emp
{
int age; char name[10];
}record;

record emp[50];

Int readfile (record *a)


{
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
{
while(!feof(fp))
{

fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}
} return (i-1);
}

void writefile(record *a,int n)


{
int i;
FILE *fp;
if((fp=fopen("sort.txt","w"))!=NULL)
{
for(i=0;i<n;i++)
{
fprintf(fp,"%d%s\n",a[i].age,a[i].name);
}
}
}

void insertion(record *a,int n)


{
int i,j;
record t;
for(i=1;i<n;i++)
{
t=a[i];
for(j=i-1;j>=0&& (strcmp(a[j].name,t.name)>=0);j--)
{
a[j+1]=a[j];
a[j]=t;
}
}
}

main()
{
int n;
n=readfile(emp);
insertion(emp,n);
writefile(emp,n);
}

Write a C program that accepts the graph as an adjacency matrix and checks if the graph is
undirected. The matrix for undirected graph is symmetric. Also calculate in degree of all vertices
Read a graph as adjacency Matrix [5]
Check the matrix is symmetric or not [10]
Calculate in degree of all vertices [10]

Q 2) Object Oriented Concepts and Programming in C++: [Total Marks 30]

Implement the following class hierarchy

Define constructors and appropriate functions tom accept and display details.
Parameterized constructor, accept and display for each class carries-(2+2+2) marks [24]
write main function to accept details of "n" Teaching-Assistants and display the details. [6]

#include<iostream.h>
#include<string.h>

// using namespace std;


class person
{
protected:
char firstname[20];
char lastname[20];
public:
person()
{
}
person(char a[],char b[])
{
strcpy(firstname,a);
strcpy(lastname,b);
}
void accept()
{
cout<<"\n enter the person details";
cout<<"\n enter first name"<<"\n";
cin>>firstname;
cout<<"\n enter last name"<<"\n";
cin>>lastname;
}
void display()
{
cout<<"\n"<<"the first name is"<<firstname;
cout<<"\n"<<"the last name is"<<lastname;
}
};

class student:public person


{
protected:
char course[20];
public:
student()
{
}
student(char c[])
{
strcpy(course,c);
}
void accept()
{
person::accept();
cout<<"\n enter the course";
cin>>course;
}
void display()
{
person::display();
cout<<"\n the course is:"<<course;
}
};

class teacher:public person


{
protected:
char dept[20];
public:
teacher()
{
}
teacher(char d[])
{
strcpy(dept,d);
}
void accept()
{
person::accept();
cout<<"\n enter the department";
cin>>dept;
}
void display()
{
person::display();
cout<<"\n"<<dept;
}
};

class teaching_assistant:public student,public teacher


{
public:
void accept()
{
student::accept();
teacher::accept();
}
void display()
{
student::display();
teacher::display();

}
};

int main()
{
teaching_assistant ta[100];
int i,n;
cout<<" Enter how many data you want:";
cin>>n;
for( i=0;i<n;i++)
ta[i].accept();
for( i=0;i<n;i++)
ta[i].display();
return 0;
}

slip7
Sort a random array of n integers (accept the value of n from user) in ascending order by using a
recursive Merge sort algorithm.
Accept & Display Function [8]
Merge Sort Function [7]

Write a C program which uses Binary search tree library and implements following functions
Create Binary search Tree [10]
Insert a node in a binary tree [10]
Display a binary tree [5]

#include<stdio.h>
#include<stdlib.h>
//#include"btree14.h"
typedef struct bnode
{
int data;
struct bnode *left,*right;
}bnode;
bnode *insert(bnode *,int);
bnode *create();
void inorder(bnode *T);
bnode *create()
{
int n,x,i;
bnode *root;
root=NULL;
printf("\nEnter no. of nodes :");
scanf("%d",&n);
printf("\nEnter tree values :");
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
return(root);
}
void inorder(bnode *T)
{
if(T!=NULL)
{
inorder(T->left);
printf("%d\t",T->data);
inorder(T->right);
}
}

bnode *insert(bnode *T,int x)


{

bnode *r;
if(T==NULL)
{
r=(bnode*)malloc(sizeof(bnode));
r->data=x;
r->left=NULL;
r->right=NULL;
return(r);
}
if(x>T->data)
{
T->right=insert(T->right,x);
return(T);
}
else

if(x<T->data)
{
T->left=insert(T->left,x);
return(T);
}
return(T);
}

void main()
{
bnode *root=NULL,*p;
int x,ch;
do
{
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Display");
printf("\n\t4.Exit)");
printf("\n enter your choice:-->");
scanf("%d",&ch);
switch(ch)
{
case 1: root=create();break;
case 2: printf("\n enter the key to thr inserted:-->");
scanf("%d",&x);
root=insert(root,x);
break;
case 3: inorder(root);
break;
case 4: exit(0);
}
}while(ch!=4);
}
Q 2) Object Oriented Concepts and Programming in C++: [Total Marks 30]

1. Write the definition for a class called "time" that has hours, minutes & seconds as integer data members.
Consider 24 hr format. The class has the following member functions:
a) void settime(int, int, int) to set the specified values of hours, minutes and seconds in object . [5]
b) void showtime() to display contents of time object. [5]
c) time add(time) add the corresponding values of hours, minutes and seconds (<60) in time object
argument to current time object and make appropriate conversions and return time. [8]d) time diff(time)
subtract values of hours, minutes and seconds in time object argument from current time object after making
appropriate conversions and return time difference. [8]
Write a main program to illustrate the use of above class and member function. [4] */

#include <iostream>
#include <stdlib.h>
#include <iomanip>
using namespace std;
class Time
{
int hours,minutes,seconds;
public :
int timeToSeconds()
{
return (hours*3600) + (minutes*60) + seconds;
}
Time add(Time &t2)
{
Time t3;
int seconds = this->timeToSeconds() + t2.timeToSeconds();
t3.hours = seconds/3600;
t3.minutes = seconds%3600/60;
t3.seconds = seconds%3600%60;
return t3;
}
Time diff(Time &t2)
{
Time t3;
int seconds = abs(this->timeToSeconds() - t2.timeToSeconds());
t3.hours = seconds/3600;
t3.minutes = seconds%3600/60;
t3.seconds = seconds%3600%60;
return t3;
}
void showTime()
{
cout << "Time : " << setfill('0')
<< setw(2) << hours << ":"
<< setw(2) << minutes << ":"
<< setw(2) << seconds << endl;
}
void setTime(int h,int m,int s)
{
hours = h % 24;
minutes = m % 60 ;
seconds = s % 60;
}
};
main()
{
Time t1,t2,t3;
t1.setTime(25,0,00);
t2.setTime(25,5,20);
t3 = t1.diff(t2);
t3.showTime();
}
Slip 8
Sort a random array of n integers (accept the value of n from user) in ascending order by using recursive
Quick sort algorithm.
Accept & Display Function [8] Quick Sort Function [7]

#include <stdio.h>
#include <stdlib.h>
void QuickSort(int *a,int lb,int ub)
{
if( lb < ub )
{
int pos;
pos = Partition(a, lb, ub);
QuickSort(a, lb, pos-1);
QuickSort(a, pos+1, ub);
}
}
int Partition(int *a, int lb, int ub)
{
int temp = a[lb];
int down, up;
down = lb; up = ub;
while( down < up )
{
while( down <= ub && a[down] <= temp ) down++;
while( arr[up] > temp ) up--;
if( down < up )
{
int t;
t = arr[down];
a[down] = a[up];
a[up] = t;
}

}
a[lb] = a[up];
a[up] = temp;
return up;
}

void display(int a[] , int no)


{
int i;
for(i=0; i<no; i++)
printf("%d,",a[i]);
printf("\n");
}
main()
{
int *a,i,no;
printf("How many numbers you want ?");
scanf("%d",&no);
a = malloc(sizeof(int)*no);
for(i=0; i<no; i++)
a[i] = random()%100;
display(a,no);
QuickSort(a,0,no-1);
printf("\nAfter sort\n");
display(a,no);
}

Write a C program which uses Binary search tree library and implements following Functions (use
Recursion). a) Create Binary search Tree [10] b) In order [5] c) Pre order [5] d) Post order [5]

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct bnode
{
int data;
struct bnode *left,*right;
}bnode;
bnode *insert(bnode *,int);
bnode *create();
void inorder(bnode *T);
void preorder(bnode *T);
void postorder(bnode *T);
bnode *create()
{
int n,x,i;
bnode *root;
root=NULL;
printf("\nEnter no. of nodes :");
scanf("%d",&n);
printf("\nEnter tree values :");
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
return(root);
}
void inorder(bnode *T)
{
if(T!=NULL)
{
inorder(T->left);
printf("%d\t",T->data);
inorder(T->right);
}
}
void preorder(bnode *T)
{
if(T!=NULL)
{
printf("%d\t",T->data);
preorder(T->left);
preorder(T->right);
}
}
void postorder(bnode *T)
{
if(T!=NULL)
{
postorder(T->left);
postorder(T->right);
printf("%d\t",T->data);
}
}

bnode *insert(bnode *T,int x)


{

bnode *r;
if(T==NULL)
{
r=(bnode*)malloc(sizeof(bnode));
r->data=x;
r->left=NULL;
r->right=NULL;
return(r);
}
if(x>T->data)
{
T->right=insert(T->right,x);
return(T);
}
else

if(x<T->data)
{
T->left=insert(T->left,x);
return(T);
}
return(T);
}

void main()
{
bnode *root=NULL,*p;
int x,ch,c;
clrscr();
do
{
printf("\n\t1.create");
printf("\n\t2.Inorder");
printf("\n\t3.PreOrder");
printf("\n\t4.PostOrder");
printf("\n\t5.Exit)");
printf("\n enter your choice:-->");
scanf("%d",&ch);
switch(ch)
{
case 1: root=create();break;
case 3: preorder(root);break;
case 4: postorder(root);break;
break;
case 2: inorder(root);
break;
case 5: exit(0);
}
}while(ch!=5);
}

Q 2) Object Oriented Concepts and Programming in C++: [Total Marks 30]

Define a class named Clock with three integer data members for hours, minutes and seconds.Define
parameterized and default constructors. Overload increment and decrement operators appropriately. Overload
extraction and insertion operators.
Default constructor and Parameterized constructor [2+3]
Overloading of each operator carries 5 marks [20]
Write a main function that uses the above class and its member functions. [5]

#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;

class Clock
{
private:
int h,m,s;
public:
Clock()
{
h=0;
m=0;
s=0;
}
Clock(int th,int tm,int ts)
{
h=th;
m=tm;
s=ts;
}
void displayTime()
{
cout << "H: " << h << " M:" << m <<" S:" << s <<endl;
}

// overloaded ++ operator
Clock operator++ ()
{
cout<<"inside +++ \n";
++s; // increment this object
if(s>=60)
{
s=0;
++m;
if(m>=60)
{
m=0;
++h;
if(h>=24)
{
h=0;
m=0;
s=0;
}
}
}
}

// overloaded -- operator
Clock operator-- ()
{
cout<<"inside --- \n";
--s; // decrement this object
if(s<=0)
{
s=59;
--m;
if(m<=0)
{
m=59;
--h;
if(h<=0)
{
h=24;
m=59;
s=59;
}
}
}
}

friend ostream &operator<<( ostream &output, const Clock &c )


{
output << "\n HH : " << c.h << "\n MM : " << c.m << "\n SS : " << c.s ;
return output;
}

friend istream &operator>>( istream &input, Clock &c )


{
cout<<"\n Enter h :";
input >> c.h;
cout<<"\n Enter m :";
input >> c.m;
cout<<"\n Enter s :";
input >> c.s;
return input;
}
};

int main()
{
Clock t1,t2(2,2,2);

++t1; // increment T1
t1.displayTime(); // display T1

--t1; // decrement T1
t1.displayTime(); // display T1

cin>>t2;
cout<<"T2="<<t2;

return(0);

Slip 9

Read the data from the „employee.txt‟ file and sort on age using Merge sort and write the sorted
data to another file 'sorted_emp_on_age.txt'.
Reading & Display the file [10]
Merge sort Function [5]

include<stdio.h>
typedef struct employee
{
char name[10];
int age;
}record;
record employee[100];
int readfile(record *a)
{
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
{while(!feof(fp))
{
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}}

return(i-1);
}

void writefile(record *a,int n)


{
int i=0;
FILE *fp;
if((fp=fopen("sorted_emp_on_age_merge.txt","w"))!=NULL)
{
for(i=0;i<n;i++)
fprintf(fp,"%d%s\n",a[i].age,a[i].name);
}
}

merge(record *a,int l,int m,int u)


{
record c[10]; int i,j,k;
i=l;
j=m+1;
k=0;
while(i<=m && j<=u)
{
if(a[i].age<a[j].age)
{
c[k]=a[i];
k++;i++;
}
else
{
c[k]=a[j];
k++;j++;
}
}
while(i<=m)
{
c[k]=a[i];
i++;k++;
}
while(j<=u)
{
c[k]=a[j];
k++;j++;
}
for(i=l,j=0;i<=u;i++,j++)
a[i]=c[j];
}

merge_sort(record *a,int i,int j)


{
int k=0;
if(i<j)
{
k=(i+j)/2;
merge_sort(a,i,k);
merge_sort(a,k+1,j);
merge(a,i,k,j);
}
}
main()
{
int n;
n=readfile(employee);
merge_sort(employee,0,n-1);
writefile(employee,n);
}
Write a C program which uses Binary search tree library and implements following functions
Create Binary search Tree [10] In order [5] Search a node in binary tree [5] Count a node in
binary tree [5]

#include<stdio.h>
#include<stdlib.h>
typedef struct bnode
{
int data;
struct bnode *left,*right;
}bnode;
bnode *insert(bnode *,int);
bnode *search(bnode *,int);
bnode *create();
void inorder(bnode *T);
int count(bnode *T);
bnode *create()
{
int n,x,i;
bnode *root;
root=NULL;
printf("\nEnter no. of nodes :");
scanf("%d",&n);
printf("\nEnter tree values :");
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
return(root);
}
void inorder(bnode *T)
{
if(T!=NULL)
{
inorder(T->left);
printf("%d\t",T->data);
inorder(T->right);
}
}

bnode *insert(bnode *T,int x)


{

bnode *r;
if(T==NULL)
{
r=(bnode*)malloc(sizeof(bnode));
r->data=x;
r->left=NULL;
r->right=NULL;
return(r);
}
if(x>T->data)
{
T->right=insert(T->right,x);
return(T);
}
else

if(x<T->data)
{
T->left=insert(T->left,x);
return(T);
}
return(T);
}
int count(bnode *T)
{
int i = 1;
if(T == NULL)
{
return(0);
}
else
{
i = i+count(T->left) + count(T->right);
return(i);
}

}
bnode *search(bnode *root,int x)
{

while(root!=NULL)
{

if(x==root->data)
return(root);
if(x>root->data)
root=root->right;
else
root=root->left;
}
return(NULL);
}
void main()
{
bnode *root=NULL,*p;
int x,ch,c;
clrscr();
do
{
printf("\n\t1.create");
printf("\n\t2.Inorder");
printf("\n\t3.Search");
printf("\n\t4.Count");
printf("\n\t5.Exit");
printf("\n enter your choice:-->");
scanf("%d",&ch);
switch(ch)
{
case 1: root=create();break;
case 3: printf("\nEnter the You Want To Search");
scanf("%d",&x);
p = search(root,x);
if(p==NULL)
{
printf("\n Element Not Found");
}
else
{
printf("\n Element Found");
}
break;
case 4: c = count(root);
printf("Total Node %d",c);
break;
case 2: inorder(root);
break;
case 5: exit(0);
}
}while(ch!=5);
}
Q 2) Object Oriented Concepts and Programming in C++: [Total Marks 30]

Implement a class data to overload following functions as follows:


a) int maximum(int, int) – returns the maximum between the two integer arguments [2]
b) int maximum(int *a, int arraylength) – returns the maximum integer from an array 'a' [5]
c) void maximum(int *a, int arrarylength , int n) – display all elements from the array 'a' which
are greater than given number 'n'. [5]d) int minimum( int, int) – returns the minimum between the two integer
arguments [2]
e) int minimum( int *, int arraylength) – returns the minimum integer from an array 'a' [5]
f) void minimum(int *a, int arrarylength , int n) –display all elements from the array 'a' which are smaller than
given number n. [5]
Write a main function that uses the above class and its member functions. [6] */

using namespace std;


class data
{
public:
data()
{
}
int maximum(int a, int b)
{
if(a > b)
return a;
return b;
}
int maximum(int *a , int n)
{
int max=0,i;
for(i=0; i<n; i++)
{
if(a[i] > max)
max=a[i];
}
return max;
}
void maximum(int *a, int alength, int n )
{
int i;
for(i=0; i<=alength; i++)
{
if(a[i] > n)
cout<<a[i]<<"\t";
}
cout<<"\n";
}
int minimum(int a, int b)
{
if(a < b)
return a;
return b;
}
int minimum(int *a , int n)
{
int mini=a[0];
for(int i=0; i<n; i++)
{
if(mini > a[i])
mini=a[i];
}
return mini;
}
void minimum(int *a, int alength, int n )
{
for(int i=0; i<alength; i++)
{
if(a[i] < n)
cout<< a[i] << "\t";
}
}
};
main()
{
data obj,obj2;
int a,n=0,no=0,length,arr[50],i,ima;
cout<<"1st function"<<endl;
cout<<"Max from 3 and 5 is :"<<endl;
n=obj.maximum(3,5);
cout<<n<<endl;
cout<<"2nd function(Maximum integer from array )"<<endl;
cout<<"enter size of array";
cin>>length;
cout<<"enter array\n";
for(i=0; i<length; i++)
cin>>arr[i];
cout<<"array\t";
for(i=0; i<length; i++)
cout<<arr[i]<<"\t";
ima=obj.maximum(arr,length);
cout<<"\nmax\t"<<ima<<endl;
cout<<"3rd function(Elements greater than given number, will be displayed )"<<endl;
cout <<"Enter number to be searched:\t";
cin>>a;
obj.maximum(arr, length, a );
cout<<"\n\n4th now minima"<<endl;
cout<<"minimum from 3 and 5"<<endl;
n=obj2.minimum(3,5);
cout<<n<<endl;
cout<<"\n\t5th function"<<endl;
cout<<"enter size of array";
cin>>length;
cout<<"enter array\n";
for(i=0; i<length; i++)
cin>>arr[i];
cout<<"array\t";
for(i=0; i<length; i++)
cout<<arr[i]<<"\t";
ima=obj2.minimum(arr,length);
cout<<"\nmin\t"<<ima<<endl;
cout<<"6th function(Elements smaller than given number, will be displayed )"<<endl;
cout <<"Entr number to be searched:\t";
cin>>a;
obj2.minimum(arr, length, a );
}

Slip 10
Read the data from the "employee.txt‟ file and sort on age using Quick sort and write the sorted data to
another file 'sortedemponage.txt'.
Reading & Display the file [10] Quick sort Function [5]

#include<stdio.h>
enum bool {false,true};

typedef struct employee


{
int age; char name[10];
}record;
record employee[50];

int readfile(record *a)


{
int i=0;
FILE *fp;
if((fp=fopen("emp.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}
}return (i-1);
}

void writefile(record *a,int n)


{
int i;
FILE *fp;
if((fp=fopen("sorted_on_age_quick.txt","w"))!=NULL)
{
for(i=0;i<n;i++)
{
fprintf(fp,"%d%s\n",a[i].age,a[i].name);
}

}
}
void quick(record *a,int l,int u)
{
record temp;int piv,left,right;
enum bool pivot_places=false;
left=l;
right=u;
piv=l;

if(l>=u)
return;

while(pivot_places==false)
{
while(a[piv].age<=a[right].age && piv!=right)
right--;
if(piv==right)
pivot_places=true;
if(a[piv].age>a[right].age)
{
temp=a[piv];
a[piv]=a[right];
a[right]=temp;
piv=right;
}
while(a[piv].age>=a[left].age && piv!=left)
left++;
if(piv==left)
pivot_places=true;
if(a[piv].age<a[left].age)
{
temp=a[piv];
a[piv]=a[left];
a[left]=temp;
piv=left;
}
}
quick(a,l,piv-1);
quick(a,piv+1,u);
}

main()
{
int n;
n=readfile(employee);
quick(employee,0,n-1);
writefile(employee,n);
}

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct bnode
{
int data;
struct bnode *left,*right;
}bnode;
bnode *insert(bnode *,int);
bnode *search(bnode *,int);
bnode *create();
void inorder(bnode *T);
bnode *create()
{
int n,x,i;
bnode *root;
root=NULL;
printf("\nEnter no. of nodes :");
scanf("%d",&n);
printf("\nEnter tree values :");
for(i=0;i<n;i++)
{
scanf("%d",&x);
root=insert(root,x);
}
return(root);
}
void inorder(bnode *T)
{
if(T!=NULL)
{
inorder(T->left);
printf("%d\t",T->data);
inorder(T->right);
}
}
bnode *insert(bnode *T,int x)
{

bnode *r;
if(T==NULL)
{
r=(bnode*)malloc(sizeof(bnode));
r->data=x;
r->left=NULL;
r->right=NULL;
return(r);
}
if(x>T->data)
{
T->right=insert(T->right,x);
return(T);
}
else

if(x<T->data)
{
T->left=insert(T->left,x);
return(T);
}
return(T);
}
bnode *search(bnode *root,int x)
{

while(root!=NULL)
{

if(x==root->data)
return(root);
if(x>root->data)
root=root->right;
else
root=root->left;
}
return(NULL);
}

void main()
{
bnode *root=NULL,*p;
int x,ch;
clrscr();
do
{
printf("\n\t1.create");
printf("\n\t2.insert");
printf("\n\t3.Display");
printf("\n\t4.Search");
printf("\n\t5.Exit");
printf("\n enter your choice:-->");
scanf("%d",&ch);
switch(ch)
{
case 1: root=create();break;
case 2: printf("\n enter the key to thr inserted:-->");
scanf("%d",&x);
root=insert(root,x);
break;
case 3: inorder(root);
break;

case 4: printf("\nEnter the You Want To Search");


scanf("%d",&x);
p = search(root,x);
if(p==NULL)
{
printf("\n Element Not Found");
}
else
{
printf("\n Element Found");
}
break;
case 5: exit(0);
}
}while(ch!=5);
}
Implement a class "invertdata" to overload "invert" function as follows:
a) int invert ( int) - returns the inverted integer. Eg. invert(5438) will return 8345 [10]
b) char * invert ( char *) – returns the reversed string – reverse(“comp”) will return 'pmoc' [8]
c) void invert( int * ) – will reverse the array order – An array [5, 7, 12, 4] will be inverted to [4, 12, 7, 5]. [7]
Write a main function that uses the above class and its member functions. [5]
*/
#include <iostream>
#include <string.h>
using namespace std;
class InvertData
{
public :
int invert(int n)
{
int r=0;
while( n )
{
r = r*10 + (n%10);
n = n/10;
}
return r;
}
void invert(char str[])
{
int j,k,i;
char rev[100];
for(i = 0; str[i] != '\0'; i++);
{
k = i-1;
}
for(j = 0; j <= i-1; j++)
{
rev[j] = str[k];
k--;
}
cout<<"The reversed string is "<<rev<<endl;
}
void invert(int arr[])
{
int temp,start=0,end=3;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
};
main()
{
InvertData obj;
int arr[] = {1,2,3,4};
obj.invert(arr);
int i;
for(i=0;i<4;i++)
cout<<"\t"<<arr[i];
cout<<"\n\n"<<obj.invert(5438)<<endl;
char name[20];
obj.invert((char *)"abc");
}

Slip 11

Create a random array of n integers. Accept a value x from user and use linear search algorithm to check
whether the number is present in the array or not and output the position if the number is present.
Accept & Display Function [8]
Linear Search Function [7]
*/
#include<stdio.h>
#include<stdlib.h>
void lsearch(int arr[],int size,int key)
{
int i;
for(i=0;i<size;i++)
{
I f(arr[i] == key)
{
printf("Key is at %d position\n",i+1);
break;
}
}
}
void Display(int arr[],int size)
{
int i;
for(i=0;i<size;i++)
printf("\t%d",arr[i]);
}
int Accept()
{
int size;
printf("Enter size of array : ");
scanf("%d",&size);
return size;
}
main()
{
int *arr,i,key;
int n = Accept();
arr = malloc(sizeof(int)* n);
for(i=0;i<n;i++)
arr[i] = random()%100;
Display(arr,n);
printf("\n\tEnter key to be searched : ");
scanf("%d",&key);
lsearch(arr,n,key);
}
Implement a stack library (dystack.h) of integers using a dynamic (linked list) implementation of
the stack and implementing the Push,Pop,IsEmpty,Init,Peek operations. Write a menu driven program
that includes stack library and calls different stack operations.
Push & Pop operation carries 8 mark each [16]
Isempty & Peek carries 4 mark each [8]
Init [1]
#include<stdio.h>
#include<stdlib.h>
#include "dstack.h"

main()
{

int choice,num;
node *top,*p;
while(1)
{
printf("\n\n1.initialize\n2.push\n3.pop\n4.peek\n5.is empty\n6.exit\nenter the choice\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
top=inti(top);
break;
case 2:
printf("\nenter the number\t");
scanf("%d",&num);
top=push(top,num);
break;
case 3:
top=pop(top);
break;
case 4:
p=peek(top);
printf("\nthe element at the top is %d",p->data);
break;
case 5:
isempty(top);
break;
case 6:
return 0;
break;
default:
printf("\n invalid option");
break;
}
}
}

// dystack.h

/* function of stack*/

#include <stdio.h>
#include<stdlib.h>

typedef struct node


{
int data;
struct node *next;
}node;

node *inti(node *top)


{
return NULL;
}

node *push(node *top,int num)


{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->data=num;
temp->next=NULL;
if(top==NULL)
{
top=temp;
return top;
}
else
{
temp->next=top;
top=temp;
return top;
}

node *pop(node *top)


{
node *temp;
temp=top;
if(top==NULL)
{
printf("\n----------------stack is empty-------------");
return NULL;
}
else
{
printf("\n --------------the element is %d------------",p->data);
top=temp->next;
free(temp);
return top;
}
}

node *peek(node *top)


{
node *temp;
temp=top;
if(temp==NULL)
{
printf("\n---------------stack is empty--------------");
}
else
{
return (temp);
}
}

void isempty(node *top)


{
if(top==NULL)
printf("\n------------------the stack is empty------------------");
else
printf("\n-----------------stack is not empty--------------------");
}

Student: id, name,


[Repeat]

Slip14

Read the data from file "sortedcities.txt" containing names of 10 cities and their STD codes. Accept a name of
the city from user and use binary search algorithm to check whether the name is present in
the file and output the STD code, otherwise output “city not in the list”.
Accept & Display Function [8]
Binary Search Function [7]

#include<stdio.h>
#include<string.h>

typedef struct city


{
char name[10];
int code;
}city;

void bubble(city e[],int n)


{
int i,j;
city temp;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(e[j].name,e[i].name)<0)
{
temp=e[j];
e[j]=e[i];
e[i]=temp;
}
}
}
}
void bsearch(city a[],char temp[],int lb, int ub)
{
int mid;
if(lb<=ub)
{
mid=(lb+ub)/2;
if(strcmp(temp,a[mid].name)==0)
{
printf("\n the city %s with %d code is found on %d
position",a[mid].name,a[mid].code,mid);

}
else if(strcmp(temp,a[mid].name)<0)
{
bsearch(a,temp,lb,mid-1);
}
else
{
bsearch(a,temp,mid+1,ub);
}
}
else
{
printf(" \n element is not found");
}
}
main()
{
FILE *f;
city c[10];
int i,j;
char temp[10];

f=fopen("cities","r");

for(i=0;!feof(f);i++)
{
fscanf(f,"%s %d",c[i].name,&c[i].code);
}

for(j=0;j<i-1;j++)
{
printf("\n%s %d",c[j].name,c[j].code);

}
fclose(f);

printf("\n \n enter the name of city to be searched \t");


scanf("%s",&temp);

bubble(c,i);
for(j=0;j<i;j++)
{
printf("\n %s",c[j].name);
}
bsearch(c,temp,0,i-1);
}
Implement a list library (singlylist.h) for a singly linked list with the insert, delete, search, display
operations. Write a menu driven program to call the operations.
Insert, Delete carries 6 marks each [12]
Display Function [4]
Search Function [5]
Creation of Singly link list [4]

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include"sslip14.h"

void main()
{
int sch,n,i;
clrscr();
do
{
printf("\n\t*****SINGLY LINK LIST MENU*****");
printf("\n\t\t1.CREATE");
printf("\n\t\t2.INSERT");
printf("\n\t\t3.DELETE");
printf("\n\t\t4.SEARCH");
printf("\n\t\t5.DISPLAY");
printf("\n\t\t6.EXIT");
printf("\n\tEnter your choice:-");
scanf("%d",&sch);
switch(sch)
{
case 1 : printf("\n\tHow many nodes? ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
create();

}
break;
case 2: insert();break;
case 3:del();break;
case 4:search();break;
case 5: display();break;
case 6: exit(0);
}

}while(sch!=6);
}

// sslip14.h
#include<stdlib.h>
typedef struct node
{

int info;
struct node *next;
};
struct node*temp,*q,*start=NULL;
create()
{

int data;
temp = malloc(sizeof(struct node*));
printf("Enter the data\n");
scanf("%d",&data);
temp->info = data;
temp->next=NULL;
if(start==NULL)
{
start=temp;

}
else
{
q=start;
while(q->next != NULL)
{
q=q->next;
}
q->next = temp;
}
return;
}
display()
{

if(start==NULL)
{
printf("List Empty\n");
}
else
{
q = start;
while(q->next !=NULL)
{
printf("%d\t",q->info);
q = q->next;
}
printf("%d\n",q->info);
}
return;
}

search()
{
int data,cnt=0;
printf("Enter the Element You Want to Search");
scanf("%d",&data);
temp=start;
while(temp && temp->info !=data)
{
temp = temp->next;
cnt++;
}
if(temp)
{
printf("%d is at Position %d\n",data,cnt);
}
else
{
printf("No data found");
}
return;
}

insert()
{
int x;
printf("\n\tAfter which node,\nEnter info:");
scanf("%d",&x);
q=start;
while(q->info!=x)
{
if(q->next==NULL)
{
printf("\n\tElement not found");
return;
}
else
{
q=q->next;
}
}
temp = malloc(sizeof(struct node));
printf("\n\tEnter the info:-");
scanf("%d",&temp->info);
temp->next=NULL;
if(q->next==NULL)
{
q->next=temp;
}
else
{
temp->next=q->next;
q->next=temp;
}
return;
}
del()
{
int x;
printf("\n\tWhich node to be deleted,\nEnter the info");
scanf("%d",&x);
q=start;
while(q->info!=x)
{
if(q->next==NULL)
{
printf("\n\tElement not found");
return;
}
else
{
temp=q;
q=q->next;
}
}
if(q==start)
{
start=q->next;
free(q);
}
else
{
if(q->next==NULL)
{
temp->next=NULL;
free(q);
}
else
{
temp->next=q->next;
free(q);
}
}
return;
}

Implement a class "invertdata" to overload "invert" function as follows:


[Repeat]

Slip15
1. Implement a stack library (ststack.h) of integers using a static implementation of the stack and
implementing the Init(),Push(),Pop(),IsEmpty()& IsFull() these operations. Write a Menu driven
program that includes stack library and calls different stack operations
Push & Pop Function each carries 5 Marks [10]
Init Function [1]
IsEmpty Function & IsFull Function each carries 2 marks [4]
/ .c File

#include<stdio.h>
#include<stdlib.h>
#include"sts_stack.h"
#define MAX 5

main()
{
int ch,x,m;

while(1)
{
printf("\n\n 1.Initialize");
printf("\n 2.Push");
printf("\n 3.Pop");
printf("\n 4.Peek");
printf("\n 5.Is empty");
printf("\n 6.Is full");
printf("\n 7.Exit \n");
printf("\n Enter choice : ");
scanf("%d", &ch);

switch(ch)
{
case 1:
printf("\n initializing stack....\n");
initialize(a);
break;

case 2:
printf("\n Enter element to push:");
scanf("%d",&x);
push(a,x);
printf("\n Push element is:%d \n",x);
break;

case 3:
pop(a);
break;

case 4:
peek(a);
break;

case 5:
isempty(a);
break;

case 6:
isfull(a);
break;

case 7:
printf("\n You have pressed exit....");
exit(0);
break;

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

}
}
}

// sts_stack.h

#include<stdio.h>
#include<stdlib.h>
#define MAX 5

int a[30];
int top;

initialize(int a[30])
{
top=-1;
}

push(int a[30],int x)
{
if(top==(MAX-1))
{
printf("\n Stack overflow \n");
}
else
{
top=top+1;
a[top]=x;
}
}

pop(int a[30])
{
if(top==-1)
{
printf("\n stack underflow \n");
}
else
{
printf("\n Element poped is:%d \n",a[top]);
top=top-1;
}
}

peek(int a[30])
{
printf("\n Element top most is:%d \n",a[top]);
}

isempty(int a[30])
{
if(top==-1)
{
printf("\n ** stack is empty ** \n");
return(1);
}
else
{
printf("\n ** stack is not empty ** \n");
return(0);
}
}
isfull(int a[30])
{
if(top==(MAX-1))
{
printf("\n ** stack is full ** \n");
return(1);
}
else
{
printf("\n ** stack is not full ** \n");
return(0);
}
}

2. Implement a list library (doublylist.h) for a doubly linked list with the insert, delete search, display
operations. Write a menu driven program to call the operations. Insert, Delete carries 6 marks each [12]
Display & Creation of doubly link list each carries 4 marks [8] Search Function

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include"dslip15.h"
void main()
{
int sch,h,i;
clrscr();
do
{
printf("\n\t*****Double LINK LIST MENU*****");
printf("\n\t\t1.CREATE");
printf("\n\t\t2.INSERT");
printf("\n\t\t3.DELETE");
printf("\n\t\t4.SEARCH");
printf("\n\t\t5.DISPLAY");
printf("\n\t\t6.EXIT");
printf("\n\tEnter your choice:-");
scanf("%d",&sch);
switch(sch)
{
case 1 : printf("\n\tHow many nodes? ");
scanf("%d",&h);
for(i=0;i<h;i++)
{
create();

}
break;
case 2: insert();break;
case 3: del();break;
case 4: search();
case 5: display();break;
case 6: exit(0);
}
}while(sch!=6);
}

//dslip15.h

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *next,*prev;
};
struct node *temp,*q,*start=NULL,*end;
create()
{
int data;
temp = malloc(sizeof(struct node));
printf("Enter The Element\n");
scanf("%d",&data);
temp->info = data;
temp->next = NULL;
temp->prev = NULL;
if(start == NULL)
{
start=temp;
end= temp;
}
else
{
q= start;
while(q->next != NULL)
{
q = q->next;
}
q->next = temp;
temp->prev=q;
end = temp;

}
return;
}

display()
{
if( start == NULL)
{
printf("List Is Empty");
}
else
{
q = start;
while(q->next != NULL)
{
printf("%d\t",q->info);
q= q->next;
}
printf("%d\t",q->info);
}
return;
}
search()
{
int data,cnt=0;
printf("Enter the Element You Want to Search");
scanf("%d",&data);
temp=start;
while(temp && temp->info !=data)
{
temp = temp->next;
cnt++;
}
if(temp)
{
printf("%d is at Position %d\n",data,cnt);
}
else
{
printf("No data found");
}
return;
}
insert()
{
int x;
printf("After which node \n Enter the info:-");
scanf("%d",&x);
q=start;
while(q->info!=x)
{
if(q->next==NULL)
{
printf("Element not found");
return;
}
else
{
q=q->next;
}}
temp=(struct node*)malloc(sizeof(struct node));
printf("Enter info:-");
scanf("%d",&temp->info);
temp->prev=NULL;
temp->next=NULL;
if(q->next==NULL)
{
q->next=temp;
temp->prev=q;
}
else
{
end=q->next;
q->next=temp;
temp->prev=q;
temp->next=end;
end->prev=temp;
}
return;
}

del()
{
int x;
printf("Which node to be deleted\nEnter the info:-");
scanf("%d",&x);
q=start;
while(q->info!=x)
{
if(q->next==NULL)
{
printf("Element not found");
return;
}
else
{
temp=q;
q=q->next;
}}
if(q==start)
{
start=q->next;
start->prev=NULL;
free(q);
}
else
{
if(q->next==NULL)
{
temp->next=NULL;
free(q);
}
else
{
end=q->next;
temp->next=end;
end->prev=temp;
free(q);
}}
return;
}

Implement a class "printdata” to overload "print" function as follows:

[Repeat]

Slip 16

Create a random array of n integers. Accept a value x from user and use linear search algorithm to check
whether the number is present in the array or not and output the position if the number is present. Accept &
Display Function [8] Linear Search Function [7]

[Repeat]

Implement a stack library (dystack.h) of integers using a dynamic (linked list) implementation
[Repeat]

Define a class named Complex for representing complex numbers

[Repeat]

Slip17
1. Implement a list library (singlylist.h) for a singly linked list with the operations create, search,
display. Write a menu driven driver program to call these operations.
Create ,Search, Display operation carries 5 each

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include"sslip17.h"

void main()
{
int sch,n,i;
clrscr();
do
{
printf("\n\t*****SINGLY LINK LIST MENU*****");
printf("\n\t\t1.CREATE");
printf("\n\t\t2.DISPLAY");
printf("\n\t\t3.SEARCH");
printf("\n\t\t4.EXIT");
printf("\n\tEnter your choice:-");
scanf("%d",&sch);
switch(sch)
{
case 1 : printf("\n\tHow many nodes? ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
create();

}
break;
case 2: display();break;
case 3:search();break;
case 4: exit(0);
}

}while(sch!=4);
}

// sslip17.h

#include<stdlib.h>
typedef struct node
{

int info;
struct node *next;
};
struct node*temp,*q,*start=NULL;
create()
{
int data;
temp = malloc(sizeof(struct node*));
printf("Enter the data\n");
scanf("%d",&data);
temp->info = data;
temp->next=NULL;
if(start==NULL)
{
start=temp;

}
else
{
q=start;
while(q->next != NULL)
{
q=q->next;
}
q->next = temp;

}
return;
}
display()
{

if(start==NULL)
{
printf("List Empty\n");
}
else
{
q = start;
while(q->next !=NULL)
{
printf("%d\t",q->info);
q = q->next;
}
printf("%d\n",q->info);
}
return;
}

search()
{
int data,cnt=0;
printf("Enter the Element You Want to Search");
scanf("%d",&data);
temp=start;
while(temp && temp->info !=data)
{
temp = temp->next;
cnt++;
}
if(temp)
{
printf("%d is at Position %d\n",data,cnt);
}
else
{
printf("No data found");
}
return;
}

Write a C program which uses Binary search tree library and implements following functions
a) Create Binary Search Tree [10] b) Count a node in binary tree [5]
c) Search a node in binary tree [5]d) Display(Inorder) a binary tree [5]

[Repeat]

Write the definition for a class called Rectangle that has floating point data Members length and
width. The class has the following member functions:
a) void setlength(float) to set the length of data member [2]
b) void setwidth(float) to set the width of data member [2]
c) float perimeter() to calculate and return the perimeter of the rectangle [2]
d) float area() to calculate and return the area of the rectangle [2]
e) void show() to display the length and width of the rectangle [2]
f) Overload compare() to- compare two rectangles by length and width [7] -compare two rectangles
by area [8]
Write main function to create two rectangle objects and display each rectangle and its area and
perimeter.

#include<iostream>
using namespace std;

class rectangle
{
float length,breadth;
public:
void setlength(float a)
{
length=a;
}
void setbreadth(float a)
{
breadth=a;
}

/*function to calculate perimeter*/


float perimeter()
{
float p;
p=(2*length)+(2*breadth);
return p;
}

/* function to calculate area*/


float area()
{
float a;
a=length*breadth;
return a;
}

void display()
{
cout<<"\nlength="<<length<<"\nbreadth="<<breadth;
}
};

int main()
{
rectangle r1,r2;
float l,b,p,a;

cout<<"\nEnter the parameters of first rectangle R1";


cout<<"\nlength\t";
cin>>l;
cout<<"\nbreadth\t";
cin>>b;

r1.setlength(l);
r1.setbreadth(b);

cout<<"\nEnter the parameters of second rectangle R2";


cout<<"\nlength\t";
cin>>l;
cout<<"\nbreadth\t";
cin>>b;

r2.setlength(l);
r2.setbreadth(b);

cout<<"\nR1:-";
r1.display();
p=r1.perimeter();
a=r1.area();
cout<<"\nperimeter="<<p<<"\narea="<<a;
cout<<"\n";

cout<<"\nR2:-";
r2.display();
p=r2.perimeter();
a=r2.area();
cout<<"\nperimeter="<<p<<"\narea="<<a;
cout<<"\n\n";
return 0;
}

Slip18
Implement a list library (doublylis.h) for a doubly linked list with the operations create, Insert,
display. Write a menu driven program to call these operations.
Create Insert at end, Display carries 5 mark each [15]
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include"dslip18.h"

void main()
{
int sch,n,i;
clrscr();
do
{
printf("\n\t*****DOUBLE LINK LIST MENU*****");
printf("\n\t\t1.CREATE");
printf("\n\t\t2.DISPLAY");
printf("\n\t\t3.INSEND");
printf("\n\t\t4.EXIT");
printf("\n\tEnter your choice:-");
scanf("%d",&sch);
switch(sch)
{
case 1 : printf("\n\tHow many nodes? ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
create();

}
break;
case 2: display();break;
case 3:inslast();break;
case 4: exit(0);
}

}while(sch!=7);
}
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *next,*prev;
};
struct node *temp,*q,*start=NULL,*end;
create()
{
int data;
temp = malloc(sizeof(struct node));
printf("Enter The Element\n");
scanf("%d",&data);
temp->info = data;
temp->next = NULL;
temp->prev = NULL;
if(start == NULL)
{
start=temp;
end= temp;
}
else
{
q= start;
while(q->next != NULL)
{
q = q->next;
}
q->next = temp;
temp->prev=q;
end = temp;

}
return;
}

display()
{
if( start == NULL)
{
printf("List Is Empty");
}
else
{
q = start;
while(q->next != NULL)
{
printf("%d\t",q->info);
q= q->next;
}
printf("%d\t",q->info);
}
return;
}

inslast()
{
int data;
temp = malloc(sizeof(struct node));
printf("Enter The Element\n");
scanf("%d",&data);
temp->info = data;
temp->next = NULL;
temp->prev = NULL;
if(start == NULL)
{
start=temp;
end= temp;
}
else
{
q= start;
while(q->next != NULL)
{
q = q->next;
}
q->next = temp;
temp->prev=q;
end = temp;

}
return;
}

Define a class Message with two data members one character pointer and an integer storing length
[Repeat]

Slip 19
Implement a list library (doublylis.h) for a doubly linked list with the operations create, Delete,
display. Write a menu driven program to call these operations.
Create ,delete at end , display operation carries 5 mark each

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include"dslip19.h"

void main()
{
int sch,n,i;
clrscr();
do
{
printf("\n\t*****DOUBLE LINK LIST MENU*****");
printf("\n\t\t1.CREATE");
printf("\n\t\t2.DISPLAY");
printf("\n\t\t3.DELLAST");
printf("\n\t\t4.EXIT");
printf("\n\tEnter your choice:-");
scanf("%d",&sch);
switch(sch)
{
case 1 : printf("\n\tHow many nodes? ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
create();

}
break;
case 2: display();break;
case 3:dellast();break;
case 4: exit(0);
}
}while(sch!=7);
}
// dslip19.h

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *next,*prev;
};
struct node *temp,*q,*start=NULL,*end;
create()
{
int data;
temp = malloc(sizeof(struct node));
printf("Enter The Element\n");
scanf("%d",&data);
temp->info = data;
temp->next = NULL;
temp->prev = NULL;
if(start == NULL)
{
start=temp;
end= temp;
}
else
{
q= start;
while(q->next != NULL)
{
q = q->next;
}
q->next = temp;
temp->prev=q;
end = temp;

}
return;
}

display()
{
if( start == NULL)
{
printf("List Is Empty");
}
else
{
q = start;
while(q->next != NULL)
{
printf("%d\t",q->info);
q= q->next;
}
printf("%d\t",q->info);
}
return;
}
dellast()
{
temp = end;
if(temp->prev==NULL)
{
free(temp);
start=NULL;
end =NULL;
return 0;
}
printf("Element Deleted %d",end->info);
end = temp->prev;
end->next =NULL;
free(temp);
return 0;
}

Write a function that checks whether a string of characters is palindrome or not. The function should
use a stack library (cststack.h) of stack of characters using a static implementation of the stack.
Push ,Pop operation carries 5 mark each [10]
Palindrome function [10]
Init,IsEmpty function [5]

Define a class named Complex for representing complex numbers.


[Repeat]

Slip20
Implement a list library (doublylis.h) for a doubly linked list with the operations create, search,
display. Write a menu driven program to call these operations.
Create ,search, display operation carries 5 mark each [15]
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include"dslip20.h"

void main()
{
int sch,n,i;
clrscr();
do
{
printf("\n\t*****DOUBLE LINK LIST MENU*****");
printf("\n\t\t1.CREATE");
printf("\n\t\t2.DISPLAY");
printf("\n\t\t3.SEARCH");
printf("\n\t\t4.EXIT");
printf("\n\tEnter your choice:-");
scanf("%d",&sch);
switch(sch)
{
case 1 : printf("\n\tHow many nodes? ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
create();

}
break;
case 2: display();break;
case 3:search();break;
case 4: exit(0);
}

}while(sch!=7);
}
//dslip20.h

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *next,*prev;
};
struct node *temp,*q,*start=NULL,*end;
create()
{
int data;
temp = malloc(sizeof(struct node));
printf("Enter The Element\n");
scanf("%d",&data);
temp->info = data;
temp->next = NULL;
temp->prev = NULL;
if(start == NULL)
{
start=temp;
end= temp;
}
else
{
q= start;
while(q->next != NULL)
{
q = q->next;
}
q->next = temp;
temp->prev=q;
end = temp;
}
return;
}

display()
{
if( start == NULL)
{
printf("List Is Empty");
}
else
{
q = start;
while(q->next != NULL)
{
printf("%d\t",q->info);
q= q->next;
}
printf("%d\t",q->info);
}
return;
}
search()
{
int data,cnt=1;
printf("Enter the Element You Want to Search");
scanf("%d",&data);
temp=start;
while(temp && temp->info !=data)
{
temp = temp->next;
cnt++;
}
if(temp)
{
printf("%d is at Position %d\n",data,cnt);
}
else
{
printf("No data found");
}
return;
}

Write a function that reverses a string of characters. The function should use a stack library
(cststack.h) of stack of characters using a static implementation of the stack.
Push ,Pop operation carries 5 mark each [10]
reverses function [10]

Define a class „Fraction‟ having integer data members numerator and denominator. Define
parameterized and default constructors (default values 0 and 1). Parameterized constructor should
store the fraction in reduced form after dividing both numerator and denominator by gcd(greatest
common divisor). Write a private function member to compute gcd of two integers. Write four
member functions for addition, subtraction. multiplication and division of fraction objects. Each
function will have two fraction objects as arguments.
gcd() function [3]
Default constructor and Parameterized constructor [1+4=5]
Addition, Subtraction and Division function - each carries 5 marks [15]
Multiplication() function [2]
Write the main function to illustrate the use of the class and member function. [

#include<iostream>
using namespace std;
class fraction
{
private:
int num,den;
int gcd(int &n1,int &d1)
{
int gcd;
for(int i=1;i<=n1 && i<=d1;i++)
{
if(n1%i==0 && d1%i == 0 )
gcd=i;
}
n1=n1/gcd;
d1=d1/gcd;
}
public:
fraction()
{
num=0;
den=1;
}
fraction(int n1,int d1)
{
num=n1;den=d1;gcd(n1,d1);
}
int add(fraction &);
int divide(fraction &);
int multiply(fraction &);
int subtract(fraction &);
fraction addition(fraction &);
void getval()
{
cout<<this->num;
cout<<this->den;
}
};
int fraction::add(fraction &f1)
{
int num1 = ((num * f1.den) + (den * f1.num));
int den1 = (den * f1.den);
gcd(num1,den1);
cout<<"ADDITION IS:"<<num1<<"/"<<den1<<endl;
}
int fraction::subtract(fraction &f1)
{
int num1 = ((num * f1.den) - (den * f1.num));
int den1 = (den * f1.den);
gcd(num1,den1);
cout<<"SUBTRACTION IS:"<<num1<<"/"<<den1<<endl;
}
int fraction::multiply(fraction &f1)
{
int num1 = (num * f1.num);
int den1 = (den * f1.den);
gcd(num1,den1);
cout<<"MULTIPLICATION IS:"<<num1<<"/"<<den1<<endl;
}
int fraction::divide(fraction &f1)
{
int num1 = (num * f1.den);
int den1 = (den * f1.num);
gcd(num1,den1);
cout<<"DIVISION IS:"<<num1<<"/"<<den1<<endl;
}
main()
{
int n1,d1,n2,d2;
cout<<"Enter Num and Den of 1st fraction: "<<endl;
cin>>n1>>d1;
cout<<"Enter Num and Den of 2nd fraction: "<<endl;
cin>>n2>>d2;
fraction f1(n1,d1),f2(n2,d2);
f1.add(f2);
f1.multiply(f2);
f1.divide(f2);
f1.subtract(f2);
}

Slip21

Implement a stack library (ststack.h) of integers using a static implementation of the stack and
implementing the Push,Pop,Isempty,IsFull operations. Write a menu driven program that includes
stack library and calls different stack operations.
Push & Pop operation carries 5 marks each [10]
Is Empty [2]
Is Full [3]
[Repeat]

Implement a list library (doublylis.h) for a doubly linked list with the operations : Create, insert,
delete, search, display. Write a menu driven program to call these operations.
Insert,Delete,create,search,display [6+6+5+4+4]

[Repeat]

Implement a class vector which contains integers in sorted order. The size of the vector varies so
the memory should be dynamically allocated. It should have three data members vectorarray – a
pointer to integer, maxsize – the maximum allocated size to take care of insertions and size – actual
size. Write member function:
a) Parametrized constructor & copy constructor-each carries 5 marks [10]
b) Set value at a particular position in vector [3]
c) To insert values in vector to keep it in sorted order [5]
d) Print the vector [2]
e) Implement the Write member functions to form intersection of two vectors. [5]
Write the main function to illustrate the use of the class and member function. [5]

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class Vector
{
int *data;
int maxsize,size;
public :
Vector() { maxsize=10; data=new int[maxsize]; size=0;}
Vector(int maxsize)
{
if( maxsize > 0 )
this->maxsize=maxsize;
else
this->maxsize = 10;
data=new int[maxsize]; size=0;
}
Vector(Vector &v)
{
this->data = new int[v.maxsize];
this->maxsize = v.maxsize;
this->size = v.size;
for(int i=0; i<v.size; ++i)
this->data[i] = v.data[i];
}
void Insert(int value)
{
if( size < maxsize )
data[size++] = value;
else
{
int *temp;
temp = data;
maxsize += 5;
data = new int[maxsize];
for(int i=0; i<size; i++)
data[i] = temp[i];
data[size++] = value;
delete[] temp;
}
int temp,j;
for(int i=1; i<this->size; ++i )
{
temp = data[i];
for(j = i-1; j >= 0 && data[j] > temp; --j )
data[j+1] = data[j];
data[j+1] = temp;
}
}
void Display()
{
cout << "maxsize :" << maxsize << "," << "actual size :" << size << endl;
cout << "data : (";
for(int i=0; i<size; i++)
cout << data[i] << "," ;
cout << ")" << endl << endl;
}
int get(int n)
{
if( n < size )
return data[n];
else
{
cout << "invalid index : " << n << endl;
return 0;
}
}
Vector InterSection(Vector &v2)
{
Vector v3;
int i,j,k;
for(i=0; i<this->size; ++i )
{
for(j=0; j<this->size; ++j )
{
if( this->data[i] == v2.data[j] )
{
for(k=0; k<v3.size; ++k )
{
if( v3.data[k] == v2.data[j] )
break;
}
if( k == v3.size )
{
v3.Insert(v2.data[j]);
break;
}
}
}
}
return v3;
}
};
main()
{
Vector v1,v2;
v2.Insert(60);
v2.Insert(90);
v2.Insert(30);
v2.Insert(50);
v1.Insert(10);
v1.Insert(50);
v1.Insert(30);
v1.Insert(20);
Vector v3;
v3 = v1.InterSection(v2);
cout << "v1 : ";
v1.Display();
cout << "v2 : ";
v2.Display();
cout << "intersection of v1 and v2 => v3 : ";
v3.Display();
}
Slip22
1 Implement a list library (doublylis.h) for a doubly linked list with the operations create, search,
display. Write a menu driven program to call these operations.
Create ,search, display operation carries 5 mark each [15]
[Repeat]

Implement a queue library (stqueue.h) of integers using a static implementation of the queue and
implementing the Insert,Delete,IsEmpty,IsFull,Init,Peek operations. Write a Menu driven program that includes
queue library and calls different queue operations.
Insert, Delete operation carries 8 mark each [16]
IsEmpty,IsFull function carries 3 mark each [6]
Init, Peek function [3]

//.c File

#include<stdio.h>
#include<stdlib.h>
#include "sts_queue.h"
#define MAX 30

main()
{
int ch,x,m;

while(1)
{
printf("\n 1 - Initialize queue");
printf("\n 2 - Insert element to rear of queue");
printf("\n 3 - Delete element from front of queue");
printf("\n 4 - Print front element of queue");
printf("\n 5 - Is empty queue");
printf("\n 6 - Is full queue");
printf("\n 7 - Exit \n");
printf("\n Enter choice : ");
scanf("%d", &ch);

switch(ch)
{
case 1:
printf("\n initializing queue....\n");
initialize();
break;
case 2:
printf("\n Enter element to add:");
scanf("%d",&x);
add(x);
printf("\n Added element is:%d \n",x);
break;

case 3:
printf("\n You have pressed delete....");
printf("\n\t %d gets deleted.........\n",a[f]);
del();
break;

case 4:
peek();
break;

case 5:
isempty(a);
break;

case 6:
isfull(a);
break;

case 7:
printf("\n You have pressed exit....");
exit(0);
break;

default:
printf("\n Wrong choice menu");

}
}
}

// sts_queue.h

#include<stdio.h>
#include<stdlib.h>
#define MAX 30

int a[30];
int r,f;

initialize()
{
r=-1;
f=-1;
}

add(int x)
{
if(r==-1)
{
r=f=0;
a[r]=x;
}
else
{
r=r+1;
a[r]=x;
}
}

del()
{
if(r==f)
{
r=-1;
f=-1;
}
else
{
a[f]=0;
f++;
}
}

peek()
{
printf("\n Element front is:%d \n",a[f]);
}

isempty()
{
if(r==(-1))
{
printf("\n ** queue is empty ** \n");
}
else
{
printf("\n ** queue is not empty ** \n");
}
}

isfull()
{
if(r==(MAX-1))
{
printf("\n ** queue is full ** \n");
}
else
{
printf("\n ** queue is not full ** \n");
}
}
Implement a class "printdata" to overload "print" function as follows:
[Repeat]

Slip 23
Create a random array of n integers. Accept a value x from user and use linear search algorithm to
check whether the number is present in the array or not and output the position if the number is
present.
[Repeat]

Write a C program which uses Binary search tree library and implements following Functions
a) Create Binary Search Tree [10]
b) Count a node in binary tree [5]
c) Search a node in binary tree [5]
d) Display a binary tree [5]

[Repeat]

Implement a class "invertdata" to overload "invert" function as follows:


[Repeat]

Slip 24
Implement a list library (doublylis.h) for a doubly linked list with the operations create, search,
display. Write a menu driven program to call these operations.
Create ,search,display operation carries 5 mark each

[Repeat]

Write a function that reverses a string of characters. The function should use a stack library
(cststack.h) of stack of characters using a static implementation of the stack.
Push ,Pop operation carries 5 mark each [10]
reverses function [10]
Init,IsEmpty function [5
[Repeat]

Implement a class "data" to overload following functions as follows:


[Repeat]

Slip25
Implement a list library (singlylist.h) for a singly linked list with the operations create, Delete,
display. Write a menu driven program to call these operations.
Create & Delete at in between, Display Operation Carries 5 each Marks. [15]
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

typedef struct node


{

int info;
struct node *next;
};
struct node*temp,*q,*start=NULL;

create()
{

int data;
temp = malloc(sizeof(struct node*));
printf("Enter the data\n");
scanf("%d",&data);
temp->info = data;
temp->next=NULL;
if(start==NULL)
{
start=temp;

}
else
{
q=start;
while(q->next != NULL)
{
q=q->next;
}
q->next = temp;

}
return;
}
display()
{

if(start==NULL)
{
printf("List Empty\n");
}
else
{
q = start;
while(q->next !=NULL)
{
printf("%d\t",q->info);
q = q->next;
}
printf("%d\n",q->info);
}
return;
}

del()
{
int x;
printf("\n\tWhich node to be deleted,\nEnter the info");
scanf("%d",&x);
q=start;
while(q->info!=x)
{
if(q->next==NULL)
{
printf("\n\tElement not found");
return;
}
else
{
temp=q;
q=q->next;
}
}
if(q==start)
{
start=q->next;
free(q);
}
else
{
if(q->next==NULL)
{
temp->next=NULL;
free(q);
}
else
{
temp->next=q->next;
free(q);
}
}
return;
}

void main()
{
int sch,n,i;
clrscr();
do
{
printf("\n\t*****SINGLY LINK LIST MENU*****");
printf("\n\t\t1.CREATE");
printf("\n\t\t2.DELETE");
printf("\n\t\t3.DISPLAY");
printf("\n\t\t4.EXIT");
printf("\n\tEnter your choice:-");
scanf("%d",&sch);
switch(sch)
{
case 1 : printf("\n\tHow many nodes? ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
create();

}
break;
case 2:del();break;
case 3: display();break;
case 4: exit(0);
}

}while(sch!=4);
}
Implement a queue library (stqueue.h) of integers using a static implementation of the queue and
implementing the insert,delete,isFull,Isempty,Init,Peek operations. Write a menu driven program that
includes queue library and calls different queue operations.
Insert, Delete operation carries 8 mark each [16]
IsEmpty,IsFull function carries 3 mark each [6]
Init ,Peek function [3]

[Repeat]

Define a class "Fraction" having integer data members numerator and denominator.

[Repeat]

Slip 26
1. Read the data from the file “employee.txt” and sort on age using insertion sort.
Reading & Display the file [10] Insertion sort Function [5]
[Repeat]

2. Implement a list library (doublylist.h) for a doubly linked list with the insert, delete search, display
operations. Write a menu driven program to call the operations.
Insert,delete,create,search,display [6+6+5+4+4]

[Repeat]

Define a class named Clock with three integer data members for hours, minutes and seconds.

[Repeat]

You might also like