Professional Documents
Culture Documents
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 *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;
#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 '_';
}
};
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();
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]
#include<iostream>
#include<string.h>
using namespace std;
class printdata
{
public:
void print(int no)
{
cout<<"\n<"<<no<<">";
}
}
}
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];
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>
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);
}
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");
}
}
#include<iostream>
#include<string.h>
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];
if((fp=fopen("emp.txt","r"))!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}
}
return(i-1);
}
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;
scanf("%d",&n);
for(j=0;j<n;j++)
scanf("%d",&g[i][j]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
printf("\t %d ",g[i][j]);
printf("\n");
scanf("%d",&in);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
indegree++;
}
printf("node=%d\t indegree=%d",in,indegree);
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];
fscanf(fp,"%d%s",&a[i].age,a[i].name);
i++;
}
} return (i-1);
}
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]
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>
}
};
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 *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;
}
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 *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);
}
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;
}
}
}
}
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);
}
#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 *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]
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};
}
}
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;
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>
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>
}
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);
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;
}
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;
}
[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]
[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;
}
void display()
{
cout<<"\nlength="<<length<<"\nbreadth="<<breadth;
}
};
int main()
{
rectangle r1,r2;
float l,b,p,a;
r1.setlength(l);
r1.setbreadth(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]
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]
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]
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>
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]