[go: up one dir, main page]

0% found this document useful (0 votes)
8 views77 pages

Dsa CPP & SQL Record

The document is a practical record for the Fourth Semester BCA program at Dr. APJ Abdul Kalam College of Professional Studies, detailing various programming assignments in Data Structures using C++ and DBMS. It includes a certificate section, an index of programs, and specific implementations for polynomial addition, searching algorithms, sorting algorithms, stack operations, and queue operations. Each program is accompanied by its aim, code, and sample output.

Uploaded by

yapaw47132
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views77 pages

Dsa CPP & SQL Record

The document is a practical record for the Fourth Semester BCA program at Dr. APJ Abdul Kalam College of Professional Studies, detailing various programming assignments in Data Structures using C++ and DBMS. It includes a certificate section, an index of programs, and specific implementations for polynomial addition, searching algorithms, sorting algorithms, stack operations, and queue operations. Each program is accompanied by its aim, code, and sample output.

Uploaded by

yapaw47132
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 77

Dr.

APJ ABDUL KALAM COLLEGE OF PROFESSIONAL


STUDIES, ULIKKAL KANNUR

[ Affiliated to Kannur University ]

FOURTH SEMESTER BCA PRACTICAL RECORD


DATA STRUCTURE USING C++

&

DBMS
Dr. APJ ABDUL KALAM COLLEGE OF PROFESSIONAL
STUDIES, ULIKKAL KANNUR

[ Affiliated to Kannur University ]

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.

Teacher in charge Head of the Department

Examiner 1 : Principal

Examiner 2 :

Place :

Date :
INDEX

Sl No Program Page No

1 Program to Add Two Polynomials. 5


2 Sequential and Binary search. 8
3 Insertion sort 12
4 Bubble sort 14
5 Quick sort 17
6 Stack operations 19
7 Queue operations 22
8 Convertion of infix expression to postfix 25
9 Menu driven program 29
10 Single linked list 33
11 Circular linked list 39
12 Doubly linked list 45
13 Implement Tree Traversal 50
14 Merge Two sorted linked list 52
INDEX

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.

AIM : 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 :

enter degree of 1st poly:3


enter degree of 2nd poly:3

for first polynomial:


enter coeffiecient of0::4
enter coeffiecient of1::3
enter coeffiecient of2::5
enter coeffiecient of3::10

for second polynomial:


enter coefficient of0::6
enter coefficient of1::7
enter coefficient of2::8
enter coefficient of3::20

polynomial 1:
10x^3+5x^2+3x^1+4x^0

polynomial 2:
20x^3+8x^2+7x^1+6x^0

Polynomial after addition:


30x^3+13x^2+10x^1+10x^0

7
2. Sequential and Binary search : Print number of comparison in each case for given
datasets.

AIM : Sequential and Binary search

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";
}

cout<<"Number of comparison is :"<<count1;


}

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 :

Enter the limit :10

Enter the array element : 7


1
2
6
3
89
56
12
11
94

Array elements are : 7 1 2 6 3 89 56 12 11 94


Linear Search
enter the elements to be searched :3

Search is successfull
Search item is found at position 5
Number of comparison is :5
Binary Search
Enter the item to be Searched :56

Before performing binary search first sort it :


Array elements are : 1 2 3 6 7 11 12 56 89 94
Search is successful

56 is found at position 8 Number of comparison is :2

11
3. Insertion Sort : number of comparisons and exchanges for given data sets.

AIM : Insertion Sort

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 :

Enter the limit :


5
Enter the array elements :
78
65
12
3
4
Array after sorting :
3
4
12
65
78

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.

AIM : Bubble Sort & Selection sort

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 :

enter the limit :


10
enter the values :
89 74 3 1 94 10 6 4 75 22
BUBBLE SORT :
sorted array is :
1
3
4
6
10
22
74
75
89
94
SELECTION SORT :
sorted array is :
1
3
4
6
10
22
74
75
89
94

16
5. Quick sort.

AIM : 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 :

enter the size :5


enter the elements :7 6 1 3 45
sorted array is :1 3 6 7 45

18
6. Stack Operation : Addition and deletion of elements.

AIM : Stack Operation

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 :

1.push 2.pop 3.display 4.exit


1
Enter the character to push :
a
do you want to continue:y/n
y
1.push 2.pop 3.display 4.exit
1
Enter the character to push :
b
do you want to continue:y/n
y
1.push 2.pop 3.display 4.exit
2
popped element is : b
do you want to continue:y/n
y
1.push 2.pop 3.display 4.exit
3
a
do you want to continue:y/n
y
1.push 2.pop 3.display 4.exit
2
popped element is : a
do you want to continue:y/n
y
1.push 2.pop 3.display 4.exit
4
exit
do you want to continue:y/n
n

21
7. Queue operation : Addition and Deletion of Elements

AIM : Queue operation

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 :

1.Enqueue 2.Dequeue 3.Display(traversal) 4.exit


