Dsa CPP & SQL Record
Dsa CPP & SQL Record
&
DBMS
Dr. APJ ABDUL KALAM COLLEGE OF PROFESSIONAL
STUDIES, ULIKKAL KANNUR
Certificate
This is to certify that this is a bona-fide record work done by ____________
Reg. No _____________ in partial fulfilment of the requirements for the
provision of BCA Degree during fourth semester.
Examiner 1 : Principal
Examiner 2 :
Place :
Date :
INDEX
Sl No Program Page No
Sl No Program Page No
1 SQL 1 55
2 SQL 2 58
3 SQL 3 61
4 SQL 4 66
5 SQL 5 69
6 SQL 6 73
1. Program to Add Two Polynomials.
Program :
#include<iostream>
using namespace std;
class poly
{
public:
int poly1[20],poly2[20],poly3[20];
int i,deg1,deg2,deg3;
void read();
};
void poly::read()
{
cout<<"enter degree of 1st poly:";
cin>>deg1;
cout<<"enter degree of 2nd poly:";
cin>>deg2;
cout<<"\n";
cout<<"for first polynomial:\n";
for(i=0;i<=deg1;i++)
{
cout<<"enter coeffiecient of"<<i<<"::";
cin>>poly1[i];
}
cout<<"\n";
cout<<"for second polynomial:\n";
for(i=0;i<=deg2;i++)
{
cout<<"enter coefficient of"<<i<<"::";
cin>>poly2[i];
}
cout<<"\n polynomial 1:\n";
for(i=deg1;i>=0;i--)
{
cout<<poly1[i]<<"x^"<<i;
if(i>0)
{
cout<<"+";
}
}
cout<<"\n";
cout<<"\n polynomial 2:\n";
for(i=deg2;i>=0;i--)
{
cout<<poly2[i]<<"x^"<<i;
if(i>0)
5
{
cout<<"+";
}
deg3=(deg1>deg2)?deg1:deg2;
for(i=0;i<=deg3;i++)
{
poly3[i]=poly1[i]+poly2[i];
}
cout<<"\n";
cout<<"\nPolynomial after addition:\n";
for(i=deg3;i>=0;i--)
{
cout<<poly3[i]<<"x^"<<i;
if(i>0)
{
cout<<"+";
}
}
}
int main()
{
poly po;
po.read();
return 0;
}
6
Output :
polynomial 1:
10x^3+5x^2+3x^1+4x^0
polynomial 2:
20x^3+8x^2+7x^1+6x^0
7
2. Sequential and Binary search : Print number of comparison in each case for given
datasets.
Program :
#include<iostream>
using namespace std;
static int flag=0;
class search
{
int a[50],beg,end,loc,mid,i,j,n,item,temp;
public:
void read();
void display();
void linearsearch();
void binarysearch();
};
void search::read()
{
cout<<"\nEnter the limit :";
cin>>n;
cout<<"\nEnter the array element : ";
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void search::display()
{
cout<<"\nArray elements are : ";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
}
void search::linearsearch()
{
int count1=0;
loc=-1;
cout<<"\nenter the elements to be searched :";
cin>>item;
for(i=0;i<n;i++)
{
count1=count1+1;
if(a[i]==item)
8
{
loc=i;
break;
}
}
if(loc>=0)
{
cout<<"\nSearch is successfull\n";
cout<<"Search item is found at position "<<loc+1<<"\n";
}
else
{
cout<<"\nSearch is Unsuccessfull \n";
}
void search::binarysearch()
{
int count2=0;
cout<<"\nEnter the item to be Searched :";
cin>>item;
cout<<"\nBefore performing binary search first sort it :";
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
display();
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
count2=count2+1;
if(item==a[mid])
{
cout<<"\nSearch is successful\n";
loc=mid;
9
cout<<"\n "<<item<<" is found at position "<<loc+1;
flag=1;
break;
}
else if(item<a[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
}
if(flag==0)
{
cout<<"\n Search is Unsuccessfull\n";
}
cout<<" Number of comparison is :"<<count2;
}
int main()
{
search obj;
//int c;
//char ch;
obj.read();
obj.display();
cout<<"\nLinear Search";
obj.linearsearch();
cout<<"\nBinary Search";
obj.binarysearch();
return 0;
}
10
Output :
Search is successfull
Search item is found at position 5
Number of comparison is :5
Binary Search
Enter the item to be Searched :56
11
3. Insertion Sort : number of comparisons and exchanges for given data sets.
Program :
#include<iostream>
using namespace std;
class insertionsort
{
int a[20],i,k,n,j,temp;
public:
void sort();
};
void insertionsort::sort()
{
cout<<"Enter the limit :\n";
cin>>n;
cout<<"Enter the array elements :\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(k=0;k<n;k++)
{
temp=a[k];
j=k-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
cout<<"Array after sorting :\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
}
int main()
{
insertionsort ins;
ins.sort();
return 0;
}
12
Output :
13
4. Bubble Sort : Print number of comparisons and exchanges for given data sets.
Selection sort : Print number of comparisons and exchanges for given data sets.
Program :
#include<iostream>
using namespace std;
class sort
{
int a[10],i,j,n,temp,min,loc;
public:
void read();
void display();
void bubble();
void selection();
};
void sort::read()
{
cout<<"enter the limit :\n";
cin>>n;
cout<<"enter the values :\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void sort::display()
{
cout<<"sorted array is :\n";
for(i=0;i<n;i++)
{
cout<<a[i]<<"\n";
}
}
void sort::bubble()
{
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
14
}
}
void sort::selection()
{
for(i=0;i<n;i++)
{
min=a[i];
loc=i;
for(j=i+1;j<n;j++)
{
if(a[j]<min)
{
min=a[j];
loc=j;
}
}
if(loc!=i)
{
temp=a[i];
a[i]=a[loc];
a[loc]=temp;
}
}
}
int main()
{
sort obj;
obj.read();
cout<<"BUBBLE SORT :\n";
obj.bubble();
obj.display();
cout<<"SELECTION SORT :\n";
obj.selection();
obj.display();
return 0;
}
15
Output :
16
5. Quick sort.
Program :
#include<iostream>
using namespace std;
class quicksort
{
public:
int a[20],i,size,j,temp,pivot;
void quick(int a[10],int first,int last);
};
int main()
{
int size,i,a[20];
quicksort qs;
cout<<"enter the size :";
cin>>size;
cout<<"enter the elements :";
for(i=0;i<size;i++)
{
cin>>a[i];
}
qs.quick(a,0,size-1);
cout<<"sorted array is :";
for(i=0;i<size;i++)
{
cout<<a[i]<<"\t";
}
return 0;
}
void quicksort::quick(int a[10],int first,int last)
{
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(a[i]<=a[pivot]&&i<last)
{
i++;
}
while(a[j]>a[pivot])
{
j--;
}
17
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
quick(a,first,j-1);
quick(a,j+1,last);
}
}
Output :
18
6. Stack Operation : Addition and deletion of elements.
Program :
#include<iostream>
#define SIZE 20
using namespace std;
class stacks
{
char a[SIZE];
int top;
public:
stacks()
{
top=-1;
}
void push(char c);
char pop();
void display();
};
void stacks::push(char c)
{
if(top==(SIZE-1))
{
cout<<"Stack is full";
return;
}
else
{
top++;
a[top]=c;
}
}
char stacks::pop()
{
if(top==-1)
{
return 0;
}
else
{
char data=a[top];
top--;
return data;
}
}
void stacks::display()
{
19
if(top==-1)
cout<<"stack is empty\n";
for(int i=0;i<=top;i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";
}
int main()
{
int menu;
char value;
stacks st;
char yesORno;
do
{
cout<<"1.push 2.pop 3.display 4.exit\n";
cin>>menu;
switch(menu)
{
case 1:cout<<"Enter the character to push :\n";
cin>>value;
st.push(value);
break;
case 2:char p;
p=st.pop();
cout<<"popped element is :";
cout<<p;
cout<<"\n";
break;
case 3:st.display();
break;
case 4:cout<<"exit\n";
break;
case 5:cout<<"invalid";
break;
}
cout<<"do you want to continue:y/n\n";
cin>>yesORno;
}while(yesORno=='y'||yesORno=='Y');
}
20
Output :
21
7. Queue operation : Addition and Deletion of Elements
Program :
#include<iostream>
#define MAX 20
using namespace std;
class queue
{
int front,rear;
int q[MAX];
public:
queue()
{
front=rear=-1;
}
void qinsert(int item);
int qdelete();
void display();
};
void queue::qinsert(int item)
{
if(rear==MAX-1)
{
cout<<"overflow";
}
else if(front==-1 && rear==-1)
{
front=0;
rear=0;
q[rear]=item;
cout<<"item inserted";
}
else
{
rear++;
q[rear]=item;
cout<<"item inserted";
}
}
int queue::qdelete()
{
int item;
if (rear==-1)
{
cout<<"Queue underflow";
}
else if(front==0 && rear==0)
22
{
item=q[front];
rear=front=-1;
cout<<"Item deleted";
}
else
{
item=q[front];
front++;
cout<<"item deleted";
}
return item;
}
void queue::display()
{
if(front==-1)
{
cout<<"empty";
}
else
{
for(int i=front;i<=rear;i++)
{
cout<<q[i];
}
}
}
int main()
{
int menu;
int value;
queue qu;
char yesORno;
do
{
cout<<"1.Enqueue 2.Dequeue 3.Display(traversal) 4.exit\n";\
cin>>menu;
switch(menu)
{
case 1:cout<<"enter the character to insert :\n";
cin>>value;
qu.qinsert(value);
break;
case 2:int p;
p=qu.qdelete();
cout<<"deleted element is : ";
cout<<p;
cout<<"\n";
break;
case 3:qu.display();
23
break;
case 4:cout<<"exit\n";
break;
case 5:cout<<"Invalid";
break;
}
cout<<"\nDo you want to continue:y/n\n";
cin>>yesORno;
}while(yesORno=='y'||yesORno=='Y');
return 0;
}
Output :
24
8. Conversion of Infix expression to Postfix .
Program :
#include<iostream>
#define SIZE 20
using namespace std;
class stacks
{
char a[SIZE];
int top;
public:
stacks()
{
top=-1;
}
void push(char ch);
char pop();
bool isempty()
{
if(top==-1)
{
return true;
} else
{
return false;
}
}
char top_st()
{
if(top==-1)
{
return '\0';
}
else
{
return a[top];
}
}
};
void stacks::push(char ch)
{
if(top==(SIZE-1))
{
cout<<"stack is full";
return;
25
}
else
{
top++;
a[top]=ch;
}
}
char stacks::pop()
{
if(top==-1)
{
return '\0';
}
else
{
char data=a[top];
top--;
return data;
}
}
class conv
{
char in[SIZE];
char pos[SIZE];
stacks obj;
public:
void read();
void display();
void convert();
int priority(char ch);
};
void conv::read()
{
cout<<"\nEnter the infix expression : ";
cin>>in;
}
void conv::display()
{
cout<<"\n Infix expression : "<<in<<"\n";
cout<<"\nPostfix expression : "<<pos<<"\n";
}
int conv::priority(char ch)
{
if(ch=='^')
{
return 3;
}
else if((ch=='%')||(ch=='*')||(ch=='/'))
26
{
return 2;
}
else if((ch=='+')||(ch=='-'))
{
return 1;
}
else
{
return 0;
}
}
void conv::convert()
{
char ch,t;
int i=0,j=0;
while(in[i]!='\0')
{
ch=in[i];
if(in[i]=='(')
{
obj.push(in[i]);
}
else if((in[i]=='%')||(in[i]=='/')||(in[i]=='*')||(in[i]=='+')||(in[i]=='-')||(in[i]=='^'))
{
if(!obj.isempty())
{
t=obj.top_st();
while((priority(t))>=(priority(ch)))
{
pos[j]=obj.pop(); j++;
if(!obj.isempty())
{
t=obj.top_st();
}
else
{
break;
}
}
}
obj.push(in[i]);
}
else if(ch==')')
{
if(!obj.isempty())
{
while(obj.top_st()!='(')
27
{
pos[j]=obj.pop();
j++;
}
t=obj.pop();
}
else
{
break;
}
}
else
{
pos[j]=ch; j++;
} i++;
}
while(obj.isempty()==false)
{
pos[j]=obj.pop(); j++;
}
pos[j]='\0';
}
int main()
{
conv c;
c.read();
c.convert();
c.display();
return 0;
}
Output :
28
9. Menu driven Program : to add / delete elements to a circular queue. Include
necessary error message.
Program :
#include<iostream>
using namespace std;
#define MAXSIZE 10
class queue
{
int q[MAXSIZE];
int rear,front;
public:
queue( )
{
rear=-1;
front=-1;
}
void insert( );
void delet( );
void display();
};
void queue::insert()
{
int item;
if(front==(rear+1)%MAXSIZE)
{
cout<<"queue is overflow :";
return;
}
else
{
cout<<"enter the element :";
cin>>item;
rear=(rear+1)%MAXSIZE;
q[rear]=item;
if(front==-1)
{
front++;
}
}
}
void queue::delet()
{
if(front==-1)
{
cout<<"queue is empty";
}
29
else
{
cout<<"deleted element is :"<<q[front];
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front=(front+1)%MAXSIZE;
}
}
}
void queue::display()
{
int i;
cout<<"\n";
if(front==-1)
{
cout<<"queue is empty";
}
else if(rear>=front)
{
for(i=front;i<=rear;i++)
{
cout<<q[i]<<"\t";
}
}
else
{
for(i=front;i<=MAXSIZE-1;i++)
{
cout<<q[i]<<"\t";
}
for(i=0;i<=rear;i++)
{
cout<<q[i]<<"\t";
}
}
}
int main()
{
queue cq;
int ch;
do
{
cout<<"\n";
cout<<"menu:\n1.insert\n2.delete\n3.display\n4.exit\n\n";
cin>>ch;
30
switch(ch)
{
case 1:cq.insert();
break;
case 2:cq.delet();
break;
case 3:cq.display();
break;
}
}
while(ch!=4);
return 0;
}
Output :
menu:
1.insert
2.delete
3.display
4.exit
1
enter the element :4
menu:
1.insert
2.delete
3.display
4.exit
1
enter the element :8
menu:
1.insert
2.delete
3.display
4.exit
4 8
31
menu:
1.insert
2.delete
3.display
4.exit
2
deleted element is :4
menu:
1.insert
2.delete
3.display
4.exit
8
menu:
1.insert
2.delete
3.display
4.exit
------------------
(program exited with code: 0)
32
10 . Single Linked list operations : Add a new node at the beginning, at the end, after
the node, delete from begining, end , print the list.
Program :
#include<iostream>
using namespace std;
struct node
{
int info;
node *next;
};
node*start;
class slink
{
public:
void insertbeg();
void insertend();
void insertspec();
void display();
void search();
void count();
void delbeg();
void delend();
void delspec();
};
void slink::insertbeg()
{
node *ptr;
ptr=new node;
cout<<"\nEnter the info field :";
cin>>ptr->info;
if(start==NULL)
{
ptr->next=NULL;
}
else
{
ptr->next=start;
}
start=ptr;
}
void slink::insertend( )
{
node *ptr,*loc; ptr=new node;
cout<<"\nEnter the info field :";
cin>>ptr->info;
33
ptr->next=NULL;
if(start==NULL)
{
start=ptr;
}
else
{
loc=start;
while(loc->next!=NULL)
{
loc=loc->next;
}
loc->next=ptr;
}
}
void slink::insertspec()
{
node*ptr,*temp;
int i,loc;
ptr=new node;
cout<<"\nEnter the info field :";
cin>>ptr->info;
cout<<"\nEnter the position :";
cin>>loc;
temp=start;
for(i=0;i<loc-1;i++)
{
temp=temp->next;
}
ptr->next=temp->next;
temp->next=ptr;
}
void slink::display()
{
node *ptr;
ptr=start;
while(ptr!=NULL)
{
cout<<ptr->info<<"->"; ptr=ptr->next;
}
}
void slink::search()
{
node*ptr;
int item,flag=0; cout<<"Enter the item to be searched :";
cin>>item;
ptr=start;
while(ptr!=NULL)
{
34
if(ptr->info==item)
{
flag=1;
break;
}
ptr=ptr->next;
}
if(flag==1)
{
cout<<"Search is success";
}
else
{
cout<<"Search is unsuccessful";
}
}
void slink::count()
{
node *ptr;
int count=0;
ptr=start;
while(ptr!=NULL)
{
count++;
ptr=ptr->next;
}
cout<<"No of node is : "<<count;
}
void slink::delbeg()
{
node *ptr;
if(start==NULL)
{
cout<<"list is empty";
return;
}
else
{
ptr=start;
start=ptr->next;
delete ptr;
}
}
void slink::delend()
{
node *ptr,*loc;
if(start==NULL)
{
cout<<"list is empty";
return;
35
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
loc=ptr; ptr=ptr->next;
}
loc->next=NULL;
delete ptr;
}
}
void slink::delspec()
{
node *ptr,*save;
int i,loc;
cout<<"Enter the position :";
cin>>loc;
ptr=start;
for(i=1;i<loc;i++)
{
save=ptr;
ptr=ptr->next;
}
save->next=ptr->next;
delete ptr;
}
int main()
{
slink obj;
int ch;
char c;
do
{
cout<<"\nMenu\n1.insert at beginning\n2.insert at end\n3.insert at a
position\n4.display\n5.search\n6.count\n7.delete from beg\n8.delete from end\n9.delete from a
position";
cout<<"\n\nEnter your choice :";
cin>>ch;
switch(ch)
{
case 1:obj.insertbeg();
break;
case 2:obj.insertend();
break;
case 3:obj.insertspec();
break;
case 4:obj.display();
break;
case 5:obj.search();
36
break;
case 6:obj.count();
break;
case 7:obj.delbeg();
break;
case 8:obj.delend();
break;
case 9:obj.delspec();
break;
default:cout<<"invalid entry !";
}
cout<<"\nDo you want to continue? press y/n : ";
cin>>c;
}
while(c=='y'||c=='Y');
return 0;
}
Output :
Menu
1.insert at beginning
2.insert at end
3.insert at a position
4.display
5.search
6.count
7.deletefrombeg
8.delete fromend
9.delete from a position
Menu
1.insert at beginning
2.insert at end
3.insert at a position
4.display
5.search
6.count
37
7.deletefrombeg
8.delete fromend
9.delete from a position
Menu
1.insert at beginning
2.insert at end
3.insert at a position
4.display
5.search
6.count
7.deletefrombeg
8.delete fromend
9.delete from a position
38
11. Circular Linked list : Add a new node at the beginning , at the end, after the node,
delete from beginning, end, print the list.
Program :
#include<iostream>
using namespace std;
struct node
{
int info;
node *next;
};
node *start,*last;
class clink
{
public:
void insertbeg();
void insertend();
void insertspec();
void display();
void delbeg();
void delend();
void delspec();
};
void clink::insertbeg()
{
node *ptr;
ptr=new node;
cout<<"\nEnter the info field :";
cin>>ptr->info;
if(start==NULL)
{
start=ptr;
ptr->next=start;
last=ptr;
}
else
{
ptr->next=start;
start=ptr;
last->next=ptr;
}
}
void clink::insertend()
{
node*ptr;
ptr=new node;
cout<<"\nEnter the info field :";
39
cin>>ptr->info;
if(start==NULL)
{
start=ptr;
ptr->next=start;
last=ptr;
}
else
{
last->next=ptr;
ptr->next=start;
last=ptr;
}
}
void clink::insertspec()
{
node *ptr ,*temp;
int i,loc;
ptr=new node;
cout<<"\nEnter the info field :";
cin>>ptr->info;
cout<<"\nEnter the position :";
cin>>loc;
temp=start;
for(i=1;i<loc-1;i++)
{
temp=temp->next;
temp->next=ptr;
}
}
void clink::display()
{
node *ptr;
ptr=start;
if(start==NULL)
{
cout<<"\nLink is Empty";
return;
}
else
{
do
{
cout<<ptr->info<<"->";
ptr=ptr->next;
}
while(ptr!=start);
}
}
40
void clink::delbeg()
{
node *ptr;
ptr=start;
if(start==NULL)
{
cout<<"List is Empty";
return;
}
else if(start==last)
{
start=last=NULL;
delete ptr;
}
else
{
start=start->next;
last->next=start;
delete ptr;
}
}
void clink::delend()
{
node *ptr,*temp;
ptr=start;
if(start==NULL)
{
cout<<"list is empty";
return;
}
else if(start==last)
{
start=last=NULL;
delete ptr;
}
else
{
while(ptr->next!=start)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=start;
last=temp;
delete ptr;
}
}
void clink::delspec()
{
node *ptr,*save;
41
int i,loc;
cout<<"Enter the position :";
cin>>loc;
ptr=start;
for(i=1;i<loc;i++)
{
save=ptr;
ptr=ptr->next;
}
save->next=ptr->next;
delete ptr;
}
int main()
{
clink obj;
int ch;
char c;
do
{
cout<<"\nMenu\n1.Insert at beg\n2.Insert at end\n3.insert a
position\n4.display\n5.delete from beg\n6.delete from end\n7. delete from a position";
cout<<"\n\nEnter your choice :";
cin>>ch;
switch(ch)
{
case 1:obj.insertbeg();
break;
case 2:obj.insertend();
break;
case 3:obj.insertspec();
break;
case 4:obj.display();
break;
case 5:obj.delbeg();
break;
case 6:obj.delend();
break;
case 7:obj.delspec();
break;
default:cout<<"Invalid entry";
}
cout<<"\nDo you want to continue ? press y/n :";
cin>>c;
}
while(c=='y'||c=='Y');
return 0;
}
42
Output :
Menu
1.Insert at beg
2.Insert at end
3.insert a position
4.display
5.delete from beg
6.delete from end
7. delete from a position
Menu
1.Insert at beg
2.Insert at end
3.insert a position
4.display
5.delete from beg
6.delete from end
7. delete from a position
Menu
1.Insert at beg
2.Insert at end
3.insert a position
4.display
5.delete from beg
6.delete from end
7. delete from a position
Menu
1.Insert at beg
2.Insert at end
3.insert a position
43
4.display
5.delete from beg
6.delete from end
7. delete from a position
Menu
1.Insert at beg
2.Insert at end
3.insert a position
4.display
5.delete from beg
6.delete from end
7. delete from a position
44
12. Doubly Linked list : Add a new node at the beginning , at the end, after the node,
delete from beginning, end, print the list.
Program :
#include<iostream>
using namespace std;
struct node
{
int info;
node *next,*prev;
};
node *start,*last;
class doublylink
{
public:
void insertbeg( );
void insertend( );
void insertspec( );
void delbeg( );
void delend( );
void delspec( );
void display( );
};
void doublylink::insertbeg( )
{
node *ptr;
ptr=new node;
cout<<"Enter the info field : ";
cin>>ptr->info;
ptr->prev=NULL;
if(start==NULL)
{
ptr->next=NULL;
start=ptr;
last=ptr;
}
else
{
start->prev=ptr;
ptr->next=start;
start=ptr;
}
}
void doublylink::insertend( )
{
node *ptr;
ptr=new node;
45
cout<<"Enter the info field : ";
cin>>ptr->info;
ptr->next=NULL;
if(start==NULL)
{
ptr->prev=NULL;
start=last=ptr;
}
else
{
ptr->prev=last;
last->next=ptr;
last=ptr;
}
}
void doublylink::insertspec( )
{
node *ptr,*save,*temp;
int i,loc;
ptr=new node;
temp=start;
cout<<"Enter the info field : ";
cin>>ptr->info;
cout<<"Enter the location : ";
cin>>loc;
for(i=1;i<loc;i++)
{
save=temp;
temp=temp->next;
}
save->next=ptr;
ptr->prev=save;
ptr->next=temp;
temp->prev=ptr;
}
void doublylink::delbeg( )
{
node *ptr;
if(start==NULL)
{
cout<<"List empty";
return;
}
else
{
ptr=start;
start=start->next;
start->prev=NULL;
delete ptr;
}
46
}
void doublylink::delend( )
{
node *ptr;
if(start==NULL)
{
cout<<"List empty";
return;
}
else
{
ptr=last;
last=last->prev;
last->next=NULL;
delete ptr;
}
}
void doublylink::delspec( )
{
node *save,*temp,*ptr;
int i,loc;
ptr=start;
cout<<"Enter the location : ";
cin>>loc;
if(start==NULL)
{
cout<<"List empty";
return;
}
else
{
for(i=1;i<loc;i++)
{
temp=ptr;
ptr=ptr->next;
}
save=ptr->next;
temp->next=save;
save->prev=temp;
delete ptr;
}
}
void doublylink::display( )
{
node *ptr;
ptr=start;
while(ptr!=NULL)
{
cout<<ptr->info<<"->";
ptr=ptr->next;
47
}
}
int main( )
{
doublylink obj;
int ch,c;
do
{
cout<<"1.Insert at beg\n2.insert at end\n3.insert at specific positon\n4.Delete at beg\n5.Delete
at end\n6.Delete at specific location\n7.Display\n";
cout<<"\n\nEnter your choice :";
cin>>ch;
switch(ch)
{
case 1: obj.insertbeg( );
break;
case 2:obj.insertend( );
break;
case 3:obj.insertspec( );
break;
case 4:obj.delbeg( );
break;
case 5:obj.delend( );
break;
case 6: obj.delspec( );
break;
case 7: obj.display( );
break;
default:cout<<"Invalid entry";
}
cout<<"\nDo you want to continue ? Press 1 : ";
cin>>c;
}
while(c==1);
return 0;
}
48
Output :
1.Insert at beg
2.insert at end
3.insert at specific positon
4.Delete at beg
5.Delete at end
6.Delete at specific location
7.Display
49
13. Implement Tree Traversal.
Program :
#include<iostream>
using namespace std;
struct node
{
int data;
struct node *left;
struct node *right;
};
void preorder(struct node *root)
{
if(root==NULL)
{
return;
}
cout<<root->data<<"\t";
preorder(root->left);
preorder(root->right);
}
void inorder(node *root)
{
if(root==NULL)
{
return;
}
inorder(root->left);
cout<<root->data<<"\t";
inorder(root->right);
}
void postorder(node *root)
{
if(root==NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
cout<<root->data<<"\t";
}
node *insert(node *root,int data)
{
if(root==NULL)
{
root=new node( );
root->data=data;
50
root->left=root->right=NULL;
}
else if(data<=root->data)
{
root->left=insert(root->left,data);
}
else
{
root->right=insert(root->right,data);
}
return root;
}
int main( )
{
node *root=NULL;
root=insert(root,2);
root=insert(root,10);
root=insert(root,22);
root=insert(root,8);
root=insert(root,11);
root=insert(root,30);
cout<<"Preorder";
preorder(root);
cout<<"\n";
cout<<"Inorder";
inorder(root);
cout<<"\n";
cout<<"Postorder";
postorder(root);
cout<<"\n";
return 0;
}
Output :
Preorder2 10 8 22 11 30
Inorder2 8 10 11 22 30
Postorder8 11 30 22 10 2
51
14. Merge Two Sorted Linked list.
Program :
#include<iostream>
using namespace std;
struct Node
{
int data;
struct Node *next;
};
struct Node *newNode(int key)
{
struct Node *temp = new Node;
temp->data = key;
temp->next = NULL;
return temp;
}
void printList(struct Node *node)
{
while (node != NULL)
{
cout<< node->data<<"->";
node = node->next;
}
}
struct Node *mergeUtil(struct Node *h1, struct Node *h2)
{
if (!h1->next)
{
h1->next = h2;
return h1;
}
struct Node *curr1 = h1, *next1 = h1->next;
struct Node *curr2 = h2, *next2 = h2->next;
while (next1 && next2)
{
if ((curr2->data) > (curr1->data) && (curr2->data) < (next1->data))
{
next2 = curr2->next;
curr1->next = curr2;
curr2->next = next1;
curr1 = curr2;
curr2 = next2;
}
else
{
if (next1->next)
52
{
next1 = next1->next;
curr1 = curr1->next;
}
else
{
next1->next = curr2;
return h1;
}
}
}
return h1;
}
struct Node *merge(struct Node *h1, struct Node *h2)
{
if (!h1)
{
return h2;
}
if (!h2)
{
return h1;
}
if (h1->data < h2->data)
{
return mergeUtil(h1, h2);
}
else
{
return mergeUtil(h2, h1);
}
}
int main( )
{
struct Node *head1 = newNode(1);
head1->next = newNode(3);
head1->next->next = newNode(5);
struct Node *head2 = newNode(0);
head2->next = newNode(2);
head2->next->next = newNode(4);
struct Node *mergedhead = merge(head1, head2);
printList(mergedhead);
return 0;
}
Output :
0->1->2->3->4->5->
53
DBMS
54
SQL 1
1. Create table students with fields sno, sname, sex, mark with sno as primary key and
assign suitable constraints for each attribute .
QUERIES
use college;
55
alter table students add column grade varchar(20);
56
select sno,sname from students where mark>(select avg(mark) from students)order by mark DESC;
57
SQL 2
2. Create a table department with fields ename, salary, dno, dname, place with dno as
primary key.
QUERIES
58
2) select ename from department where salary>6000 and salary<10000;
4) select ename from department where salary between 5000 and 10000;
59
select * from starz;
60
SQL 3
3. Create a table department with fields dno, dname, dmanager and place with dno as
primary key.
Create a table emp with fields eno, ename, job, dno, salary, with eno as primary key .
Set dno as foreign key. insert five records into each table.
QUERIES
61
create table emp(eno int primary key,ename varchar(20),job varchar(20),salary int,dno int,foreign
key(dno) references departments(dno));
62
3) select dmanager from departments where dname=‘Accounting’;
63
select min(salary) as minimum_salary from emp;
6) select 5+2;
64
select 10-5;
select 6/2;
select 10%3;
select 6*5;
65
SQL 4
4. Create a table emp with fields eno,ename, job, manager and salary, with eno as
primary key.
Insert values into the table.
1.Display ename, salary from emp who are getting salary more than average salary of
the organization.
2. ADD 20%DA as extra salary to all employees. Label the column as 'New Salary'
3. Create a query to display the eno and ename for all employees who eam more than
the average salary. Sort the results in descending order of salary.
4. Create a view called emp_view based on the eno, ename from emp table change the
heading for the ename to 'EMPLOY'.
5. Write a query that will display the eno and ename for all employees whose name
contains a 'T'.
QUERIES
66
1) select ename,salary from emp where salary>(select avg(salary) from emp);
3) select eno,ename from emp where salary>(select avg(salary) from emp) order by salary DESC;
67
5) select eno,ename from emp where ename like ‘%T%’;
68
SQL 5
5. Create a table department with fields dno, ename, salary, Designation, dname and
place with dno as primary key. Insert values into the table.
2. Create a query to display the employee number and name for all employees who
earn more than the average salary. Sort the results in descending order of salary.
4. Display ename, salary, Designation for those who got salary more than 5000 or his
Designation is 'clerk'.
5. Display ename and designation those who are not a clerk or manager.
6. Display the names of all employees where the third letter of their name is an 'A'.
QUERIES
create table department ( dno int primary key, ename varchar(50), salary int, designation
varchar(50), dname varchar(50), place varchar(50));
insert into department values(1, 'Akshara', 7000, 'Manager', 'HR', 'New York');
insert into department values(2, 'Nandhu', 8500, 'Developer', 'IT', 'San Francisco');
insert into department values(4, 'Alan', 9500, 'Analyst', 'Marketing', 'Los Angeles');
insert into department values(5, 'Aman', 5000, 'Sales person', 'Sales', 'Houston');
69
1. select upper(ename) from department;
70
select concat(ename, ' - ', designation) as employee_info from department;
2.select dno, ename, salary from department where salary > (select avg(salary) from department)
order by salary DESC;
4.select ename, salary, designation from department where salary > 5000 or designation = 'Clerk';
71
5.select ename, designation from department where designation not in ('Clerk', 'Manager');
72
SQL 6
6. Create a table Customer with fields cid, cname, date_of_birth and place Create table
loan with fields loanno, cid and bname assigning suitable constraints.
Create table depositor with fields accno, cid, balance and bname assigning suitable
constraints.
Insert 5 Records into each table.
1. Add one more field amount to loan table. Update each record. Display cname
forcid=2.
2. Calculate Rs 150 extra for all customers having loan. The added loan amount will
display in a new column.
3.Display loanno, cname and place of a customer who is residing in Kannur city.
QUERIES
create table customer (cid int primary key,cname varchar(50),date_of_birth date,place varchar(50));
create table Loan (loanno int primary key,cid int,bname varchar(50) ,foreign key (cid) references
customer(cid));
create table Depositor (accno int primary key,cid int, balance int,bname varchar(50) ,foreign key
(cid) references customer(cid));
73
insert into Loan values (101, 1, 'Bank A');
74
select * from Depositor;
75
2.select loanno, cid, amount, (amount + 150) AS updated_amount from Loan;
76
5.select distinct customer.cid, customer.cname from customer join Loan on customer.cid = Loan.cid
join Depositor on customer.cid = Depositor.cid;
77