1
enter the character to insert :
78
item inserted
Do you want to continue:y/n
y
1.Enqueue 2.Dequeue 3.Display(traversal) 4.exit
3
78
Do you want to continue:y/n
y
1.Enqueue 2.Dequeue 3.Display(traversal) 4.exit
2
Item deleteddeleted element is : 78

Do you want to continue:y/n


y
1.Enqueue 2.Dequeue 3.Display(traversal) 4.exit
4
exit

Do you want to continue:y/n


n

24
8. Conversion of Infix expression to Postfix .

AIM : 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 :

Enter the infix expression : (a+b)*c/d+e^f/g

Infix expression : (a+b)*c/d+e^f/g

Postfix expression : ab+c*d/ef^g/+

28
9. Menu driven Program : to add / delete elements to a circular queue. Include
necessary error message.

AIM : Menu driven Program : to add / delete elements to a circular queue

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.

AIM : Single Linked list operations

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

Enter your choice :1

Enter the info field :55

Do you want to continue? press y : y

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

Enter your choice :2

Enter the info field :67

Do you want to continue? press y : y

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

Enter your choice :4


55->67->
Do you want to continue? press y : n

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.

AIM : Circular Linked 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

Enter your choice :1

Enter the info field :45

Do you want to continue ? press y/n :y

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

Enter your choice :2

Enter the info field :65

Do you want to continue ? press y/n :y

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

Enter your choice :4


45->65->
Do you want to continue ? press y/n :y

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

Enter your choice :5

Do you want to continue ? press y/n :y

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

Enter your choice :4


65->
Do you want to continue ? press y/n :n

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.

AIM : Doubly Linked 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

Enter your choice :1


Enter the info field : 55

Do you want to continue ? Press 1 : 1


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

Enter your choice :2


Enter the info field : 65

Do you want to continue ? Press 1 : 1


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

Enter your choice :7


55->65->
Do you want to continue ? Press 1 : 1
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

Enter your choice :5


Do you want to continue ? Press 1 : 2

49
13. Implement Tree Traversal.

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

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

Insert five records into the table.

l. Alter the table by adding one more field rank.


2. Display all boy students with their name.
3. Find the Average mark
4. Create a query to display the sno and sname for all students who got More than the
average mark.
5.Sorts the results in descending order of mark.
6. Display all girl student names for those who have marks greater than 20 and less
than 40.

QUERIES

create database college;

use college;

create table students(sno int primary key,sname char(20),sex char(20),mark int);

insert into students value(1,'Amala','Female',38);

insert into students value(2,'Athulya','Female',34);

insert into students value(3,'Hareesh','Male',32);

insert into students value(4,'Pranav','Male',32);

insert into students value(5,'Amal','Male',25);

select *from students;

55
alter table students add column grade varchar(20);

update students set grade='A+' where sno=1;

update students set grade='A' where sno=2;

update students set grade='B' where sno=3;

update students set grade='B' where sno=4;

update students set grade='C' where sno=5;

select * from students;

select sname from students where sex='male';

select avg(mark) as AverageMark from students;

56
select sno,sname from students where mark>(select avg(mark) from students)order by mark DESC;

select sname from students where sex='Female' and (mark>20 or mark<40);

57
SQL 2

2. Create a table department with fields ename, salary, dno, dname, place with dno as
primary key.

lnsert five records into the table.

1. Rename the field 'place' with 'city'


2. Display the employees who got salary more than Rs.6000 and less than l0000 /-
3. Display total salary of the organization
4. Display ename for those who are getting salary in between 5000 and 10000.
5. Create a view named 'Star' with field ename, salary & place
6. Display ename and salary with salary rounded with 10 digits'*'

QUERIES

create table department(ename varchar(20),salary double(20,15),dno int primary key,dname


varchar(20),place varchar(20));

insert into department values(‘Amala’,5000.00,1,‘sales’, ‘Mananthavadi’);

insert into department values(‘Akarsh’,10000.00,2,‘exporting’, ‘Kozhikode’);

insert into department values(‘manasa’,7000.00,3,‘marketing’, ‘Thrissur’);

insert into department values(‘Kichu’,12000.00,4,‘importing’, ‘Alappuzha’);

insert into department values(‘Adithyan’,6000.00,5,‘driver’, ‘Kozhikode’);

select * from department;

1) alter table department change column place city varchar(20);

58
2) select ename from department where salary>6000 and salary<10000;

3) select sum(salary) as totalsalary from department;

4) select ename from department where salary between 5000 and 10000;

5) create view starz as select ename,salary,city from department;

59
select * from starz;

6) select ename,round(salary,10) from department;

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.

1.Display the ename and salary, salary with ascending order


2. Display ename and salary for eno=20.
3. Display the manager for the accounting Department
4. Display the name,salary and manager of all employees who are getting salary >
5000.
5, Write the queries using various group functions.
6. Write the queries using various Number functions.

QUERIES

create table department(dno int primary key,dname varchar(20),dmanager varchar(20),place


varchar(20));

insert into departments values(100,‘sales’,‘Ashwin’,‘Kochi’);

insert into departments values (101,‘Accounting’,‘Varsha’,‘Mumbai’);

insert into departments values(102,‘Marketing’,‘Deepu’,‘Delhi’);

insert into departments values(103,‘Importing’,‘Surya’,‘Chennai’);

insert into departments values (104,‘Exporting’,‘Akash’,‘Kerala’);

select * from departments;

61
create table emp(eno int primary key,ename varchar(20),job varchar(20),salary int,dno int,foreign
key(dno) references departments(dno));

insert into emp values(10,‘Akshara’,‘Manager’,40000,100);

insert ito emp values(20,‘Nandhu’,‘Manager’,40000,101);

insert into emp values(30,‘Athul’,‘Salesman’,6000,102);

insert into emp values(40,‘Alan’,‘Asst Manager’,12000,103);

insert into emp values(50,‘Supriya’,‘Supervisor’,20000,104);

select * from emp;

1) select ename,salary from emp order by salary ASC;

2) select ename,salary from emp where eno=20;

62
3) select dmanager from departments where dname=‘Accounting’;

4) select ename,salary,dmanager from emp,departments where salary>5000 and


emp.dno=departments.dno;

5) select sum(salary) as sum_salary from emp;

select max(salary) as minimum_salary from emp;

63
select min(salary) as minimum_salary from emp;

select avg(salary) as average_salary from emp;

select count(*) as number_of_rows from emp;

select count(*) as number_of_rows from departments;

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

create table emp(eno int primary key,ename varchar(25),job varchar(25),manager varchar(25),salary


int);

insert into emp values(101,‘Martin’,‘Clerk’,‘Alan’,15000);

insert into emp values(102,‘Dennis’,‘Salesman’,‘Joseph’,18000);

insert into emp values(103,‘Vipin’,‘Accountant’,‘Amal’,20000);

insert into emp values(104,‘Athul’,‘Asst Manager’,‘Hiran’,25000);

insert into emp values(105,‘Adithyan’,‘Salesman’,‘Zen Louis’,17500);

select * from emp;

66
1) select ename,salary from emp where salary>(select avg(salary) from emp);

2)alter table emp add new_salary int;

update emp set new_salary=salary+((salary*20)/100);

select * from emp;

3) select eno,ename from emp where salary>(select avg(salary) from emp) order by salary DESC;

4) create view emp_view1(eno,employ) as select eno,ename from emp;

select * from emp_view1;

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.

1. Write the queries using various Character functions in ename field.

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.

3. Display all employees who got salary between 5000 &10000

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(3, 'Athul', 6000, 'Clerk', 'Finance', 'Chicago');

insert into department values(4, 'Alan', 9500, 'Analyst', 'Marketing', 'Los Angeles');

insert into department values(5, 'Aman', 5000, 'Sales person', 'Sales', 'Houston');

select * from department;

69
1. select upper(ename) from department;

select lower(ename) from department;

select ename, length(ename) from department;

select ename, left(ename, 3) 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;

3.select * from department where salary between 5000 and 10000;

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');

6.select ename from department where ename like '_A%';

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.

4.Display all information from loan table for loanno 2,8,10.

5.Display all customers who have both loan and deposit.

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

insert into customer values(1, 'Akshara', '1990-05-15', 'Kochi');

insert into customer values(2, 'Nandhu', '1985-08-20', 'Kannur');

insert into customer values(3, 'Athul', '1992-11-10', 'Thalassery');

insert into customer values(4, 'Alan', '1988-04-25', 'Kozhikode');

insert into customer values(5, 'Amal', '1995-07-30', 'Kannur');

select * from customer;

73
insert into Loan values (101, 1, 'Bank A');

insert into Loan values(102, 2, 'Bank B');

insert into Loan values(103, 3, 'Bank C');

insert into Loan values(104, 4, 'Bank A');

insert into Loan values(105, 5, 'Bank B');

select * from Loan;

insert into Depositor values(201, 1, 5000, 'Bank A');

insert into Depositor values(202, 2, 7000, 'Bank B');

insert into Depositor values(203, 3, 6000, 'Bank C');

insert into Depositor values(204, 4, 8000, 'Bank A');

insert into Depositor values(205, 5, 10000, 'Bank B');

74
select * from Depositor;

1.alter table Loan add amount int;

update Loan set amount = 10000 where loanno = 101;

update Loan set amount = 15000 where loanno = 102;

update Loan set amount = 20000 where loanno = 103;

update Loan set amount = 25000 where loanno = 104;

update Loan set amount = 18000 where loanno = 105;

select * from Loan;

select cname from customer where cid = 2;

75
2.select loanno, cid, amount, (amount + 150) AS updated_amount from Loan;

3.select Loan.loanno, customer.cname, customer.place from Loan join customer on Loan.cid =


customer.cid where customer.place = 'Kannur';

4.select * from Loan where loanno in (2, 8, 10);

select * from Loan where loanno in (101,103,105);

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

You might also like